Debian Codegraph @

test/core/bad_client/bad_client.cc

raw
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/bad_client/bad_client.h"
20
21 #include <inttypes.h>
22 #include <limits.h>
23
24 #include <grpc/slice_buffer.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/sync.h>
28 #include <grpc/support/time.h>
29
30 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
31 #include "src/core/lib/channel/channel_args_preconditioning.h"
32 #include "src/core/lib/channel/channelz.h"
33 #include "src/core/lib/config/core_configuration.h"
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/gprpp/thd.h"
36 #include "src/core/lib/iomgr/closure.h"
37 #include "src/core/lib/iomgr/endpoint.h"
38 #include "src/core/lib/iomgr/endpoint_pair.h"
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/iomgr/exec_ctx.h"
41 #include "src/core/lib/surface/completion_queue.h"
42 #include "src/core/lib/surface/server.h"
43 #include "src/core/lib/transport/transport_fwd.h"
44 #include "test/core/end2end/cq_verifier.h"
45 #include "test/core/util/test_config.h"
46
47 #define MIN_HTTP2_FRAME_SIZE 9
48
49 /* Args to provide to thread running server side validator */
50 typedef struct {
51 grpc_server* server;
52 grpc_completion_queue* cq;
53 grpc_bad_client_server_side_validator validator;
54 void* registered_method;
55 gpr_event done_thd;
56 } thd_args;
57
58 /* Run the server side validator and set done_thd once done */
59 static void thd_func(void* arg) {
60 thd_args* a = static_cast<thd_args*>(arg);
61 if (a->validator != nullptr) {
62 a->validator(a->server, a->cq, a->registered_method);
63 }
64 gpr_event_set(&a->done_thd, reinterpret_cast<void*>(1));
65 }
66
67 /* Sets the done_write event */
68 static void set_done_write(void* arg, grpc_error_handle /*error*/) {
69 gpr_event* done_write = static_cast<gpr_event*>(arg);
70 gpr_event_set(done_write, reinterpret_cast<void*>(1));
71 }
72
73 static void server_setup_transport(void* ts, grpc_transport* transport) {
74 thd_args* a = static_cast<thd_args*>(ts);
75 grpc_core::ExecCtx exec_ctx;
76 grpc_core::Server* core_server = grpc_core::Server::FromC(a->server);
77 GPR_ASSERT(GRPC_LOG_IF_ERROR(
78 "SetupTransport",
79 core_server->SetupTransport(transport, /*accepting_pollset=*/nullptr,
80 core_server->channel_args(),
81 /*socket_node=*/nullptr)));
82 }
83
84 /* Sets the read_done event */
85 static void set_read_done(void* arg, grpc_error_handle /*error*/) {
86 gpr_event* read_done = static_cast<gpr_event*>(arg);
87 gpr_event_set(read_done, reinterpret_cast<void*>(1));
88 }
89
90 /* shutdown client */
91 static void shutdown_client(grpc_endpoint** client_fd) {
92 if (*client_fd != nullptr) {
93 grpc_endpoint_shutdown(*client_fd, GRPC_ERROR_CREATE("Forced Disconnect"));
94 grpc_endpoint_destroy(*client_fd);
95 grpc_core::ExecCtx::Get()->Flush();
96 *client_fd = nullptr;
97 }
98 }
99
100 /* Runs client side validator */
101 void grpc_run_client_side_validator(grpc_bad_client_arg* arg, uint32_t flags,
102 grpc_endpoint_pair* sfd,
103 grpc_completion_queue* client_cq) {
104 char* hex;
105 gpr_event done_write;
106 if (arg->client_payload_length < 4 * 1024) {
107 hex = gpr_dump(arg->client_payload, arg->client_payload_length,
108 GPR_DUMP_HEX | GPR_DUMP_ASCII);
109 /* Add a debug log */
110 gpr_log(GPR_INFO, "TEST: %s", hex);
111 gpr_free(hex);
112 } else {
113 gpr_log(GPR_INFO, "TEST: (%" PRIdPTR " byte long string)",
114 arg->client_payload_length);
115 }
116
117 grpc_slice slice = grpc_slice_from_copied_buffer(arg->client_payload,
118 arg->client_payload_length);
119 grpc_slice_buffer outgoing;
120 grpc_closure done_write_closure;
121 gpr_event_init(&done_write);
122
123 grpc_slice_buffer_init(&outgoing);
124 grpc_slice_buffer_add(&outgoing, slice);
125 GRPC_CLOSURE_INIT(&done_write_closure, set_done_write, &done_write,
126 grpc_schedule_on_exec_ctx);
127
128 /* Write data */
129 grpc_endpoint_write(sfd->client, &outgoing, &done_write_closure, nullptr,
130 /*max_frame_size=*/INT_MAX);
131 grpc_core::ExecCtx::Get()->Flush();
132
133 /* Await completion, unless the request is large and write may not finish
134 * before the peer shuts down. */
135 if (!(flags & GRPC_BAD_CLIENT_LARGE_REQUEST)) {
136 GPR_ASSERT(
137 gpr_event_wait(&done_write, grpc_timeout_seconds_to_deadline(5)));
138 }
139
140 if (flags & GRPC_BAD_CLIENT_DISCONNECT) {
141 shutdown_client(&sfd->client);
142 }
143
144 if (sfd->client != nullptr) {
145 /* Validate client stream, if requested. */
146 if (arg->client_validator != nullptr) {
147 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
148 grpc_slice_buffer incoming;
149 grpc_slice_buffer_init(&incoming);
150 /* We may need to do multiple reads to read the complete server
151 * response. */
152 while (true) {
153 gpr_event read_done_event;
154 gpr_event_init(&read_done_event);
155 grpc_closure read_done_closure;
156 GRPC_CLOSURE_INIT(&read_done_closure, set_read_done, &read_done_event,
157 grpc_schedule_on_exec_ctx);
158 grpc_endpoint_read(sfd->client, &incoming, &read_done_closure,
159 /*urgent=*/true, /*min_progress_size=*/1);
160 grpc_core::ExecCtx::Get()->Flush();
161 do {
162 GPR_ASSERT(gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) > 0);
163 /* Perform a cq next just to provide a thread that can read incoming
164 bytes on the client fd */
165 GPR_ASSERT(grpc_completion_queue_next(
166 client_cq, grpc_timeout_milliseconds_to_deadline(100),
167 nullptr)
168 .type == GRPC_QUEUE_TIMEOUT);
169 } while (!gpr_event_get(&read_done_event));
170 if (arg->client_validator(&incoming, arg->client_validator_arg)) break;
171 gpr_log(GPR_INFO,
172 "client validator failed; trying additional read "
173 "in case we didn't get all the data");
174 }
175 grpc_slice_buffer_destroy(&incoming);
176 }
177 grpc_core::ExecCtx::Get()->Flush();
178 }
179
180 /* If the request was too large, then we need to forcefully shut down the
181 * client, so that the write can be considered completed */
182 if (flags & GRPC_BAD_CLIENT_LARGE_REQUEST) {
183 shutdown_client(&sfd->client);
184 }
185
186 /* Make sure that the client is done writing */
187 while (!gpr_event_get(&done_write)) {
188 GPR_ASSERT(
189 grpc_completion_queue_next(
190 client_cq, grpc_timeout_milliseconds_to_deadline(100), nullptr)
191 .type == GRPC_QUEUE_TIMEOUT);
192 }
193
194 grpc_slice_buffer_destroy(&outgoing);
195 grpc_core::ExecCtx::Get()->Flush();
196 }
197
198 void grpc_run_bad_client_test(
199 grpc_bad_client_server_side_validator server_validator,
200 grpc_bad_client_arg args[], int num_args, uint32_t flags) {
201 grpc_endpoint_pair sfd;
202 thd_args a;
203 grpc_transport* transport;
204 grpc_core::ExecCtx exec_ctx;
205 grpc_completion_queue* shutdown_cq;
206 grpc_completion_queue* client_cq;
207
208 /* Init grpc */
209 grpc_init();
210
211 sfd = grpc_iomgr_create_endpoint_pair("fixture", nullptr);
212 /* Create server, completion events */
213 a.server = grpc_server_create(nullptr, nullptr);
214 a.cq = grpc_completion_queue_create_for_next(nullptr);
215 client_cq = grpc_completion_queue_create_for_next(nullptr);
216 grpc_server_register_completion_queue(a.server, a.cq, nullptr);
217 a.registered_method =
218 grpc_server_register_method(a.server, GRPC_BAD_CLIENT_REGISTERED_METHOD,
219 GRPC_BAD_CLIENT_REGISTERED_HOST,
220 GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER, 0);
221 grpc_server_start(a.server);
222 transport =
223 grpc_create_chttp2_transport(grpc_core::CoreConfiguration::Get()
224 .channel_args_preconditioning()
225 .PreconditionChannelArgs(nullptr),
226 sfd.server, false);
227 server_setup_transport(&a, transport);
228 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
229
230 /* Bind fds to pollsets */
231 grpc_endpoint_add_to_pollset(sfd.client, grpc_cq_pollset(client_cq));
232 grpc_endpoint_add_to_pollset(sfd.server, grpc_cq_pollset(a.cq));
233
234 /* Check a ground truth */
235 GPR_ASSERT(grpc_core::Server::FromC(a.server)->HasOpenConnections());
236
237 gpr_event_init(&a.done_thd);
238 a.validator = server_validator;
239 /* Start validator */
240
241 grpc_core::Thread server_validator_thd("grpc_bad_client", thd_func, &a);
242 server_validator_thd.Start();
243 for (int i = 0; i < num_args; i++) {
244 grpc_run_client_side_validator(&args[i], i == (num_args - 1) ? flags : 0,
245 &sfd, client_cq);
246 }
247 /* Wait for server thread to finish */
248 GPR_ASSERT(gpr_event_wait(&a.done_thd, grpc_timeout_seconds_to_deadline(1)));
249
250 /* Shutdown. */
251 shutdown_client(&sfd.client);
252 server_validator_thd.Join();
253 shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
254 grpc_server_shutdown_and_notify(a.server, shutdown_cq, nullptr);
255 GPR_ASSERT(grpc_completion_queue_pluck(shutdown_cq, nullptr,
256 grpc_timeout_seconds_to_deadline(1),
257 nullptr)
258 .type == GRPC_OP_COMPLETE);
259 grpc_completion_queue_destroy(shutdown_cq);
260 grpc_server_destroy(a.server);
261 grpc_completion_queue_destroy(a.cq);
262 grpc_completion_queue_destroy(client_cq);
263 grpc_shutdown();
264 }
265
266 bool client_connection_preface_validator(grpc_slice_buffer* incoming,
267 void* /*arg*/) {
268 if (incoming->count < 1) {
269 return false;
270 }
271 grpc_slice slice = incoming->slices[0];
272 /* There should be at least one settings frame present */
273 if (GRPC_SLICE_LENGTH(slice) < MIN_HTTP2_FRAME_SIZE) {
274 return false;
275 }
276 const uint8_t* p = GRPC_SLICE_START_PTR(slice);
277 /* Check the frame type (SETTINGS) */
278 return *(p + 3) == 4;
279 }
280
281 /* connection preface and settings frame to be sent by the client */
282 #define CONNECTION_PREFACE_FROM_CLIENT \
283 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" \
284 "\x00\x00\x00\x04\x00\x00\x00\x00\x00"
285
286 grpc_bad_client_arg connection_preface_arg = {
287 client_connection_preface_validator, nullptr,
288 CONNECTION_PREFACE_FROM_CLIENT, sizeof(CONNECTION_PREFACE_FROM_CLIENT) - 1};
289
290 bool rst_stream_client_validator(grpc_slice_buffer* incoming, void* /*arg*/) {
291 // Get last frame from incoming slice buffer.
292 constexpr int kExpectedFrameLength = 13;
293 if (incoming->length < kExpectedFrameLength) return false;
294 grpc_slice_buffer last_frame_buffer;
295 grpc_slice_buffer_init(&last_frame_buffer);
296 grpc_slice_buffer_trim_end(incoming, kExpectedFrameLength,
297 &last_frame_buffer);
298 GPR_ASSERT(last_frame_buffer.count == 1);
299 grpc_slice last_frame = last_frame_buffer.slices[0];
300
301 const uint8_t* p = GRPC_SLICE_START_PTR(last_frame);
302 bool success =
303 // Length == 4
304 *p++ != 0 || *p++ != 0 || *p++ != 4 ||
305 // Frame type (RST_STREAM)
306 *p++ != 3 ||
307 // Flags
308 *p++ != 0 ||
309 // Stream ID.
310 *p++ != 0 || *p++ != 0 || *p++ != 0 || *p++ != 1 ||
311 // Payload (error code)
312 *p++ == 0 || *p++ == 0 || *p++ == 0 || *p == 0 || *p == 11;
313
314 if (!success) {
315 gpr_log(GPR_INFO, "client expected RST_STREAM frame, not found");
316 }
317
318 grpc_slice_buffer_destroy(&last_frame_buffer);
319 return success;
320 }
321
322 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
323
324 void server_verifier_request_call(grpc_server* server,
325 grpc_completion_queue* cq,
326 void* /*registered_method*/) {
327 grpc_call_error error;
328 grpc_call* s;
329 grpc_call_details call_details;
330 grpc_core::CqVerifier cqv(cq);
331 grpc_metadata_array request_metadata_recv;
332
333 grpc_call_details_init(&call_details);
334 grpc_metadata_array_init(&request_metadata_recv);
335
336 error = grpc_server_request_call(server, &s, &call_details,
337 &request_metadata_recv, cq, cq, tag(101));
338 GPR_ASSERT(GRPC_CALL_OK == error);
339 cqv.Expect(tag(101), true);
340 cqv.Verify();
341
342 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.host, "localhost"));
343 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo/bar"));
344
345 grpc_metadata_array_destroy(&request_metadata_recv);
346 grpc_call_details_destroy(&call_details);
347 grpc_call_unref(s);
348 }