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