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