• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2018 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 "src/core/tsi/alts/handshaker/alts_handshaker_client.h"
20 
21 #include <gtest/gtest.h>
22 
23 #include "upb/mem/arena.hpp"
24 
25 #include <grpc/grpc.h>
26 #include <grpc/grpc_security.h>
27 
28 #include "src/core/lib/gprpp/env.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "src/core/tsi/alts/handshaker/alts_shared_resource.h"
31 #include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h"
32 #include "src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h"
33 #include "src/core/tsi/transport_security.h"
34 #include "src/core/tsi/transport_security_interface.h"
35 #include "test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h"
36 #include "test/core/util/test_config.h"
37 
38 #define ALTS_HANDSHAKER_CLIENT_TEST_OUT_FRAME "Hello Google"
39 #define ALTS_HANDSHAKER_CLIENT_TEST_TARGET_NAME "bigtable.google.api.com"
40 #define ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT1 "A@google.com"
41 #define ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT2 "B@google.com"
42 #define ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE (64 * 1024)
43 
44 const char kMaxConcurrentStreamsEnvironmentVariable[] =
45     "GRPC_ALTS_MAX_CONCURRENT_HANDSHAKES";
46 const size_t kHandshakerClientOpNum = 4;
47 const size_t kMaxRpcVersionMajor = 3;
48 const size_t kMaxRpcVersionMinor = 2;
49 const size_t kMinRpcVersionMajor = 2;
50 const size_t kMinRpcVersionMinor = 1;
51 
52 using grpc_core::internal::alts_handshaker_client_get_closure_for_testing;
53 using grpc_core::internal::
54     alts_handshaker_client_get_initial_metadata_for_testing;
55 using grpc_core::internal::
56     alts_handshaker_client_get_recv_buffer_addr_for_testing;
57 using grpc_core::internal::alts_handshaker_client_get_send_buffer_for_testing;
58 using grpc_core::internal::
59     alts_handshaker_client_on_status_received_for_testing;
60 using grpc_core::internal::alts_handshaker_client_set_cb_for_testing;
61 using grpc_core::internal::alts_handshaker_client_set_grpc_caller_for_testing;
62 
63 typedef struct alts_handshaker_client_test_config {
64   grpc_channel* channel;
65   grpc_completion_queue* cq;
66   alts_handshaker_client* client;
67   alts_handshaker_client* server;
68   grpc_slice out_frame;
69 } alts_handshaker_client_test_config;
70 
validate_rpc_protocol_versions(const grpc_gcp_RpcProtocolVersions * versions)71 static void validate_rpc_protocol_versions(
72     const grpc_gcp_RpcProtocolVersions* versions) {
73   ASSERT_NE(versions, nullptr);
74   const grpc_gcp_RpcProtocolVersions_Version* max_version =
75       grpc_gcp_RpcProtocolVersions_max_rpc_version(versions);
76   const grpc_gcp_RpcProtocolVersions_Version* min_version =
77       grpc_gcp_RpcProtocolVersions_min_rpc_version(versions);
78   ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_major(max_version),
79             kMaxRpcVersionMajor);
80   ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_minor(max_version),
81             kMaxRpcVersionMinor);
82   ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_major(min_version),
83             kMinRpcVersionMajor);
84   ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_minor(min_version),
85             kMinRpcVersionMinor);
86 }
87 
validate_target_identities(const grpc_gcp_Identity * const * target_identities,size_t target_identities_count)88 static void validate_target_identities(
89     const grpc_gcp_Identity* const* target_identities,
90     size_t target_identities_count) {
91   ASSERT_EQ(target_identities_count, 2);
92   const grpc_gcp_Identity* identity1 = target_identities[1];
93   const grpc_gcp_Identity* identity2 = target_identities[0];
94   ASSERT_TRUE(upb_StringView_IsEqual(
95       grpc_gcp_Identity_service_account(identity1),
96       upb_StringView_FromString(
97           ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT1)));
98   ASSERT_TRUE(upb_StringView_IsEqual(
99       grpc_gcp_Identity_service_account(identity2),
100       upb_StringView_FromString(
101           ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT2)));
102 }
103 
104 ///
105 /// Validate if grpc operation data is correctly populated with the fields of
106 /// ALTS handshaker client.
107 ///
validate_op(alts_handshaker_client * c,const grpc_op * op,size_t nops,bool is_start)108 static bool validate_op(alts_handshaker_client* c, const grpc_op* op,
109                         size_t nops, bool is_start) {
110   EXPECT_TRUE(c != nullptr && op != nullptr && nops != 0);
111   bool ok = true;
112   grpc_op* start_op = const_cast<grpc_op*>(op);
113   if (is_start) {
114     ok &= (op->op == GRPC_OP_SEND_INITIAL_METADATA);
115     ok &= (op->data.send_initial_metadata.count == 0);
116     op++;
117     EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
118     ok &= (op->op == GRPC_OP_RECV_INITIAL_METADATA);
119     ok &= (op->data.recv_initial_metadata.recv_initial_metadata ==
120            alts_handshaker_client_get_initial_metadata_for_testing(c));
121     op++;
122     EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
123   }
124   ok &= (op->op == GRPC_OP_SEND_MESSAGE);
125   ok &= (op->data.send_message.send_message ==
126          alts_handshaker_client_get_send_buffer_for_testing(c));
127   op++;
128   EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
129   ok &= (op->op == GRPC_OP_RECV_MESSAGE);
130   ok &= (op->data.recv_message.recv_message ==
131          alts_handshaker_client_get_recv_buffer_addr_for_testing(c));
132   op++;
133   EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
134   return ok;
135 }
136 
deserialize_handshaker_req(grpc_byte_buffer * buffer,upb_Arena * arena)137 static grpc_gcp_HandshakerReq* deserialize_handshaker_req(
138     grpc_byte_buffer* buffer, upb_Arena* arena) {
139   EXPECT_NE(buffer, nullptr);
140   grpc_byte_buffer_reader bbr;
141   EXPECT_TRUE(grpc_byte_buffer_reader_init(&bbr, buffer));
142   grpc_slice slice = grpc_byte_buffer_reader_readall(&bbr);
143   grpc_gcp_HandshakerReq* req = grpc_gcp_handshaker_req_decode(slice, arena);
144   EXPECT_NE(req, nullptr);
145   grpc_slice_unref(slice);
146   grpc_byte_buffer_reader_destroy(&bbr);
147   return req;
148 }
149 
is_recv_status_op(const grpc_op * op,size_t nops)150 static bool is_recv_status_op(const grpc_op* op, size_t nops) {
151   return nops == 1 && op->op == GRPC_OP_RECV_STATUS_ON_CLIENT;
152 }
153 
154 ///
155 /// A mock grpc_caller used to check if client_start, server_start, and next
156 /// operations correctly handle invalid arguments. It should not be called.
157 ///
check_must_not_be_called(grpc_call *,const grpc_op *,size_t,grpc_closure *)158 static grpc_call_error check_must_not_be_called(grpc_call* /*call*/,
159                                                 const grpc_op* /*ops*/,
160                                                 size_t /*nops*/,
161                                                 grpc_closure* /*tag*/) {
162   abort();
163 }
164 
165 ///
166 /// A mock grpc_caller used to check correct execution of client_start
167 /// operation. It checks if the client_start handshaker request is populated
168 /// with correct handshake_security_protocol, application_protocol,
169 /// record_protocol and max_frame_size, and op is correctly populated.
170 ///
check_client_start_success(grpc_call *,const grpc_op * op,size_t nops,grpc_closure * closure)171 static grpc_call_error check_client_start_success(grpc_call* /*call*/,
172                                                   const grpc_op* op,
173                                                   size_t nops,
174                                                   grpc_closure* closure) {
175   // RECV_STATUS ops are asserted to always succeed
176   if (is_recv_status_op(op, nops)) {
177     return GRPC_CALL_OK;
178   }
179   upb::Arena arena;
180   alts_handshaker_client* client =
181       static_cast<alts_handshaker_client*>(closure->cb_arg);
182   EXPECT_EQ(alts_handshaker_client_get_closure_for_testing(client), closure);
183   grpc_gcp_HandshakerReq* req = deserialize_handshaker_req(
184       alts_handshaker_client_get_send_buffer_for_testing(client), arena.ptr());
185   const grpc_gcp_StartClientHandshakeReq* client_start =
186       grpc_gcp_HandshakerReq_client_start(req);
187   EXPECT_EQ(grpc_gcp_StartClientHandshakeReq_handshake_security_protocol(
188                 client_start),
189             grpc_gcp_ALTS);
190   upb_StringView const* application_protocols =
191       grpc_gcp_StartClientHandshakeReq_application_protocols(client_start,
192                                                              nullptr);
193   EXPECT_TRUE(upb_StringView_IsEqual(
194       application_protocols[0],
195       upb_StringView_FromString(ALTS_APPLICATION_PROTOCOL)));
196   upb_StringView const* record_protocols =
197       grpc_gcp_StartClientHandshakeReq_record_protocols(client_start, nullptr);
198   EXPECT_TRUE(upb_StringView_IsEqual(
199       record_protocols[0], upb_StringView_FromString(ALTS_RECORD_PROTOCOL)));
200   const grpc_gcp_RpcProtocolVersions* rpc_protocol_versions =
201       grpc_gcp_StartClientHandshakeReq_rpc_versions(client_start);
202   validate_rpc_protocol_versions(rpc_protocol_versions);
203   size_t target_identities_count;
204   const grpc_gcp_Identity* const* target_identities =
205       grpc_gcp_StartClientHandshakeReq_target_identities(
206           client_start, &target_identities_count);
207   validate_target_identities(target_identities, target_identities_count);
208   EXPECT_TRUE(upb_StringView_IsEqual(
209       grpc_gcp_StartClientHandshakeReq_target_name(client_start),
210       upb_StringView_FromString(ALTS_HANDSHAKER_CLIENT_TEST_TARGET_NAME)));
211   EXPECT_EQ(grpc_gcp_StartClientHandshakeReq_max_frame_size(client_start),
212             ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE);
213   EXPECT_TRUE(validate_op(client, op, nops, true /* is_start */));
214   return GRPC_CALL_OK;
215 }
216 
217 ///
218 /// A mock grpc_caller used to check correct execution of server_start
219 /// operation. It checks if the server_start handshaker request is populated
220 /// with correct handshake_security_protocol, application_protocol,
221 /// record_protocol and max_frame_size, and op is correctly populated.
222 ///
check_server_start_success(grpc_call *,const grpc_op * op,size_t nops,grpc_closure * closure)223 static grpc_call_error check_server_start_success(grpc_call* /*call*/,
224                                                   const grpc_op* op,
225                                                   size_t nops,
226                                                   grpc_closure* closure) {
227   // RECV_STATUS ops are asserted to always succeed
228   if (is_recv_status_op(op, nops)) {
229     return GRPC_CALL_OK;
230   }
231   upb::Arena arena;
232   alts_handshaker_client* client =
233       static_cast<alts_handshaker_client*>(closure->cb_arg);
234   EXPECT_EQ(alts_handshaker_client_get_closure_for_testing(client), closure);
235   grpc_gcp_HandshakerReq* req = deserialize_handshaker_req(
236       alts_handshaker_client_get_send_buffer_for_testing(client), arena.ptr());
237   const grpc_gcp_StartServerHandshakeReq* server_start =
238       grpc_gcp_HandshakerReq_server_start(req);
239   upb_StringView const* application_protocols =
240       grpc_gcp_StartServerHandshakeReq_application_protocols(server_start,
241                                                              nullptr);
242   EXPECT_TRUE(upb_StringView_IsEqual(
243       application_protocols[0],
244       upb_StringView_FromString(ALTS_APPLICATION_PROTOCOL)));
245   EXPECT_EQ(
246       grpc_gcp_StartServerHandshakeReq_handshake_parameters_size(server_start),
247       1);
248   grpc_gcp_ServerHandshakeParameters* value;
249   EXPECT_TRUE(grpc_gcp_StartServerHandshakeReq_handshake_parameters_get(
250       server_start, grpc_gcp_ALTS, &value));
251   upb_StringView const* record_protocols =
252       grpc_gcp_ServerHandshakeParameters_record_protocols(value, nullptr);
253   EXPECT_TRUE(upb_StringView_IsEqual(
254       record_protocols[0], upb_StringView_FromString(ALTS_RECORD_PROTOCOL)));
255   validate_rpc_protocol_versions(
256       grpc_gcp_StartServerHandshakeReq_rpc_versions(server_start));
257   EXPECT_EQ(grpc_gcp_StartServerHandshakeReq_max_frame_size(server_start),
258             ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE);
259   EXPECT_TRUE(validate_op(client, op, nops, true /* is_start */));
260   return GRPC_CALL_OK;
261 }
262 
263 ///
264 /// A mock grpc_caller used to check correct execution of next operation. It
265 /// checks if the next handshaker request is populated with correct information,
266 /// and op is correctly populated.
267 ///
check_next_success(grpc_call *,const grpc_op * op,size_t nops,grpc_closure * closure)268 static grpc_call_error check_next_success(grpc_call* /*call*/,
269                                           const grpc_op* op, size_t nops,
270                                           grpc_closure* closure) {
271   upb::Arena arena;
272   alts_handshaker_client* client =
273       static_cast<alts_handshaker_client*>(closure->cb_arg);
274   EXPECT_EQ(alts_handshaker_client_get_closure_for_testing(client), closure);
275   grpc_gcp_HandshakerReq* req = deserialize_handshaker_req(
276       alts_handshaker_client_get_send_buffer_for_testing(client), arena.ptr());
277   const grpc_gcp_NextHandshakeMessageReq* next =
278       grpc_gcp_HandshakerReq_next(req);
279   EXPECT_TRUE(upb_StringView_IsEqual(
280       grpc_gcp_NextHandshakeMessageReq_in_bytes(next),
281       upb_StringView_FromString(ALTS_HANDSHAKER_CLIENT_TEST_OUT_FRAME)));
282   EXPECT_TRUE(validate_op(client, op, nops, false /* is_start */));
283   return GRPC_CALL_OK;
284 }
285 
286 ///
287 /// A mock grpc_caller used to check if client_start, server_start, and next
288 /// operations correctly handle the situation when the grpc call made to the
289 /// handshaker service fails.
290 ///
check_grpc_call_failure(grpc_call *,const grpc_op * op,size_t nops,grpc_closure *)291 static grpc_call_error check_grpc_call_failure(grpc_call* /*call*/,
292                                                const grpc_op* op, size_t nops,
293                                                grpc_closure* /*tag*/) {
294   // RECV_STATUS ops are asserted to always succeed
295   if (is_recv_status_op(op, nops)) {
296     return GRPC_CALL_OK;
297   }
298   return GRPC_CALL_ERROR;
299 }
300 
create_credentials_options(bool is_client)301 static grpc_alts_credentials_options* create_credentials_options(
302     bool is_client) {
303   grpc_alts_credentials_options* options =
304       is_client ? grpc_alts_credentials_client_options_create()
305                 : grpc_alts_credentials_server_options_create();
306   if (is_client) {
307     grpc_alts_credentials_client_options_add_target_service_account(
308         options, ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT1);
309     grpc_alts_credentials_client_options_add_target_service_account(
310         options, ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT2);
311   }
312   grpc_gcp_rpc_protocol_versions* versions = &options->rpc_versions;
313   EXPECT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(
314       versions, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
315   EXPECT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(
316       versions, kMinRpcVersionMajor, kMinRpcVersionMinor));
317   return options;
318 }
319 
create_config()320 static alts_handshaker_client_test_config* create_config() {
321   alts_handshaker_client_test_config* config =
322       static_cast<alts_handshaker_client_test_config*>(
323           gpr_zalloc(sizeof(*config)));
324   grpc_channel_credentials* creds = grpc_insecure_credentials_create();
325   config->channel = grpc_channel_create(ALTS_HANDSHAKER_SERVICE_URL_FOR_TESTING,
326                                         creds, nullptr);
327   grpc_channel_credentials_release(creds);
328   config->cq = grpc_completion_queue_create_for_next(nullptr);
329   grpc_alts_credentials_options* client_options =
330       create_credentials_options(true /* is_client */);
331   grpc_alts_credentials_options* server_options =
332       create_credentials_options(false /*  is_client */);
333   config->server = alts_grpc_handshaker_client_create(
334       nullptr, config->channel, ALTS_HANDSHAKER_SERVICE_URL_FOR_TESTING,
335       nullptr, server_options,
336       grpc_slice_from_static_string(ALTS_HANDSHAKER_CLIENT_TEST_TARGET_NAME),
337       nullptr, nullptr, nullptr, nullptr, false,
338       ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE, nullptr);
339   config->client = alts_grpc_handshaker_client_create(
340       nullptr, config->channel, ALTS_HANDSHAKER_SERVICE_URL_FOR_TESTING,
341       nullptr, client_options,
342       grpc_slice_from_static_string(ALTS_HANDSHAKER_CLIENT_TEST_TARGET_NAME),
343       nullptr, nullptr, nullptr, nullptr, true,
344       ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE, nullptr);
345   EXPECT_NE(config->client, nullptr);
346   EXPECT_NE(config->server, nullptr);
347   grpc_alts_credentials_options_destroy(client_options);
348   grpc_alts_credentials_options_destroy(server_options);
349   config->out_frame =
350       grpc_slice_from_static_string(ALTS_HANDSHAKER_CLIENT_TEST_OUT_FRAME);
351   return config;
352 }
353 
destroy_config(alts_handshaker_client_test_config * config)354 static void destroy_config(alts_handshaker_client_test_config* config) {
355   if (config == nullptr) {
356     return;
357   }
358   grpc_completion_queue_destroy(config->cq);
359   grpc_channel_destroy(config->channel);
360   alts_handshaker_client_destroy(config->client);
361   alts_handshaker_client_destroy(config->server);
362   grpc_slice_unref(config->out_frame);
363   gpr_free(config);
364 }
365 
TEST(AltsHandshakerClientTest,ScheduleRequestInvalidArgTest)366 TEST(AltsHandshakerClientTest, ScheduleRequestInvalidArgTest) {
367   // Initialization.
368   alts_handshaker_client_test_config* config = create_config();
369   // Tests.
370   alts_handshaker_client_set_grpc_caller_for_testing(config->client,
371                                                      check_must_not_be_called);
372   // Check client_start.
373   {
374     grpc_core::ExecCtx exec_ctx;
375     ASSERT_EQ(alts_handshaker_client_start_client(nullptr),
376               TSI_INVALID_ARGUMENT);
377   }
378   // Check server_start.
379   {
380     grpc_core::ExecCtx exec_ctx;
381     ASSERT_EQ(alts_handshaker_client_start_server(config->server, nullptr),
382               TSI_INVALID_ARGUMENT);
383   }
384   {
385     grpc_core::ExecCtx exec_ctx;
386     ASSERT_EQ(alts_handshaker_client_start_server(nullptr, &config->out_frame),
387               TSI_INVALID_ARGUMENT);
388   }
389   // Check next.
390   {
391     grpc_core::ExecCtx exec_ctx;
392     ASSERT_EQ(alts_handshaker_client_next(config->client, nullptr),
393               TSI_INVALID_ARGUMENT);
394   }
395   {
396     grpc_core::ExecCtx exec_ctx;
397     ASSERT_EQ(alts_handshaker_client_next(nullptr, &config->out_frame),
398               TSI_INVALID_ARGUMENT);
399   }
400   // Check shutdown.
401   alts_handshaker_client_shutdown(nullptr);
402   // Cleanup.
403   destroy_config(config);
404 }
405 
TEST(AltsHandshakerClientTest,ScheduleRequestSuccessTest)406 TEST(AltsHandshakerClientTest, ScheduleRequestSuccessTest) {
407   // Initialization.
408   alts_handshaker_client_test_config* config = create_config();
409   // Check client_start success.
410   alts_handshaker_client_set_grpc_caller_for_testing(
411       config->client, check_client_start_success);
412   {
413     grpc_core::ExecCtx exec_ctx;
414     ASSERT_EQ(alts_handshaker_client_start_client(config->client), TSI_OK);
415   }
416   {
417     grpc_core::ExecCtx exec_ctx;
418     ASSERT_EQ(alts_handshaker_client_next(nullptr, &config->out_frame),
419               TSI_INVALID_ARGUMENT);
420   }
421   // Check server_start success.
422   alts_handshaker_client_set_grpc_caller_for_testing(
423       config->server, check_server_start_success);
424   {
425     grpc_core::ExecCtx exec_ctx;
426     ASSERT_EQ(
427         alts_handshaker_client_start_server(config->server, &config->out_frame),
428         TSI_OK);
429   }
430   // Check client next success.
431   alts_handshaker_client_set_grpc_caller_for_testing(config->client,
432                                                      check_next_success);
433   {
434     grpc_core::ExecCtx exec_ctx;
435     ASSERT_EQ(alts_handshaker_client_next(config->client, &config->out_frame),
436               TSI_OK);
437   }
438   // Check server next success.
439   alts_handshaker_client_set_grpc_caller_for_testing(config->server,
440                                                      check_next_success);
441   {
442     grpc_core::ExecCtx exec_ctx;
443     ASSERT_EQ(alts_handshaker_client_next(config->server, &config->out_frame),
444               TSI_OK);
445   }
446   // Cleanup.
447   {
448     grpc_core::ExecCtx exec_ctx;
449     alts_handshaker_client_on_status_received_for_testing(
450         config->client, GRPC_STATUS_OK, absl::OkStatus());
451     alts_handshaker_client_on_status_received_for_testing(
452         config->server, GRPC_STATUS_OK, absl::OkStatus());
453   }
454   destroy_config(config);
455 }
456 
tsi_cb_assert_tsi_internal_error(tsi_result status,void *,const unsigned char *,size_t,tsi_handshaker_result *)457 static void tsi_cb_assert_tsi_internal_error(
458     tsi_result status, void* /*user_data*/,
459     const unsigned char* /*bytes_to_send*/, size_t /*bytes_to_send_size*/,
460     tsi_handshaker_result* /*result*/) {
461   ASSERT_EQ(status, TSI_INTERNAL_ERROR);
462 }
463 
TEST(AltsHandshakerClientTest,ScheduleRequestGrpcCallFailureTest)464 TEST(AltsHandshakerClientTest, ScheduleRequestGrpcCallFailureTest) {
465   // Initialization.
466   alts_handshaker_client_test_config* config = create_config();
467   // Check client_start failure.
468   alts_handshaker_client_set_grpc_caller_for_testing(config->client,
469                                                      check_grpc_call_failure);
470   {
471     grpc_core::ExecCtx exec_ctx;
472     // TODO(apolcyn): go back to asserting TSI_INTERNAL_ERROR as return
473     // value instead of callback status, after removing the global
474     // queue in https://github.com/grpc/grpc/pull/20722
475     alts_handshaker_client_set_cb_for_testing(config->client,
476                                               tsi_cb_assert_tsi_internal_error);
477     alts_handshaker_client_start_client(config->client);
478   }
479   // Check server_start failure.
480   alts_handshaker_client_set_grpc_caller_for_testing(config->server,
481                                                      check_grpc_call_failure);
482   {
483     grpc_core::ExecCtx exec_ctx;
484     // TODO(apolcyn): go back to asserting TSI_INTERNAL_ERROR as return
485     // value instead of callback status, after removing the global
486     // queue in https://github.com/grpc/grpc/pull/20722
487     alts_handshaker_client_set_cb_for_testing(config->server,
488                                               tsi_cb_assert_tsi_internal_error);
489     alts_handshaker_client_start_server(config->server, &config->out_frame);
490   }
491   {
492     grpc_core::ExecCtx exec_ctx;
493     // Check client next failure.
494     ASSERT_EQ(alts_handshaker_client_next(config->client, &config->out_frame),
495               TSI_INTERNAL_ERROR);
496   }
497   {
498     grpc_core::ExecCtx exec_ctx;
499     // Check server next failure.
500     ASSERT_EQ(alts_handshaker_client_next(config->server, &config->out_frame),
501               TSI_INTERNAL_ERROR);
502   }
503   // Cleanup.
504   {
505     grpc_core::ExecCtx exec_ctx;
506     alts_handshaker_client_on_status_received_for_testing(
507         config->client, GRPC_STATUS_OK, absl::OkStatus());
508     alts_handshaker_client_on_status_received_for_testing(
509         config->server, GRPC_STATUS_OK, absl::OkStatus());
510   }
511   destroy_config(config);
512 }
513 
TEST(MaxNumberOfConcurrentHandshakesTest,Default)514 TEST(MaxNumberOfConcurrentHandshakesTest, Default) {
515   grpc_core::UnsetEnv(kMaxConcurrentStreamsEnvironmentVariable);
516   EXPECT_EQ(MaxNumberOfConcurrentHandshakes(), 40);
517 }
518 
TEST(MaxNumberOfConcurrentHandshakesTest,EnvVarNotInt)519 TEST(MaxNumberOfConcurrentHandshakesTest, EnvVarNotInt) {
520   grpc_core::SetEnv(kMaxConcurrentStreamsEnvironmentVariable, "not-a-number");
521   EXPECT_EQ(MaxNumberOfConcurrentHandshakes(), 40);
522 }
523 
TEST(MaxNumberOfConcurrentHandshakesTest,EnvVarNegative)524 TEST(MaxNumberOfConcurrentHandshakesTest, EnvVarNegative) {
525   grpc_core::SetEnv(kMaxConcurrentStreamsEnvironmentVariable, "-10");
526   EXPECT_EQ(MaxNumberOfConcurrentHandshakes(), 40);
527 }
528 
TEST(MaxNumberOfConcurrentHandshakesTest,EnvVarSuccess)529 TEST(MaxNumberOfConcurrentHandshakesTest, EnvVarSuccess) {
530   grpc_core::SetEnv(kMaxConcurrentStreamsEnvironmentVariable, "10");
531   EXPECT_EQ(MaxNumberOfConcurrentHandshakes(), 10);
532 }
533 
main(int argc,char ** argv)534 int main(int argc, char** argv) {
535   grpc::testing::TestEnvironment env(&argc, argv);
536   ::testing::InitGoogleTest(&argc, argv);
537   grpc::testing::TestGrpcScope grpc_scope;
538   grpc_alts_shared_resource_dedicated_init();
539   int ret = RUN_ALL_TESTS();
540   grpc_alts_shared_resource_dedicated_shutdown();
541   return ret;
542 }
543