Debian Codegraph @

test/core/end2end/bad_server_response_test.cc

raw
1 /*
2 *
3 * Copyright 2016 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 <inttypes.h>
20 #include <limits.h>
21 #include <string.h>
22
23 #include <memory>
24 #include <string>
25 #include <vector>
26
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_security.h>
29 #include <grpc/impl/codegen/propagation_bits.h>
30 #include <grpc/slice.h>
31 #include <grpc/slice_buffer.h>
32 #include <grpc/status.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/atm.h>
35 #include <grpc/support/log.h>
36 #include <grpc/support/sync.h>
37 #include <grpc/support/time.h>
38
39 #include "src/core/lib/gpr/string.h"
40 #include "src/core/lib/gprpp/host_port.h"
41 #include "src/core/lib/gprpp/status_helper.h"
42 #include "src/core/lib/gprpp/thd.h"
43 #include "src/core/lib/iomgr/closure.h"
44 #include "src/core/lib/iomgr/endpoint.h"
45 #include "src/core/lib/iomgr/error.h"
46 #include "src/core/lib/iomgr/exec_ctx.h"
47 #include "src/core/lib/iomgr/iomgr_fwd.h"
48 #include "src/core/lib/iomgr/tcp_server.h"
49 #include "src/core/lib/slice/slice_string_helpers.h"
50 #include "test/core/end2end/cq_verifier.h"
51 #include "test/core/util/port.h"
52 #include "test/core/util/test_config.h"
53 #include "test/core/util/test_tcp_server.h"
54
55 #define HTTP1_RESP_400 \
56 "HTTP/1.0 400 Bad Request\n" \
57 "Content-Type: text/html; charset=UTF-8\n" \
58 "Content-Length: 0\n" \
59 "Date: Tue, 07 Jun 2016 17:43:20 GMT\n\n"
60
61 #define HTTP2_SETTINGS_FRAME "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
62
63 #define HTTP2_RESP(STATUS_CODE) \
64 "\x00\x00>\x01\x04\x00\x00\x00\x01" \
65 "\x10\x0e" \
66 "content-length\x01" \
67 "0" \
68 "\x10\x0c" \
69 "content-type\x10" \
70 "application/grpc" \
71 "\x10\x07:status\x03" #STATUS_CODE
72
73 #define UNPARSEABLE_RESP "Bad Request\n"
74
75 #define HTTP2_DETAIL_MSG(STATUS_CODE) \
76 "Received http2 header with status: " #STATUS_CODE
77
78 /* TODO(zyc) Check the content of incoming data instead of using this length */
79 /* The 'bad' server will start sending responses after reading this amount of
80 * data from the client. */
81 #define SERVER_INCOMING_DATA_LENGTH_LOWER_THRESHOLD (size_t)200
82
83 struct rpc_state {
84 std::string target;
85 grpc_completion_queue* cq;
86 grpc_channel* channel;
87 grpc_call* call;
88 size_t incoming_data_length;
89 grpc_slice_buffer temp_incoming_buffer;
90 grpc_slice_buffer outgoing_buffer;
91 grpc_endpoint* tcp;
92 gpr_atm done_atm;
93 bool http2_response;
94 bool send_settings;
95 const char* response_payload;
96 size_t response_payload_length;
97 bool connection_attempt_made;
98 };
99
100 static int server_port;
101 static struct rpc_state state;
102 static grpc_closure on_read;
103 static grpc_closure on_writing_settings_frame;
104 static grpc_closure on_write;
105
106 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
107
108 static void done_write(void* /*arg*/, grpc_error_handle error) {
109 GPR_ASSERT(error.ok());
110 gpr_atm_rel_store(&state.done_atm, 1);
111 }
112
113 static void done_writing_settings_frame(void* /* arg */,
114 grpc_error_handle error) {
115 GPR_ASSERT(error.ok());
116 grpc_endpoint_read(state.tcp, &state.temp_incoming_buffer, &on_read,
117 /*urgent=*/false, /*min_progress_size=*/1);
118 }
119
120 static void handle_write() {
121 grpc_slice slice = grpc_slice_from_copied_buffer(
122 state.response_payload, state.response_payload_length);
123
124 grpc_slice_buffer_reset_and_unref(&state.outgoing_buffer);
125 grpc_slice_buffer_add(&state.outgoing_buffer, slice);
126 grpc_endpoint_write(state.tcp, &state.outgoing_buffer, &on_write, nullptr,
127 /*max_frame_size=*/INT_MAX);
128 }
129
130 static void handle_read(void* /*arg*/, grpc_error_handle error) {
131 if (!error.ok()) {
132 gpr_log(GPR_ERROR, "handle_read error: %s",
133 grpc_core::StatusToString(error).c_str());
134 return;
135 }
136 state.incoming_data_length += state.temp_incoming_buffer.length;
137
138 size_t i;
139 for (i = 0; i < state.temp_incoming_buffer.count; i++) {
140 char* dump = grpc_dump_slice(state.temp_incoming_buffer.slices[i],
141 GPR_DUMP_HEX | GPR_DUMP_ASCII);
142 gpr_log(GPR_DEBUG, "Server received: %s", dump);
143 gpr_free(dump);
144 }
145
146 gpr_log(GPR_DEBUG,
147 "got %" PRIuPTR " bytes, expected %" PRIuPTR
148 " bytes or a non-HTTP2 response to be sent",
149 state.incoming_data_length,
150 SERVER_INCOMING_DATA_LENGTH_LOWER_THRESHOLD);
151 if (state.incoming_data_length >=
152 SERVER_INCOMING_DATA_LENGTH_LOWER_THRESHOLD ||
153 !state.http2_response) {
154 handle_write();
155 } else {
156 grpc_endpoint_read(state.tcp, &state.temp_incoming_buffer, &on_read,
157 /*urgent=*/false, /*min_progress_size=*/1);
158 }
159 }
160
161 static void on_connect(void* arg, grpc_endpoint* tcp,
162 grpc_pollset* /*accepting_pollset*/,
163 grpc_tcp_server_acceptor* acceptor) {
164 gpr_free(acceptor);
165 test_tcp_server* server = static_cast<test_tcp_server*>(arg);
166 GRPC_CLOSURE_INIT(&on_read, handle_read, nullptr, grpc_schedule_on_exec_ctx);
167 GRPC_CLOSURE_INIT(&on_writing_settings_frame, done_writing_settings_frame,
168 nullptr, grpc_schedule_on_exec_ctx);
169 GRPC_CLOSURE_INIT(&on_write, done_write, nullptr, grpc_schedule_on_exec_ctx);
170 grpc_slice_buffer_init(&state.temp_incoming_buffer);
171 grpc_slice_buffer_init(&state.outgoing_buffer);
172 state.connection_attempt_made = true;
173 state.tcp = tcp;
174 state.incoming_data_length = 0;
175 grpc_endpoint_add_to_pollset(tcp, server->pollset[0]);
176 if (state.send_settings) {
177 // Send settings frame from server
178 grpc_slice slice = grpc_slice_from_static_buffer(
179 HTTP2_SETTINGS_FRAME, sizeof(HTTP2_SETTINGS_FRAME) - 1);
180 grpc_slice_buffer_add(&state.outgoing_buffer, slice);
181 grpc_endpoint_write(state.tcp, &state.outgoing_buffer,
182 &on_writing_settings_frame, nullptr,
183 /*max_frame_size=*/INT_MAX);
184 } else {
185 grpc_endpoint_read(state.tcp, &state.temp_incoming_buffer, &on_read,
186 /*urgent=*/false, /*min_progress_size=*/1);
187 }
188 }
189
190 static gpr_timespec n_sec_deadline(int seconds) {
191 return gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
192 gpr_time_from_seconds(seconds, GPR_TIMESPAN));
193 }
194
195 static void start_rpc(int target_port, grpc_status_code expected_status,
196 const char* expected_detail) {
197 grpc_op ops[6];
198 grpc_op* op;
199 grpc_metadata_array initial_metadata_recv;
200 grpc_metadata_array trailing_metadata_recv;
201 grpc_status_code status;
202 grpc_call_error error;
203 grpc_slice details;
204
205 state.cq = grpc_completion_queue_create_for_next(nullptr);
206 grpc_core::CqVerifier cqv(state.cq);
207 state.target = grpc_core::JoinHostPort("127.0.0.1", target_port);
208
209 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
210 state.channel = grpc_channel_create(state.target.c_str(), creds, nullptr);
211 grpc_channel_credentials_release(creds);
212 grpc_slice host = grpc_slice_from_static_string("localhost");
213 // The default connect deadline is 20 seconds, so reduce the RPC deadline to 1
214 // second. This helps us verify - a) If the server responded with a non-HTTP2
215 // response, the connect fails immediately resulting in
216 // GRPC_STATUS_UNAVAILABLE instead of GRPC_STATUS_DEADLINE_EXCEEDED. b) If the
217 // server does not send a HTTP2 SETTINGs frame, the RPC fails with a
218 // DEADLINE_EXCEEDED.
219 state.call = grpc_channel_create_call(
220 state.channel, nullptr, GRPC_PROPAGATE_DEFAULTS, state.cq,
221 grpc_slice_from_static_string("/Service/Method"), &host,
222 n_sec_deadline(5), nullptr);
223
224 grpc_metadata_array_init(&initial_metadata_recv);
225 grpc_metadata_array_init(&trailing_metadata_recv);
226
227 memset(ops, 0, sizeof(ops));
228 op = ops;
229 op->op = GRPC_OP_SEND_INITIAL_METADATA;
230 op->data.send_initial_metadata.count = 0;
231 op->flags = 0;
232 op->reserved = nullptr;
233 op++;
234 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
235 op->flags = 0;
236 op->reserved = nullptr;
237 op++;
238 op->op = GRPC_OP_RECV_INITIAL_METADATA;
239 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
240 op->flags = 0;
241 op->reserved = nullptr;
242 op++;
243 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
244 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
245 op->data.recv_status_on_client.status = &status;
246 op->data.recv_status_on_client.status_details = &details;
247 op->flags = 0;
248 op->reserved = nullptr;
249 op++;
250 error = grpc_call_start_batch(state.call, ops, static_cast<size_t>(op - ops),
251 tag(1), nullptr);
252
253 GPR_ASSERT(GRPC_CALL_OK == error);
254
255 cqv.Expect(tag(1), true);
256 cqv.Verify();
257
258 GPR_ASSERT(status == expected_status);
259 if (expected_detail != nullptr) {
260 GPR_ASSERT(-1 != grpc_slice_slice(details, grpc_slice_from_static_string(
261 expected_detail)));
262 }
263
264 grpc_metadata_array_destroy(&initial_metadata_recv);
265 grpc_metadata_array_destroy(&trailing_metadata_recv);
266 grpc_slice_unref(details);
267 }
268
269 static void cleanup_rpc() {
270 grpc_event ev;
271 grpc_slice_buffer_destroy(&state.temp_incoming_buffer);
272 grpc_slice_buffer_destroy(&state.outgoing_buffer);
273 grpc_call_unref(state.call);
274 grpc_completion_queue_shutdown(state.cq);
275 do {
276 ev = grpc_completion_queue_next(state.cq, n_sec_deadline(1), nullptr);
277 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
278 grpc_completion_queue_destroy(state.cq);
279 grpc_channel_destroy(state.channel);
280 state.target.clear();
281 }
282
283 typedef struct {
284 test_tcp_server* server;
285 gpr_event* signal_when_done;
286 } poll_args;
287
288 static void actually_poll_server(void* arg) {
289 poll_args* pa = static_cast<poll_args*>(arg);
290 gpr_timespec deadline = n_sec_deadline(5);
291 while (true) {
292 bool done = gpr_atm_acq_load(&state.done_atm) != 0;
293 gpr_timespec time_left =
294 gpr_time_sub(deadline, gpr_now(GPR_CLOCK_REALTIME));
295 gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64 ".%09d", done,
296 time_left.tv_sec, time_left.tv_nsec);
297 if (done || gpr_time_cmp(time_left, gpr_time_0(GPR_TIMESPAN)) < 0) {
298 break;
299 }
300 test_tcp_server_poll(pa->server, 1000);
301 }
302 gpr_event_set(pa->signal_when_done, reinterpret_cast<void*>(1));
303 gpr_free(pa);
304 }
305
306 static grpc_core::Thread* poll_server_until_read_done(
307 test_tcp_server* server, gpr_event* signal_when_done) {
308 gpr_atm_rel_store(&state.done_atm, 0);
309 state.connection_attempt_made = false;
310 poll_args* pa = static_cast<poll_args*>(gpr_malloc(sizeof(*pa)));
311 pa->server = server;
312 pa->signal_when_done = signal_when_done;
313 auto* th =
314 new grpc_core::Thread("grpc_poll_server", actually_poll_server, pa);
315 th->Start();
316 return th;
317 }
318
319 static void run_test(bool http2_response, bool send_settings,
320 const char* response_payload,
321 size_t response_payload_length,
322 grpc_status_code expected_status,
323 const char* expected_detail) {
324 test_tcp_server test_server;
325 grpc_core::ExecCtx exec_ctx;
326 gpr_event ev;
327
328 grpc_init();
329 gpr_event_init(&ev);
330 server_port = grpc_pick_unused_port_or_die();
331 test_tcp_server_init(&test_server, on_connect, &test_server);
332 test_tcp_server_start(&test_server, server_port);
333 state.http2_response = http2_response;
334 state.send_settings = send_settings;
335 state.response_payload = response_payload;
336 state.response_payload_length = response_payload_length;
337
338 /* poll server until sending out the response */
339 std::unique_ptr<grpc_core::Thread> thdptr(
340 poll_server_until_read_done(&test_server, &ev));
341 start_rpc(server_port, expected_status, expected_detail);
342 gpr_event_wait(&ev, gpr_inf_future(GPR_CLOCK_REALTIME));
343 thdptr->Join();
344 /* Proof that the server accepted the TCP connection. */
345 GPR_ASSERT(state.connection_attempt_made == true);
346 /* clean up */
347 grpc_endpoint_shutdown(state.tcp, GRPC_ERROR_CREATE("Test Shutdown"));
348 grpc_endpoint_destroy(state.tcp);
349 cleanup_rpc();
350 grpc_core::ExecCtx::Get()->Flush();
351 test_tcp_server_destroy(&test_server);
352
353 grpc_shutdown();
354 }
355
356 int main(int argc, char** argv) {
357 grpc::testing::TestEnvironment env(&argc, argv);
358 grpc_init();
359 /* status defined in hpack static table */
360 run_test(true, true, HTTP2_RESP(204), sizeof(HTTP2_RESP(204)) - 1,
361 GRPC_STATUS_UNKNOWN, HTTP2_DETAIL_MSG(204));
362 run_test(true, true, HTTP2_RESP(206), sizeof(HTTP2_RESP(206)) - 1,
363 GRPC_STATUS_UNKNOWN, HTTP2_DETAIL_MSG(206));
364 run_test(true, true, HTTP2_RESP(304), sizeof(HTTP2_RESP(304)) - 1,
365 GRPC_STATUS_UNKNOWN, HTTP2_DETAIL_MSG(304));
366 run_test(true, true, HTTP2_RESP(400), sizeof(HTTP2_RESP(400)) - 1,
367 GRPC_STATUS_INTERNAL, HTTP2_DETAIL_MSG(400));
368 run_test(true, true, HTTP2_RESP(404), sizeof(HTTP2_RESP(404)) - 1,
369 GRPC_STATUS_UNIMPLEMENTED, HTTP2_DETAIL_MSG(404));
370 run_test(true, true, HTTP2_RESP(500), sizeof(HTTP2_RESP(500)) - 1,
371 GRPC_STATUS_UNKNOWN, HTTP2_DETAIL_MSG(500));
372
373 /* status not defined in hpack static table */
374 run_test(true, true, HTTP2_RESP(401), sizeof(HTTP2_RESP(401)) - 1,
375 GRPC_STATUS_UNAUTHENTICATED, HTTP2_DETAIL_MSG(401));
376 run_test(true, true, HTTP2_RESP(403), sizeof(HTTP2_RESP(403)) - 1,
377 GRPC_STATUS_PERMISSION_DENIED, HTTP2_DETAIL_MSG(403));
378 run_test(true, true, HTTP2_RESP(429), sizeof(HTTP2_RESP(429)) - 1,
379 GRPC_STATUS_UNAVAILABLE, HTTP2_DETAIL_MSG(429));
380 run_test(true, true, HTTP2_RESP(499), sizeof(HTTP2_RESP(499)) - 1,
381 GRPC_STATUS_UNKNOWN, HTTP2_DETAIL_MSG(499));
382 run_test(true, true, HTTP2_RESP(502), sizeof(HTTP2_RESP(502)) - 1,
383 GRPC_STATUS_UNAVAILABLE, HTTP2_DETAIL_MSG(502));
384 run_test(true, true, HTTP2_RESP(503), sizeof(HTTP2_RESP(503)) - 1,
385 GRPC_STATUS_UNAVAILABLE, HTTP2_DETAIL_MSG(503));
386 run_test(true, true, HTTP2_RESP(504), sizeof(HTTP2_RESP(504)) - 1,
387 GRPC_STATUS_UNAVAILABLE, HTTP2_DETAIL_MSG(504));
388 /* unparseable response. RPC should fail immediately due to a connect
389 * failure.
390 */
391 run_test(false, false, UNPARSEABLE_RESP, sizeof(UNPARSEABLE_RESP) - 1,
392 GRPC_STATUS_UNAVAILABLE, nullptr);
393
394 /* http1 response. RPC should fail immediately due to a connect failure. */
395 run_test(false, false, HTTP1_RESP_400, sizeof(HTTP1_RESP_400) - 1,
396 GRPC_STATUS_UNAVAILABLE, nullptr);
397
398 /* http2 response without sending a SETTINGs frame. RPC should fail with
399 * DEADLINE_EXCEEDED since the RPC deadline is lower than the connection
400 * attempt deadline. */
401 run_test(true, false, HTTP2_RESP(404), sizeof(HTTP2_RESP(404)) - 1,
402 GRPC_STATUS_DEADLINE_EXCEEDED, nullptr);
403 grpc_shutdown();
404 return 0;
405 }