1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /**
20  * This test is for checking whether proxy authentication is working with HTTP
21  * Connect.
22  */
23 #include "test/core/end2end/end2end_tests.h"
24 #include "test/core/end2end/fixtures/http_proxy_fixture.h"
25 
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include <grpc/byte_buffer.h>
30 #include <grpc/grpc.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/time.h>
34 #include "src/core/lib/gpr/string.h"
35 #include "test/core/end2end/cq_verifier.h"
36 
tag(intptr_t t)37 static void* tag(intptr_t t) { return (void*)t; }
38 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)39 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
40                                             const char* test_name,
41                                             grpc_channel_args* client_args,
42                                             grpc_channel_args* server_args) {
43   grpc_end2end_test_fixture f;
44   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
45   f = config.create_fixture(client_args, server_args);
46   config.init_server(&f, server_args);
47   config.init_client(&f, client_args);
48   return f;
49 }
50 
n_seconds_from_now(int n)51 static gpr_timespec n_seconds_from_now(int n) {
52   return grpc_timeout_seconds_to_deadline(n);
53 }
54 
five_seconds_from_now(void)55 static gpr_timespec five_seconds_from_now(void) {
56   return n_seconds_from_now(5);
57 }
58 
drain_cq(grpc_completion_queue * cq)59 static void drain_cq(grpc_completion_queue* cq) {
60   grpc_event ev;
61   do {
62     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
63   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
64 }
65 
shutdown_server(grpc_end2end_test_fixture * f)66 static void shutdown_server(grpc_end2end_test_fixture* f) {
67   if (!f->server) return;
68   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
69   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
70                                          grpc_timeout_seconds_to_deadline(5),
71                                          nullptr)
72                  .type == GRPC_OP_COMPLETE);
73   grpc_server_destroy(f->server);
74   f->server = nullptr;
75 }
76 
shutdown_client(grpc_end2end_test_fixture * f)77 static void shutdown_client(grpc_end2end_test_fixture* f) {
78   if (!f->client) return;
79   grpc_channel_destroy(f->client);
80   f->client = nullptr;
81 }
82 
end_test(grpc_end2end_test_fixture * f)83 static void end_test(grpc_end2end_test_fixture* f) {
84   shutdown_server(f);
85   shutdown_client(f);
86 
87   grpc_completion_queue_shutdown(f->cq);
88   drain_cq(f->cq);
89   grpc_completion_queue_destroy(f->cq);
90   grpc_completion_queue_destroy(f->shutdown_cq);
91 }
92 
simple_request_body(grpc_end2end_test_config,grpc_end2end_test_fixture f)93 static void simple_request_body(grpc_end2end_test_config /*config*/,
94                                 grpc_end2end_test_fixture f) {
95   grpc_call* c;
96   grpc_call* s;
97   cq_verifier* cqv = cq_verifier_create(f.cq);
98   grpc_op ops[6];
99   grpc_op* op;
100   grpc_metadata_array initial_metadata_recv;
101   grpc_metadata_array trailing_metadata_recv;
102   grpc_metadata_array request_metadata_recv;
103   grpc_call_details call_details;
104   grpc_status_code status;
105   grpc_call_error error;
106   grpc_slice details;
107   int was_cancelled = 2;
108   char* peer;
109 
110   gpr_timespec deadline = five_seconds_from_now();
111   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
112                                grpc_slice_from_static_string("/foo"), nullptr,
113                                deadline, nullptr);
114   GPR_ASSERT(c);
115 
116   peer = grpc_call_get_peer(c);
117   GPR_ASSERT(peer != nullptr);
118   gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
119   gpr_free(peer);
120 
121   grpc_metadata_array_init(&initial_metadata_recv);
122   grpc_metadata_array_init(&trailing_metadata_recv);
123   grpc_metadata_array_init(&request_metadata_recv);
124   grpc_call_details_init(&call_details);
125 
126   memset(ops, 0, sizeof(ops));
127   op = ops;
128   op->op = GRPC_OP_SEND_INITIAL_METADATA;
129   op->data.send_initial_metadata.count = 0;
130   op->flags = 0;
131   op->reserved = nullptr;
132   op++;
133   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
134   op->flags = 0;
135   op->reserved = nullptr;
136   op++;
137   op->op = GRPC_OP_RECV_INITIAL_METADATA;
138   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
139   op->flags = 0;
140   op->reserved = nullptr;
141   op++;
142   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
143   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
144   op->data.recv_status_on_client.status = &status;
145   op->data.recv_status_on_client.status_details = &details;
146   op->flags = 0;
147   op->reserved = nullptr;
148   op++;
149   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
150                                 nullptr);
151   GPR_ASSERT(GRPC_CALL_OK == error);
152 
153   error =
154       grpc_server_request_call(f.server, &s, &call_details,
155                                &request_metadata_recv, f.cq, f.cq, tag(101));
156   GPR_ASSERT(GRPC_CALL_OK == error);
157   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
158   cq_verify(cqv);
159 
160   peer = grpc_call_get_peer(s);
161   GPR_ASSERT(peer != nullptr);
162   gpr_log(GPR_DEBUG, "server_peer=%s", peer);
163   gpr_free(peer);
164   peer = grpc_call_get_peer(c);
165   GPR_ASSERT(peer != nullptr);
166   gpr_log(GPR_DEBUG, "client_peer=%s", peer);
167   gpr_free(peer);
168 
169   memset(ops, 0, sizeof(ops));
170   op = ops;
171   op->op = GRPC_OP_SEND_INITIAL_METADATA;
172   op->data.send_initial_metadata.count = 0;
173   op->flags = 0;
174   op->reserved = nullptr;
175   op++;
176   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
177   op->data.send_status_from_server.trailing_metadata_count = 0;
178   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
179   grpc_slice status_details = grpc_slice_from_static_string("xyz");
180   op->data.send_status_from_server.status_details = &status_details;
181   op->flags = 0;
182   op->reserved = nullptr;
183   op++;
184   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
185   op->data.recv_close_on_server.cancelled = &was_cancelled;
186   op->flags = 0;
187   op->reserved = nullptr;
188   op++;
189   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
190                                 nullptr);
191   GPR_ASSERT(GRPC_CALL_OK == error);
192 
193   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
194   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
195   cq_verify(cqv);
196 
197   GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
198   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
199   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
200   GPR_ASSERT(0 == call_details.flags);
201   GPR_ASSERT(was_cancelled == 1);
202 
203   grpc_slice_unref(details);
204   grpc_metadata_array_destroy(&initial_metadata_recv);
205   grpc_metadata_array_destroy(&trailing_metadata_recv);
206   grpc_metadata_array_destroy(&request_metadata_recv);
207   grpc_call_details_destroy(&call_details);
208 
209   grpc_call_unref(c);
210   grpc_call_unref(s);
211 
212   cq_verifier_destroy(cqv);
213 }
214 
test_invoke_proxy_auth(grpc_end2end_test_config config)215 static void test_invoke_proxy_auth(grpc_end2end_test_config config) {
216   /* Indicate that the proxy requires user auth */
217   grpc_arg client_arg;
218   client_arg.type = GRPC_ARG_STRING;
219   client_arg.key = const_cast<char*>(GRPC_ARG_HTTP_PROXY_AUTH_CREDS);
220   client_arg.value.string = const_cast<char*>(GRPC_TEST_HTTP_PROXY_AUTH_CREDS);
221   grpc_channel_args client_args = {1, &client_arg};
222   grpc_end2end_test_fixture f =
223       begin_test(config, "test_invoke_proxy_auth", &client_args, nullptr);
224   simple_request_body(config, f);
225   end_test(&f);
226   config.tear_down_data(&f);
227 }
228 
proxy_auth(grpc_end2end_test_config config)229 void proxy_auth(grpc_end2end_test_config config) {
230   test_invoke_proxy_auth(config);
231 }
232 
proxy_auth_pre_init(void)233 void proxy_auth_pre_init(void) {}
234