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