bad_client.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "test/core/bad_client/bad_client.h"
  34. #include "src/core/channel/channel_stack.h"
  35. #include "src/core/channel/http_server_filter.h"
  36. #include "src/core/iomgr/endpoint_pair.h"
  37. #include "src/core/surface/completion_queue.h"
  38. #include "src/core/surface/server.h"
  39. #include "src/core/support/string.h"
  40. #include "src/core/transport/chttp2_transport.h"
  41. #include <grpc/support/alloc.h>
  42. #include <grpc/support/sync.h>
  43. #include <grpc/support/thd.h>
  44. typedef struct
  45. {
  46. grpc_server *server;
  47. grpc_completion_queue *cq;
  48. grpc_bad_client_server_side_validator validator;
  49. gpr_event done_thd;
  50. gpr_event done_write;
  51. } thd_args;
  52. static void
  53. thd_func (void *arg)
  54. {
  55. thd_args *a = arg;
  56. a->validator (a->server, a->cq);
  57. gpr_event_set (&a->done_thd, (void *) 1);
  58. }
  59. static void
  60. done_write (grpc_exec_ctx * exec_ctx, void *arg, int success)
  61. {
  62. thd_args *a = arg;
  63. gpr_event_set (&a->done_write, (void *) 1);
  64. }
  65. static void
  66. server_setup_transport (void *ts, grpc_transport * transport, grpc_mdctx * mdctx)
  67. {
  68. thd_args *a = ts;
  69. static grpc_channel_filter const *extra_filters[] = {
  70. &grpc_http_server_filter
  71. };
  72. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  73. grpc_server_setup_transport (a->server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (a->server), &closure_list);
  74. grpc_exec_ctx_finish (&exec_ctx);
  75. }
  76. void
  77. grpc_run_bad_client_test (grpc_bad_client_server_side_validator validator, const char *client_payload, size_t client_payload_length, gpr_uint32 flags)
  78. {
  79. grpc_endpoint_pair sfd;
  80. thd_args a;
  81. gpr_thd_id id;
  82. char *hex;
  83. grpc_transport *transport;
  84. grpc_mdctx *mdctx = grpc_mdctx_create ();
  85. gpr_slice slice = gpr_slice_from_copied_buffer (client_payload, client_payload_length);
  86. gpr_slice_buffer outgoing;
  87. grpc_closure done_write_closure;
  88. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  89. hex = gpr_dump (client_payload, client_payload_length, GPR_DUMP_HEX | GPR_DUMP_ASCII);
  90. /* Add a debug log */
  91. gpr_log (GPR_INFO, "TEST: %s", hex);
  92. gpr_free (hex);
  93. /* Init grpc */
  94. grpc_init ();
  95. /* Create endpoints */
  96. sfd = grpc_iomgr_create_endpoint_pair ("fixture", 65536);
  97. /* Create server, completion events */
  98. a.server = grpc_server_create_from_filters (NULL, 0, NULL);
  99. a.cq = grpc_completion_queue_create (NULL);
  100. gpr_event_init (&a.done_thd);
  101. gpr_event_init (&a.done_write);
  102. a.validator = validator;
  103. grpc_server_register_completion_queue (a.server, a.cq, NULL);
  104. grpc_server_start (a.server);
  105. transport = grpc_create_chttp2_transport (NULL, sfd.server, mdctx, 0, &closure_list);
  106. server_setup_transport (&a, transport, mdctx);
  107. grpc_chttp2_transport_start_reading (transport, NULL, 0, &closure_list);
  108. grpc_exec_ctx_finish (&exec_ctx);
  109. /* Bind everything into the same pollset */
  110. grpc_endpoint_add_to_pollset (sfd.client, grpc_cq_pollset (a.cq), &closure_list);
  111. grpc_endpoint_add_to_pollset (sfd.server, grpc_cq_pollset (a.cq), &closure_list);
  112. /* Check a ground truth */
  113. GPR_ASSERT (grpc_server_has_open_connections (a.server));
  114. /* Start validator */
  115. gpr_thd_new (&id, thd_func, &a, NULL);
  116. gpr_slice_buffer_init (&outgoing);
  117. gpr_slice_buffer_add (&outgoing, slice);
  118. grpc_closure_init (&done_write_closure, done_write, &a);
  119. /* Write data */
  120. grpc_endpoint_write (sfd.client, &outgoing, &done_write_closure, &closure_list);
  121. grpc_exec_ctx_finish (&exec_ctx);
  122. /* Await completion */
  123. GPR_ASSERT (gpr_event_wait (&a.done_write, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5)));
  124. if (flags & GRPC_BAD_CLIENT_DISCONNECT)
  125. {
  126. grpc_endpoint_shutdown (sfd.client, &closure_list);
  127. grpc_endpoint_destroy (sfd.client, &closure_list);
  128. grpc_exec_ctx_finish (&exec_ctx);
  129. sfd.client = NULL;
  130. }
  131. GPR_ASSERT (gpr_event_wait (&a.done_thd, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (5)));
  132. /* Shutdown */
  133. if (sfd.client)
  134. {
  135. grpc_endpoint_shutdown (sfd.client, &closure_list);
  136. grpc_endpoint_destroy (sfd.client, &closure_list);
  137. grpc_exec_ctx_finish (&exec_ctx);
  138. }
  139. grpc_server_shutdown_and_notify (a.server, a.cq, NULL);
  140. GPR_ASSERT (grpc_completion_queue_pluck (a.cq, NULL, GRPC_TIMEOUT_SECONDS_TO_DEADLINE (1), NULL).type == GRPC_OP_COMPLETE);
  141. grpc_server_destroy (a.server);
  142. grpc_completion_queue_destroy (a.cq);
  143. gpr_slice_buffer_destroy (&outgoing);
  144. grpc_exec_ctx_finish (&exec_ctx);
  145. grpc_shutdown ();
  146. }