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