server_common.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_ssl/server_common.h"
  19. #include <signal.h>
  20. #include <grpc/support/log.h>
  21. #include "test/core/util/cmdline.h"
  22. #include "test/core/util/test_config.h"
  23. /* Common server implementation details for all servers in servers/.
  24. * There's nothing *wrong* with these servers per-se, but they are
  25. * configured to cause some failure case in the SSL connection path.
  26. */
  27. static int got_sigint = 0;
  28. static void sigint_handler(int /*x*/) { got_sigint = 1; }
  29. const char* bad_ssl_addr(int argc, char** argv) {
  30. gpr_cmdline* cl;
  31. const char* addr = nullptr;
  32. cl = gpr_cmdline_create("test server");
  33. gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
  34. gpr_cmdline_parse(cl, argc, argv);
  35. gpr_cmdline_destroy(cl);
  36. GPR_ASSERT(addr);
  37. return addr;
  38. }
  39. void bad_ssl_run(grpc_server* server) {
  40. int shutdown_started = 0;
  41. int shutdown_finished = 0;
  42. grpc_event ev;
  43. grpc_call_error error;
  44. grpc_call* s = nullptr;
  45. grpc_call_details call_details;
  46. grpc_metadata_array request_metadata_recv;
  47. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  48. grpc_completion_queue* shutdown_cq;
  49. grpc_call_details_init(&call_details);
  50. grpc_metadata_array_init(&request_metadata_recv);
  51. grpc_server_register_completion_queue(server, cq, nullptr);
  52. grpc_server_start(server);
  53. error = grpc_server_request_call(server, &s, &call_details,
  54. &request_metadata_recv, cq, cq,
  55. reinterpret_cast<void*>(1));
  56. GPR_ASSERT(GRPC_CALL_OK == error);
  57. signal(SIGINT, sigint_handler);
  58. while (!shutdown_finished) {
  59. if (got_sigint && !shutdown_started) {
  60. gpr_log(GPR_INFO, "Shutting down due to SIGINT");
  61. shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  62. grpc_server_shutdown_and_notify(server, shutdown_cq, nullptr);
  63. GPR_ASSERT(grpc_completion_queue_pluck(
  64. shutdown_cq, nullptr, grpc_timeout_seconds_to_deadline(5),
  65. nullptr)
  66. .type == GRPC_OP_COMPLETE);
  67. grpc_completion_queue_destroy(shutdown_cq);
  68. grpc_completion_queue_shutdown(cq);
  69. shutdown_started = 1;
  70. }
  71. ev = grpc_completion_queue_next(
  72. cq,
  73. gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  74. gpr_time_from_micros(1000000, GPR_TIMESPAN)),
  75. nullptr);
  76. switch (ev.type) {
  77. case GRPC_OP_COMPLETE:
  78. GPR_ASSERT(ev.tag == (void*)1);
  79. GPR_ASSERT(ev.success == 0);
  80. break;
  81. case GRPC_QUEUE_SHUTDOWN:
  82. GPR_ASSERT(shutdown_started);
  83. shutdown_finished = 1;
  84. break;
  85. case GRPC_QUEUE_TIMEOUT:
  86. break;
  87. }
  88. }
  89. GPR_ASSERT(s == nullptr);
  90. grpc_call_details_destroy(&call_details);
  91. grpc_metadata_array_destroy(&request_metadata_recv);
  92. }