• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 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 <grpc/grpc.h>
20 #include <grpc/grpc_security.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 
24 #include "src/core/lib/channel/channel_args.h"
25 #include "src/core/lib/gprpp/host_port.h"
26 #include "src/core/lib/gprpp/memory.h"
27 #include "src/core/lib/gprpp/thd.h"
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/iomgr/load_file.h"
30 #include "test/core/util/port.h"
31 #include "test/core/util/test_config.h"
32 
33 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
34 
35 typedef struct test_fixture {
36   const char* name;
37   grpc_channel* (*create_channel)(const char* addr);
38 } test_fixture;
39 
40 static size_t next_tag = 1;
41 
channel_idle_start_watch(grpc_channel * channel,grpc_completion_queue * cq)42 static void channel_idle_start_watch(grpc_channel* channel,
43                                      grpc_completion_queue* cq) {
44   gpr_timespec connect_deadline = grpc_timeout_milliseconds_to_deadline(1);
45   GPR_ASSERT(grpc_channel_check_connectivity_state(channel, 0) ==
46              GRPC_CHANNEL_IDLE);
47 
48   grpc_channel_watch_connectivity_state(
49       channel, GRPC_CHANNEL_IDLE, connect_deadline, cq, (void*)(next_tag++));
50   gpr_log(GPR_DEBUG, "number of active connect watchers: %d",
51           grpc_channel_num_external_connectivity_watchers(channel));
52 }
53 
channel_idle_poll_for_timeout(grpc_channel * channel,grpc_completion_queue * cq)54 static void channel_idle_poll_for_timeout(grpc_channel* channel,
55                                           grpc_completion_queue* cq) {
56   grpc_event ev = grpc_completion_queue_next(
57       cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
58 
59   /* expect watch_connectivity_state to end with a timeout */
60   GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
61   GPR_ASSERT(ev.success == false);
62   GPR_ASSERT(grpc_channel_check_connectivity_state(channel, 0) ==
63              GRPC_CHANNEL_IDLE);
64 }
65 
66 /* Test and use the "num_external_watchers" call to make sure
67  * that "connectivity watcher" structs are free'd just after, if
68  * their corresponding timeouts occur. */
run_timeouts_test(const test_fixture * fixture)69 static void run_timeouts_test(const test_fixture* fixture) {
70   gpr_log(GPR_INFO, "TEST: %s", fixture->name);
71 
72   grpc_init();
73   std::string addr =
74       grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
75 
76   grpc_channel* channel = fixture->create_channel(addr.c_str());
77   grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
78 
79   /* start 1 watcher and then let it time out */
80   channel_idle_start_watch(channel, cq);
81   channel_idle_poll_for_timeout(channel, cq);
82   GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
83 
84   /* start 3 watchers and then let them all time out */
85   for (size_t i = 1; i <= 3; i++) {
86     channel_idle_start_watch(channel, cq);
87   }
88   for (size_t i = 1; i <= 3; i++) {
89     channel_idle_poll_for_timeout(channel, cq);
90   }
91   GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
92 
93   /* start 3 watchers, see one time out, start another 3, and then see them all
94    * time out */
95   for (size_t i = 1; i <= 3; i++) {
96     channel_idle_start_watch(channel, cq);
97   }
98   channel_idle_poll_for_timeout(channel, cq);
99   for (size_t i = 3; i <= 5; i++) {
100     channel_idle_start_watch(channel, cq);
101   }
102   for (size_t i = 1; i <= 5; i++) {
103     channel_idle_poll_for_timeout(channel, cq);
104   }
105   GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
106 
107   grpc_channel_destroy(channel);
108   grpc_completion_queue_shutdown(cq);
109   GPR_ASSERT(grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
110                                         nullptr)
111                  .type == GRPC_QUEUE_SHUTDOWN);
112   grpc_completion_queue_destroy(cq);
113 
114   grpc_shutdown();
115 }
116 
117 /* An edge scenario; sets channel state to explicitly, and outside
118  * of a polling call. */
run_channel_shutdown_before_timeout_test(const test_fixture * fixture)119 static void run_channel_shutdown_before_timeout_test(
120     const test_fixture* fixture) {
121   gpr_log(GPR_INFO, "TEST: %s", fixture->name);
122 
123   grpc_init();
124   std::string addr =
125       grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
126 
127   grpc_channel* channel = fixture->create_channel(addr.c_str());
128   grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
129 
130   /* start 1 watcher and then shut down the channel before the timer goes off */
131   GPR_ASSERT(grpc_channel_num_external_connectivity_watchers(channel) == 0);
132 
133   /* expecting a 30 second timeout to go off much later than the shutdown. */
134   gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30);
135   GPR_ASSERT(grpc_channel_check_connectivity_state(channel, 0) ==
136              GRPC_CHANNEL_IDLE);
137 
138   grpc_channel_watch_connectivity_state(channel, GRPC_CHANNEL_IDLE,
139                                         connect_deadline, cq, (void*)1);
140   grpc_channel_destroy(channel);
141 
142   grpc_event ev = grpc_completion_queue_next(
143       cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
144   GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
145   /* expect success with a state transition to CHANNEL_SHUTDOWN */
146   GPR_ASSERT(ev.success == true);
147 
148   grpc_completion_queue_shutdown(cq);
149   GPR_ASSERT(grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
150                                         nullptr)
151                  .type == GRPC_QUEUE_SHUTDOWN);
152   grpc_completion_queue_destroy(cq);
153 
154   grpc_shutdown();
155 }
156 
insecure_test_create_channel(const char * addr)157 static grpc_channel* insecure_test_create_channel(const char* addr) {
158   return grpc_insecure_channel_create(addr, nullptr, nullptr);
159 }
160 
161 static const test_fixture insecure_test = {
162     "insecure",
163     insecure_test_create_channel,
164 };
165 
secure_test_create_channel(const char * addr)166 static grpc_channel* secure_test_create_channel(const char* addr) {
167   grpc_slice ca_slice;
168   GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
169                                grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
170   const char* test_root_cert =
171       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
172   grpc_channel_credentials* ssl_creds =
173       grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
174   grpc_slice_unref(ca_slice);
175   grpc_arg ssl_name_override = {
176       GRPC_ARG_STRING,
177       const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
178       {const_cast<char*>("foo.test.google.fr")}};
179   grpc_channel_args* new_client_args =
180       grpc_channel_args_copy_and_add(nullptr, &ssl_name_override, 1);
181   grpc_channel* channel =
182       grpc_secure_channel_create(ssl_creds, addr, new_client_args, nullptr);
183   {
184     grpc_core::ExecCtx exec_ctx;
185     grpc_channel_args_destroy(new_client_args);
186   }
187   grpc_channel_credentials_release(ssl_creds);
188   return channel;
189 }
190 
191 static const test_fixture secure_test = {
192     "secure",
193     secure_test_create_channel,
194 };
195 
main(int argc,char ** argv)196 int main(int argc, char** argv) {
197   grpc::testing::TestEnvironment env(argc, argv);
198 
199   run_timeouts_test(&insecure_test);
200   run_timeouts_test(&secure_test);
201 
202   run_channel_shutdown_before_timeout_test(&insecure_test);
203   run_channel_shutdown_before_timeout_test(&secure_test);
204 }
205