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