xref: /aosp_15_r20/external/grpc-grpc/test/core/surface/sequential_connectivity_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <stddef.h>
20 
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24 
25 #include "gtest/gtest.h"
26 
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_security.h>
29 #include <grpc/impl/channel_arg_names.h>
30 #include <grpc/slice.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/time.h>
33 
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/lib/gprpp/host_port.h"
36 #include "src/core/lib/gprpp/thd.h"
37 #include "src/core/lib/iomgr/error.h"
38 #include "test/core/util/port.h"
39 #include "test/core/util/test_config.h"
40 #include "test/core/util/tls_utils.h"
41 
42 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
43 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
44 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
45 
46 typedef struct test_fixture {
47   const char* name;
48   void (*add_server_port)(grpc_server* server, const char* addr);
49   // Have the creds here so all the channels will share the same one to enabled
50   // subchannel sharing if needed.
51   grpc_channel_credentials* creds;
52 } test_fixture;
53 
54 #define NUM_CONNECTIONS 100
55 
56 typedef struct {
57   grpc_server* server;
58   grpc_completion_queue* cq;
59 } server_thread_args;
60 
server_thread_func(void * args)61 static void server_thread_func(void* args) {
62   server_thread_args* a = static_cast<server_thread_args*>(args);
63   grpc_event ev = grpc_completion_queue_next(
64       a->cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
65   ASSERT_EQ(ev.type, GRPC_OP_COMPLETE);
66   ASSERT_EQ(ev.tag, nullptr);
67   ASSERT_EQ(ev.success, true);
68 }
69 
create_test_channel(const char * addr,grpc_channel_credentials * creds,bool share_subchannel)70 static grpc_channel* create_test_channel(const char* addr,
71                                          grpc_channel_credentials* creds,
72                                          bool share_subchannel) {
73   grpc_channel* channel = nullptr;
74   std::vector<grpc_arg> args;
75   args.push_back(grpc_channel_arg_integer_create(
76       const_cast<char*>(GRPC_ARG_USE_LOCAL_SUBCHANNEL_POOL),
77       !share_subchannel));
78   if (creds != nullptr) {
79     args.push_back(grpc_channel_arg_string_create(
80         const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
81         const_cast<char*>("foo.test.google.fr")));
82   }
83   grpc_channel_args channel_args = {args.size(), args.data()};
84   if (creds != nullptr) {
85     channel = grpc_channel_create(addr, creds, &channel_args);
86   } else {
87     grpc_channel_credentials* insecure_creds =
88         grpc_insecure_credentials_create();
89     channel = grpc_channel_create(addr, insecure_creds, &channel_args);
90     grpc_channel_credentials_release(insecure_creds);
91   }
92   return channel;
93 }
94 
run_test(const test_fixture * fixture,bool share_subchannel)95 static void run_test(const test_fixture* fixture, bool share_subchannel) {
96   gpr_log(GPR_INFO, "TEST: %s sharing subchannel: %d", fixture->name,
97           share_subchannel);
98 
99   std::string addr =
100       grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
101 
102   grpc_server* server = grpc_server_create(nullptr, nullptr);
103   fixture->add_server_port(server, addr.c_str());
104   grpc_completion_queue* server_cq =
105       grpc_completion_queue_create_for_next(nullptr);
106   grpc_server_register_completion_queue(server, server_cq, nullptr);
107   grpc_server_start(server);
108 
109   server_thread_args sta = {server, server_cq};
110   grpc_core::Thread server_thread("grpc_server", server_thread_func, &sta);
111   server_thread.Start();
112 
113   grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
114   grpc_channel* channels[NUM_CONNECTIONS];
115   for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
116     channels[i] =
117         create_test_channel(addr.c_str(), fixture->creds, share_subchannel);
118 
119     gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30);
120     grpc_connectivity_state state;
121     while ((state = grpc_channel_check_connectivity_state(channels[i], 1)) !=
122            GRPC_CHANNEL_READY) {
123       grpc_channel_watch_connectivity_state(channels[i], state,
124                                             connect_deadline, cq, nullptr);
125       grpc_event ev = grpc_completion_queue_next(
126           cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
127       ASSERT_EQ(ev.type, GRPC_OP_COMPLETE);
128       ASSERT_EQ(ev.tag, nullptr);
129       ASSERT_EQ(ev.success, true);
130     }
131   }
132 
133   grpc_server_shutdown_and_notify(server, server_cq, nullptr);
134   server_thread.Join();
135 
136   grpc_completion_queue_shutdown(server_cq);
137   grpc_completion_queue_shutdown(cq);
138 
139   while (grpc_completion_queue_next(server_cq,
140                                     gpr_inf_future(GPR_CLOCK_REALTIME), nullptr)
141              .type != GRPC_QUEUE_SHUTDOWN) {
142   }
143   while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
144                                     nullptr)
145              .type != GRPC_QUEUE_SHUTDOWN) {
146   }
147 
148   for (size_t i = 0; i < NUM_CONNECTIONS; i++) {
149     grpc_channel_destroy(channels[i]);
150   }
151 
152   grpc_server_destroy(server);
153   grpc_completion_queue_destroy(server_cq);
154   grpc_completion_queue_destroy(cq);
155 }
156 
insecure_test_add_port(grpc_server * server,const char * addr)157 static void insecure_test_add_port(grpc_server* server, const char* addr) {
158   grpc_server_credentials* server_creds =
159       grpc_insecure_server_credentials_create();
160   grpc_server_add_http2_port(server, addr, server_creds);
161   grpc_server_credentials_release(server_creds);
162 }
163 
secure_test_add_port(grpc_server * server,const char * addr)164 static void secure_test_add_port(grpc_server* server, const char* addr) {
165   std::string server_cert =
166       grpc_core::testing::GetFileContents(SERVER_CERT_PATH);
167   std::string server_key = grpc_core::testing::GetFileContents(SERVER_KEY_PATH);
168   grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {server_key.c_str(),
169                                                   server_cert.c_str()};
170   grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
171       nullptr, &pem_key_cert_pair, 1, 0, nullptr);
172   grpc_server_add_http2_port(server, addr, ssl_creds);
173   grpc_server_credentials_release(ssl_creds);
174 }
175 
TEST(SequentialConnectivityTest,MainTest)176 TEST(SequentialConnectivityTest, MainTest) {
177   grpc_init();
178 
179   const test_fixture insecure_test = {
180       "insecure",
181       insecure_test_add_port,
182       nullptr,
183   };
184   run_test(&insecure_test, /*share_subchannel=*/true);
185   run_test(&insecure_test, /*share_subchannel=*/false);
186 
187   std::string test_root_cert =
188       grpc_core::testing::GetFileContents(CA_CERT_PATH);
189   grpc_channel_credentials* ssl_creds = grpc_ssl_credentials_create(
190       test_root_cert.c_str(), nullptr, nullptr, nullptr);
191   const test_fixture secure_test = {
192       "secure",
193       secure_test_add_port,
194       ssl_creds,
195   };
196   run_test(&secure_test, /*share_subchannel=*/true);
197   run_test(&secure_test, /*share_subchannel=*/false);
198   grpc_channel_credentials_release(ssl_creds);
199 
200   grpc_shutdown();
201 }
202 
main(int argc,char ** argv)203 int main(int argc, char** argv) {
204   grpc::testing::TestEnvironment env(&argc, argv);
205   ::testing::InitGoogleTest(&argc, argv);
206   grpc::testing::TestGrpcScope grpc_scope;
207   return RUN_ALL_TESTS();
208 }
209