bad_client.cc 12 KB

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