• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "test/core/end2end/end2end_tests.h"
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <grpc/byte_buffer.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
28 #include "test/core/end2end/cq_verifier.h"
29 
tag(intptr_t t)30 static void* tag(intptr_t t) { return (void*)t; }
31 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)32 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
33                                             const char* test_name,
34                                             grpc_channel_args* client_args,
35                                             grpc_channel_args* server_args) {
36   grpc_end2end_test_fixture f;
37   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
38   f = config.create_fixture(client_args, server_args);
39   config.init_server(&f, server_args);
40   config.init_client(&f, client_args);
41   return f;
42 }
43 
n_seconds_from_now(int n)44 static gpr_timespec n_seconds_from_now(int n) {
45   return grpc_timeout_seconds_to_deadline(n);
46 }
47 
five_seconds_from_now(void)48 static gpr_timespec five_seconds_from_now(void) {
49   return n_seconds_from_now(5);
50 }
51 
drain_cq(grpc_completion_queue * cq)52 static void drain_cq(grpc_completion_queue* cq) {
53   grpc_event ev;
54   do {
55     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
56   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
57 }
58 
shutdown_server(grpc_end2end_test_fixture * f)59 static void shutdown_server(grpc_end2end_test_fixture* f) {
60   if (!f->server) return;
61   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
62   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
63                                          grpc_timeout_seconds_to_deadline(5),
64                                          nullptr)
65                  .type == GRPC_OP_COMPLETE);
66   grpc_server_destroy(f->server);
67   f->server = nullptr;
68 }
69 
shutdown_client(grpc_end2end_test_fixture * f)70 static void shutdown_client(grpc_end2end_test_fixture* f) {
71   if (!f->client) return;
72   grpc_channel_destroy(f->client);
73   f->client = nullptr;
74 }
75 
end_test(grpc_end2end_test_fixture * f)76 static void end_test(grpc_end2end_test_fixture* f) {
77   shutdown_server(f);
78   shutdown_client(f);
79 
80   grpc_completion_queue_shutdown(f->cq);
81   drain_cq(f->cq);
82   grpc_completion_queue_destroy(f->cq);
83   grpc_completion_queue_destroy(f->shutdown_cq);
84 }
85 
86 /* Request/response with metadata and payload.*/
test_request_response_with_metadata_and_payload(grpc_end2end_test_config config)87 static void test_request_response_with_metadata_and_payload(
88     grpc_end2end_test_config config) {
89   grpc_call* c;
90   grpc_call* s;
91   grpc_slice request_payload_slice =
92       grpc_slice_from_copied_string("hello world");
93   grpc_slice response_payload_slice =
94       grpc_slice_from_copied_string("hello you");
95   grpc_byte_buffer* request_payload =
96       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
97   grpc_byte_buffer* response_payload =
98       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
99   grpc_metadata meta_c[2] = {
100       {grpc_slice_from_static_string("key1-bin"),
101        grpc_slice_from_static_string(
102            "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc"),
103        0,
104        {{nullptr, nullptr, nullptr, nullptr}}},
105       {grpc_slice_from_static_string("key2-bin"),
106        grpc_slice_from_static_string(
107            "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d"),
108        0,
109        {{nullptr, nullptr, nullptr, nullptr}}}};
110   grpc_metadata meta_s[2] = {
111       {grpc_slice_from_static_string("key3-bin"),
112        grpc_slice_from_static_string(
113            "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee"),
114        0,
115        {{nullptr, nullptr, nullptr, nullptr}}},
116       {grpc_slice_from_static_string("key4-bin"),
117        grpc_slice_from_static_string(
118            "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"),
119        0,
120        {{nullptr, nullptr, nullptr, nullptr}}}};
121   grpc_end2end_test_fixture f =
122       begin_test(config, "test_request_response_with_metadata_and_payload",
123                  nullptr, nullptr);
124   cq_verifier* cqv = cq_verifier_create(f.cq);
125   grpc_op ops[6];
126   grpc_op* op;
127   grpc_metadata_array initial_metadata_recv;
128   grpc_metadata_array trailing_metadata_recv;
129   grpc_metadata_array request_metadata_recv;
130   grpc_byte_buffer* request_payload_recv = nullptr;
131   grpc_byte_buffer* response_payload_recv = nullptr;
132   grpc_call_details call_details;
133   grpc_status_code status;
134   grpc_call_error error;
135   grpc_slice details;
136   int was_cancelled = 2;
137 
138   gpr_timespec deadline = five_seconds_from_now();
139   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
140                                grpc_slice_from_static_string("/foo"), nullptr,
141                                deadline, nullptr);
142   GPR_ASSERT(c);
143 
144   grpc_metadata_array_init(&initial_metadata_recv);
145   grpc_metadata_array_init(&trailing_metadata_recv);
146   grpc_metadata_array_init(&request_metadata_recv);
147   grpc_call_details_init(&call_details);
148 
149   memset(ops, 0, sizeof(ops));
150   op = ops;
151   op->op = GRPC_OP_SEND_INITIAL_METADATA;
152   op->data.send_initial_metadata.count = 2;
153   op->data.send_initial_metadata.metadata = meta_c;
154   op->flags = 0;
155   op->reserved = nullptr;
156   op++;
157   op->op = GRPC_OP_SEND_MESSAGE;
158   op->data.send_message.send_message = request_payload;
159   op->flags = 0;
160   op->reserved = nullptr;
161   op++;
162   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
163   op->flags = 0;
164   op->reserved = nullptr;
165   op++;
166   op->op = GRPC_OP_RECV_INITIAL_METADATA;
167   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
168   op->flags = 0;
169   op->reserved = nullptr;
170   op++;
171   op->op = GRPC_OP_RECV_MESSAGE;
172   op->data.recv_message.recv_message = &response_payload_recv;
173   op->flags = 0;
174   op->reserved = nullptr;
175   op++;
176   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
177   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
178   op->data.recv_status_on_client.status = &status;
179   op->data.recv_status_on_client.status_details = &details;
180   op->flags = 0;
181   op->reserved = nullptr;
182   op++;
183   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
184                                 nullptr);
185   GPR_ASSERT(GRPC_CALL_OK == error);
186 
187   error =
188       grpc_server_request_call(f.server, &s, &call_details,
189                                &request_metadata_recv, f.cq, f.cq, tag(101));
190   GPR_ASSERT(GRPC_CALL_OK == error);
191   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
192   cq_verify(cqv);
193 
194   memset(ops, 0, sizeof(ops));
195   op = ops;
196   op->op = GRPC_OP_SEND_INITIAL_METADATA;
197   op->data.send_initial_metadata.count = 2;
198   op->data.send_initial_metadata.metadata = meta_s;
199   op->flags = 0;
200   op->reserved = nullptr;
201   op++;
202   op->op = GRPC_OP_RECV_MESSAGE;
203   op->data.recv_message.recv_message = &request_payload_recv;
204   op->flags = 0;
205   op->reserved = nullptr;
206   op++;
207   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
208                                 nullptr);
209   GPR_ASSERT(GRPC_CALL_OK == error);
210 
211   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
212   cq_verify(cqv);
213 
214   memset(ops, 0, sizeof(ops));
215   op = ops;
216   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
217   op->data.recv_close_on_server.cancelled = &was_cancelled;
218   op->flags = 0;
219   op->reserved = nullptr;
220   op++;
221   op->op = GRPC_OP_SEND_MESSAGE;
222   op->data.send_message.send_message = response_payload;
223   op->flags = 0;
224   op->reserved = nullptr;
225   op++;
226   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
227   op->data.send_status_from_server.trailing_metadata_count = 0;
228   op->data.send_status_from_server.status = GRPC_STATUS_OK;
229   grpc_slice status_string = grpc_slice_from_static_string(
230       "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12"
231       "\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24"
232       "\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36"
233       "\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48"
234       "\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a"
235       "\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c"
236       "\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e"
237       "\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90"
238       "\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2"
239       "\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4"
240       "\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6"
241       "\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8"
242       "\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea"
243       "\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc"
244       "\xfd\xfe\xff");
245   op->data.send_status_from_server.status_details = &status_string;
246   op->flags = 0;
247   op->reserved = nullptr;
248   op++;
249   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
250                                 nullptr);
251   GPR_ASSERT(GRPC_CALL_OK == error);
252 
253   CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
254   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
255   cq_verify(cqv);
256 
257   GPR_ASSERT(status == GRPC_STATUS_OK);
258   GPR_ASSERT(
259       0 ==
260       grpc_slice_str_cmp(
261           details,
262           "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
263           "\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
264           "\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30"
265           "\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40"
266           "\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50"
267           "\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60"
268           "\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70"
269           "\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80"
270           "\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90"
271           "\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0"
272           "\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0"
273           "\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0"
274           "\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0"
275           "\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0"
276           "\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0"
277           "\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"));
278   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
279   GPR_ASSERT(was_cancelled == 0);
280   GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
281   GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you"));
282   GPR_ASSERT(contains_metadata(
283       &request_metadata_recv, "key1-bin",
284       "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc"));
285   GPR_ASSERT(contains_metadata(
286       &request_metadata_recv, "key2-bin",
287       "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d"));
288   GPR_ASSERT(contains_metadata(
289       &initial_metadata_recv, "key3-bin",
290       "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee"));
291   GPR_ASSERT(contains_metadata(
292       &initial_metadata_recv, "key4-bin",
293       "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"));
294 
295   grpc_slice_unref(details);
296   grpc_metadata_array_destroy(&initial_metadata_recv);
297   grpc_metadata_array_destroy(&trailing_metadata_recv);
298   grpc_metadata_array_destroy(&request_metadata_recv);
299   grpc_call_details_destroy(&call_details);
300 
301   grpc_call_unref(c);
302   grpc_call_unref(s);
303 
304   cq_verifier_destroy(cqv);
305 
306   grpc_byte_buffer_destroy(request_payload);
307   grpc_byte_buffer_destroy(response_payload);
308   grpc_byte_buffer_destroy(request_payload_recv);
309   grpc_byte_buffer_destroy(response_payload_recv);
310 
311   end_test(&f);
312   config.tear_down_data(&f);
313 }
314 
binary_metadata(grpc_end2end_test_config config)315 void binary_metadata(grpc_end2end_test_config config) {
316   test_request_response_with_metadata_and_payload(config);
317 }
318 
binary_metadata_pre_init(void)319 void binary_metadata_pre_init(void) {}
320