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
30 enum { TIMEOUT = 200000 };
31
tag(intptr_t t)32 static void* tag(intptr_t t) { return (void*)t; }
33
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)34 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
35 const char* test_name,
36 grpc_channel_args* client_args,
37 grpc_channel_args* server_args) {
38 grpc_end2end_test_fixture f;
39 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
40 f = config.create_fixture(client_args, server_args);
41 config.init_server(&f, server_args);
42 config.init_client(&f, client_args);
43 return f;
44 }
45
n_seconds_from_now(int n)46 static gpr_timespec n_seconds_from_now(int n) {
47 return grpc_timeout_seconds_to_deadline(n);
48 }
49
five_seconds_from_now(void)50 static gpr_timespec five_seconds_from_now(void) {
51 return n_seconds_from_now(5);
52 }
53
drain_cq(grpc_completion_queue * cq)54 static void drain_cq(grpc_completion_queue* cq) {
55 grpc_event ev;
56 do {
57 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
58 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
59 }
60
shutdown_server(grpc_end2end_test_fixture * f)61 static void shutdown_server(grpc_end2end_test_fixture* f) {
62 if (!f->server) return;
63 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
64 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
65 grpc_timeout_seconds_to_deadline(5),
66 nullptr)
67 .type == GRPC_OP_COMPLETE);
68 grpc_server_destroy(f->server);
69 f->server = nullptr;
70 }
71
shutdown_client(grpc_end2end_test_fixture * f)72 static void shutdown_client(grpc_end2end_test_fixture* f) {
73 if (!f->client) return;
74 grpc_channel_destroy(f->client);
75 f->client = nullptr;
76 }
77
end_test(grpc_end2end_test_fixture * f)78 static void end_test(grpc_end2end_test_fixture* f) {
79 shutdown_server(f);
80 shutdown_client(f);
81
82 grpc_completion_queue_shutdown(f->cq);
83 drain_cq(f->cq);
84 grpc_completion_queue_destroy(f->cq);
85 grpc_completion_queue_destroy(f->shutdown_cq);
86 }
87
88 /* Request/response with metadata and payload.*/
test_cacheable_request_response_with_metadata_and_payload(grpc_end2end_test_config config)89 static void test_cacheable_request_response_with_metadata_and_payload(
90 grpc_end2end_test_config config) {
91 grpc_call* c;
92 grpc_call* s;
93 grpc_slice request_payload_slice =
94 grpc_slice_from_copied_string("hello world");
95 grpc_slice response_payload_slice =
96 grpc_slice_from_copied_string("hello you");
97 grpc_byte_buffer* request_payload =
98 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
99 grpc_byte_buffer* response_payload =
100 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
101 grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"),
102 grpc_slice_from_static_string("val1"),
103 0,
104 {{nullptr, nullptr, nullptr, nullptr}}},
105 {grpc_slice_from_static_string("key2"),
106 grpc_slice_from_static_string("val2"),
107 0,
108 {{nullptr, nullptr, nullptr, nullptr}}}};
109 grpc_metadata meta_s[2] = {{grpc_slice_from_static_string("key3"),
110 grpc_slice_from_static_string("val3"),
111 0,
112 {{nullptr, nullptr, nullptr, nullptr}}},
113 {grpc_slice_from_static_string("key4"),
114 grpc_slice_from_static_string("val4"),
115 0,
116 {{nullptr, nullptr, nullptr, nullptr}}}};
117 grpc_end2end_test_fixture f = begin_test(
118 config, "test_cacheable_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 = GRPC_INITIAL_METADATA_CACHEABLE_REQUEST;
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_details = grpc_slice_from_static_string("xyz");
226 op->data.send_status_from_server.status_details = &status_details;
227 op->flags = 0;
228 op->reserved = nullptr;
229 op++;
230 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
231 nullptr);
232 GPR_ASSERT(GRPC_CALL_OK == error);
233
234 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
235 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
236 cq_verify(cqv);
237
238 GPR_ASSERT(status == GRPC_STATUS_OK);
239 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
240 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
241 if (config.feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) {
242 // Our simple proxy does not support cacheable requests
243 } else {
244 GPR_ASSERT(GRPC_INITIAL_METADATA_CACHEABLE_REQUEST & call_details.flags);
245 }
246 GPR_ASSERT(was_cancelled == 0);
247 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
248 GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you"));
249 GPR_ASSERT(contains_metadata(&request_metadata_recv, "key1", "val1"));
250 GPR_ASSERT(contains_metadata(&request_metadata_recv, "key2", "val2"));
251 GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key3", "val3"));
252 GPR_ASSERT(contains_metadata(&initial_metadata_recv, "key4", "val4"));
253
254 grpc_slice_unref(details);
255 grpc_metadata_array_destroy(&initial_metadata_recv);
256 grpc_metadata_array_destroy(&trailing_metadata_recv);
257 grpc_metadata_array_destroy(&request_metadata_recv);
258 grpc_call_details_destroy(&call_details);
259
260 grpc_call_unref(c);
261 grpc_call_unref(s);
262
263 cq_verifier_destroy(cqv);
264
265 grpc_byte_buffer_destroy(request_payload);
266 grpc_byte_buffer_destroy(response_payload);
267 grpc_byte_buffer_destroy(request_payload_recv);
268 grpc_byte_buffer_destroy(response_payload_recv);
269
270 end_test(&f);
271 config.tear_down_data(&f);
272 }
273
simple_cacheable_request(grpc_end2end_test_config config)274 void simple_cacheable_request(grpc_end2end_test_config config) {
275 test_cacheable_request_response_with_metadata_and_payload(config);
276 }
277
simple_cacheable_request_pre_init(void)278 void simple_cacheable_request_pre_init(void) {}
279