gtestify test/core/tsi (#30211)

* Fix

* test/core/tsi/alts/frame_protector

* Fix

* Fix
pull/30233/head
Esun Kim 2 years ago committed by GitHub
parent 87c0bb1a98
commit 70a311b736
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2538
      CMakeLists.txt
  2. 319
      build_autogenerated.yaml
  3. 3
      test/core/tsi/BUILD
  4. 1
      test/core/tsi/alts/crypt/BUILD
  5. 167
      test/core/tsi/alts/crypt/aes_gcm_test.cc
  6. 4
      test/core/tsi/alts/frame_protector/BUILD
  7. 89
      test/core/tsi/alts/frame_protector/alts_counter_test.cc
  8. 132
      test/core/tsi/alts/frame_protector/alts_crypter_test.cc
  9. 67
      test/core/tsi/alts/frame_protector/alts_frame_protector_test.cc
  10. 106
      test/core/tsi/alts/frame_protector/frame_handler_test.cc
  11. 4
      test/core/tsi/alts/handshaker/BUILD
  12. 164
      test/core/tsi/alts/handshaker/alts_handshaker_client_test.cc
  13. 460
      test/core/tsi/alts/handshaker/alts_tsi_handshaker_test.cc
  14. 42
      test/core/tsi/alts/handshaker/alts_tsi_utils_test.cc
  15. 150
      test/core/tsi/alts/handshaker/transport_security_common_api_test.cc
  16. 3
      test/core/tsi/alts/zero_copy_frame_protector/BUILD
  17. 183
      test/core/tsi/alts/zero_copy_frame_protector/alts_grpc_record_protocol_test.cc
  18. 192
      test/core/tsi/alts/zero_copy_frame_protector/alts_iovec_record_protocol_test.cc
  19. 108
      test/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector_test.cc
  20. 41
      test/core/tsi/fake_transport_security_test.cc
  21. 391
      test/core/tsi/ssl_transport_security_test.cc
  22. 97
      test/core/tsi/transport_security_test.cc
  23. 742
      tools/run_tests/generated/tests.json

2538
CMakeLists.txt generated

File diff suppressed because it is too large Load Diff

@ -3210,78 +3210,6 @@ targets:
- test/core/transport/chttp2/alpn_test.cc
deps:
- grpc_test_util
- name: alts_counter_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/alts_counter_test.cc
deps:
- grpc_test_util
- name: alts_crypt_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/aes_gcm_test.cc
- test/core/tsi/alts/crypt/gsec_test_util.cc
deps:
- grpc_test_util
- name: alts_crypter_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/alts_crypter_test.cc
deps:
- grpc_test_util
- name: alts_frame_protector_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
- test/core/tsi/transport_security_test_lib.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/alts_frame_protector_test.cc
- test/core/tsi/transport_security_test_lib.cc
deps:
- grpc_test_util
- name: alts_grpc_record_protocol_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/zero_copy_frame_protector/alts_grpc_record_protocol_test.cc
deps:
- grpc_test_util
- name: alts_handshaker_client_test
build: test
language: c
headers:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h
src:
- test/core/tsi/alts/handshaker/alts_handshaker_client_test.cc
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.cc
deps:
- grpc_test_util
- name: alts_iovec_record_protocol_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/zero_copy_frame_protector/alts_iovec_record_protocol_test.cc
deps:
- grpc_test_util
- name: alts_security_connector_test
build: test
language: c
@ -3290,36 +3218,6 @@ targets:
- test/core/security/alts_security_connector_test.cc
deps:
- grpc_test_util
- name: alts_tsi_handshaker_test
build: test
language: c
headers:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h
src:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.cc
- test/core/tsi/alts/handshaker/alts_tsi_handshaker_test.cc
deps:
- grpc_test_util
- name: alts_tsi_utils_test
build: test
language: c
headers:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h
src:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.cc
- test/core/tsi/alts/handshaker/alts_tsi_utils_test.cc
deps:
- grpc_test_util
- name: alts_zero_copy_grpc_protector_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector_test.cc
deps:
- grpc_test_util
- name: auth_context_test
build: test
language: c
@ -3532,16 +3430,6 @@ targets:
deps:
- grpc_test_util
uses_polling: false
- name: fake_transport_security_test
build: test
language: c
headers:
- test/core/tsi/transport_security_test_lib.h
src:
- test/core/tsi/fake_transport_security_test.cc
- test/core/tsi/transport_security_test_lib.cc
deps:
- grpc_test_util
- name: fd_conservation_posix_test
build: test
language: c
@ -3626,16 +3514,6 @@ targets:
- test/core/http/format_request_test.cc
deps:
- grpc_test_util
- name: frame_handler_test
build: test
language: c
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/frame_handler_test.cc
deps:
- grpc_test_util
- name: goaway_server_test
build: test
language: c
@ -3959,20 +3837,6 @@ targets:
- test/core/security/ssl_credentials_test.cc
deps:
- grpc_test_util
- name: ssl_transport_security_test
build: test
language: c
headers:
- test/core/tsi/transport_security_test_lib.h
src:
- test/core/tsi/ssl_transport_security_test.cc
- test/core/tsi/transport_security_test_lib.cc
deps:
- grpc_test_util
platforms:
- linux
- posix
- mac
- name: status_conversion_test
build: test
language: c
@ -4099,22 +3963,6 @@ targets:
deps:
- grpc_test_util
uses_polling: false
- name: transport_security_common_api_test
build: test
language: c
headers: []
src:
- test/core/tsi/alts/handshaker/transport_security_common_api_test.cc
deps:
- grpc_test_util
- name: transport_security_test
build: test
language: c
headers: []
src:
- test/core/tsi/transport_security_test.cc
deps:
- grpc_test_util
- name: varint_test
build: test
language: c
@ -4328,6 +4176,107 @@ targets:
platforms:
- linux
- posix
- name: alts_counter_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/alts_counter_test.cc
deps:
- grpc_test_util
- name: alts_crypt_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/aes_gcm_test.cc
- test/core/tsi/alts/crypt/gsec_test_util.cc
deps:
- grpc_test_util
- name: alts_crypter_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/alts_crypter_test.cc
deps:
- grpc_test_util
- name: alts_frame_protector_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
- test/core/tsi/transport_security_test_lib.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/alts_frame_protector_test.cc
- test/core/tsi/transport_security_test_lib.cc
deps:
- grpc_test_util
- name: alts_grpc_record_protocol_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/zero_copy_frame_protector/alts_grpc_record_protocol_test.cc
deps:
- grpc_test_util
- name: alts_handshaker_client_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h
src:
- test/core/tsi/alts/handshaker/alts_handshaker_client_test.cc
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.cc
deps:
- grpc_test_util
- name: alts_iovec_record_protocol_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/zero_copy_frame_protector/alts_iovec_record_protocol_test.cc
deps:
- grpc_test_util
- name: alts_tsi_handshaker_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h
src:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.cc
- test/core/tsi/alts/handshaker/alts_tsi_handshaker_test.cc
deps:
- grpc_test_util
- name: alts_tsi_utils_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h
src:
- test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.cc
- test/core/tsi/alts/handshaker/alts_tsi_utils_test.cc
deps:
- grpc_test_util
- name: alts_util_test
gtest: true
build: test
@ -4338,6 +4287,17 @@ targets:
deps:
- grpc++_alts
- grpc++_test_util
- name: alts_zero_copy_grpc_protector_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector_test.cc
deps:
- grpc_test_util
- name: arena_promise_test
gtest: true
build: test
@ -5678,6 +5638,17 @@ targets:
- test/core/client_channel/resolvers/fake_resolver_test.cc
deps:
- grpc_test_util
- name: fake_transport_security_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/transport_security_test_lib.h
src:
- test/core/tsi/fake_transport_security_test.cc
- test/core/tsi/transport_security_test_lib.cc
deps:
- grpc_test_util
- name: file_watcher_certificate_provider_factory_test
gtest: true
build: test
@ -5888,6 +5859,17 @@ targets:
- gpr
- upb
uses_polling: false
- name: frame_handler_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/alts/crypt/gsec_test_util.h
src:
- test/core/tsi/alts/crypt/gsec_test_util.cc
- test/core/tsi/alts/frame_protector/frame_handler_test.cc
deps:
- grpc_test_util
- name: fuzzing_event_engine_test
gtest: true
build: test
@ -8131,6 +8113,21 @@ targets:
- test/core/address_utils/sockaddr_utils_test.cc
deps:
- grpc_test_util
- name: ssl_transport_security_test
gtest: true
build: test
language: c++
headers:
- test/core/tsi/transport_security_test_lib.h
src:
- test/core/tsi/ssl_transport_security_test.cc
- test/core/tsi/transport_security_test_lib.cc
deps:
- grpc_test_util
platforms:
- linux
- posix
- mac
- name: stack_tracer_test
gtest: true
build: test
@ -8956,6 +8953,24 @@ targets:
deps:
- grpc++_test_config
- grpc++_test_util
- name: transport_security_common_api_test
gtest: true
build: test
language: c++
headers: []
src:
- test/core/tsi/alts/handshaker/transport_security_common_api_test.cc
deps:
- grpc_test_util
- name: transport_security_test
gtest: true
build: test
language: c++
headers: []
src:
- test/core/tsi/transport_security_test.cc
deps:
- grpc_test_util
- name: transport_stream_receiver_test
gtest: true
build: test

@ -33,6 +33,7 @@ grpc_cc_library(
grpc_cc_test(
name = "fake_transport_security_test",
srcs = ["fake_transport_security_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
":transport_security_test_lib",
@ -74,6 +75,7 @@ grpc_cc_test(
"//src/core/tsi/test_creds:server1.key",
"//src/core/tsi/test_creds:server1.pem",
],
external_deps = ["gtest"],
language = "C++",
tags = ["no_windows"],
deps = [
@ -112,6 +114,7 @@ grpc_cc_test(
grpc_cc_test(
name = "transport_security_test",
srcs = ["transport_security_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",

@ -24,6 +24,7 @@ grpc_package(
grpc_cc_test(
name = "alts_crypt_test",
srcs = ["aes_gcm_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
":alts_crypt_test_util",

@ -16,6 +16,8 @@
*
*/
#include <gtest/gtest.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -74,13 +76,13 @@ static void gsec_assert_ok(grpc_status_code status, const char* error_detail) {
if (status != GRPC_STATUS_OK) {
fprintf(stderr, "Status is not ok: %s\n", error_detail);
}
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
}
static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
size_t aad_length,
size_t message_length) {
GPR_ASSERT(crypter != nullptr);
ASSERT_NE(crypter, nullptr);
size_t nonce_length, tag_length;
uint8_t *nonce, *aad, *message;
gsec_aead_crypter_nonce_length(crypter, &nonce_length, nullptr);
@ -105,8 +107,8 @@ static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
ciphertext_and_tag_length,
&ciphertext_bytes_written, &error_buffer),
error_buffer);
GPR_ASSERT(message_length + tag_length == ciphertext_and_tag_length);
GPR_ASSERT(ciphertext_bytes_written == ciphertext_and_tag_length);
ASSERT_EQ(message_length + tag_length, ciphertext_and_tag_length);
ASSERT_EQ(ciphertext_bytes_written, ciphertext_and_tag_length);
/* Test decryption */
size_t plaintext_length, plaintext_bytes_written = 0;
@ -118,10 +120,10 @@ static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
ciphertext_bytes_written, plaintext, plaintext_length,
&plaintext_bytes_written, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(message_length == plaintext_bytes_written);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(message_length, plaintext_bytes_written);
if (message_length != 0) {
GPR_ASSERT(memcmp(message, plaintext, message_length) == 0);
ASSERT_EQ(memcmp(message, plaintext, message_length), 0);
}
/**
@ -140,12 +142,12 @@ static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
ciphertext_and_tag, ciphertext_bytes_written, plaintext,
plaintext_length, &plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, "Checking tag failed.",
error_message));
GPR_ASSERT(plaintext_bytes_written == 0);
ASSERT_EQ(plaintext_bytes_written, 0);
if (plaintext_length != 0) {
GPR_ASSERT(memcmp(zero_message, plaintext, plaintext_length) == 0);
ASSERT_EQ(memcmp(zero_message, plaintext, plaintext_length), 0);
}
gpr_free(corrupt_nonce);
gpr_free(error_message);
@ -162,12 +164,12 @@ static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
corrupt_ciphertext_and_tag, ciphertext_bytes_written, plaintext,
plaintext_length, &plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
GPR_ASSERT(plaintext_bytes_written == 0);
ASSERT_EQ(plaintext_bytes_written, 0);
if (plaintext_length != 0) {
GPR_ASSERT(memcmp(zero_message, plaintext, plaintext_length) == 0);
ASSERT_EQ(memcmp(zero_message, plaintext, plaintext_length), 0);
}
gpr_free(error_message);
gpr_free(corrupt_ciphertext_and_tag);
@ -181,11 +183,11 @@ static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
crypter, nonce, nonce_length, aad, aad_length,
corrupt_ciphertext_and_tag, ciphertext_bytes_written, plaintext,
plaintext_length, &plaintext_bytes_written, &error_message);
GPR_ASSERT(plaintext_bytes_written == 0);
ASSERT_EQ(plaintext_bytes_written, 0);
if (plaintext_length != 0) {
GPR_ASSERT(memcmp(zero_message, plaintext, plaintext_length) == 0);
ASSERT_EQ(memcmp(zero_message, plaintext, plaintext_length), 0);
}
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
gpr_free(error_message);
@ -202,12 +204,12 @@ static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
corrupt_ciphertext_and_tag, ciphertext_bytes_written, plaintext,
plaintext_length, &plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
GPR_ASSERT(plaintext_bytes_written == 0);
ASSERT_EQ(plaintext_bytes_written, 0);
if (plaintext_length != 0) {
GPR_ASSERT(memcmp(zero_message, plaintext, plaintext_length) == 0);
ASSERT_EQ(memcmp(zero_message, plaintext, plaintext_length), 0);
}
gpr_free(error_message);
gpr_free(corrupt_ciphertext_and_tag);
@ -222,7 +224,7 @@ static void gsec_test_random_encrypt_decrypt(gsec_aead_crypter* crypter,
}
static void gsec_test_encrypt_decrypt(gsec_aead_crypter* crypter) {
GPR_ASSERT(crypter != nullptr);
ASSERT_NE(crypter, nullptr);
size_t aad_length, message_length;
aad_length = gsec_test_bias_random_uint32(kTestMaxLength);
message_length = gsec_test_bias_random_uint32(kTestMaxLength);
@ -234,7 +236,7 @@ static void gsec_test_encrypt_decrypt(gsec_aead_crypter* crypter) {
static void gsec_test_multiple_random_encrypt_decrypt(
gsec_aead_crypter* crypter, size_t* aad_lengths, size_t* message_lengths,
size_t count) {
GPR_ASSERT(crypter != nullptr);
ASSERT_NE(crypter, nullptr);
size_t nonce_length, tag_length;
uint8_t **nonces, **aads, **messages;
nonces = static_cast<uint8_t**>(gpr_malloc(sizeof(uint8_t*) * count));
@ -281,10 +283,9 @@ static void gsec_test_multiple_random_encrypt_decrypt(
messages[ind], message_length, ciphertext_and_tags[ind],
ciphertext_and_tag_lengths[ind], &(ciphertext_bytes_writtens[ind]),
nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(message_length + tag_length == ciphertext_and_tag_lengths[ind]);
GPR_ASSERT(ciphertext_bytes_writtens[ind] ==
ciphertext_and_tag_lengths[ind]);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(message_length + tag_length, ciphertext_and_tag_lengths[ind]);
ASSERT_EQ(ciphertext_bytes_writtens[ind], ciphertext_and_tag_lengths[ind]);
}
/* Do Decryption */
for (ind = 0; ind < count; ind++) {
@ -300,10 +301,10 @@ static void gsec_test_multiple_random_encrypt_decrypt(
ciphertext_and_tags[ind], ciphertext_bytes_writtens[ind],
plaintexts[ind], plaintext_lengths[ind],
&(plaintext_bytes_writtens[ind]), nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(message_length == plaintext_bytes_writtens[ind]);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(message_length, plaintext_bytes_writtens[ind]);
if (message_length != 0) {
GPR_ASSERT(memcmp(messages[ind], plaintexts[ind], message_length) == 0);
ASSERT_EQ(memcmp(messages[ind], plaintexts[ind], message_length), 0);
}
}
@ -334,8 +335,9 @@ static void gsec_test_multiple_random_encrypt_decrypt(
message_vecs, message_vecs_length, another_ciphertext_vec,
&ciphertext_bytes_written, &error_details),
error_details);
GPR_ASSERT(memcmp(ciphertext_and_tags[ind], another_ciphertext_vec.iov_base,
ciphertext_length) == 0);
ASSERT_EQ(memcmp(ciphertext_and_tags[ind], another_ciphertext_vec.iov_base,
ciphertext_length),
0);
free(another_ciphertext);
free(aad_vecs);
free(message_vecs);
@ -369,10 +371,10 @@ static void gsec_test_multiple_random_encrypt_decrypt(
aad_vecs_length, ciphertext_vecs, ciphertext_vecs_length,
decrypted_vec, &decrypted_length, &error_details),
error_details);
GPR_ASSERT(decrypted_vec.iov_len == message_length);
ASSERT_EQ(decrypted_vec.iov_len, message_length);
if (message_length != 0) {
GPR_ASSERT(
memcmp(decrypted_vec.iov_base, messages[ind], message_length) == 0);
ASSERT_EQ(memcmp(decrypted_vec.iov_base, messages[ind], message_length),
0);
}
free(decrypted);
free(aad_vecs);
@ -398,7 +400,7 @@ static void gsec_test_multiple_random_encrypt_decrypt(
}
static void gsec_test_multiple_encrypt_decrypt(gsec_aead_crypter* crypter) {
GPR_ASSERT(crypter != nullptr);
ASSERT_NE(crypter, nullptr);
size_t count = kTestNumEncryptions;
size_t* aad_lengths =
static_cast<size_t*>(gpr_malloc(sizeof(size_t) * count));
@ -420,7 +422,7 @@ static void gsec_test_multiple_encrypt_decrypt(gsec_aead_crypter* crypter) {
}
static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
GPR_ASSERT(crypter != nullptr);
ASSERT_NE(crypter, nullptr);
size_t aad_length = kTestMaxLength;
size_t message_length = kTestMaxLength;
size_t nonce_length;
@ -444,7 +446,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
crypter, nullptr, nonce_length, aad, aad_length, message, message_length,
ciphertext_and_tag, ciphertext_and_tag_length, &ciphertext_bytes_written,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Nonce buffer is nullptr."));
gpr_free(error_message);
@ -454,7 +456,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length + 1, aad, aad_length, message,
message_length, ciphertext_and_tag, ciphertext_and_tag_length,
&ciphertext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Nonce buffer has the wrong length."));
gpr_free(error_message);
@ -464,7 +466,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length - 1, aad, aad_length, message,
message_length, ciphertext_and_tag, ciphertext_and_tag_length,
&ciphertext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Nonce buffer has the wrong length."));
gpr_free(error_message);
@ -474,7 +476,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length, nullptr, aad_length, message,
message_length, ciphertext_and_tag, ciphertext_and_tag_length,
&ciphertext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message, "aad is nullptr."));
gpr_free(error_message);
@ -491,7 +493,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length, aad, aad_length, nullptr, message_length,
ciphertext_and_tag, ciphertext_and_tag_length, &ciphertext_bytes_written,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"plaintext is nullptr."));
gpr_free(error_message);
@ -501,7 +503,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
aad_length, message, message_length,
nullptr, ciphertext_and_tag_length,
&ciphertext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"ciphertext is nullptr."));
gpr_free(error_message);
@ -511,7 +513,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length, aad, aad_length, message, message_length,
ciphertext_and_tag, ciphertext_and_tag_length - 1,
&ciphertext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"ciphertext is too small to hold a tag."));
gpr_free(error_message);
@ -520,7 +522,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
status = gsec_aead_crypter_encrypt(
crypter, nonce, nonce_length, aad, aad_length, message, message_length,
ciphertext_and_tag, ciphertext_and_tag_length, nullptr, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"bytes_written is nullptr."));
gpr_free(error_message);
@ -537,7 +539,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length, aad, aad_length, message, message_length,
ciphertext_and_tag, ciphertext_and_tag_length, &ciphertext_bytes_written,
&error_message);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
gpr_free(message);
gpr_free(aad);
@ -546,7 +548,7 @@ static void gsec_test_encryption_failure(gsec_aead_crypter* crypter) {
}
static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
GPR_ASSERT(crypter != nullptr);
ASSERT_NE(crypter, nullptr);
size_t aad_length = kTestMaxLength;
size_t message_length = kTestMaxLength;
size_t nonce_length, tag_length;
@ -569,8 +571,8 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length, aad, aad_length, message, message_length,
ciphertext_and_tag, ciphertext_and_tag_length, &ciphertext_bytes_written,
nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(ciphertext_bytes_written == ciphertext_and_tag_length);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(ciphertext_bytes_written, ciphertext_and_tag_length);
size_t plaintext_length, plaintext_bytes_written = 0;
gsec_aead_crypter_max_plaintext_length(crypter, ciphertext_bytes_written,
@ -583,7 +585,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
crypter, nullptr, nonce_length, aad, aad_length, ciphertext_and_tag,
ciphertext_and_tag_length, plaintext, plaintext_length,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Nonce buffer is nullptr."));
gpr_free(error_message);
@ -593,7 +595,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length + 1, aad, aad_length, ciphertext_and_tag,
ciphertext_and_tag_length, plaintext, plaintext_length,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Nonce buffer has the wrong length."));
gpr_free(error_message);
@ -603,7 +605,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length - 1, aad, aad_length, ciphertext_and_tag,
ciphertext_and_tag_length, plaintext, plaintext_length,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Nonce buffer has the wrong length."));
gpr_free(error_message);
@ -613,7 +615,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length, nullptr, aad_length, ciphertext_and_tag,
ciphertext_and_tag_length, plaintext, plaintext_length,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message, "aad is nullptr."));
gpr_free(error_message);
@ -622,13 +624,13 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
crypter, nonce, nonce_length, nullptr, 0, message, message_length,
ciphertext_and_tag, ciphertext_and_tag_length, &ciphertext_bytes_written,
&error_message);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
status = gsec_aead_crypter_decrypt(
crypter, nonce, nonce_length, nullptr, 0, ciphertext_and_tag,
ciphertext_and_tag_length, plaintext, plaintext_length,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Small ciphertext */
if (tag_length > 0) {
@ -637,7 +639,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
tag_length - 1, plaintext, plaintext_length, &plaintext_bytes_written,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"ciphertext is too small to hold a tag."));
gpr_free(error_message);
@ -649,7 +651,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
ciphertext_and_tag_length, plaintext, plaintext_length,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"ciphertext is nullptr."));
gpr_free(error_message);
@ -660,7 +662,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
ciphertext_and_tag_length, nullptr, plaintext_length,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"plaintext is nullptr, but plaintext_length is positive."));
gpr_free(error_message);
@ -671,7 +673,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
ciphertext_and_tag_length, plaintext, plaintext_length - 1,
&plaintext_bytes_written, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Not enough plaintext buffer to hold encrypted ciphertext."));
gpr_free(error_message);
@ -682,7 +684,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
ciphertext_and_tag_length, plaintext,
plaintext_length, nullptr, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"bytes_written is nullptr."));
gpr_free(error_message);
@ -696,7 +698,7 @@ static void gsec_test_decryption_failure(gsec_aead_crypter* crypter) {
static void gsec_test_encrypt_decrypt_test_vector(
gsec_aead_crypter* crypter, gsec_aead_test_vector* test_vector) {
GPR_ASSERT(crypter != nullptr);
ASSERT_NE(crypter, nullptr);
/* Test byte-based encryption interface. */
size_t ciphertext_and_tag_length, ciphertext_bytes_written = 0;
gsec_aead_crypter_max_ciphertext_and_tag_length(
@ -710,10 +712,11 @@ static void gsec_test_encrypt_decrypt_test_vector(
test_vector->plaintext_length, ciphertext_and_tag_bytes,
ciphertext_and_tag_length, &ciphertext_bytes_written, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(ciphertext_bytes_written == ciphertext_and_tag_length);
GPR_ASSERT(memcmp(test_vector->ciphertext_and_tag, ciphertext_and_tag_bytes,
ciphertext_and_tag_length) == 0);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(ciphertext_bytes_written, ciphertext_and_tag_length);
ASSERT_EQ(memcmp(test_vector->ciphertext_and_tag, ciphertext_and_tag_bytes,
ciphertext_and_tag_length),
0);
/* Test byte-based decryption interface */
size_t plaintext_length, plaintext_bytes_written = 0;
@ -726,10 +729,11 @@ static void gsec_test_encrypt_decrypt_test_vector(
test_vector->aad_length, test_vector->ciphertext_and_tag,
test_vector->ciphertext_and_tag_length, plaintext_bytes, plaintext_length,
&plaintext_bytes_written, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
if (plaintext_bytes_written != 0) {
GPR_ASSERT(memcmp(test_vector->plaintext, plaintext_bytes,
plaintext_bytes_written) == 0);
ASSERT_EQ(memcmp(test_vector->plaintext, plaintext_bytes,
plaintext_bytes_written),
0);
}
gpr_free(ciphertext_and_tag_bytes);
@ -740,14 +744,14 @@ static void gsec_test_get_crypter_from_test_vector(
gsec_aead_crypter** crypter, gsec_aead_test_vector* test_vector,
bool rekey = false) {
size_t key_length = test_vector->key_length;
GPR_ASSERT(key_length == kAes128GcmKeyLength ||
key_length == kAes256GcmKeyLength ||
key_length == kAes128GcmRekeyKeyLength);
ASSERT_TRUE(key_length == kAes128GcmKeyLength ||
key_length == kAes256GcmKeyLength ||
key_length == kAes128GcmRekeyKeyLength);
size_t nonce_length = test_vector->nonce_length;
GPR_ASSERT(nonce_length == kAesGcmNonceLength);
ASSERT_EQ(nonce_length, kAesGcmNonceLength);
size_t plaintext_length = test_vector->plaintext_length;
size_t ciphertext_and_tag_length = test_vector->ciphertext_and_tag_length;
GPR_ASSERT(ciphertext_and_tag_length == plaintext_length + kAesGcmTagLength);
ASSERT_EQ(ciphertext_and_tag_length, plaintext_length + kAesGcmTagLength);
size_t tag_length = ciphertext_and_tag_length - plaintext_length;
gsec_aes_gcm_aead_crypter_create(test_vector->key, key_length, nonce_length,
tag_length, rekey, crypter, nullptr);
@ -817,7 +821,7 @@ static void gsec_test_get_random_aes_gcm_crypters(
kAesGcmTagLength, /*rekey=*/true);
}
static void gsec_test_do_generic_crypter_tests() {
TEST(AltsCryptTest, GsecTestDoGenericCrypterTests) {
gsec_aead_crypter** crypters;
gsec_test_get_random_aes_gcm_crypters(&crypters);
size_t ind;
@ -833,7 +837,7 @@ static void gsec_test_do_generic_crypter_tests() {
gpr_free(crypters);
}
static void gsec_test_do_vector_tests_rekey_nist() {
TEST(AltsCryptTest, GsecTestDoVectorTestsRekeyNist) {
// NIST vectors from:
// http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
//
@ -1048,7 +1052,7 @@ static void gsec_test_do_vector_tests_rekey_nist() {
gsec_test_verify_crypter_on_test_vector(&vec, /*rekey=*/true);
}
static void gsec_test_do_vector_tests_rekey_ieee() {
TEST(AltsCryptTest, GsecTestDoVectorTestsRekeyIeee) {
// IEEE vectors from:
// http://www.ieee802.org/1/files/public/docs2011/bn-randall-test-vectors-0511-v1.pdf
//
@ -1464,7 +1468,7 @@ static void gsec_test_do_vector_tests_rekey_ieee() {
gsec_test_verify_crypter_on_test_vector(&vec, /*rekey=*/true);
}
static void gsec_test_do_vector_tests_nist() {
TEST(AltsCryptTest, GsecTestDoVectorTestsNist) {
/**
* From:
* http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/
@ -1588,7 +1592,7 @@ static void gsec_test_do_vector_tests_nist() {
gsec_aead_free_test_vector(test_vector_4);
}
static void gsec_test_do_vector_tests_ieee() {
TEST(AltsCryptTest, GsecTestDoVectorTestsIeee) {
/**
* From:
* http://www.ieee802.org/1/files/public/docs2011/
@ -2112,12 +2116,7 @@ static void gsec_test_do_vector_tests_ieee() {
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
grpc_init();
gsec_test_do_generic_crypter_tests();
gsec_test_do_vector_tests_nist();
gsec_test_do_vector_tests_ieee();
gsec_test_do_vector_tests_rekey_nist();
gsec_test_do_vector_tests_rekey_ieee();
grpc_shutdown();
return 0;
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
return RUN_ALL_TESTS();
}

@ -21,6 +21,7 @@ grpc_package(name = "test/core/tsi/alts/frame_protector")
grpc_cc_test(
name = "alts_counter_test",
srcs = ["alts_counter_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",
@ -33,6 +34,7 @@ grpc_cc_test(
grpc_cc_test(
name = "alts_crypter_test",
srcs = ["alts_crypter_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",
@ -45,6 +47,7 @@ grpc_cc_test(
grpc_cc_test(
name = "alts_frame_protector_test",
srcs = ["alts_frame_protector_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",
@ -58,6 +61,7 @@ grpc_cc_test(
grpc_cc_test(
name = "frame_handler_test",
srcs = ["frame_handler_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",

@ -18,6 +18,8 @@
#include "src/core/tsi/alts/frame_protector/alts_counter.h"
#include <gtest/gtest.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -42,14 +44,14 @@ static void alts_counter_test_input_sanity_check(size_t counter_size,
/* Invalid counter size. */
grpc_status_code status =
alts_counter_create(true, 0, overflow_size, &ctr, &error_details);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
"counter_size is invalid."));
gpr_free(error_details);
/* Invalid overflow size. */
status = alts_counter_create(true, counter_size, 0, &ctr, &error_details);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
"overflow_size is invalid."));
gpr_free(error_details);
@ -57,26 +59,26 @@ static void alts_counter_test_input_sanity_check(size_t counter_size,
/* alts_counter is nullptr. */
status = alts_counter_create(true, counter_size, overflow_size, nullptr,
&error_details);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
"crypter_counter is nullptr."));
gpr_free(error_details);
status = alts_counter_create(true, counter_size, overflow_size, &ctr,
&error_details);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Input sanity check on alts_counter_increment(). */
/* crypter_counter is nullptr. */
bool is_overflow = false;
status = alts_counter_increment(nullptr, &is_overflow, &error_details);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
"crypter_counter is nullptr."));
gpr_free(error_details);
/* is_overflow is nullptr. */
status = alts_counter_increment(ctr, nullptr, &error_details);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
"is_overflow is nullptr."));
gpr_free(error_details);
@ -90,21 +92,21 @@ static void alts_counter_test_overflow_full_range(bool is_client,
char* error_details = nullptr;
grpc_status_code status = alts_counter_create(
is_client, counter_size, overflow_size, &ctr, &error_details);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
unsigned char* expected =
static_cast<unsigned char*>(gpr_zalloc(counter_size));
if (is_client) {
expected[counter_size - 1] = 0x80;
}
/* Do a single iteration to ensure the counter is initialized as expected. */
GPR_ASSERT(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size) == is_client);
GPR_ASSERT(memcmp(alts_counter_get_counter(ctr), expected, counter_size) ==
0);
ASSERT_EQ(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size),
is_client);
ASSERT_EQ(memcmp(alts_counter_get_counter(ctr), expected, counter_size), 0);
bool is_overflow = false;
GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
GRPC_STATUS_OK);
GPR_ASSERT(!is_overflow);
ASSERT_EQ(alts_counter_increment(ctr, &is_overflow, &error_details),
GRPC_STATUS_OK);
ASSERT_FALSE(is_overflow);
/**
* The counter can return 2^{overflow_size * 8} counters. The
* high-order bit is fixed to the client/server. The last call will yield a
@ -113,17 +115,19 @@ static void alts_counter_test_overflow_full_range(bool is_client,
int iterations = 1 << (overflow_size * 8);
int ind = 1;
for (ind = 1; ind < iterations - 1; ind++) {
GPR_ASSERT(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size) == is_client);
GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
GRPC_STATUS_OK);
GPR_ASSERT(!is_overflow);
ASSERT_EQ(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size),
is_client);
ASSERT_EQ(alts_counter_increment(ctr, &is_overflow, &error_details),
GRPC_STATUS_OK);
ASSERT_FALSE(is_overflow);
}
GPR_ASSERT(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size) == is_client);
GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
GRPC_STATUS_FAILED_PRECONDITION);
GPR_ASSERT(is_overflow);
ASSERT_EQ(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size),
is_client);
ASSERT_EQ(alts_counter_increment(ctr, &is_overflow, &error_details),
GRPC_STATUS_FAILED_PRECONDITION);
ASSERT_TRUE(is_overflow);
gpr_free(expected);
alts_counter_destroy(ctr);
}
@ -136,7 +140,7 @@ static void alts_counter_test_overflow_single_increment(bool is_client,
char* error_details = nullptr;
grpc_status_code status = alts_counter_create(
is_client, counter_size, overflow_size, &ctr, &error_details);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
unsigned char* expected =
static_cast<unsigned char*>(gpr_zalloc(counter_size));
memset(expected, 0xFF, overflow_size);
@ -146,27 +150,27 @@ static void alts_counter_test_overflow_single_increment(bool is_client,
expected[counter_size - 1] = 0x80;
}
memcpy(ctr->counter, expected, counter_size);
GPR_ASSERT(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size) == is_client);
GPR_ASSERT(memcmp(expected, alts_counter_get_counter(ctr), counter_size) ==
0);
ASSERT_EQ(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size),
is_client);
ASSERT_EQ(memcmp(expected, alts_counter_get_counter(ctr), counter_size), 0);
bool is_overflow = false;
GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
GRPC_STATUS_OK);
GPR_ASSERT(!is_overflow);
GPR_ASSERT(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size) == is_client);
ASSERT_EQ(alts_counter_increment(ctr, &is_overflow, &error_details),
GRPC_STATUS_OK);
ASSERT_FALSE(is_overflow);
ASSERT_EQ(do_bytes_represent_client(ctr, alts_counter_get_counter(ctr),
counter_size),
is_client);
expected[0] = static_cast<unsigned char>(expected[0] + 1);
GPR_ASSERT(memcmp(expected, alts_counter_get_counter(ctr), counter_size) ==
0);
GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
GRPC_STATUS_FAILED_PRECONDITION);
GPR_ASSERT(is_overflow);
ASSERT_EQ(memcmp(expected, alts_counter_get_counter(ctr), counter_size), 0);
ASSERT_EQ(alts_counter_increment(ctr, &is_overflow, &error_details),
GRPC_STATUS_FAILED_PRECONDITION);
ASSERT_TRUE(is_overflow);
gpr_free(expected);
alts_counter_destroy(ctr);
}
int main(int /*argc*/, char** /*argv*/) {
TEST(AltsCounterTest, MainTest) {
alts_counter_test_input_sanity_check(kGcmCounterSize, kGcmOverflowSize);
alts_counter_test_overflow_full_range(true, kSmallCounterSize,
kSmallOverflowSize);
@ -176,6 +180,9 @@ int main(int /*argc*/, char** /*argv*/) {
kGcmOverflowSize);
alts_counter_test_overflow_single_increment(false, kGcmCounterSize,
kGcmOverflowSize);
}
return 0;
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -22,6 +22,8 @@
#include <stdlib.h>
#include <string.h>
#include <gtest/gtest.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -43,23 +45,23 @@ static void alts_crypter_test_random_seal_unseal(alts_crypter* server_seal,
size_t size = data_size;
grpc_status_code status = alts_crypter_process_in_place(
client_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
status = alts_crypter_process_in_place(
server_unseal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(data_buffer, duplicate_buffer, data_size) == 0);
GPR_ASSERT(size == data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(data_buffer, duplicate_buffer, data_size), 0);
ASSERT_EQ(size, data_size);
/* Server seal and client unseal */
status = alts_crypter_process_in_place(
server_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
status = alts_crypter_process_in_place(
client_unseal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(data_buffer, duplicate_buffer, data_size) == 0);
GPR_ASSERT(size == data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(data_buffer, duplicate_buffer, data_size), 0);
ASSERT_EQ(size, data_size);
gpr_free(data_buffer);
gpr_free(duplicate_buffer);
}
@ -86,42 +88,42 @@ static void alts_crypter_test_multiple_random_seal_unseal(
size_t size1 = data_size, size2 = data_size;
grpc_status_code status = alts_crypter_process_in_place(
client_seal, data_buffer1, protected_data_size, size1, &size1, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size1 == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size1, protected_data_size);
status = alts_crypter_process_in_place(
client_seal, data_buffer2, protected_data_size, size2, &size2, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size2 == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size2, protected_data_size);
status = alts_crypter_process_in_place(
server_unseal, data_buffer1, protected_data_size, size1, &size1, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(data_buffer1, duplicate_buffer1, data_size) == 0);
GPR_ASSERT(size1 == data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(data_buffer1, duplicate_buffer1, data_size), 0);
ASSERT_EQ(size1, data_size);
status = alts_crypter_process_in_place(
server_unseal, data_buffer2, protected_data_size, size2, &size2, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(data_buffer2, duplicate_buffer2, data_size) == 0);
GPR_ASSERT(size2 == data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(data_buffer2, duplicate_buffer2, data_size), 0);
ASSERT_EQ(size2, data_size);
/* Server seal and client unseal */
status = alts_crypter_process_in_place(
server_seal, data_buffer1, protected_data_size, size1, &size1, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size1 == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size1, protected_data_size);
status = alts_crypter_process_in_place(
server_seal, data_buffer2, protected_data_size, size2, &size2, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size2 == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size2, protected_data_size);
status = alts_crypter_process_in_place(
client_unseal, data_buffer1, protected_data_size, size1, &size1, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(data_buffer1, duplicate_buffer1, data_size) == 0);
GPR_ASSERT(size1 == data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(data_buffer1, duplicate_buffer1, data_size), 0);
ASSERT_EQ(size1, data_size);
status = alts_crypter_process_in_place(
client_unseal, data_buffer2, protected_data_size, size2, &size2, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(data_buffer2, duplicate_buffer2, data_size) == 0);
GPR_ASSERT(size2 == data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(data_buffer2, duplicate_buffer2, data_size), 0);
ASSERT_EQ(size2, data_size);
gpr_free(data_buffer1);
gpr_free(data_buffer2);
@ -143,8 +145,8 @@ static void alts_crypter_test_corrupted_unseal(
gsec_test_random_bytes(data_buffer, data_size);
grpc_status_code status = alts_crypter_process_in_place(
client_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
uint8_t* corrupted_data_buffer;
char* error_message = nullptr;
gsec_test_copy_and_alter_random_byte(data_buffer, &corrupted_data_buffer,
@ -152,10 +154,10 @@ static void alts_crypter_test_corrupted_unseal(
status = alts_crypter_process_in_place(server_unseal, corrupted_data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
GPR_ASSERT(memcmp(corrupted_data_buffer, zero_buffer, data_size) == 0);
ASSERT_EQ(memcmp(corrupted_data_buffer, zero_buffer, data_size), 0);
gpr_free(corrupted_data_buffer);
gpr_free(error_message);
@ -164,17 +166,17 @@ static void alts_crypter_test_corrupted_unseal(
gsec_test_random_bytes(data_buffer, data_size);
status = alts_crypter_process_in_place(
client_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
gsec_test_copy(data_buffer, &corrupted_data_buffer, protected_data_size);
(*corrupted_data_buffer)++;
status = alts_crypter_process_in_place(server_unseal, corrupted_data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
GPR_ASSERT(memcmp(corrupted_data_buffer, zero_buffer, data_size) == 0);
ASSERT_EQ(memcmp(corrupted_data_buffer, zero_buffer, data_size), 0);
gpr_free(corrupted_data_buffer);
gpr_free(error_message);
@ -183,17 +185,17 @@ static void alts_crypter_test_corrupted_unseal(
gsec_test_random_bytes(data_buffer, data_size);
status = alts_crypter_process_in_place(
client_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
gsec_test_copy(data_buffer, &corrupted_data_buffer, protected_data_size);
(*(corrupted_data_buffer + protected_data_size - 1))++;
status = alts_crypter_process_in_place(server_unseal, corrupted_data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
GPR_ASSERT(memcmp(corrupted_data_buffer, zero_buffer, data_size) == 0);
ASSERT_EQ(memcmp(corrupted_data_buffer, zero_buffer, data_size), 0);
gpr_free(corrupted_data_buffer);
gpr_free(error_message);
@ -216,24 +218,24 @@ static void alts_crypter_test_unsync_seal_unseal(alts_crypter* server_seal,
gsec_test_random_bytes(data_buffer, data_size);
grpc_status_code status = alts_crypter_process_in_place(
client_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
size = data_size;
gsec_test_random_bytes(data_buffer, data_size);
status = alts_crypter_process_in_place(
client_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
char* error_message = nullptr;
status = alts_crypter_process_in_place(server_unseal, data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
GPR_ASSERT(memcmp(data_buffer, zero_buffer, data_size) == 0);
ASSERT_EQ(memcmp(data_buffer, zero_buffer, data_size), 0);
gpr_free(error_message);
/* Perform two seals at server, one unseal at client. */
@ -241,23 +243,23 @@ static void alts_crypter_test_unsync_seal_unseal(alts_crypter* server_seal,
gsec_test_random_bytes(data_buffer, data_size);
status = alts_crypter_process_in_place(
server_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
size = data_size;
gsec_test_random_bytes(data_buffer, data_size);
status = alts_crypter_process_in_place(
server_seal, data_buffer, protected_data_size, size, &size, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(size == protected_data_size);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(size, protected_data_size);
status = alts_crypter_process_in_place(client_unseal, data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Checking tag failed"));
GPR_ASSERT(memcmp(data_buffer, zero_buffer, data_size) == 0);
ASSERT_EQ(memcmp(data_buffer, zero_buffer, data_size), 0);
gpr_free(error_message);
gpr_free(data_buffer);
gpr_free(zero_buffer);
@ -276,7 +278,7 @@ static void alts_crypter_test_input_sanity_check(alts_crypter* crypter_seal,
/* Crypter is nullptr. */
grpc_status_code status = alts_crypter_process_in_place(
nullptr, data_buffer, protected_data_size, size, &size, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"crypter or crypter->vtable has not been initialized properly."));
gpr_free(error_message);
@ -285,7 +287,7 @@ static void alts_crypter_test_input_sanity_check(alts_crypter* crypter_seal,
size = data_size;
status = alts_crypter_process_in_place(
crypter_seal, nullptr, protected_data_size, size, &size, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message, "data is nullptr."));
gpr_free(error_message);
@ -294,7 +296,7 @@ static void alts_crypter_test_input_sanity_check(alts_crypter* crypter_seal,
status = alts_crypter_process_in_place(crypter_seal, data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"data_size is zero."));
gpr_free(error_message);
@ -304,7 +306,7 @@ static void alts_crypter_test_input_sanity_check(alts_crypter* crypter_seal,
status = alts_crypter_process_in_place(crypter_seal, data_buffer,
protected_data_size - 1, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"data_allocated_size is smaller than sum of data_size and "
"num_overhead_bytes."));
@ -315,7 +317,7 @@ static void alts_crypter_test_input_sanity_check(alts_crypter* crypter_seal,
status = alts_crypter_process_in_place(crypter_unseal, nullptr,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message, "data is nullptr."));
gpr_free(error_message);
@ -324,7 +326,7 @@ static void alts_crypter_test_input_sanity_check(alts_crypter* crypter_seal,
status = alts_crypter_process_in_place(crypter_unseal, data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"data_size is smaller than num_overhead_bytes."));
gpr_free(error_message);
@ -334,7 +336,7 @@ static void alts_crypter_test_input_sanity_check(alts_crypter* crypter_seal,
status = alts_crypter_process_in_place(crypter_unseal, data_buffer,
protected_data_size, size, &size,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"data_size is smaller than num_overhead_bytes."));
gpr_free(error_message);
@ -386,7 +388,7 @@ static void destroy_random_alts_seal_crypter(alts_crypter* server_seal,
alts_crypter_destroy(client_unseal);
}
static void alts_crypter_do_generic_tests() {
TEST(AltsCrypterTest, AltsCrypterDoGenericTests) {
alts_crypter *server_seal = nullptr, *server_unseal = nullptr,
*client_seal = nullptr, *client_unseal = nullptr;
gsec_aead_crypter *server_crypter_seal = nullptr,
@ -487,7 +489,7 @@ static void alts_crypter_do_generic_tests() {
client_unseal);
}
int main(int /*argc*/, char** /*argv*/) {
alts_crypter_do_generic_tests();
return 0;
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -20,6 +20,8 @@
#include <stdbool.h>
#include <gtest/gtest.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -37,8 +39,8 @@ static void alts_test_do_round_trip_check_frames(
const size_t client_frame_size, const uint8_t* server_message,
const size_t server_message_size, const uint8_t* server_expected_frames,
const size_t server_frame_size) {
GPR_ASSERT(fixture != nullptr);
GPR_ASSERT(fixture->config != nullptr);
ASSERT_NE(fixture, nullptr);
ASSERT_NE(fixture->config, nullptr);
tsi_frame_protector* client_frame_protector = nullptr;
tsi_frame_protector* server_frame_protector = nullptr;
tsi_test_frame_protector_config* config = fixture->config;
@ -46,21 +48,22 @@ static void alts_test_do_round_trip_check_frames(
/* Create a client frame protector. */
size_t client_max_output_protected_frame_size =
config->client_max_output_protected_frame_size;
GPR_ASSERT(
ASSERT_EQ(
alts_create_frame_protector(key, key_size, /*is_client=*/true, rekey,
client_max_output_protected_frame_size == 0
? nullptr
: &client_max_output_protected_frame_size,
&client_frame_protector) == TSI_OK);
/* Create a server frame protector. */
&client_frame_protector),
TSI_OK); /* Create a server frame protector. */
size_t server_max_output_protected_frame_size =
config->server_max_output_protected_frame_size;
GPR_ASSERT(
ASSERT_EQ(
alts_create_frame_protector(key, key_size, /*is_client=*/false, rekey,
server_max_output_protected_frame_size == 0
? nullptr
: &server_max_output_protected_frame_size,
&server_frame_protector) == TSI_OK);
&server_frame_protector),
TSI_OK);
tsi_test_frame_protector_fixture_init(fixture, client_frame_protector,
server_frame_protector);
/* Client sends a message to server. */
@ -71,18 +74,20 @@ static void alts_test_do_round_trip_check_frames(
client_frame_protector,
/*is_client=*/true);
/* Verify if the generated frame is the same as the expected. */
GPR_ASSERT(channel->bytes_written_to_server_channel == client_frame_size);
GPR_ASSERT(memcmp(client_expected_frames, channel->server_channel,
client_frame_size) == 0);
ASSERT_EQ(channel->bytes_written_to_server_channel, client_frame_size);
ASSERT_EQ(memcmp(client_expected_frames, channel->server_channel,
client_frame_size),
0);
unsigned char* server_received_message =
static_cast<unsigned char*>(gpr_malloc(kChannelSize));
size_t server_received_message_size = 0;
tsi_test_frame_protector_receive_message_from_peer(
config, channel, server_frame_protector, server_received_message,
&server_received_message_size, /*is_client=*/false);
GPR_ASSERT(config->client_message_size == server_received_message_size);
GPR_ASSERT(memcmp(config->client_message, server_received_message,
server_received_message_size) == 0);
ASSERT_EQ(config->client_message_size, server_received_message_size);
ASSERT_EQ(memcmp(config->client_message, server_received_message,
server_received_message_size),
0);
/* Server sends a message to client. */
uint8_t* saved_server_message = config->server_message;
config->server_message = const_cast<uint8_t*>(server_message);
@ -91,9 +96,10 @@ static void alts_test_do_round_trip_check_frames(
server_frame_protector,
/*is_client=*/false);
/* Verify if the generated frame is the same as the expected. */
GPR_ASSERT(channel->bytes_written_to_client_channel == server_frame_size);
GPR_ASSERT(memcmp(server_expected_frames, channel->client_channel,
server_frame_size) == 0);
ASSERT_EQ(channel->bytes_written_to_client_channel, server_frame_size);
ASSERT_EQ(memcmp(server_expected_frames, channel->client_channel,
server_frame_size),
0);
unsigned char* client_received_message =
static_cast<unsigned char*>(gpr_malloc(kChannelSize));
size_t client_received_message_size = 0;
@ -101,9 +107,10 @@ static void alts_test_do_round_trip_check_frames(
config, channel, client_frame_protector, client_received_message,
&client_received_message_size,
/*is_client=*/true);
GPR_ASSERT(config->server_message_size == client_received_message_size);
GPR_ASSERT(memcmp(config->server_message, client_received_message,
client_received_message_size) == 0);
ASSERT_EQ(config->server_message_size, client_received_message_size);
ASSERT_EQ(memcmp(config->server_message, client_received_message,
client_received_message_size),
0);
config->client_message = saved_client_message;
config->server_message = saved_server_message;
/* Destroy server and client frame protectors. */
@ -330,8 +337,8 @@ static void alts_test_do_round_trip_vector_tests() {
static void alts_test_do_round_trip(tsi_test_frame_protector_fixture* fixture,
bool rekey) {
GPR_ASSERT(fixture != nullptr);
GPR_ASSERT(fixture->config != nullptr);
ASSERT_NE(fixture, nullptr);
ASSERT_NE(fixture->config, nullptr);
tsi_frame_protector* client_frame_protector = nullptr;
tsi_frame_protector* server_frame_protector = nullptr;
tsi_test_frame_protector_config* config = fixture->config;
@ -342,21 +349,23 @@ static void alts_test_do_round_trip(tsi_test_frame_protector_fixture* fixture,
/* Create a client frame protector. */
size_t client_max_output_protected_frame_size =
config->client_max_output_protected_frame_size;
GPR_ASSERT(
ASSERT_EQ(
alts_create_frame_protector(key, key_length, /*is_client=*/true, rekey,
client_max_output_protected_frame_size == 0
? nullptr
: &client_max_output_protected_frame_size,
&client_frame_protector) == TSI_OK);
&client_frame_protector),
TSI_OK);
/* Create a server frame protector. */
size_t server_max_output_protected_frame_size =
config->server_max_output_protected_frame_size;
GPR_ASSERT(
ASSERT_EQ(
alts_create_frame_protector(key, key_length, /*is_client=*/false, rekey,
server_max_output_protected_frame_size == 0
? nullptr
: &server_max_output_protected_frame_size,
&server_frame_protector) == TSI_OK);
&server_frame_protector),
TSI_OK);
tsi_test_frame_protector_fixture_init(fixture, client_frame_protector,
server_frame_protector);
tsi_test_frame_protector_do_round_trip_no_handshake(fixture);
@ -387,9 +396,13 @@ static void alts_test_do_round_trip_all(bool rekey) {
gpr_free(bit_array);
}
int main(int /*argc*/, char** /*argv*/) {
TEST(AltsFrameProtectorTest, MainTest) {
alts_test_do_round_trip_vector_tests();
alts_test_do_round_trip_all(/*rekey=*/false);
alts_test_do_round_trip_all(/*rekey=*/true);
return 0;
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -24,6 +24,8 @@
#include <algorithm>
#include <gtest/gtest.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -67,33 +69,34 @@ static void frame(frame_handler* handler, unsigned char* payload,
handler->buffer_size = frame_length(payload_length);
handler->buffer =
static_cast<unsigned char*>(gpr_malloc(handler->buffer_size));
GPR_ASSERT(alts_reset_frame_writer(handler->writer, payload, payload_length));
ASSERT_TRUE(
alts_reset_frame_writer(handler->writer, payload, payload_length));
size_t offset = 0;
while (offset < handler->buffer_size &&
!alts_is_frame_writer_done(handler->writer)) {
size_t bytes_written =
std::min(write_length, handler->buffer_size - offset);
GPR_ASSERT(alts_write_frame_bytes(handler->writer, handler->buffer + offset,
&bytes_written));
ASSERT_TRUE(alts_write_frame_bytes(
handler->writer, handler->buffer + offset, &bytes_written));
offset += bytes_written;
}
GPR_ASSERT(alts_is_frame_writer_done(handler->writer));
GPR_ASSERT(handler->buffer_size == offset);
ASSERT_TRUE(alts_is_frame_writer_done(handler->writer));
ASSERT_EQ(handler->buffer_size, offset);
}
static size_t deframe(frame_handler* handler, unsigned char* bytes,
size_t read_length) {
GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
EXPECT_TRUE(alts_reset_frame_reader(handler->reader, bytes));
size_t offset = 0;
while (offset < handler->buffer_size &&
!alts_is_frame_reader_done(handler->reader)) {
size_t bytes_read = std::min(read_length, handler->buffer_size - offset);
GPR_ASSERT(alts_read_frame_bytes(handler->reader, handler->buffer + offset,
&bytes_read));
EXPECT_TRUE(alts_read_frame_bytes(handler->reader, handler->buffer + offset,
&bytes_read));
offset += bytes_read;
}
GPR_ASSERT(alts_is_frame_reader_done(handler->reader));
GPR_ASSERT(handler->buffer_size == offset);
EXPECT_TRUE(alts_is_frame_reader_done(handler->reader));
EXPECT_EQ(handler->buffer_size, offset);
return offset - handler->reader->header_bytes_read;
}
@ -104,12 +107,12 @@ static void frame_n_deframe(frame_handler* handler, unsigned char* payload,
unsigned char* bytes =
static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
size_t deframed_payload_length = deframe(handler, bytes, read_length);
GPR_ASSERT(payload_length == deframed_payload_length);
GPR_ASSERT(memcmp(payload, bytes, payload_length) == 0);
ASSERT_EQ(payload_length, deframed_payload_length);
ASSERT_EQ(memcmp(payload, bytes, payload_length), 0);
gpr_free(bytes);
}
static void frame_handler_test_frame_deframe() {
TEST(FrameHandlerTest, FrameHandlerTestFrameDeframe) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
@ -118,7 +121,7 @@ static void frame_handler_test_frame_deframe() {
destroy_frame_handler(handler);
}
static void frame_handler_test_small_buffer() {
TEST(FrameHandlerTest, FrameHandlerTestSmallBuffer) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
@ -126,40 +129,42 @@ static void frame_handler_test_small_buffer() {
destroy_frame_handler(handler);
}
static void frame_handler_test_null_input_stream() {
TEST(FrameHandlerTest, FrameHandlerTestNullInputStream) {
frame_handler* handler = create_frame_handler();
GPR_ASSERT(!alts_reset_frame_writer(handler->writer, nullptr, 0));
ASSERT_FALSE(alts_reset_frame_writer(handler->writer, nullptr, 0));
destroy_frame_handler(handler);
}
static void frame_handler_test_bad_input_length() {
TEST(FrameHandlerTest, FrameHandlerTestBadInputLength) {
unsigned char payload[] = "hello world";
frame_handler* handler = create_frame_handler();
GPR_ASSERT(!alts_reset_frame_writer(handler->writer, payload, SIZE_MAX));
ASSERT_FALSE(alts_reset_frame_writer(handler->writer, payload, SIZE_MAX));
destroy_frame_handler(handler);
}
static void frame_handler_test_null_writer_byte_length() {
TEST(FrameHandlerTest, FrameHandlerTestNullWriterByteLength) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
GPR_ASSERT(alts_reset_frame_writer(handler->writer, payload, payload_length));
GPR_ASSERT(
ASSERT_TRUE(
alts_reset_frame_writer(handler->writer, payload, payload_length));
ASSERT_TRUE(
!alts_write_frame_bytes(handler->writer, handler->buffer, nullptr));
destroy_frame_handler(handler);
}
static void frame_handler_test_null_writer_bytes() {
TEST(FrameHandlerTest, FrameHandlerTestNullWriterBytes) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
GPR_ASSERT(alts_reset_frame_writer(handler->writer, payload, payload_length));
GPR_ASSERT(
ASSERT_TRUE(
alts_reset_frame_writer(handler->writer, payload, payload_length));
ASSERT_TRUE(
!alts_write_frame_bytes(handler->writer, nullptr, &payload_length));
destroy_frame_handler(handler);
}
static void frame_handler_test_bad_frame_length() {
TEST(FrameHandlerTest, FrameHandlerTestBadFrameLength) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
@ -167,17 +172,17 @@ static void frame_handler_test_bad_frame_length() {
memset(handler->buffer, 0x00, kFrameLengthFieldSize);
unsigned char* bytes =
static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
ASSERT_TRUE(alts_reset_frame_reader(handler->reader, bytes));
size_t bytes_read = handler->buffer_size;
GPR_ASSERT(
ASSERT_TRUE(
!alts_read_frame_bytes(handler->reader, handler->buffer, &bytes_read));
GPR_ASSERT(alts_is_frame_reader_done(handler->reader));
GPR_ASSERT(bytes_read == 0);
ASSERT_TRUE(alts_is_frame_reader_done(handler->reader));
ASSERT_EQ(bytes_read, 0);
gpr_free(bytes);
destroy_frame_handler(handler);
}
static void frame_handler_test_unsupported_message_type() {
TEST(FrameHandlerTest, FrameHandlerTestUnsupportedMessageType) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
@ -186,63 +191,54 @@ static void frame_handler_test_unsupported_message_type() {
kFrameMessageTypeFieldSize);
unsigned char* bytes =
static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
ASSERT_TRUE(alts_reset_frame_reader(handler->reader, bytes));
size_t bytes_read = handler->buffer_size;
GPR_ASSERT(
ASSERT_TRUE(
!alts_read_frame_bytes(handler->reader, handler->buffer, &bytes_read));
GPR_ASSERT(alts_is_frame_reader_done(handler->reader));
GPR_ASSERT(bytes_read == 0);
ASSERT_TRUE(alts_is_frame_reader_done(handler->reader));
ASSERT_EQ(bytes_read, 0);
gpr_free(bytes);
destroy_frame_handler(handler);
}
static void frame_handler_test_null_output_stream() {
TEST(FrameHandlerTest, FrameHandlerTestNullOutputStream) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
frame(handler, payload, payload_length, payload_length);
GPR_ASSERT(!alts_reset_frame_reader(handler->reader, nullptr));
ASSERT_FALSE(alts_reset_frame_reader(handler->reader, nullptr));
destroy_frame_handler(handler);
}
static void frame_handler_test_null_reader_byte_length() {
TEST(FrameHandlerTest, FrameHandlerTestNullReaderByteLength) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
frame(handler, payload, payload_length, payload_length);
unsigned char* bytes =
static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
GPR_ASSERT(!alts_read_frame_bytes(handler->reader, handler->buffer, nullptr));
ASSERT_TRUE(alts_reset_frame_reader(handler->reader, bytes));
ASSERT_FALSE(
alts_read_frame_bytes(handler->reader, handler->buffer, nullptr));
gpr_free(bytes);
destroy_frame_handler(handler);
}
static void frame_handler_test_null_reader_bytes() {
TEST(FrameHandlerTest, FrameHandlerTestNullReaderBytes) {
unsigned char payload[] = "hello world";
size_t payload_length = strlen(reinterpret_cast<char*>(payload)) + 1;
frame_handler* handler = create_frame_handler();
frame(handler, payload, payload_length, payload_length);
unsigned char* bytes =
static_cast<unsigned char*>(gpr_malloc(kFrameHandlerTestBufferSize));
GPR_ASSERT(alts_reset_frame_reader(handler->reader, bytes));
ASSERT_TRUE(alts_reset_frame_reader(handler->reader, bytes));
size_t bytes_read = handler->buffer_size;
GPR_ASSERT(!alts_read_frame_bytes(handler->reader, nullptr, &bytes_read));
ASSERT_FALSE(alts_read_frame_bytes(handler->reader, nullptr, &bytes_read));
gpr_free(bytes);
destroy_frame_handler(handler);
}
int main(int /*argc*/, char** /*argv*/) {
frame_handler_test_frame_deframe();
frame_handler_test_small_buffer();
frame_handler_test_null_input_stream();
frame_handler_test_bad_input_length();
frame_handler_test_null_writer_byte_length();
frame_handler_test_null_writer_bytes();
frame_handler_test_bad_frame_length();
frame_handler_test_unsupported_message_type();
frame_handler_test_null_output_stream();
frame_handler_test_null_reader_byte_length();
frame_handler_test_null_reader_bytes();
return 0;
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -30,6 +30,7 @@ grpc_cc_library(
grpc_cc_test(
name = "alts_handshaker_client_test",
srcs = ["alts_handshaker_client_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
":alts_handshaker_service_api_test_lib",
@ -41,6 +42,7 @@ grpc_cc_test(
grpc_cc_test(
name = "alts_tsi_handshaker_test",
srcs = ["alts_tsi_handshaker_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
":alts_handshaker_service_api_test_lib",
@ -54,6 +56,7 @@ grpc_cc_test(
grpc_cc_test(
name = "alts_tsi_utils_test",
srcs = ["alts_tsi_utils_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
":alts_handshaker_service_api_test_lib",
@ -65,6 +68,7 @@ grpc_cc_test(
grpc_cc_test(
name = "transport_security_common_api_test",
srcs = ["transport_security_common_api_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:grpc",

@ -18,6 +18,8 @@
#include "src/core/tsi/alts/handshaker/alts_handshaker_client.h"
#include <gtest/gtest.h>
#include "upb/upb.hpp"
#include <grpc/grpc.h>
@ -64,32 +66,32 @@ typedef struct alts_handshaker_client_test_config {
static void validate_rpc_protocol_versions(
const grpc_gcp_RpcProtocolVersions* versions) {
GPR_ASSERT(versions != nullptr);
ASSERT_NE(versions, nullptr);
const grpc_gcp_RpcProtocolVersions_Version* max_version =
grpc_gcp_RpcProtocolVersions_max_rpc_version(versions);
const grpc_gcp_RpcProtocolVersions_Version* min_version =
grpc_gcp_RpcProtocolVersions_min_rpc_version(versions);
GPR_ASSERT(grpc_gcp_RpcProtocolVersions_Version_major(max_version) ==
kMaxRpcVersionMajor);
GPR_ASSERT(grpc_gcp_RpcProtocolVersions_Version_minor(max_version) ==
kMaxRpcVersionMinor);
GPR_ASSERT(grpc_gcp_RpcProtocolVersions_Version_major(min_version) ==
kMinRpcVersionMajor);
GPR_ASSERT(grpc_gcp_RpcProtocolVersions_Version_minor(min_version) ==
kMinRpcVersionMinor);
ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_major(max_version),
kMaxRpcVersionMajor);
ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_minor(max_version),
kMaxRpcVersionMinor);
ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_major(min_version),
kMinRpcVersionMajor);
ASSERT_EQ(grpc_gcp_RpcProtocolVersions_Version_minor(min_version),
kMinRpcVersionMinor);
}
static void validate_target_identities(
const grpc_gcp_Identity* const* target_identities,
size_t target_identities_count) {
GPR_ASSERT(target_identities_count == 2);
ASSERT_EQ(target_identities_count, 2);
const grpc_gcp_Identity* identity1 = target_identities[1];
const grpc_gcp_Identity* identity2 = target_identities[0];
GPR_ASSERT(upb_StringView_IsEqual(
ASSERT_TRUE(upb_StringView_IsEqual(
grpc_gcp_Identity_service_account(identity1),
upb_StringView_FromString(
ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT1)));
GPR_ASSERT(upb_StringView_IsEqual(
ASSERT_TRUE(upb_StringView_IsEqual(
grpc_gcp_Identity_service_account(identity2),
upb_StringView_FromString(
ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT2)));
@ -101,41 +103,41 @@ static void validate_target_identities(
*/
static bool validate_op(alts_handshaker_client* c, const grpc_op* op,
size_t nops, bool is_start) {
GPR_ASSERT(c != nullptr && op != nullptr && nops != 0);
EXPECT_TRUE(c != nullptr && op != nullptr && nops != 0);
bool ok = true;
grpc_op* start_op = const_cast<grpc_op*>(op);
if (is_start) {
ok &= (op->op == GRPC_OP_SEND_INITIAL_METADATA);
ok &= (op->data.send_initial_metadata.count == 0);
op++;
GPR_ASSERT((size_t)(op - start_op) <= kHandshakerClientOpNum);
EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
ok &= (op->op == GRPC_OP_RECV_INITIAL_METADATA);
ok &= (op->data.recv_initial_metadata.recv_initial_metadata ==
alts_handshaker_client_get_initial_metadata_for_testing(c));
op++;
GPR_ASSERT((size_t)(op - start_op) <= kHandshakerClientOpNum);
EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
}
ok &= (op->op == GRPC_OP_SEND_MESSAGE);
ok &= (op->data.send_message.send_message ==
alts_handshaker_client_get_send_buffer_for_testing(c));
op++;
GPR_ASSERT((size_t)(op - start_op) <= kHandshakerClientOpNum);
EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
ok &= (op->op == GRPC_OP_RECV_MESSAGE);
ok &= (op->data.recv_message.recv_message ==
alts_handshaker_client_get_recv_buffer_addr_for_testing(c));
op++;
GPR_ASSERT((size_t)(op - start_op) <= kHandshakerClientOpNum);
EXPECT_LE((size_t)(op - start_op), kHandshakerClientOpNum);
return ok;
}
static grpc_gcp_HandshakerReq* deserialize_handshaker_req(
grpc_byte_buffer* buffer, upb_Arena* arena) {
GPR_ASSERT(buffer != nullptr);
EXPECT_NE(buffer, nullptr);
grpc_byte_buffer_reader bbr;
GPR_ASSERT(grpc_byte_buffer_reader_init(&bbr, buffer));
EXPECT_TRUE(grpc_byte_buffer_reader_init(&bbr, buffer));
grpc_slice slice = grpc_byte_buffer_reader_readall(&bbr);
grpc_gcp_HandshakerReq* req = grpc_gcp_handshaker_req_decode(slice, arena);
GPR_ASSERT(req != nullptr);
EXPECT_NE(req, nullptr);
grpc_slice_unref(slice);
grpc_byte_buffer_reader_destroy(&bbr);
return req;
@ -153,7 +155,7 @@ static grpc_call_error check_must_not_be_called(grpc_call* /*call*/,
const grpc_op* /*ops*/,
size_t /*nops*/,
grpc_closure* /*tag*/) {
GPR_ASSERT(0);
abort();
}
/**
@ -173,22 +175,23 @@ static grpc_call_error check_client_start_success(grpc_call* /*call*/,
upb::Arena arena;
alts_handshaker_client* client =
static_cast<alts_handshaker_client*>(closure->cb_arg);
GPR_ASSERT(alts_handshaker_client_get_closure_for_testing(client) == closure);
EXPECT_EQ(alts_handshaker_client_get_closure_for_testing(client), closure);
grpc_gcp_HandshakerReq* req = deserialize_handshaker_req(
alts_handshaker_client_get_send_buffer_for_testing(client), arena.ptr());
const grpc_gcp_StartClientHandshakeReq* client_start =
grpc_gcp_HandshakerReq_client_start(req);
GPR_ASSERT(grpc_gcp_StartClientHandshakeReq_handshake_security_protocol(
client_start) == grpc_gcp_ALTS);
EXPECT_EQ(grpc_gcp_StartClientHandshakeReq_handshake_security_protocol(
client_start),
grpc_gcp_ALTS);
upb_StringView const* application_protocols =
grpc_gcp_StartClientHandshakeReq_application_protocols(client_start,
nullptr);
GPR_ASSERT(upb_StringView_IsEqual(
EXPECT_TRUE(upb_StringView_IsEqual(
application_protocols[0],
upb_StringView_FromString(ALTS_APPLICATION_PROTOCOL)));
upb_StringView const* record_protocols =
grpc_gcp_StartClientHandshakeReq_record_protocols(client_start, nullptr);
GPR_ASSERT(upb_StringView_IsEqual(
EXPECT_TRUE(upb_StringView_IsEqual(
record_protocols[0], upb_StringView_FromString(ALTS_RECORD_PROTOCOL)));
const grpc_gcp_RpcProtocolVersions* rpc_protocol_versions =
grpc_gcp_StartClientHandshakeReq_rpc_versions(client_start);
@ -198,12 +201,12 @@ static grpc_call_error check_client_start_success(grpc_call* /*call*/,
grpc_gcp_StartClientHandshakeReq_target_identities(
client_start, &target_identities_count);
validate_target_identities(target_identities, target_identities_count);
GPR_ASSERT(upb_StringView_IsEqual(
EXPECT_TRUE(upb_StringView_IsEqual(
grpc_gcp_StartClientHandshakeReq_target_name(client_start),
upb_StringView_FromString(ALTS_HANDSHAKER_CLIENT_TEST_TARGET_NAME)));
GPR_ASSERT(grpc_gcp_StartClientHandshakeReq_max_frame_size(client_start) ==
ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE);
GPR_ASSERT(validate_op(client, op, nops, true /* is_start */));
EXPECT_EQ(grpc_gcp_StartClientHandshakeReq_max_frame_size(client_start),
ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE);
EXPECT_TRUE(validate_op(client, op, nops, true /* is_start */));
return GRPC_CALL_OK;
}
@ -224,7 +227,7 @@ static grpc_call_error check_server_start_success(grpc_call* /*call*/,
upb::Arena arena;
alts_handshaker_client* client =
static_cast<alts_handshaker_client*>(closure->cb_arg);
GPR_ASSERT(alts_handshaker_client_get_closure_for_testing(client) == closure);
EXPECT_EQ(alts_handshaker_client_get_closure_for_testing(client), closure);
grpc_gcp_HandshakerReq* req = deserialize_handshaker_req(
alts_handshaker_client_get_send_buffer_for_testing(client), arena.ptr());
const grpc_gcp_StartServerHandshakeReq* server_start =
@ -232,23 +235,24 @@ static grpc_call_error check_server_start_success(grpc_call* /*call*/,
upb_StringView const* application_protocols =
grpc_gcp_StartServerHandshakeReq_application_protocols(server_start,
nullptr);
GPR_ASSERT(upb_StringView_IsEqual(
EXPECT_TRUE(upb_StringView_IsEqual(
application_protocols[0],
upb_StringView_FromString(ALTS_APPLICATION_PROTOCOL)));
GPR_ASSERT(grpc_gcp_StartServerHandshakeReq_handshake_parameters_size(
server_start) == 1);
EXPECT_EQ(
grpc_gcp_StartServerHandshakeReq_handshake_parameters_size(server_start),
1);
grpc_gcp_ServerHandshakeParameters* value;
GPR_ASSERT(grpc_gcp_StartServerHandshakeReq_handshake_parameters_get(
EXPECT_TRUE(grpc_gcp_StartServerHandshakeReq_handshake_parameters_get(
server_start, grpc_gcp_ALTS, &value));
upb_StringView const* record_protocols =
grpc_gcp_ServerHandshakeParameters_record_protocols(value, nullptr);
GPR_ASSERT(upb_StringView_IsEqual(
EXPECT_TRUE(upb_StringView_IsEqual(
record_protocols[0], upb_StringView_FromString(ALTS_RECORD_PROTOCOL)));
validate_rpc_protocol_versions(
grpc_gcp_StartServerHandshakeReq_rpc_versions(server_start));
GPR_ASSERT(grpc_gcp_StartServerHandshakeReq_max_frame_size(server_start) ==
ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE);
GPR_ASSERT(validate_op(client, op, nops, true /* is_start */));
EXPECT_EQ(grpc_gcp_StartServerHandshakeReq_max_frame_size(server_start),
ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE);
EXPECT_TRUE(validate_op(client, op, nops, true /* is_start */));
return GRPC_CALL_OK;
}
@ -263,15 +267,15 @@ static grpc_call_error check_next_success(grpc_call* /*call*/,
upb::Arena arena;
alts_handshaker_client* client =
static_cast<alts_handshaker_client*>(closure->cb_arg);
GPR_ASSERT(alts_handshaker_client_get_closure_for_testing(client) == closure);
EXPECT_EQ(alts_handshaker_client_get_closure_for_testing(client), closure);
grpc_gcp_HandshakerReq* req = deserialize_handshaker_req(
alts_handshaker_client_get_send_buffer_for_testing(client), arena.ptr());
const grpc_gcp_NextHandshakeMessageReq* next =
grpc_gcp_HandshakerReq_next(req);
GPR_ASSERT(upb_StringView_IsEqual(
EXPECT_TRUE(upb_StringView_IsEqual(
grpc_gcp_NextHandshakeMessageReq_in_bytes(next),
upb_StringView_FromString(ALTS_HANDSHAKER_CLIENT_TEST_OUT_FRAME)));
GPR_ASSERT(validate_op(client, op, nops, false /* is_start */));
EXPECT_TRUE(validate_op(client, op, nops, false /* is_start */));
return GRPC_CALL_OK;
}
@ -302,9 +306,9 @@ static grpc_alts_credentials_options* create_credentials_options(
options, ALTS_HANDSHAKER_CLIENT_TEST_TARGET_SERVICE_ACCOUNT2);
}
grpc_gcp_rpc_protocol_versions* versions = &options->rpc_versions;
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(
EXPECT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(
versions, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(
EXPECT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(
versions, kMinRpcVersionMajor, kMinRpcVersionMinor));
return options;
}
@ -334,8 +338,8 @@ static alts_handshaker_client_test_config* create_config() {
grpc_slice_from_static_string(ALTS_HANDSHAKER_CLIENT_TEST_TARGET_NAME),
nullptr, nullptr, nullptr, nullptr, true,
ALTS_HANDSHAKER_CLIENT_TEST_MAX_FRAME_SIZE);
GPR_ASSERT(config->client != nullptr);
GPR_ASSERT(config->server != nullptr);
EXPECT_NE(config->client, nullptr);
EXPECT_NE(config->server, nullptr);
grpc_alts_credentials_options_destroy(client_options);
grpc_alts_credentials_options_destroy(server_options);
config->out_frame =
@ -355,7 +359,7 @@ static void destroy_config(alts_handshaker_client_test_config* config) {
gpr_free(config);
}
static void schedule_request_invalid_arg_test() {
TEST(AltsHandshakerClientTest, ScheduleRequestInvalidArgTest) {
/* Initialization. */
alts_handshaker_client_test_config* config = create_config();
/* Tests. */
@ -364,30 +368,30 @@ static void schedule_request_invalid_arg_test() {
/* Check client_start. */
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_start_client(nullptr) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(alts_handshaker_client_start_client(nullptr),
TSI_INVALID_ARGUMENT);
}
/* Check server_start. */
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_start_server(config->server, nullptr) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(alts_handshaker_client_start_server(config->server, nullptr),
TSI_INVALID_ARGUMENT);
}
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_start_server(
nullptr, &config->out_frame) == TSI_INVALID_ARGUMENT);
ASSERT_EQ(alts_handshaker_client_start_server(nullptr, &config->out_frame),
TSI_INVALID_ARGUMENT);
}
/* Check next. */
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_next(config->client, nullptr) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(alts_handshaker_client_next(config->client, nullptr),
TSI_INVALID_ARGUMENT);
}
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_next(nullptr, &config->out_frame) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(alts_handshaker_client_next(nullptr, &config->out_frame),
TSI_INVALID_ARGUMENT);
}
/* Check shutdown. */
alts_handshaker_client_shutdown(nullptr);
@ -395,7 +399,7 @@ static void schedule_request_invalid_arg_test() {
destroy_config(config);
}
static void schedule_request_success_test() {
TEST(AltsHandshakerClientTest, ScheduleRequestSuccessTest) {
/* Initialization. */
alts_handshaker_client_test_config* config = create_config();
/* Check client_start success. */
@ -403,36 +407,37 @@ static void schedule_request_success_test() {
config->client, check_client_start_success);
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_start_client(config->client) == TSI_OK);
ASSERT_EQ(alts_handshaker_client_start_client(config->client), TSI_OK);
}
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_next(nullptr, &config->out_frame) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(alts_handshaker_client_next(nullptr, &config->out_frame),
TSI_INVALID_ARGUMENT);
}
/* Check server_start success. */
alts_handshaker_client_set_grpc_caller_for_testing(
config->server, check_server_start_success);
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_start_server(
config->server, &config->out_frame) == TSI_OK);
ASSERT_EQ(
alts_handshaker_client_start_server(config->server, &config->out_frame),
TSI_OK);
}
/* Check client next success. */
alts_handshaker_client_set_grpc_caller_for_testing(config->client,
check_next_success);
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_next(config->client,
&config->out_frame) == TSI_OK);
ASSERT_EQ(alts_handshaker_client_next(config->client, &config->out_frame),
TSI_OK);
}
/* Check server next success. */
alts_handshaker_client_set_grpc_caller_for_testing(config->server,
check_next_success);
{
grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(alts_handshaker_client_next(config->server,
&config->out_frame) == TSI_OK);
ASSERT_EQ(alts_handshaker_client_next(config->server, &config->out_frame),
TSI_OK);
}
/* Cleanup. */
{
@ -449,10 +454,10 @@ static void tsi_cb_assert_tsi_internal_error(
tsi_result status, void* /*user_data*/,
const unsigned char* /*bytes_to_send*/, size_t /*bytes_to_send_size*/,
tsi_handshaker_result* /*result*/) {
GPR_ASSERT(status == TSI_INTERNAL_ERROR);
ASSERT_EQ(status, TSI_INTERNAL_ERROR);
}
static void schedule_request_grpc_call_failure_test() {
TEST(AltsHandshakerClientTest, ScheduleRequestGrpcCallFailureTest) {
/* Initialization. */
alts_handshaker_client_test_config* config = create_config();
/* Check client_start failure. */
@ -482,14 +487,14 @@ static void schedule_request_grpc_call_failure_test() {
{
grpc_core::ExecCtx exec_ctx;
/* Check client next failure. */
GPR_ASSERT(alts_handshaker_client_next(
config->client, &config->out_frame) == TSI_INTERNAL_ERROR);
ASSERT_EQ(alts_handshaker_client_next(config->client, &config->out_frame),
TSI_INTERNAL_ERROR);
}
{
grpc_core::ExecCtx exec_ctx;
/* Check server next failure. */
GPR_ASSERT(alts_handshaker_client_next(
config->server, &config->out_frame) == TSI_INTERNAL_ERROR);
ASSERT_EQ(alts_handshaker_client_next(config->server, &config->out_frame),
TSI_INTERNAL_ERROR);
}
/* Cleanup. */
{
@ -504,15 +509,10 @@ static void schedule_request_grpc_call_failure_test() {
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
/* Initialization. */
grpc_init();
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
grpc_alts_shared_resource_dedicated_init();
/* Tests. */
schedule_request_invalid_arg_test();
schedule_request_success_test();
schedule_request_grpc_call_failure_test();
/* Cleanup. */
int ret = RUN_ALL_TESTS();
grpc_alts_shared_resource_dedicated_shutdown();
grpc_shutdown();
return 0;
return ret;
}

@ -21,6 +21,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <gtest/gtest.h>
#include "upb/upb.hpp"
#include <grpc/grpc.h>
@ -162,7 +164,7 @@ static grpc_byte_buffer* generate_handshaker_response(
upb_StringView_FromString(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY));
grpc_gcp_HandshakerResult_set_key_data(
result, upb_StringView_FromString(ALTS_TSI_HANDSHAKER_TEST_KEY_DATA));
GPR_ASSERT(grpc_gcp_handshaker_resp_set_peer_rpc_versions(
EXPECT_TRUE(grpc_gcp_handshaker_resp_set_peer_rpc_versions(
resp, arena.ptr(), ALTS_TSI_HANDSHAKER_TEST_MAX_RPC_VERSION_MAJOR,
ALTS_TSI_HANDSHAKER_TEST_MAX_RPC_VERSION_MINOR,
ALTS_TSI_HANDSHAKER_TEST_MIN_RPC_VERSION_MAJOR,
@ -199,7 +201,7 @@ static grpc_byte_buffer* generate_handshaker_response(
upb_StringView_FromString(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY));
grpc_gcp_HandshakerResult_set_key_data(
result, upb_StringView_FromString(ALTS_TSI_HANDSHAKER_TEST_KEY_DATA));
GPR_ASSERT(grpc_gcp_handshaker_resp_set_peer_rpc_versions(
EXPECT_TRUE(grpc_gcp_handshaker_resp_set_peer_rpc_versions(
resp, arena.ptr(), ALTS_TSI_HANDSHAKER_TEST_MAX_RPC_VERSION_MAJOR,
ALTS_TSI_HANDSHAKER_TEST_MAX_RPC_VERSION_MINOR,
ALTS_TSI_HANDSHAKER_TEST_MIN_RPC_VERSION_MAJOR,
@ -240,33 +242,35 @@ static void check_must_not_be_called(tsi_result /*status*/, void* /*user_data*/,
const unsigned char* /*bytes_to_send*/,
size_t /*bytes_to_send_size*/,
tsi_handshaker_result* /*result*/) {
GPR_ASSERT(0);
ASSERT_TRUE(0);
}
static void on_client_start_success_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send_size == strlen(ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME));
GPR_ASSERT(memcmp(bytes_to_send, ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME,
bytes_to_send_size) == 0);
GPR_ASSERT(result == nullptr);
ASSERT_EQ(status, TSI_OK);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send_size, strlen(ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME));
ASSERT_EQ(memcmp(bytes_to_send, ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME,
bytes_to_send_size),
0);
ASSERT_EQ(result, nullptr);
/* Validate peer identity. */
tsi_peer peer;
GPR_ASSERT(tsi_handshaker_result_extract_peer(result, &peer) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_result_extract_peer(result, &peer),
TSI_INVALID_ARGUMENT);
/* Validate frame protector. */
tsi_frame_protector* protector = nullptr;
GPR_ASSERT(tsi_handshaker_result_create_frame_protector(
result, nullptr, &protector) == TSI_INVALID_ARGUMENT);
ASSERT_EQ(
tsi_handshaker_result_create_frame_protector(result, nullptr, &protector),
TSI_INVALID_ARGUMENT);
/* Validate unused bytes. */
const unsigned char* unused_bytes = nullptr;
size_t unused_bytes_size = 0;
GPR_ASSERT(tsi_handshaker_result_get_unused_bytes(result, &unused_bytes,
&unused_bytes_size) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_result_get_unused_bytes(result, &unused_bytes,
&unused_bytes_size),
TSI_INVALID_ARGUMENT);
signal(&tsi_to_caller_notification);
}
@ -274,26 +278,28 @@ static void on_server_start_success_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send_size == strlen(ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME));
GPR_ASSERT(memcmp(bytes_to_send, ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME,
bytes_to_send_size) == 0);
GPR_ASSERT(result == nullptr);
ASSERT_EQ(status, TSI_OK);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send_size, strlen(ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME));
ASSERT_EQ(memcmp(bytes_to_send, ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME,
bytes_to_send_size),
0);
ASSERT_EQ(result, nullptr);
/* Validate peer identity. */
tsi_peer peer;
GPR_ASSERT(tsi_handshaker_result_extract_peer(result, &peer) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_result_extract_peer(result, &peer),
TSI_INVALID_ARGUMENT);
/* Validate frame protector. */
tsi_frame_protector* protector = nullptr;
GPR_ASSERT(tsi_handshaker_result_create_frame_protector(
result, nullptr, &protector) == TSI_INVALID_ARGUMENT);
ASSERT_EQ(
tsi_handshaker_result_create_frame_protector(result, nullptr, &protector),
TSI_INVALID_ARGUMENT);
/* Validate unused bytes. */
const unsigned char* unused_bytes = nullptr;
size_t unused_bytes_size = 0;
GPR_ASSERT(tsi_handshaker_result_get_unused_bytes(result, &unused_bytes,
&unused_bytes_size) ==
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_result_get_unused_bytes(result, &unused_bytes,
&unused_bytes_size),
TSI_INVALID_ARGUMENT);
signal(&tsi_to_caller_notification);
}
@ -301,56 +307,64 @@ static void on_client_next_success_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send_size == strlen(ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME));
GPR_ASSERT(memcmp(bytes_to_send, ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME,
bytes_to_send_size) == 0);
GPR_ASSERT(result != nullptr);
ASSERT_EQ(status, TSI_OK);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send_size, strlen(ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME));
ASSERT_EQ(memcmp(bytes_to_send, ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME,
bytes_to_send_size),
0);
ASSERT_NE(result, nullptr);
// Validate max frame size value after Frame Size Negotiation. Here peer max
// frame size is greater than default value, and user specified max frame size
// is absent.
tsi_zero_copy_grpc_protector* zero_copy_protector = nullptr;
GPR_ASSERT(tsi_handshaker_result_create_zero_copy_grpc_protector(
result, nullptr, &zero_copy_protector) == TSI_OK);
ASSERT_EQ(tsi_handshaker_result_create_zero_copy_grpc_protector(
result, nullptr, &zero_copy_protector),
TSI_OK);
size_t actual_max_frame_size;
tsi_zero_copy_grpc_protector_max_frame_size(zero_copy_protector,
&actual_max_frame_size);
GPR_ASSERT(actual_max_frame_size == kTsiAltsMaxFrameSize);
ASSERT_EQ(actual_max_frame_size, kTsiAltsMaxFrameSize);
tsi_zero_copy_grpc_protector_destroy(zero_copy_protector);
/* Validate peer identity. */
tsi_peer peer;
GPR_ASSERT(tsi_handshaker_result_extract_peer(result, &peer) == TSI_OK);
GPR_ASSERT(peer.property_count == kTsiAltsNumOfPeerProperties);
GPR_ASSERT(memcmp(TSI_ALTS_CERTIFICATE_TYPE, peer.properties[0].value.data,
peer.properties[0].value.length) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY,
peer.properties[1].value.data,
peer.properties[1].value.length) == 0);
ASSERT_EQ(tsi_handshaker_result_extract_peer(result, &peer), TSI_OK);
ASSERT_EQ(peer.property_count, kTsiAltsNumOfPeerProperties);
ASSERT_EQ(memcmp(TSI_ALTS_CERTIFICATE_TYPE, peer.properties[0].value.data,
peer.properties[0].value.length),
0);
ASSERT_EQ(
memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY,
peer.properties[1].value.data, peer.properties[1].value.length),
0);
/* Validate alts context. */
upb::Arena context_arena;
grpc_gcp_AltsContext* ctx = grpc_gcp_AltsContext_parse(
peer.properties[3].value.data, peer.properties[3].value.length,
context_arena.ptr());
GPR_ASSERT(ctx != nullptr);
ASSERT_NE(ctx, nullptr);
upb_StringView application_protocol =
grpc_gcp_AltsContext_application_protocol(ctx);
upb_StringView record_protocol = grpc_gcp_AltsContext_record_protocol(ctx);
upb_StringView peer_account = grpc_gcp_AltsContext_peer_service_account(ctx);
upb_StringView local_account =
grpc_gcp_AltsContext_local_service_account(ctx);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_APPLICATION_PROTOCOL,
application_protocol.data, application_protocol.size) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_RECORD_PROTOCOL,
record_protocol.data, record_protocol.size) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY, peer_account.data,
peer_account.size) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_LOCAL_IDENTITY, local_account.data,
local_account.size) == 0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_APPLICATION_PROTOCOL,
application_protocol.data, application_protocol.size),
0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_RECORD_PROTOCOL,
record_protocol.data, record_protocol.size),
0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY, peer_account.data,
peer_account.size),
0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_LOCAL_IDENTITY, local_account.data,
local_account.size),
0);
size_t iter = kUpb_Map_Begin;
grpc_gcp_AltsContext_PeerAttributesEntry* peer_attributes_entry =
grpc_gcp_AltsContext_peer_attributes_nextmutable(ctx, &iter);
GPR_ASSERT(peer_attributes_entry != nullptr);
ASSERT_NE(peer_attributes_entry, nullptr);
while (peer_attributes_entry != nullptr) {
upb_StringView key = grpc_gcp_AltsContext_PeerAttributesEntry_key(
const_cast<grpc_gcp_AltsContext_PeerAttributesEntry*>(
@ -358,33 +372,35 @@ static void on_client_next_success_cb(tsi_result status, void* user_data,
upb_StringView val = grpc_gcp_AltsContext_PeerAttributesEntry_value(
const_cast<grpc_gcp_AltsContext_PeerAttributesEntry*>(
peer_attributes_entry));
GPR_ASSERT(upb_StringView_IsEqual(
ASSERT_TRUE(upb_StringView_IsEqual(
key, upb_StringView_FromString(
ALTS_TSI_HANDSHAKER_TEST_PEER_ATTRIBUTES_KEY)));
GPR_ASSERT(upb_StringView_IsEqual(
ASSERT_TRUE(upb_StringView_IsEqual(
val, upb_StringView_FromString(
ALTS_TSI_HANDSHAKER_TEST_PEER_ATTRIBUTES_VALUE)));
peer_attributes_entry =
grpc_gcp_AltsContext_peer_attributes_nextmutable(ctx, &iter);
}
/* Validate security level. */
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_SECURITY_LEVEL,
peer.properties[4].value.data,
peer.properties[4].value.length) == 0);
ASSERT_EQ(
memcmp(ALTS_TSI_HANDSHAKER_TEST_SECURITY_LEVEL,
peer.properties[4].value.data, peer.properties[4].value.length),
0);
tsi_peer_destruct(&peer);
/* Validate unused bytes. */
const unsigned char* bytes = nullptr;
size_t bytes_size = 0;
GPR_ASSERT(tsi_handshaker_result_get_unused_bytes(result, &bytes,
&bytes_size) == TSI_OK);
GPR_ASSERT(bytes_size == strlen(ALTS_TSI_HANDSHAKER_TEST_REMAIN_BYTES));
GPR_ASSERT(memcmp(bytes, ALTS_TSI_HANDSHAKER_TEST_REMAIN_BYTES, bytes_size) ==
0);
ASSERT_EQ(tsi_handshaker_result_get_unused_bytes(result, &bytes, &bytes_size),
TSI_OK);
ASSERT_EQ(bytes_size, strlen(ALTS_TSI_HANDSHAKER_TEST_REMAIN_BYTES));
ASSERT_EQ(memcmp(bytes, ALTS_TSI_HANDSHAKER_TEST_REMAIN_BYTES, bytes_size),
0);
/* Validate frame protector. */
tsi_frame_protector* protector = nullptr;
GPR_ASSERT(tsi_handshaker_result_create_frame_protector(
result, nullptr, &protector) == TSI_OK);
GPR_ASSERT(protector != nullptr);
ASSERT_EQ(
tsi_handshaker_result_create_frame_protector(result, nullptr, &protector),
TSI_OK);
ASSERT_NE(protector, nullptr);
tsi_frame_protector_destroy(protector);
tsi_handshaker_result_destroy(result);
signal(&tsi_to_caller_notification);
@ -394,58 +410,64 @@ static void on_server_next_success_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send_size == 0);
GPR_ASSERT(bytes_to_send == nullptr);
GPR_ASSERT(result != nullptr);
ASSERT_EQ(status, TSI_OK);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send_size, 0);
ASSERT_EQ(bytes_to_send, nullptr);
ASSERT_NE(result, nullptr);
// Validate max frame size value after Frame Size Negotiation. The negotiated
// frame size value equals minimum send frame size, due to the absence of peer
// max frame size.
tsi_zero_copy_grpc_protector* zero_copy_protector = nullptr;
size_t user_specified_max_frame_size =
ALTS_TSI_HANDSHAKER_TEST_MAX_FRAME_SIZE;
GPR_ASSERT(tsi_handshaker_result_create_zero_copy_grpc_protector(
result, &user_specified_max_frame_size,
&zero_copy_protector) == TSI_OK);
ASSERT_EQ(tsi_handshaker_result_create_zero_copy_grpc_protector(
result, &user_specified_max_frame_size, &zero_copy_protector),
TSI_OK);
size_t actual_max_frame_size;
tsi_zero_copy_grpc_protector_max_frame_size(zero_copy_protector,
&actual_max_frame_size);
GPR_ASSERT(actual_max_frame_size == kTsiAltsMinFrameSize);
ASSERT_EQ(actual_max_frame_size, kTsiAltsMinFrameSize);
tsi_zero_copy_grpc_protector_destroy(zero_copy_protector);
/* Validate peer identity. */
tsi_peer peer;
GPR_ASSERT(tsi_handshaker_result_extract_peer(result, &peer) == TSI_OK);
GPR_ASSERT(peer.property_count == kTsiAltsNumOfPeerProperties);
GPR_ASSERT(memcmp(TSI_ALTS_CERTIFICATE_TYPE, peer.properties[0].value.data,
peer.properties[0].value.length) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY,
peer.properties[1].value.data,
peer.properties[1].value.length) == 0);
ASSERT_EQ(tsi_handshaker_result_extract_peer(result, &peer), TSI_OK);
ASSERT_EQ(peer.property_count, kTsiAltsNumOfPeerProperties);
ASSERT_EQ(memcmp(TSI_ALTS_CERTIFICATE_TYPE, peer.properties[0].value.data,
peer.properties[0].value.length),
0);
ASSERT_EQ(
memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY,
peer.properties[1].value.data, peer.properties[1].value.length),
0);
/* Validate alts context. */
upb::Arena context_arena;
grpc_gcp_AltsContext* ctx = grpc_gcp_AltsContext_parse(
peer.properties[3].value.data, peer.properties[3].value.length,
context_arena.ptr());
GPR_ASSERT(ctx != nullptr);
ASSERT_NE(ctx, nullptr);
upb_StringView application_protocol =
grpc_gcp_AltsContext_application_protocol(ctx);
upb_StringView record_protocol = grpc_gcp_AltsContext_record_protocol(ctx);
upb_StringView peer_account = grpc_gcp_AltsContext_peer_service_account(ctx);
upb_StringView local_account =
grpc_gcp_AltsContext_local_service_account(ctx);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_APPLICATION_PROTOCOL,
application_protocol.data, application_protocol.size) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_RECORD_PROTOCOL,
record_protocol.data, record_protocol.size) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY, peer_account.data,
peer_account.size) == 0);
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_LOCAL_IDENTITY, local_account.data,
local_account.size) == 0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_APPLICATION_PROTOCOL,
application_protocol.data, application_protocol.size),
0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_RECORD_PROTOCOL,
record_protocol.data, record_protocol.size),
0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_PEER_IDENTITY, peer_account.data,
peer_account.size),
0);
ASSERT_EQ(memcmp(ALTS_TSI_HANDSHAKER_TEST_LOCAL_IDENTITY, local_account.data,
local_account.size),
0);
size_t iter = kUpb_Map_Begin;
grpc_gcp_AltsContext_PeerAttributesEntry* peer_attributes_entry =
grpc_gcp_AltsContext_peer_attributes_nextmutable(ctx, &iter);
GPR_ASSERT(peer_attributes_entry != nullptr);
ASSERT_NE(peer_attributes_entry, nullptr);
while (peer_attributes_entry != nullptr) {
upb_StringView key = grpc_gcp_AltsContext_PeerAttributesEntry_key(
const_cast<grpc_gcp_AltsContext_PeerAttributesEntry*>(
@ -453,33 +475,35 @@ static void on_server_next_success_cb(tsi_result status, void* user_data,
upb_StringView val = grpc_gcp_AltsContext_PeerAttributesEntry_value(
const_cast<grpc_gcp_AltsContext_PeerAttributesEntry*>(
peer_attributes_entry));
GPR_ASSERT(upb_StringView_IsEqual(
ASSERT_TRUE(upb_StringView_IsEqual(
key, upb_StringView_FromString(
ALTS_TSI_HANDSHAKER_TEST_PEER_ATTRIBUTES_KEY)));
GPR_ASSERT(upb_StringView_IsEqual(
ASSERT_TRUE(upb_StringView_IsEqual(
val, upb_StringView_FromString(
ALTS_TSI_HANDSHAKER_TEST_PEER_ATTRIBUTES_VALUE)));
peer_attributes_entry =
grpc_gcp_AltsContext_peer_attributes_nextmutable(ctx, &iter);
}
/* Check security level. */
GPR_ASSERT(memcmp(ALTS_TSI_HANDSHAKER_TEST_SECURITY_LEVEL,
peer.properties[4].value.data,
peer.properties[4].value.length) == 0);
ASSERT_EQ(
memcmp(ALTS_TSI_HANDSHAKER_TEST_SECURITY_LEVEL,
peer.properties[4].value.data, peer.properties[4].value.length),
0);
tsi_peer_destruct(&peer);
/* Validate unused bytes. */
const unsigned char* bytes = nullptr;
size_t bytes_size = 0;
GPR_ASSERT(tsi_handshaker_result_get_unused_bytes(result, &bytes,
&bytes_size) == TSI_OK);
GPR_ASSERT(bytes_size == 0);
GPR_ASSERT(bytes == nullptr);
ASSERT_EQ(tsi_handshaker_result_get_unused_bytes(result, &bytes, &bytes_size),
TSI_OK);
ASSERT_EQ(bytes_size, 0);
ASSERT_EQ(bytes, nullptr);
/* Validate frame protector. */
tsi_frame_protector* protector = nullptr;
GPR_ASSERT(tsi_handshaker_result_create_frame_protector(
result, nullptr, &protector) == TSI_OK);
GPR_ASSERT(protector != nullptr);
ASSERT_EQ(
tsi_handshaker_result_create_frame_protector(result, nullptr, &protector),
TSI_OK);
ASSERT_NE(protector, nullptr);
tsi_frame_protector_destroy(protector);
tsi_handshaker_result_destroy(result);
signal(&tsi_to_caller_notification);
@ -514,7 +538,7 @@ static tsi_result mock_server_start(alts_handshaker_client* client,
alts_handshaker_client_check_fields_for_testing(
client, on_server_start_success_cb, nullptr, true, nullptr);
grpc_slice slice = grpc_empty_slice();
GPR_ASSERT(grpc_slice_cmp(*bytes_received, slice) == 0);
EXPECT_EQ(grpc_slice_cmp(*bytes_received, slice), 0);
/* Populate handshaker response for server_start request. */
grpc_byte_buffer** recv_buffer_ptr =
alts_handshaker_client_get_recv_buffer_addr_for_testing(client);
@ -539,10 +563,11 @@ static tsi_result mock_next(alts_handshaker_client* client,
alts_handshaker_client_set_recv_bytes_for_testing(client, bytes_received);
alts_handshaker_client_check_fields_for_testing(client, cb, nullptr, true,
bytes_received);
GPR_ASSERT(bytes_received != nullptr);
GPR_ASSERT(memcmp(GRPC_SLICE_START_PTR(*bytes_received),
ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
GRPC_SLICE_LENGTH(*bytes_received)) == 0);
EXPECT_NE(bytes_received, nullptr);
EXPECT_EQ(memcmp(GRPC_SLICE_START_PTR(*bytes_received),
ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
GRPC_SLICE_LENGTH(*bytes_received)),
0);
/* Populate handshaker response for next request. */
grpc_slice out_frame =
grpc_slice_from_static_string(ALTS_TSI_HANDSHAKER_TEST_OUT_FRAME);
@ -583,22 +608,24 @@ static void run_tsi_handshaker_destroy_with_exec_ctx(
tsi_handshaker_destroy(handshaker);
}
static void check_handshaker_next_invalid_input() {
TEST(AltsTsiHandshakerTest, CheckHandshakerNextInvalidInput) {
should_handshaker_client_api_succeed = true;
/* Initialization. */
tsi_handshaker* handshaker = create_test_handshaker(true);
/* Check nullptr handshaker. */
GPR_ASSERT(tsi_handshaker_next(nullptr, nullptr, 0, nullptr, nullptr, nullptr,
check_must_not_be_called,
nullptr) == TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_next(nullptr, nullptr, 0, nullptr, nullptr, nullptr,
check_must_not_be_called, nullptr),
TSI_INVALID_ARGUMENT);
/* Check nullptr callback. */
GPR_ASSERT(tsi_handshaker_next(handshaker, nullptr, 0, nullptr, nullptr,
nullptr, nullptr,
nullptr) == TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_next(handshaker, nullptr, 0, nullptr, nullptr,
nullptr, nullptr, nullptr),
TSI_INVALID_ARGUMENT);
/* Cleanup. */
run_tsi_handshaker_destroy_with_exec_ctx(handshaker);
}
static void check_handshaker_shutdown_invalid_input() {
TEST(AltsTsiHandshakerTest, CheckHandshakerShutdownInvalidInput) {
should_handshaker_client_api_succeed = false;
/* Initialization. */
tsi_handshaker* handshaker = create_test_handshaker(true /* is_client */);
/* Check nullptr handshaker. */
@ -617,28 +644,30 @@ static void check_handshaker_next_success() {
tsi_handshaker* server_handshaker =
create_test_handshaker(false /* is_client */);
/* Client start. */
GPR_ASSERT(tsi_handshaker_next(client_handshaker, nullptr, 0, nullptr,
nullptr, nullptr, on_client_start_success_cb,
nullptr) == TSI_ASYNC);
ASSERT_EQ(tsi_handshaker_next(client_handshaker, nullptr, 0, nullptr, nullptr,
nullptr, on_client_start_success_cb, nullptr),
TSI_ASYNC);
wait(&tsi_to_caller_notification);
/* Client next. */
GPR_ASSERT(tsi_handshaker_next(
client_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, on_client_next_success_cb, nullptr) == TSI_ASYNC);
ASSERT_EQ(tsi_handshaker_next(
client_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, on_client_next_success_cb, nullptr),
TSI_ASYNC);
wait(&tsi_to_caller_notification);
/* Server start. */
GPR_ASSERT(tsi_handshaker_next(server_handshaker, nullptr, 0, nullptr,
nullptr, nullptr, on_server_start_success_cb,
nullptr) == TSI_ASYNC);
ASSERT_EQ(tsi_handshaker_next(server_handshaker, nullptr, 0, nullptr, nullptr,
nullptr, on_server_start_success_cb, nullptr),
TSI_ASYNC);
wait(&tsi_to_caller_notification);
/* Server next. */
GPR_ASSERT(tsi_handshaker_next(
server_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, on_server_next_success_cb, nullptr) == TSI_ASYNC);
ASSERT_EQ(tsi_handshaker_next(
server_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, on_server_next_success_cb, nullptr),
TSI_ASYNC);
wait(&tsi_to_caller_notification);
/* Cleanup. */
run_tsi_handshaker_destroy_with_exec_ctx(server_handshaker);
@ -648,17 +677,17 @@ static void check_handshaker_next_success() {
static void check_handshaker_next_with_shutdown() {
tsi_handshaker* handshaker = create_test_handshaker(true /* is_client*/);
/* next(success) -- shutdown(success) -- next (fail) */
GPR_ASSERT(tsi_handshaker_next(handshaker, nullptr, 0, nullptr, nullptr,
nullptr, on_client_start_success_cb,
nullptr) == TSI_ASYNC);
ASSERT_EQ(tsi_handshaker_next(handshaker, nullptr, 0, nullptr, nullptr,
nullptr, on_client_start_success_cb, nullptr),
TSI_ASYNC);
wait(&tsi_to_caller_notification);
tsi_handshaker_shutdown(handshaker);
GPR_ASSERT(tsi_handshaker_next(
handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, on_client_next_success_cb,
nullptr) == TSI_HANDSHAKE_SHUTDOWN);
ASSERT_EQ(
tsi_handshaker_next(
handshaker, (const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, on_client_next_success_cb, nullptr),
TSI_HANDSHAKE_SHUTDOWN);
/* Cleanup. */
run_tsi_handshaker_destroy_with_exec_ctx(handshaker);
}
@ -668,7 +697,8 @@ static void check_handle_response_with_shutdown(void* /*unused*/) {
alts_handshaker_client_handle_response(cb_event, true /* is_ok */);
}
static void check_handshaker_next_failure() {
TEST(AltsTsiHandshakerTest, CheckHandshakerNextFailure) {
should_handshaker_client_api_succeed = false;
/**
* Create handshakers for which internal mock client is always going to fail.
*/
@ -677,27 +707,27 @@ static void check_handshaker_next_failure() {
tsi_handshaker* server_handshaker =
create_test_handshaker(false /* is_client */);
/* Client start. */
GPR_ASSERT(tsi_handshaker_next(client_handshaker, nullptr, 0, nullptr,
nullptr, nullptr, check_must_not_be_called,
nullptr) == TSI_INTERNAL_ERROR);
ASSERT_EQ(tsi_handshaker_next(client_handshaker, nullptr, 0, nullptr, nullptr,
nullptr, check_must_not_be_called, nullptr),
TSI_INTERNAL_ERROR);
/* Server start. */
GPR_ASSERT(tsi_handshaker_next(server_handshaker, nullptr, 0, nullptr,
nullptr, nullptr, check_must_not_be_called,
nullptr) == TSI_INTERNAL_ERROR);
ASSERT_EQ(tsi_handshaker_next(server_handshaker, nullptr, 0, nullptr, nullptr,
nullptr, check_must_not_be_called, nullptr),
TSI_INTERNAL_ERROR);
/* Server next. */
GPR_ASSERT(tsi_handshaker_next(
server_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, check_must_not_be_called,
nullptr) == TSI_INTERNAL_ERROR);
ASSERT_EQ(tsi_handshaker_next(
server_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, check_must_not_be_called, nullptr),
TSI_INTERNAL_ERROR);
/* Client next. */
GPR_ASSERT(tsi_handshaker_next(
client_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, check_must_not_be_called,
nullptr) == TSI_INTERNAL_ERROR);
ASSERT_EQ(tsi_handshaker_next(
client_handshaker,
(const unsigned char*)ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES,
strlen(ALTS_TSI_HANDSHAKER_TEST_RECV_BYTES), nullptr, nullptr,
nullptr, check_must_not_be_called, nullptr),
TSI_INTERNAL_ERROR);
/* Cleanup. */
run_tsi_handshaker_destroy_with_exec_ctx(server_handshaker);
run_tsi_handshaker_destroy_with_exec_ctx(client_handshaker);
@ -707,25 +737,26 @@ static void on_invalid_input_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_INTERNAL_ERROR);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send == nullptr);
GPR_ASSERT(bytes_to_send_size == 0);
GPR_ASSERT(result == nullptr);
ASSERT_EQ(status, TSI_INTERNAL_ERROR);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send, nullptr);
ASSERT_EQ(bytes_to_send_size, 0);
ASSERT_EQ(result, nullptr);
}
static void on_failed_grpc_call_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_INTERNAL_ERROR);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send == nullptr);
GPR_ASSERT(bytes_to_send_size == 0);
GPR_ASSERT(result == nullptr);
ASSERT_EQ(status, TSI_INTERNAL_ERROR);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send, nullptr);
ASSERT_EQ(bytes_to_send_size, 0);
ASSERT_EQ(result, nullptr);
}
static void check_handle_response_nullptr_handshaker() {
TEST(AltsTsiHandshakerTest, CheckHandleResponseNullptrHandshaker) {
should_handshaker_client_api_succeed = false;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -764,7 +795,8 @@ static void check_handle_response_nullptr_handshaker() {
notification_destroy(&tsi_to_caller_notification);
}
static void check_handle_response_nullptr_recv_bytes() {
TEST(AltsTsiHandshakerTest, CheckHandleResponseNullptrRecvBytes) {
should_handshaker_client_api_succeed = false;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -796,7 +828,9 @@ static void check_handle_response_nullptr_recv_bytes() {
notification_destroy(&tsi_to_caller_notification);
}
static void check_handle_response_failed_grpc_call_to_handshaker_service() {
TEST(AltsTsiHandshakerTest,
CheckHandleResponseFailedGrpcCallToHandshakerService) {
should_handshaker_client_api_succeed = false;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -831,8 +865,9 @@ static void check_handle_response_failed_grpc_call_to_handshaker_service() {
notification_destroy(&tsi_to_caller_notification);
}
static void
check_handle_response_failed_recv_message_from_handshaker_service() {
TEST(AltsTsiHandshakerTest,
CheckHandleResponseFailedRecvMessageFromHandshakerService) {
should_handshaker_client_api_succeed = false;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -871,14 +906,15 @@ static void on_invalid_resp_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_DATA_CORRUPTED);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send == nullptr);
GPR_ASSERT(bytes_to_send_size == 0);
GPR_ASSERT(result == nullptr);
ASSERT_EQ(status, TSI_DATA_CORRUPTED);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send, nullptr);
ASSERT_EQ(bytes_to_send_size, 0);
ASSERT_EQ(result, nullptr);
}
static void check_handle_response_invalid_resp() {
TEST(AltsTsiHandshakerTest, CheckHandleResponseInvalidResp) {
should_handshaker_client_api_succeed = false;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -942,14 +978,15 @@ static void on_failed_resp_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_INVALID_ARGUMENT);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send == nullptr);
GPR_ASSERT(bytes_to_send_size == 0);
GPR_ASSERT(result == nullptr);
ASSERT_EQ(status, TSI_INVALID_ARGUMENT);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send, nullptr);
ASSERT_EQ(bytes_to_send_size, 0);
ASSERT_EQ(result, nullptr);
}
static void check_handle_response_failure() {
TEST(AltsTsiHandshakerTest, CheckHandleResponseFailure) {
should_handshaker_client_api_succeed = false;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -986,14 +1023,15 @@ static void on_shutdown_resp_cb(tsi_result status, void* user_data,
const unsigned char* bytes_to_send,
size_t bytes_to_send_size,
tsi_handshaker_result* result) {
GPR_ASSERT(status == TSI_HANDSHAKE_SHUTDOWN);
GPR_ASSERT(user_data == nullptr);
GPR_ASSERT(bytes_to_send == nullptr);
GPR_ASSERT(bytes_to_send_size == 0);
GPR_ASSERT(result == nullptr);
ASSERT_EQ(status, TSI_HANDSHAKE_SHUTDOWN);
ASSERT_EQ(user_data, nullptr);
ASSERT_EQ(bytes_to_send, nullptr);
ASSERT_EQ(bytes_to_send_size, 0);
ASSERT_EQ(result, nullptr);
}
static void check_handle_response_after_shutdown() {
TEST(AltsTsiHandshakerTest, CheckHandleResponseAfterShutdown) {
should_handshaker_client_api_succeed = true;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -1027,7 +1065,8 @@ static void check_handle_response_after_shutdown() {
notification_destroy(&tsi_to_caller_notification);
}
void check_handshaker_next_fails_after_shutdown() {
TEST(AltsTsiHandshakerTest, CheckHandshakerNextFailsAfterShutdown) {
should_handshaker_client_api_succeed = true;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -1043,7 +1082,8 @@ void check_handshaker_next_fails_after_shutdown() {
notification_destroy(&tsi_to_caller_notification);
}
void check_handshaker_success() {
TEST(AltsTsiHandshakerTest, CheckHandshakerSuccess) {
should_handshaker_client_api_succeed = true;
/* Initialization. */
notification_init(&caller_to_tsi_notification);
notification_init(&tsi_to_caller_notification);
@ -1060,26 +1100,10 @@ void check_handshaker_success() {
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
/* Initialization. */
grpc_init();
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
grpc_alts_shared_resource_dedicated_init();
/* Tests. */
should_handshaker_client_api_succeed = true;
check_handshaker_success();
check_handshaker_next_invalid_input();
check_handshaker_next_fails_after_shutdown();
check_handle_response_after_shutdown();
should_handshaker_client_api_succeed = false;
check_handshaker_shutdown_invalid_input();
check_handshaker_next_failure();
check_handle_response_nullptr_handshaker();
check_handle_response_nullptr_recv_bytes();
check_handle_response_failed_grpc_call_to_handshaker_service();
check_handle_response_failed_recv_message_from_handshaker_service();
check_handle_response_invalid_resp();
check_handle_response_failure();
/* Cleanup. */
int ret = RUN_ALL_TESTS();
grpc_alts_shared_resource_dedicated_shutdown();
grpc_shutdown();
return 0;
return ret;
}

@ -18,6 +18,8 @@
#include "src/core/tsi/alts/handshaker/alts_tsi_utils.h"
#include <gtest/gtest.h>
#include "upb/upb.hpp"
#include "test/core/tsi/alts/handshaker/alts_handshaker_service_api_test_lib.h"
@ -25,21 +27,21 @@
#define ALTS_TSI_UTILS_TEST_OUT_FRAME "Hello Google"
static void convert_to_tsi_result_test() {
GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OK) == TSI_OK);
GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_UNKNOWN) ==
TSI_UNKNOWN_ERROR);
GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(
GRPC_STATUS_INVALID_ARGUMENT) == TSI_INVALID_ARGUMENT);
GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OUT_OF_RANGE) ==
TSI_UNKNOWN_ERROR);
GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_INTERNAL) ==
TSI_INTERNAL_ERROR);
GPR_ASSERT(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_NOT_FOUND) ==
TSI_NOT_FOUND);
TEST(AltsTsiUtilsTest, ConvertToTsiResultTest) {
ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OK), TSI_OK);
ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_UNKNOWN),
TSI_UNKNOWN_ERROR);
ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_INVALID_ARGUMENT),
TSI_INVALID_ARGUMENT);
ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_OUT_OF_RANGE),
TSI_UNKNOWN_ERROR);
ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_INTERNAL),
TSI_INTERNAL_ERROR);
ASSERT_EQ(alts_tsi_utils_convert_to_tsi_result(GRPC_STATUS_NOT_FOUND),
TSI_NOT_FOUND);
}
static void deserialize_response_test() {
TEST(AltsTsiUtilsTest, DeserializeResponseTest) {
upb::Arena arena;
grpc_gcp_HandshakerResp* resp = grpc_gcp_HandshakerResp_new(arena.ptr());
grpc_gcp_HandshakerResp_set_out_frames(
@ -54,15 +56,14 @@ static void deserialize_response_test() {
grpc_raw_byte_buffer_create(&slice, 1 /* number of slices */);
grpc_gcp_HandshakerResp* decoded_resp =
alts_tsi_utils_deserialize_response(buffer, arena2.ptr());
GPR_ASSERT(grpc_gcp_handshaker_resp_equals(resp, decoded_resp));
ASSERT_TRUE(grpc_gcp_handshaker_resp_equals(resp, decoded_resp));
grpc_byte_buffer_destroy(buffer);
/* Invalid serialization. */
grpc_slice bad_slice =
grpc_slice_split_head(&slice, GRPC_SLICE_LENGTH(slice) - 1);
buffer = grpc_raw_byte_buffer_create(&bad_slice, 1 /* number of slices */);
GPR_ASSERT(alts_tsi_utils_deserialize_response(buffer, arena2.ptr()) ==
nullptr);
ASSERT_EQ(alts_tsi_utils_deserialize_response(buffer, arena2.ptr()), nullptr);
/* Clean up. */
grpc_slice_unref(slice);
@ -72,10 +73,7 @@ static void deserialize_response_test() {
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
/* Tests. */
grpc_init();
deserialize_response_test();
convert_to_tsi_result_test();
grpc_shutdown();
return 0;
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
return RUN_ALL_TESTS();
}

@ -22,6 +22,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <gtest/gtest.h>
const size_t kMaxRpcVersionMajor = 3;
const size_t kMaxRpcVersionMinor = 2;
const size_t kMinRpcVersionMajor = 2;
@ -30,7 +32,7 @@ const size_t kMinRpcVersionMinor = 1;
static bool grpc_gcp_rpc_protocol_versions_equal(
grpc_gcp_rpc_protocol_versions* l_versions,
grpc_gcp_rpc_protocol_versions* r_versions) {
GPR_ASSERT(l_versions != nullptr && r_versions != nullptr);
EXPECT_TRUE(l_versions != nullptr && r_versions != nullptr);
if ((l_versions->max_rpc_version.major !=
r_versions->max_rpc_version.major) ||
(l_versions->max_rpc_version.minor !=
@ -46,121 +48,121 @@ static bool grpc_gcp_rpc_protocol_versions_equal(
return true;
}
static void test_success() {
TEST(TransportSecurityCommonApiTest, TestSuccess) {
grpc_gcp_rpc_protocol_versions version;
grpc_gcp_rpc_protocol_versions decoded_version;
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(
&version, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(
&version, kMinRpcVersionMajor, kMinRpcVersionMinor));
/* Serializes to grpc slice. */
grpc_slice encoded_slice;
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_encode(&version, &encoded_slice));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_encode(&version, &encoded_slice));
/* Deserializes and compares with the original version. */
GPR_ASSERT(
ASSERT_TRUE(
grpc_gcp_rpc_protocol_versions_decode(encoded_slice, &decoded_version));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_equal(&version, &decoded_version));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_equal(&version, &decoded_version));
grpc_slice_unref(encoded_slice);
}
static void test_failure() {
TEST(TransportSecurityCommonApiTest, TestFailure) {
grpc_gcp_rpc_protocol_versions version, decoded_version;
grpc_slice encoded_slice;
/* Test for invalid arguments. */
GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_set_max(
ASSERT_FALSE(grpc_gcp_rpc_protocol_versions_set_max(
nullptr, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_set_min(
ASSERT_FALSE(grpc_gcp_rpc_protocol_versions_set_min(
nullptr, kMinRpcVersionMajor, kMinRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(
&version, kMaxRpcVersionMajor, kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(
&version, kMinRpcVersionMajor, kMinRpcVersionMinor));
GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_encode(nullptr, &encoded_slice));
GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_encode(&version, nullptr));
GPR_ASSERT(!grpc_gcp_rpc_protocol_versions_decode(encoded_slice, nullptr));
ASSERT_FALSE(grpc_gcp_rpc_protocol_versions_encode(nullptr, &encoded_slice));
ASSERT_FALSE(grpc_gcp_rpc_protocol_versions_encode(&version, nullptr));
ASSERT_FALSE(grpc_gcp_rpc_protocol_versions_decode(encoded_slice, nullptr));
/* Test for upb decode. */
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_encode(&version, &encoded_slice));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_encode(&version, &encoded_slice));
grpc_slice bad_slice = grpc_slice_split_head(
&encoded_slice, GRPC_SLICE_LENGTH(encoded_slice) - 1);
grpc_slice_unref(encoded_slice);
GPR_ASSERT(
ASSERT_TRUE(
!grpc_gcp_rpc_protocol_versions_decode(bad_slice, &decoded_version));
grpc_slice_unref(bad_slice);
}
static void test_copy() {
TEST(TransportSecurityCommonApiTest, TestCopy) {
grpc_gcp_rpc_protocol_versions src;
grpc_gcp_rpc_protocol_versions des;
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&src, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&src, kMinRpcVersionMajor,
kMinRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_copy(&src, &des));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_equal(&src, &des));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(&src, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(&src, kMinRpcVersionMajor,
kMinRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_copy(&src, &des));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_equal(&src, &des));
}
static void test_check_success() {
TEST(TransportSecurityCommonApiTest, TestCheckSuccess) {
grpc_gcp_rpc_protocol_versions v1;
grpc_gcp_rpc_protocol_versions v2;
grpc_gcp_rpc_protocol_versions_version highest_common_version;
/* test equality. */
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_check(
(const grpc_gcp_rpc_protocol_versions*)&v1,
(const grpc_gcp_rpc_protocol_versions*)&v2,
&highest_common_version) == 1);
GPR_ASSERT(grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
&highest_common_version, &v1.max_rpc_version) == 0);
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_EQ(
grpc_gcp_rpc_protocol_versions_check(
(const grpc_gcp_rpc_protocol_versions*)&v1,
(const grpc_gcp_rpc_protocol_versions*)&v2, &highest_common_version),
1);
ASSERT_EQ(grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
&highest_common_version, &v1.max_rpc_version),
0);
/* test inequality. */
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMinRpcVersionMinor,
kMinRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
kMinRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMinRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_check(
(const grpc_gcp_rpc_protocol_versions*)&v1,
(const grpc_gcp_rpc_protocol_versions*)&v2,
&highest_common_version) == 1);
GPR_ASSERT(grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
&highest_common_version, &v2.max_rpc_version) == 0);
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMinRpcVersionMinor,
kMinRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
kMinRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMinRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_EQ(
grpc_gcp_rpc_protocol_versions_check(
(const grpc_gcp_rpc_protocol_versions*)&v1,
(const grpc_gcp_rpc_protocol_versions*)&v2, &highest_common_version),
1);
ASSERT_EQ(grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
&highest_common_version, &v2.max_rpc_version),
0);
}
static void test_check_failure() {
TEST(TransportSecurityCommonApiTest, TestCheckFailure) {
grpc_gcp_rpc_protocol_versions v1;
grpc_gcp_rpc_protocol_versions v2;
grpc_gcp_rpc_protocol_versions_version highest_common_version;
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMinRpcVersionMajor,
kMinRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMinRpcVersionMajor,
kMinRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
GPR_ASSERT(grpc_gcp_rpc_protocol_versions_check(
(const grpc_gcp_rpc_protocol_versions*)&v1,
(const grpc_gcp_rpc_protocol_versions*)&v2,
&highest_common_version) == 0);
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(&v1, kMinRpcVersionMajor,
kMinRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(&v1, kMinRpcVersionMajor,
kMinRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_max(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_TRUE(grpc_gcp_rpc_protocol_versions_set_min(&v2, kMaxRpcVersionMajor,
kMaxRpcVersionMinor));
ASSERT_EQ(
grpc_gcp_rpc_protocol_versions_check(
(const grpc_gcp_rpc_protocol_versions*)&v1,
(const grpc_gcp_rpc_protocol_versions*)&v2, &highest_common_version),
0);
}
int main(int /*argc*/, char** /*argv*/) {
/* Run tests. */
test_success();
test_failure();
test_copy();
test_check_success();
test_check_failure();
return 0;
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -21,6 +21,7 @@ grpc_package(name = "test/core/tsi/alts/zero_copy_frame_protector")
grpc_cc_test(
name = "alts_grpc_record_protocol_test",
srcs = ["alts_grpc_record_protocol_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",
@ -34,6 +35,7 @@ grpc_cc_test(
grpc_cc_test(
name = "alts_iovec_record_protocol_test",
srcs = ["alts_iovec_record_protocol_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",
@ -46,6 +48,7 @@ grpc_cc_test(
grpc_cc_test(
name = "alts_zero_copy_grpc_protector_test",
srcs = ["alts_zero_copy_grpc_protector_test.cc"],
external_deps = ["gtest"],
language = "C++",
deps = [
"//:gpr",

@ -18,6 +18,8 @@
#include "src/core/tsi/alts/zero_copy_frame_protector/alts_grpc_record_protocol.h"
#include <gtest/gtest.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -55,7 +57,7 @@ struct alts_grpc_record_protocol_test_var {
/* --- Test utility functions. --- */
static void create_random_slice_buffer(grpc_slice_buffer* sb) {
GPR_ASSERT(sb != nullptr);
ASSERT_NE(sb, nullptr);
size_t slice_count = gsec_test_bias_random_uint32(kMaxSlices) + 1;
for (size_t i = 0; i < slice_count; i++) {
size_t slice_length = gsec_test_bias_random_uint32(kMaxSliceLength) + 1;
@ -66,8 +68,8 @@ static void create_random_slice_buffer(grpc_slice_buffer* sb) {
}
static uint8_t* pointer_to_nth_byte(grpc_slice_buffer* sb, size_t index) {
GPR_ASSERT(sb != nullptr);
GPR_ASSERT(index < sb->length);
EXPECT_NE(sb, nullptr);
EXPECT_LT(index, sb->length);
for (size_t i = 0; i < sb->count; i++) {
if (index < GRPC_SLICE_LENGTH(sb->slices[i])) {
return GRPC_SLICE_START_PTR(sb->slices[i]) + index;
@ -82,16 +84,16 @@ static uint8_t* pointer_to_nth_byte(grpc_slice_buffer* sb, size_t index) {
* but OK for testing. */
static bool are_slice_buffers_equal(grpc_slice_buffer* first,
grpc_slice_buffer* second) {
GPR_ASSERT(first != nullptr);
GPR_ASSERT(second != nullptr);
EXPECT_NE(first, nullptr);
EXPECT_NE(second, nullptr);
if (first->length != second->length) {
return false;
}
for (size_t i = 0; i < first->length; i++) {
uint8_t* first_ptr = pointer_to_nth_byte(first, i);
uint8_t* second_ptr = pointer_to_nth_byte(second, i);
GPR_ASSERT(first_ptr != nullptr);
GPR_ASSERT(second_ptr != nullptr);
EXPECT_NE(first_ptr, nullptr);
EXPECT_NE(second_ptr, nullptr);
if ((*first_ptr) != (*second_ptr)) {
return false;
}
@ -100,7 +102,7 @@ static bool are_slice_buffers_equal(grpc_slice_buffer* first,
}
static void alter_random_byte(grpc_slice_buffer* sb) {
GPR_ASSERT(sb != nullptr);
ASSERT_NE(sb, nullptr);
if (sb->length == 0) {
return;
}
@ -121,33 +123,41 @@ test_fixture_integrity_only_create(bool rekey, bool extra_copy) {
gsec_aead_crypter* crypter = nullptr;
/* Create client record protocol for protect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/true,
extra_copy, &fixture->client_protect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/true, extra_copy,
&fixture->client_protect),
TSI_OK);
/* Create client record protocol for unprotect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/false,
extra_copy, &fixture->client_unprotect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/false,
extra_copy, &fixture->client_unprotect),
TSI_OK);
/* Create server record protocol for protect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/true,
extra_copy, &fixture->server_protect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/true,
extra_copy, &fixture->server_protect),
TSI_OK);
/* Create server record protocol for unprotect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/false,
extra_copy, &fixture->server_unprotect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_integrity_only_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/false,
extra_copy, &fixture->server_unprotect),
TSI_OK);
gpr_free(key);
return fixture;
@ -179,34 +189,41 @@ test_fixture_privacy_integrity_create(bool rekey) {
gsec_aead_crypter* crypter = nullptr;
/* Create client record protocol for protect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/true,
&fixture->client_protect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/true,
&fixture->client_protect),
TSI_OK);
/* Create client record protocol for unprotect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/false,
&fixture->client_unprotect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/true, /*is_protect=*/false,
&fixture->client_unprotect),
TSI_OK);
/* Create server record protocol for protect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/true,
&fixture->server_protect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/true,
&fixture->server_protect),
TSI_OK);
/* Create server record protocol for unprotect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/false,
&fixture->server_unprotect) == TSI_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_grpc_privacy_integrity_record_protocol_create(
crypter, 8, /*is_client=*/false, /*is_protect=*/false,
&fixture->server_unprotect),
TSI_OK);
gpr_free(key);
return fixture;
}
@ -280,13 +297,13 @@ static void random_seal_unseal(alts_grpc_record_protocol* sender,
size_t data_length = var->original_sb.length;
tsi_result status = alts_grpc_record_protocol_protect(
sender, &var->original_sb, &var->protected_sb);
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(var->protected_sb.length ==
data_length + var->header_length + var->tag_length);
ASSERT_EQ(status, TSI_OK);
ASSERT_EQ(var->protected_sb.length,
data_length + var->header_length + var->tag_length);
status = alts_grpc_record_protocol_unprotect(receiver, &var->protected_sb,
&var->unprotected_sb);
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(
ASSERT_EQ(status, TSI_OK);
ASSERT_TRUE(
are_slice_buffers_equal(&var->unprotected_sb, &var->duplicate_sb));
alts_grpc_record_protocol_test_var_destroy(var);
}
@ -304,13 +321,12 @@ static void empty_seal_unseal(alts_grpc_record_protocol* sender,
grpc_slice_buffer_reset_and_unref_internal(&var->duplicate_sb);
tsi_result status = alts_grpc_record_protocol_protect(
sender, &var->original_sb, &var->protected_sb);
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(var->protected_sb.length ==
var->header_length + var->tag_length);
ASSERT_EQ(status, TSI_OK);
ASSERT_EQ(var->protected_sb.length, var->header_length + var->tag_length);
status = alts_grpc_record_protocol_unprotect(receiver, &var->protected_sb,
&var->unprotected_sb);
GPR_ASSERT(status == TSI_OK);
GPR_ASSERT(
ASSERT_EQ(status, TSI_OK);
ASSERT_TRUE(
are_slice_buffers_equal(&var->unprotected_sb, &var->duplicate_sb));
alts_grpc_record_protocol_test_var_destroy(var);
}
@ -326,16 +342,16 @@ static void unsync_seal_unseal(alts_grpc_record_protocol* sender,
/* Seals once. */
status = alts_grpc_record_protocol_protect(sender, &var->original_sb,
&var->protected_sb);
GPR_ASSERT(status == TSI_OK);
ASSERT_EQ(status, TSI_OK);
grpc_slice_buffer_reset_and_unref_internal(&var->protected_sb);
/* Seals again. */
status = alts_grpc_record_protocol_protect(sender, &var->duplicate_sb,
&var->protected_sb);
GPR_ASSERT(status == TSI_OK);
ASSERT_EQ(status, TSI_OK);
/* Unseals the second frame. */
status = alts_grpc_record_protocol_unprotect(receiver, &var->protected_sb,
&var->unprotected_sb);
GPR_ASSERT(status == TSI_INTERNAL_ERROR);
ASSERT_EQ(status, TSI_INTERNAL_ERROR);
alts_grpc_record_protocol_test_var_destroy(var);
grpc_core::ExecCtx::Get()->Flush();
}
@ -349,12 +365,12 @@ static void corrupted_data(alts_grpc_record_protocol* sender,
/* Seals once. */
status = alts_grpc_record_protocol_protect(sender, &var->original_sb,
&var->protected_sb);
GPR_ASSERT(status == TSI_OK);
ASSERT_EQ(status, TSI_OK);
/* Corrupts one byte in protected_sb and tries to unprotect. */
alter_random_byte(&var->protected_sb);
status = alts_grpc_record_protocol_unprotect(receiver, &var->protected_sb,
&var->unprotected_sb);
GPR_ASSERT(status == TSI_INTERNAL_ERROR);
ASSERT_EQ(status, TSI_INTERNAL_ERROR);
alts_grpc_record_protocol_test_var_destroy(var);
grpc_core::ExecCtx::Get()->Flush();
}
@ -366,18 +382,18 @@ static void input_check(alts_grpc_record_protocol* rp) {
alts_grpc_record_protocol_test_var_create();
/* Protects with nullptr input. */
status = alts_grpc_record_protocol_protect(rp, nullptr, &var->protected_sb);
GPR_ASSERT(status == TSI_INVALID_ARGUMENT);
ASSERT_EQ(status, TSI_INVALID_ARGUMENT);
status = alts_grpc_record_protocol_protect(rp, &var->original_sb, nullptr);
GPR_ASSERT(status == TSI_INVALID_ARGUMENT);
ASSERT_EQ(status, TSI_INVALID_ARGUMENT);
/* Unprotects with nullptr input. */
status = alts_grpc_record_protocol_protect(rp, &var->original_sb,
&var->protected_sb);
GPR_ASSERT(status == TSI_OK);
ASSERT_EQ(status, TSI_OK);
status =
alts_grpc_record_protocol_unprotect(rp, nullptr, &var->unprotected_sb);
GPR_ASSERT(status == TSI_INVALID_ARGUMENT);
ASSERT_EQ(status, TSI_INVALID_ARGUMENT);
status = alts_grpc_record_protocol_unprotect(rp, &var->protected_sb, nullptr);
GPR_ASSERT(status == TSI_INVALID_ARGUMENT);
ASSERT_EQ(status, TSI_INVALID_ARGUMENT);
/* Unprotects on a temporary slice buffer which length is smaller than header
* length plus tag length. */
grpc_slice_buffer temp_sb;
@ -386,7 +402,7 @@ static void input_check(alts_grpc_record_protocol* rp) {
&var->protected_sb, var->header_length + var->tag_length - 1, &temp_sb);
status =
alts_grpc_record_protocol_unprotect(rp, &temp_sb, &var->unprotected_sb);
GPR_ASSERT(status == TSI_INVALID_ARGUMENT);
ASSERT_EQ(status, TSI_INVALID_ARGUMENT);
grpc_slice_buffer_destroy_internal(&temp_sb);
alts_grpc_record_protocol_test_var_destroy(var);
grpc_core::ExecCtx::Get()->Flush();
@ -446,8 +462,7 @@ static void alts_grpc_record_protocol_tests(
alts_grpc_record_protocol_test_fixture_destroy(fixture_5);
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
TEST(AltsGrpcRecordProtocolTest, MainTest) {
grpc_init();
alts_grpc_record_protocol_tests(
&test_fixture_integrity_only_no_rekey_no_extra_copy_create);
@ -458,5 +473,11 @@ int main(int argc, char** argv) {
&test_fixture_privacy_integrity_no_rekey_create);
alts_grpc_record_protocol_tests(&test_fixture_privacy_integrity_rekey_create);
grpc_shutdown();
return 0;
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
return RUN_ALL_TESTS();
}

@ -18,6 +18,8 @@
#include "src/core/tsi/alts/zero_copy_frame_protector/alts_iovec_record_protocol.h"
#include <gtest/gtest.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -78,7 +80,7 @@ static void randomly_slice(uint8_t* input, size_t input_length,
}
static size_t alter_random_byte(uint8_t* buf, size_t buf_length) {
GPR_ASSERT(buf != nullptr);
EXPECT_NE(buf, nullptr);
uint32_t offset =
gsec_test_bias_random_uint32(static_cast<uint32_t>(buf_length));
(*(buf + offset))++;
@ -86,7 +88,7 @@ static size_t alter_random_byte(uint8_t* buf, size_t buf_length) {
}
static void revert_back_alter(uint8_t* buf, size_t offset) {
GPR_ASSERT(buf != nullptr);
ASSERT_NE(buf, nullptr);
(*(buf + offset))--;
}
@ -102,38 +104,41 @@ alts_iovec_record_protocol_test_fixture_create(bool rekey,
gsec_test_random_array(&key, key_length);
gsec_aead_crypter* crypter = nullptr;
/* Create client record protocol for protect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/true, integrity_only,
/*is_protect=*/true, &fixture->client_protect,
nullptr) == GRPC_STATUS_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/true, integrity_only,
/*is_protect=*/true, &fixture->client_protect, nullptr),
GRPC_STATUS_OK);
/* Create client record protocol for unprotect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/true, integrity_only,
/*is_protect=*/false, &fixture->client_unprotect,
nullptr) == GRPC_STATUS_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/true, integrity_only,
/*is_protect=*/false, &fixture->client_unprotect, nullptr),
GRPC_STATUS_OK);
/* Create server record protocol for protect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/false, integrity_only,
/*is_protect=*/true, &fixture->server_protect,
nullptr) == GRPC_STATUS_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/false, integrity_only,
/*is_protect=*/true, &fixture->server_protect, nullptr),
GRPC_STATUS_OK);
/* Create server record protocol for unprotect. */
GPR_ASSERT(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr) == GRPC_STATUS_OK);
GPR_ASSERT(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/false, integrity_only,
/*is_protect=*/false, &fixture->server_unprotect,
nullptr) == GRPC_STATUS_OK);
EXPECT_EQ(gsec_aes_gcm_aead_crypter_create(
key, key_length, kAesGcmNonceLength, kAesGcmTagLength, rekey,
&crypter, nullptr),
GRPC_STATUS_OK);
EXPECT_EQ(alts_iovec_record_protocol_create(
crypter, overflow_size, /*is_client=*/false, integrity_only,
/*is_protect=*/false, &fixture->server_unprotect, nullptr),
GRPC_STATUS_OK);
gpr_free(key);
return fixture;
}
@ -210,7 +215,7 @@ static void integrity_only_random_seal_unseal(
grpc_status_code status = alts_iovec_record_protocol_integrity_only_protect(
sender, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
gpr_free(var->data_iovec);
/* Randomly slices data buffer again. */
randomly_slice(var->data_buf, var->data_length, &var->data_iovec,
@ -218,10 +223,10 @@ static void integrity_only_random_seal_unseal(
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Makes sure data buffer has not been modified during
* seal/unseal. */
GPR_ASSERT(memcmp(var->data_buf, var->dup_buf, var->data_length) == 0);
ASSERT_EQ(memcmp(var->data_buf, var->dup_buf, var->data_length), 0);
alts_iovec_record_protocol_test_var_destroy(var);
}
}
@ -234,10 +239,10 @@ static void integrity_only_empty_seal_unseal(
/* Seals and then unseals empty payload. */
grpc_status_code status = alts_iovec_record_protocol_integrity_only_protect(
sender, nullptr, 0, var->header_iovec, var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, nullptr, 0, var->header_iovec, var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
alts_iovec_record_protocol_test_var_destroy(var);
}
}
@ -250,20 +255,20 @@ static void integrity_only_unsync_seal_unseal(
grpc_status_code status = alts_iovec_record_protocol_integrity_only_protect(
sender, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
alts_iovec_record_protocol_test_var_destroy(var);
/* Seals again. */
var = alts_iovec_record_protocol_test_var_create();
status = alts_iovec_record_protocol_integrity_only_protect(
sender, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Unseals the second frame. */
char* error_message = nullptr;
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message,
"Frame tag verification failed."));
gpr_free(error_message);
@ -278,7 +283,7 @@ static void integrity_only_corrupted_data(
grpc_status_code status = alts_iovec_record_protocol_integrity_only_protect(
sender, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Alter frame length field. */
char* error_message = nullptr;
size_t offset =
@ -286,7 +291,7 @@ static void integrity_only_corrupted_data(
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message, "Bad frame length."));
gpr_free(error_message);
revert_back_alter(var->header_buf, offset);
@ -296,7 +301,7 @@ static void integrity_only_corrupted_data(
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message,
"Unsupported message type."));
gpr_free(error_message);
@ -306,7 +311,7 @@ static void integrity_only_corrupted_data(
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message,
"Frame tag verification failed."));
gpr_free(error_message);
@ -316,7 +321,7 @@ static void integrity_only_corrupted_data(
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message,
"Frame tag verification failed."));
gpr_free(error_message);
@ -325,8 +330,8 @@ static void integrity_only_corrupted_data(
status = alts_iovec_record_protocol_integrity_only_unprotect(
receiver, var->data_iovec, var->data_iovec_length, var->header_iovec,
var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(var->data_buf, var->dup_buf, var->data_length) == 0);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(var->data_buf, var->dup_buf, var->data_length), 0);
alts_iovec_record_protocol_test_var_destroy(var);
}
@ -339,7 +344,7 @@ static void integrity_only_protect_input_check(alts_iovec_record_protocol* rp) {
grpc_status_code status = alts_iovec_record_protocol_integrity_only_protect(
rp, var->data_iovec, var->data_iovec_length, header_iovec, var->tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Header is nullptr."));
gpr_free(error_message);
@ -349,7 +354,7 @@ static void integrity_only_protect_input_check(alts_iovec_record_protocol* rp) {
status = alts_iovec_record_protocol_integrity_only_protect(
rp, var->data_iovec, var->data_iovec_length, header_iovec, var->tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Header length is incorrect."));
gpr_free(error_message);
@ -358,7 +363,7 @@ static void integrity_only_protect_input_check(alts_iovec_record_protocol* rp) {
status = alts_iovec_record_protocol_integrity_only_protect(
rp, var->data_iovec, var->data_iovec_length, var->header_iovec, tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message, "Tag is nullptr."));
gpr_free(error_message);
/* Tag buffer length is 0. */
@ -367,7 +372,7 @@ static void integrity_only_protect_input_check(alts_iovec_record_protocol* rp) {
status = alts_iovec_record_protocol_integrity_only_protect(
rp, var->data_iovec, var->data_iovec_length, var->header_iovec, tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Tag length is incorrect."));
gpr_free(error_message);
@ -384,7 +389,7 @@ static void integrity_only_unprotect_input_check(
grpc_status_code status = alts_iovec_record_protocol_integrity_only_unprotect(
rp, var->data_iovec, var->data_iovec_length, header_iovec, var->tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Header is nullptr."));
gpr_free(error_message);
@ -394,7 +399,7 @@ static void integrity_only_unprotect_input_check(
status = alts_iovec_record_protocol_integrity_only_unprotect(
rp, var->data_iovec, var->data_iovec_length, header_iovec, var->tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Header length is incorrect."));
gpr_free(error_message);
@ -403,7 +408,7 @@ static void integrity_only_unprotect_input_check(
status = alts_iovec_record_protocol_integrity_only_unprotect(
rp, var->data_iovec, var->data_iovec_length, var->header_iovec, tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message, "Tag is nullptr."));
gpr_free(error_message);
/* Tag buffer length is 0. */
@ -412,7 +417,7 @@ static void integrity_only_unprotect_input_check(
status = alts_iovec_record_protocol_integrity_only_unprotect(
rp, var->data_iovec, var->data_iovec_length, var->header_iovec, tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Tag length is incorrect."));
gpr_free(error_message);
@ -431,7 +436,7 @@ static void privacy_integrity_random_seal_unseal(
alts_iovec_record_protocol_privacy_integrity_protect(
sender, var->data_iovec, var->data_iovec_length,
var->protected_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
iovec_t header_iovec = {var->protected_buf, var->header_length};
gpr_free(var->data_iovec);
/* Randomly slices protected buffer, excluding the header. */
@ -441,9 +446,9 @@ static void privacy_integrity_random_seal_unseal(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
receiver, header_iovec, var->data_iovec, var->data_iovec_length,
var->unprotected_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Makes sure unprotected data are the same as the original. */
GPR_ASSERT(memcmp(var->data_buf, var->dup_buf, var->data_length) == 0);
ASSERT_EQ(memcmp(var->data_buf, var->dup_buf, var->data_length), 0);
alts_iovec_record_protocol_test_var_destroy(var);
}
}
@ -463,11 +468,11 @@ static void privacy_integrity_empty_seal_unseal(
grpc_status_code status =
alts_iovec_record_protocol_privacy_integrity_protect(
sender, nullptr, 0, protected_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
iovec_t header_iovec = {protected_buf, var->header_length};
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
receiver, header_iovec, &data_iovec, 1, unprotected_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
}
gpr_free(protected_buf);
alts_iovec_record_protocol_test_var_destroy(var);
@ -482,14 +487,14 @@ static void privacy_integrity_unsync_seal_unseal(
alts_iovec_record_protocol_privacy_integrity_protect(
sender, var->data_iovec, var->data_iovec_length, var->protected_iovec,
nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
alts_iovec_record_protocol_test_var_destroy(var);
/* Seals again. */
var = alts_iovec_record_protocol_test_var_create();
status = alts_iovec_record_protocol_privacy_integrity_protect(
sender, var->data_iovec, var->data_iovec_length, var->protected_iovec,
nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Unseals the second frame. */
char* error_message = nullptr;
iovec_t header_iovec = {var->protected_buf, var->header_length};
@ -498,7 +503,7 @@ static void privacy_integrity_unsync_seal_unseal(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
receiver, header_iovec, &protected_iovec, 1, var->unprotected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message, "Frame decryption failed."));
gpr_free(error_message);
alts_iovec_record_protocol_test_var_destroy(var);
@ -513,7 +518,7 @@ static void privacy_integrity_corrupted_data(
alts_iovec_record_protocol_privacy_integrity_protect(
sender, var->data_iovec, var->data_iovec_length, var->protected_iovec,
nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
char* error_message = nullptr;
uint8_t* header_buf = var->protected_buf;
size_t header_length = var->header_length;
@ -527,7 +532,7 @@ static void privacy_integrity_corrupted_data(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
receiver, header_iovec, &protected_iovec, 1, var->unprotected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message, "Bad frame length."));
gpr_free(error_message);
revert_back_alter(header_buf, offset);
@ -537,7 +542,7 @@ static void privacy_integrity_corrupted_data(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
receiver, header_iovec, &protected_iovec, 1, var->unprotected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message,
"Unsupported message type."));
gpr_free(error_message);
@ -547,7 +552,7 @@ static void privacy_integrity_corrupted_data(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
receiver, header_iovec, &protected_iovec, 1, var->unprotected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message, "Frame decryption failed."));
gpr_free(error_message);
revert_back_alter(protected_buf, offset);
@ -555,8 +560,8 @@ static void privacy_integrity_corrupted_data(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
receiver, header_iovec, &protected_iovec, 1, var->unprotected_iovec,
nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
GPR_ASSERT(memcmp(var->data_buf, var->dup_buf, var->data_length) == 0);
ASSERT_EQ(status, GRPC_STATUS_OK);
ASSERT_EQ(memcmp(var->data_buf, var->dup_buf, var->data_length), 0);
alts_iovec_record_protocol_test_var_destroy(var);
}
@ -571,7 +576,7 @@ static void privacy_integrity_protect_input_check(
alts_iovec_record_protocol_privacy_integrity_protect(
rp, var->data_iovec, var->data_iovec_length, protected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Protected frame is nullptr."));
gpr_free(error_message);
@ -581,7 +586,7 @@ static void privacy_integrity_protect_input_check(
status = alts_iovec_record_protocol_privacy_integrity_protect(
rp, var->data_iovec, var->data_iovec_length, protected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Protected frame size is incorrect."));
gpr_free(error_message);
@ -602,7 +607,7 @@ static void privacy_integrity_unprotect_input_check(
alts_iovec_record_protocol_privacy_integrity_unprotect(
rp, header_iovec, &protected_iovec, 1, var->unprotected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Header is nullptr."));
gpr_free(error_message);
@ -612,7 +617,7 @@ static void privacy_integrity_unprotect_input_check(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
rp, header_iovec, &protected_iovec, 1, var->unprotected_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Header length is incorrect."));
gpr_free(error_message);
@ -621,7 +626,7 @@ static void privacy_integrity_unprotect_input_check(
iovec_t unprotected_iovec = {var->data_buf, var->data_length - 1};
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
rp, header_iovec, &protected_iovec, 1, unprotected_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INVALID_ARGUMENT, error_message,
"Unprotected data size is incorrect."));
gpr_free(error_message);
@ -643,7 +648,7 @@ static void record_protocol_wrong_mode(
status = alts_iovec_record_protocol_integrity_only_protect(
privacy_integrity_protect_rp, var->data_iovec, var->data_iovec_length,
var->header_iovec, var->tag_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Integrity-only operations are not allowed for this object."));
gpr_free(error_message);
@ -651,7 +656,7 @@ static void record_protocol_wrong_mode(
status = alts_iovec_record_protocol_integrity_only_unprotect(
privacy_integrity_unprotect_rp, var->data_iovec, var->data_iovec_length,
var->header_iovec, var->tag_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Integrity-only operations are not allowed for this object."));
gpr_free(error_message);
@ -659,7 +664,7 @@ static void record_protocol_wrong_mode(
status = alts_iovec_record_protocol_privacy_integrity_protect(
integrity_only_protect_rp, var->data_iovec, var->data_iovec_length,
var->protected_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Privacy-integrity operations are not allowed for this object."));
gpr_free(error_message);
@ -667,7 +672,7 @@ static void record_protocol_wrong_mode(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
integrity_only_unprotect_rp, var->header_iovec, var->data_iovec,
var->data_iovec_length, var->unprotected_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_FAILED_PRECONDITION, error_message,
"Privacy-integrity operations are not allowed for this object."));
gpr_free(error_message);
@ -685,7 +690,7 @@ static void integrity_seal_privacy_unseal(
status = alts_iovec_record_protocol_integrity_only_protect(
integrity_only_sender, var->data_iovec, var->data_iovec_length,
var->header_iovec, var->tag_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Unseal with privacy-integrity unprotect. */
memcpy(var->protected_buf, var->data_buf, var->data_length);
memcpy(var->protected_buf + var->data_length, var->tag_buf, var->tag_length);
@ -694,7 +699,7 @@ static void integrity_seal_privacy_unseal(
status = alts_iovec_record_protocol_privacy_integrity_unprotect(
privacy_integrity_receiver, var->header_iovec, &protected_iovec, 1,
var->unprotected_iovec, &error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message, "Frame decryption failed."));
gpr_free(error_message);
alts_iovec_record_protocol_test_var_destroy(var);
@ -711,7 +716,7 @@ static void privacy_seal_integrity_unseal(
status = alts_iovec_record_protocol_privacy_integrity_protect(
privacy_integrity_sender, var->data_iovec, var->data_iovec_length,
var->protected_iovec, nullptr);
GPR_ASSERT(status == GRPC_STATUS_OK);
ASSERT_EQ(status, GRPC_STATUS_OK);
/* Unseal with integrity-only unprotect. */
iovec_t header_iovec = {var->protected_buf, var->header_length};
iovec_t data_iovec = {var->protected_buf + var->header_length,
@ -722,7 +727,7 @@ static void privacy_seal_integrity_unseal(
status = alts_iovec_record_protocol_integrity_only_unprotect(
integrity_only_receiver, &data_iovec, 1, header_iovec, tag_iovec,
&error_message);
GPR_ASSERT(gsec_test_expect_compare_code_and_substr(
ASSERT_TRUE(gsec_test_expect_compare_code_and_substr(
status, GRPC_STATUS_INTERNAL, error_message,
"Frame tag verification failed."));
gpr_free(error_message);
@ -731,7 +736,8 @@ static void privacy_seal_integrity_unseal(
/* --- Test cases. --- */
static void alts_iovec_record_protocol_random_seal_unseal_tests() {
TEST(AltsIovecRecordProtocolTest,
AltsIovecRecordProtocolRandomSealUnsealTests) {
alts_iovec_record_protocol_test_fixture* fixture =
alts_iovec_record_protocol_test_fixture_create(
/*rekey=*/false, /*integrity_only=*/true);
@ -766,7 +772,7 @@ static void alts_iovec_record_protocol_random_seal_unseal_tests() {
alts_iovec_record_protocol_test_fixture_destroy(fixture);
}
static void alts_iovec_record_protocol_empty_seal_unseal_tests() {
TEST(AltsIovecRecordProtocolTest, AltsIovecRecordProtocolEmptySealUnsealTests) {
alts_iovec_record_protocol_test_fixture* fixture =
alts_iovec_record_protocol_test_fixture_create(
/*rekey=*/false, /*integrity_only=*/true);
@ -801,7 +807,8 @@ static void alts_iovec_record_protocol_empty_seal_unseal_tests() {
alts_iovec_record_protocol_test_fixture_destroy(fixture);
}
static void alts_iovec_record_protocol_unsync_seal_unseal_tests() {
TEST(AltsIovecRecordProtocolTest,
AltsIovecRecordProtocolUnsyncSealUnsealTests) {
alts_iovec_record_protocol_test_fixture* fixture =
alts_iovec_record_protocol_test_fixture_create(
/*rekey=*/false, /*integrity_only=*/true);
@ -836,7 +843,7 @@ static void alts_iovec_record_protocol_unsync_seal_unseal_tests() {
alts_iovec_record_protocol_test_fixture_destroy(fixture);
}
static void alts_iovec_record_protocol_corrupted_data_tests() {
TEST(AltsIovecRecordProtocolTest, AltsIovecRecordProtocolCorruptedDataTests) {
alts_iovec_record_protocol_test_fixture* fixture =
alts_iovec_record_protocol_test_fixture_create(
/*rekey=*/false, /*integrity_only=*/true);
@ -871,7 +878,7 @@ static void alts_iovec_record_protocol_corrupted_data_tests() {
alts_iovec_record_protocol_test_fixture_destroy(fixture);
}
static void alts_iovec_record_protocol_input_check_tests() {
TEST(AltsIovecRecordProtocolTest, AltsIovecRecordProtocolInputCheckTests) {
alts_iovec_record_protocol_test_fixture* fixture =
alts_iovec_record_protocol_test_fixture_create(
/*rekey=*/false, /*integrity_only=*/true);
@ -898,7 +905,7 @@ static void alts_iovec_record_protocol_input_check_tests() {
alts_iovec_record_protocol_test_fixture_destroy(fixture);
}
static void alts_iovec_record_protocol_mix_operations_tests() {
TEST(AltsIovecRecordProtocolTest, AltsIovecRecordProtocolMixOperationsTests) {
alts_iovec_record_protocol_test_fixture* fixture_1 =
alts_iovec_record_protocol_test_fixture_create(
/*rekey=*/false, /*integrity_only=*/true);
@ -918,12 +925,7 @@ static void alts_iovec_record_protocol_mix_operations_tests() {
alts_iovec_record_protocol_test_fixture_destroy(fixture_2);
}
int main(int /*argc*/, char** /*argv*/) {
alts_iovec_record_protocol_random_seal_unseal_tests();
alts_iovec_record_protocol_empty_seal_unseal_tests();
alts_iovec_record_protocol_unsync_seal_unseal_tests();
alts_iovec_record_protocol_corrupted_data_tests();
alts_iovec_record_protocol_input_check_tests();
alts_iovec_record_protocol_mix_operations_tests();
return 0;
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -18,6 +18,8 @@
#include "src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.h"
#include <gtest/gtest.h>
#include <grpc/slice_buffer.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -59,9 +61,9 @@ struct alts_zero_copy_grpc_protector_test_var {
static void create_random_slice_buffer(grpc_slice_buffer* sb,
grpc_slice_buffer* dup_sb,
size_t length) {
GPR_ASSERT(sb != nullptr);
GPR_ASSERT(dup_sb != nullptr);
GPR_ASSERT(length > 0);
ASSERT_NE(sb, nullptr);
ASSERT_NE(dup_sb, nullptr);
ASSERT_GT(length, 0);
grpc_slice slice = GRPC_SLICE_MALLOC(length);
gsec_test_random_bytes(GRPC_SLICE_START_PTR(slice), length);
grpc_slice_buffer_add(sb, grpc_slice_ref(slice));
@ -69,8 +71,8 @@ static void create_random_slice_buffer(grpc_slice_buffer* sb,
}
static uint8_t* pointer_to_nth_byte(grpc_slice_buffer* sb, size_t index) {
GPR_ASSERT(sb != nullptr);
GPR_ASSERT(index < sb->length);
EXPECT_NE(sb, nullptr);
EXPECT_LT(index, sb->length);
for (size_t i = 0; i < sb->count; i++) {
if (index < GRPC_SLICE_LENGTH(sb->slices[i])) {
return GRPC_SLICE_START_PTR(sb->slices[i]) + index;
@ -85,15 +87,15 @@ static uint8_t* pointer_to_nth_byte(grpc_slice_buffer* sb, size_t index) {
* but OK for testing. */
static bool are_slice_buffers_equal(grpc_slice_buffer* first,
grpc_slice_buffer* second) {
GPR_ASSERT(first != nullptr);
GPR_ASSERT(second != nullptr);
EXPECT_NE(first, nullptr);
EXPECT_NE(second, nullptr);
if (first->length != second->length) {
return false;
}
for (size_t i = 0; i < first->length; i++) {
uint8_t* first_ptr = pointer_to_nth_byte(first, i);
uint8_t* second_ptr = pointer_to_nth_byte(second, i);
GPR_ASSERT(first_ptr != nullptr && second_ptr != nullptr);
EXPECT_TRUE(first_ptr != nullptr && second_ptr != nullptr);
if ((*first_ptr) != (*second_ptr)) {
return false;
}
@ -114,20 +116,22 @@ alts_zero_copy_grpc_protector_test_fixture_create(bool rekey,
size_t max_protected_frame_size = 1024;
size_t actual_max_protected_frame_size;
gsec_test_random_array(&key, key_length);
GPR_ASSERT(alts_zero_copy_grpc_protector_create(
key, key_length, rekey, /*is_client=*/true, integrity_only,
enable_extra_copy, &max_protected_frame_size,
&fixture->client) == TSI_OK);
GPR_ASSERT(tsi_zero_copy_grpc_protector_max_frame_size(
fixture->client, &actual_max_protected_frame_size) == TSI_OK);
GPR_ASSERT(actual_max_protected_frame_size == max_protected_frame_size);
GPR_ASSERT(alts_zero_copy_grpc_protector_create(
key, key_length, rekey, /*is_client=*/false, integrity_only,
enable_extra_copy, &max_protected_frame_size,
&fixture->server) == TSI_OK);
GPR_ASSERT(tsi_zero_copy_grpc_protector_max_frame_size(
fixture->server, &actual_max_protected_frame_size) == TSI_OK);
GPR_ASSERT(actual_max_protected_frame_size == max_protected_frame_size);
EXPECT_EQ(alts_zero_copy_grpc_protector_create(
key, key_length, rekey, /*is_client=*/true, integrity_only,
enable_extra_copy, &max_protected_frame_size, &fixture->client),
TSI_OK);
EXPECT_EQ(tsi_zero_copy_grpc_protector_max_frame_size(
fixture->client, &actual_max_protected_frame_size),
TSI_OK);
EXPECT_EQ(actual_max_protected_frame_size, max_protected_frame_size);
EXPECT_EQ(alts_zero_copy_grpc_protector_create(
key, key_length, rekey, /*is_client=*/false, integrity_only,
enable_extra_copy, &max_protected_frame_size, &fixture->server),
TSI_OK);
EXPECT_EQ(tsi_zero_copy_grpc_protector_max_frame_size(
fixture->server, &actual_max_protected_frame_size),
TSI_OK);
EXPECT_EQ(actual_max_protected_frame_size, max_protected_frame_size);
gpr_free(key);
grpc_core::ExecCtx::Get()->Flush();
return fixture;
@ -183,8 +187,9 @@ static void seal_unseal_small_buffer(tsi_zero_copy_grpc_protector* sender,
/* Creates a random small slice buffer and calls protect(). */
create_random_slice_buffer(&var->original_sb, &var->duplicate_sb,
kSmallBufferSize);
GPR_ASSERT(tsi_zero_copy_grpc_protector_protect(
sender, &var->original_sb, &var->protected_sb) == TSI_OK);
ASSERT_EQ(tsi_zero_copy_grpc_protector_protect(sender, &var->original_sb,
&var->protected_sb),
TSI_OK);
/* Splits protected slice buffer into two: first one is staging_sb, and
* second one is protected_sb. */
uint32_t staging_sb_size =
@ -194,22 +199,23 @@ static void seal_unseal_small_buffer(tsi_zero_copy_grpc_protector* sender,
grpc_slice_buffer_move_first(&var->protected_sb, staging_sb_size,
&var->staging_sb);
/* Unprotects one by one. */
GPR_ASSERT(tsi_zero_copy_grpc_protector_unprotect(
receiver, &var->staging_sb, &var->unprotected_sb,
&min_progress_size) == TSI_OK);
ASSERT_EQ(tsi_zero_copy_grpc_protector_unprotect(receiver, &var->staging_sb,
&var->unprotected_sb,
&min_progress_size),
TSI_OK);
if (staging_sb_size >= kZeroCopyFrameLengthFieldSize) {
GPR_ASSERT(min_progress_size ==
static_cast<int>(var->protected_sb.length));
ASSERT_EQ(min_progress_size, static_cast<int>(var->protected_sb.length));
} else {
GPR_ASSERT(min_progress_size == 1);
ASSERT_EQ(min_progress_size, 1);
}
GPR_ASSERT(var->unprotected_sb.length == 0);
GPR_ASSERT(tsi_zero_copy_grpc_protector_unprotect(
receiver, &var->protected_sb, &var->unprotected_sb,
&min_progress_size) == TSI_OK);
GPR_ASSERT(
ASSERT_EQ(var->unprotected_sb.length, 0);
ASSERT_EQ(tsi_zero_copy_grpc_protector_unprotect(
receiver, &var->protected_sb, &var->unprotected_sb,
&min_progress_size),
TSI_OK);
ASSERT_TRUE(
are_slice_buffers_equal(&var->unprotected_sb, &var->duplicate_sb));
GPR_ASSERT(min_progress_size == 1);
ASSERT_EQ(min_progress_size, 1);
alts_zero_copy_grpc_protector_test_var_destroy(var);
}
grpc_core::ExecCtx::Get()->Flush();
@ -224,8 +230,9 @@ static void seal_unseal_large_buffer(tsi_zero_copy_grpc_protector* sender,
/* Creates a random large slice buffer and calls protect(). */
create_random_slice_buffer(&var->original_sb, &var->duplicate_sb,
kLargeBufferSize);
GPR_ASSERT(tsi_zero_copy_grpc_protector_protect(
sender, &var->original_sb, &var->protected_sb) == TSI_OK);
ASSERT_EQ(tsi_zero_copy_grpc_protector_protect(sender, &var->original_sb,
&var->protected_sb),
TSI_OK);
/* Splits protected slice buffer into multiple pieces. Receiver unprotects
* each slice buffer one by one. */
uint32_t channel_size = gsec_test_bias_random_uint32(static_cast<uint32_t>(
@ -235,14 +242,14 @@ static void seal_unseal_large_buffer(tsi_zero_copy_grpc_protector* sender,
grpc_slice_buffer_reset_and_unref_internal(&var->staging_sb);
grpc_slice_buffer_move_first(&var->protected_sb, channel_size,
&var->staging_sb);
GPR_ASSERT(tsi_zero_copy_grpc_protector_unprotect(
receiver, &var->staging_sb, &var->unprotected_sb,
nullptr) == TSI_OK);
ASSERT_EQ(tsi_zero_copy_grpc_protector_unprotect(
receiver, &var->staging_sb, &var->unprotected_sb, nullptr),
TSI_OK);
}
GPR_ASSERT(tsi_zero_copy_grpc_protector_unprotect(
receiver, &var->protected_sb, &var->unprotected_sb,
nullptr) == TSI_OK);
GPR_ASSERT(
ASSERT_EQ(tsi_zero_copy_grpc_protector_unprotect(
receiver, &var->protected_sb, &var->unprotected_sb, nullptr),
TSI_OK);
ASSERT_TRUE(
are_slice_buffers_equal(&var->unprotected_sb, &var->duplicate_sb));
alts_zero_copy_grpc_protector_test_var_destroy(var);
}
@ -307,8 +314,7 @@ static void alts_zero_copy_protector_seal_unseal_large_buffer_tests(
alts_zero_copy_grpc_protector_test_fixture_destroy(fixture);
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
TEST(AltsZeroCopyGrpcProtectorTest, MainTest) {
grpc_init();
alts_zero_copy_protector_seal_unseal_small_buffer_tests(
/*enable_extra_copy=*/false);
@ -319,5 +325,11 @@ int main(int argc, char** argv) {
alts_zero_copy_protector_seal_unseal_large_buffer_tests(
/*enable_extra_copy=*/true);
grpc_shutdown();
return 0;
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
return RUN_ALL_TESTS();
}

@ -22,6 +22,8 @@
#include <stdio.h>
#include <string.h>
#include <gtest/gtest.h>
#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
@ -43,19 +45,21 @@ static void fake_test_setup_handshakers(tsi_test_fixture* fixture) {
}
static void validate_handshaker_peers(tsi_handshaker_result* result) {
GPR_ASSERT(result != nullptr);
ASSERT_NE(result, nullptr);
tsi_peer peer;
GPR_ASSERT(tsi_handshaker_result_extract_peer(result, &peer) == TSI_OK);
ASSERT_EQ(tsi_handshaker_result_extract_peer(result, &peer), TSI_OK);
const tsi_peer_property* property =
tsi_peer_get_property_by_name(&peer, TSI_CERTIFICATE_TYPE_PEER_PROPERTY);
GPR_ASSERT(property != nullptr);
GPR_ASSERT(memcmp(property->value.data, TSI_FAKE_CERTIFICATE_TYPE,
property->value.length) == 0);
ASSERT_NE(property, nullptr);
ASSERT_EQ(memcmp(property->value.data, TSI_FAKE_CERTIFICATE_TYPE,
property->value.length),
0);
property =
tsi_peer_get_property_by_name(&peer, TSI_SECURITY_LEVEL_PEER_PROPERTY);
GPR_ASSERT(property != nullptr);
GPR_ASSERT(memcmp(property->value.data, TSI_FAKE_SECURITY_LEVEL,
property->value.length) == 0);
ASSERT_NE(property, nullptr);
ASSERT_EQ(memcmp(property->value.data, TSI_FAKE_SECURITY_LEVEL,
property->value.length),
0);
tsi_peer_destruct(&peer);
}
@ -78,27 +82,27 @@ static tsi_test_fixture* fake_tsi_test_fixture_create() {
return &fake_fixture->base;
}
void fake_tsi_test_do_handshake_tiny_handshake_buffer() {
TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshakeTinyHandshakeBuffer) {
tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
fixture->handshake_buffer_size = TSI_TEST_TINY_HANDSHAKE_BUFFER_SIZE;
tsi_test_do_handshake(fixture);
tsi_test_fixture_destroy(fixture);
}
void fake_tsi_test_do_handshake_small_handshake_buffer() {
TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshakeSmallHandshakeBuffer) {
tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
fixture->handshake_buffer_size = TSI_TEST_SMALL_HANDSHAKE_BUFFER_SIZE;
tsi_test_do_handshake(fixture);
tsi_test_fixture_destroy(fixture);
}
void fake_tsi_test_do_handshake() {
TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshake) {
tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
tsi_test_do_handshake(fixture);
tsi_test_fixture_destroy(fixture);
}
void fake_tsi_test_do_round_trip_for_all_configs() {
TEST(FakeTransportSecurityTest, FakeTsiTestDoRoundTripForAllConfigs) {
unsigned int* bit_array = static_cast<unsigned int*>(
gpr_zalloc(sizeof(unsigned int) * TSI_TEST_NUM_OF_ARGUMENTS));
const unsigned int mask = 1U << (TSI_TEST_NUM_OF_ARGUMENTS - 1);
@ -121,7 +125,7 @@ void fake_tsi_test_do_round_trip_for_all_configs() {
gpr_free(bit_array);
}
void fake_tsi_test_do_round_trip_odd_buffer_size() {
TEST(FakeTransportSecurityTest, FakeTsiTestDoRoundTripOddBufferSize) {
const size_t odd_sizes[] = {1025, 2051, 4103, 8207, 16409};
const size_t size = sizeof(odd_sizes) / sizeof(size_t);
for (size_t ind1 = 0; ind1 < size; ind1++) {
@ -146,12 +150,7 @@ void fake_tsi_test_do_round_trip_odd_buffer_size() {
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
grpc_init();
fake_tsi_test_do_handshake_tiny_handshake_buffer();
fake_tsi_test_do_handshake_small_handshake_buffer();
fake_tsi_test_do_handshake();
fake_tsi_test_do_round_trip_for_all_configs();
fake_tsi_test_do_round_trip_odd_buffer_size();
grpc_shutdown();
return 0;
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
return RUN_ALL_TESTS();
}

@ -22,6 +22,7 @@
#include <stdio.h>
#include <string.h>
#include <gtest/gtest.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/pem.h>
@ -108,9 +109,9 @@ typedef struct ssl_tsi_test_fixture {
static void ssl_test_setup_handshakers(tsi_test_fixture* fixture) {
ssl_tsi_test_fixture* ssl_fixture =
reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
GPR_ASSERT(ssl_fixture != nullptr);
GPR_ASSERT(ssl_fixture->key_cert_lib != nullptr);
GPR_ASSERT(ssl_fixture->alpn_lib != nullptr);
ASSERT_NE(ssl_fixture, nullptr);
ASSERT_NE(ssl_fixture->key_cert_lib, nullptr);
ASSERT_NE(ssl_fixture->alpn_lib, nullptr);
ssl_key_cert_lib* key_cert_lib = ssl_fixture->key_cert_lib;
ssl_alpn_lib* alpn_lib = ssl_fixture->alpn_lib;
/* Create client handshaker factory. */
@ -135,9 +136,9 @@ static void ssl_test_setup_handshakers(tsi_test_fixture* fixture) {
}
client_options.min_tls_version = test_tls_version;
client_options.max_tls_version = test_tls_version;
GPR_ASSERT(tsi_create_ssl_client_handshaker_factory_with_options(
&client_options, &ssl_fixture->client_handshaker_factory) ==
TSI_OK);
ASSERT_EQ(tsi_create_ssl_client_handshaker_factory_with_options(
&client_options, &ssl_fixture->client_handshaker_factory),
TSI_OK);
/* Create server handshaker factory. */
tsi_ssl_server_handshaker_options server_options;
if (alpn_lib->alpn_mode == ALPN_SERVER_NO_CLIENT ||
@ -169,59 +170,64 @@ static void ssl_test_setup_handshakers(tsi_test_fixture* fixture) {
server_options.session_ticket_key_size = ssl_fixture->session_ticket_key_size;
server_options.min_tls_version = test_tls_version;
server_options.max_tls_version = test_tls_version;
GPR_ASSERT(tsi_create_ssl_server_handshaker_factory_with_options(
&server_options, &ssl_fixture->server_handshaker_factory) ==
TSI_OK);
ASSERT_EQ(tsi_create_ssl_server_handshaker_factory_with_options(
&server_options, &ssl_fixture->server_handshaker_factory),
TSI_OK);
/* Create server and client handshakers. */
GPR_ASSERT(tsi_ssl_client_handshaker_factory_create_handshaker(
ssl_fixture->client_handshaker_factory,
ssl_fixture->server_name_indication,
ssl_fixture->network_bio_buf_size,
ssl_fixture->ssl_bio_buf_size,
&ssl_fixture->base.client_handshaker) == TSI_OK);
GPR_ASSERT(tsi_ssl_server_handshaker_factory_create_handshaker(
ssl_fixture->server_handshaker_factory,
ssl_fixture->network_bio_buf_size,
ssl_fixture->ssl_bio_buf_size,
&ssl_fixture->base.server_handshaker) == TSI_OK);
ASSERT_EQ(
tsi_ssl_client_handshaker_factory_create_handshaker(
ssl_fixture->client_handshaker_factory,
ssl_fixture->server_name_indication,
ssl_fixture->network_bio_buf_size, ssl_fixture->ssl_bio_buf_size,
&ssl_fixture->base.client_handshaker),
TSI_OK);
ASSERT_EQ(
tsi_ssl_server_handshaker_factory_create_handshaker(
ssl_fixture->server_handshaker_factory,
ssl_fixture->network_bio_buf_size, ssl_fixture->ssl_bio_buf_size,
&ssl_fixture->base.server_handshaker),
TSI_OK);
}
static void check_alpn(ssl_tsi_test_fixture* ssl_fixture,
const tsi_peer* peer) {
GPR_ASSERT(ssl_fixture != nullptr);
GPR_ASSERT(ssl_fixture->alpn_lib != nullptr);
ASSERT_NE(ssl_fixture, nullptr);
ASSERT_NE(ssl_fixture->alpn_lib, nullptr);
ssl_alpn_lib* alpn_lib = ssl_fixture->alpn_lib;
const tsi_peer_property* alpn_property =
tsi_peer_get_property_by_name(peer, TSI_SSL_ALPN_SELECTED_PROTOCOL);
if (alpn_lib->alpn_mode != ALPN_CLIENT_SERVER_OK) {
GPR_ASSERT(alpn_property == nullptr);
ASSERT_EQ(alpn_property, nullptr);
} else {
GPR_ASSERT(alpn_property != nullptr);
ASSERT_NE(alpn_property, nullptr);
const char* expected_match = "baz";
GPR_ASSERT(memcmp(alpn_property->value.data, expected_match,
alpn_property->value.length) == 0);
ASSERT_EQ(memcmp(alpn_property->value.data, expected_match,
alpn_property->value.length),
0);
}
}
static void check_security_level(const tsi_peer* peer) {
const tsi_peer_property* security_level =
tsi_peer_get_property_by_name(peer, TSI_SECURITY_LEVEL_PEER_PROPERTY);
GPR_ASSERT(security_level != nullptr);
ASSERT_NE(security_level, nullptr);
const char* expected_match = "TSI_PRIVACY_AND_INTEGRITY";
GPR_ASSERT(memcmp(security_level->value.data, expected_match,
security_level->value.length) == 0);
ASSERT_EQ(memcmp(security_level->value.data, expected_match,
security_level->value.length),
0);
}
static const tsi_peer_property*
check_basic_authenticated_peer_and_get_common_name(const tsi_peer* peer) {
const tsi_peer_property* cert_type_property =
tsi_peer_get_property_by_name(peer, TSI_CERTIFICATE_TYPE_PEER_PROPERTY);
GPR_ASSERT(cert_type_property != nullptr);
GPR_ASSERT(memcmp(cert_type_property->value.data, TSI_X509_CERTIFICATE_TYPE,
cert_type_property->value.length) == 0);
EXPECT_NE(cert_type_property, nullptr);
EXPECT_EQ(memcmp(cert_type_property->value.data, TSI_X509_CERTIFICATE_TYPE,
cert_type_property->value.length),
0);
const tsi_peer_property* property = tsi_peer_get_property_by_name(
peer, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY);
GPR_ASSERT(property != nullptr);
EXPECT_NE(property, nullptr);
return property;
}
@ -229,13 +235,15 @@ static void check_session_reusage(ssl_tsi_test_fixture* ssl_fixture,
tsi_peer* peer) {
const tsi_peer_property* session_reused =
tsi_peer_get_property_by_name(peer, TSI_SSL_SESSION_REUSED_PEER_PROPERTY);
GPR_ASSERT(session_reused != nullptr);
ASSERT_NE(session_reused, nullptr);
if (ssl_fixture->session_reused) {
GPR_ASSERT(strncmp(session_reused->value.data, "true",
session_reused->value.length) == 0);
ASSERT_EQ(strncmp(session_reused->value.data, "true",
session_reused->value.length),
0);
} else {
GPR_ASSERT(strncmp(session_reused->value.data, "false",
session_reused->value.length) == 0);
ASSERT_EQ(strncmp(session_reused->value.data, "false",
session_reused->value.length),
0);
}
}
@ -243,20 +251,19 @@ void check_server0_peer(tsi_peer* peer) {
const tsi_peer_property* property =
check_basic_authenticated_peer_and_get_common_name(peer);
const char* expected_match = "*.test.google.com.au";
GPR_ASSERT(memcmp(property->value.data, expected_match,
property->value.length) == 0);
GPR_ASSERT(tsi_peer_get_property_by_name(
peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) ==
nullptr);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "foo.test.google.com.au") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "bar.test.google.com.au") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "BAR.TEST.GOOGLE.COM.AU") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "Bar.Test.Google.Com.Au") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "bAr.TeST.gOOgle.cOm.AU") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "bar.test.google.blah") == 0);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "foo.bar.test.google.com.au") ==
0);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "test.google.com.au") == 0);
ASSERT_EQ(
memcmp(property->value.data, expected_match, property->value.length), 0);
ASSERT_EQ(tsi_peer_get_property_by_name(
peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY),
nullptr);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.test.google.com.au"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.test.google.com.au"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "BAR.TEST.GOOGLE.COM.AU"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "Bar.Test.Google.Com.Au"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bAr.TeST.gOOgle.cOm.AU"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.test.google.blah"), 0);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.bar.test.google.com.au"), 0);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "test.google.com.au"), 0);
tsi_peer_destruct(peer);
}
@ -278,39 +285,42 @@ void check_server1_peer(tsi_peer* peer) {
const tsi_peer_property* property =
check_basic_authenticated_peer_and_get_common_name(peer);
const char* expected_match = "*.test.google.com";
GPR_ASSERT(memcmp(property->value.data, expected_match,
property->value.length) == 0);
GPR_ASSERT(check_property(peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"*.test.google.fr") == 1);
GPR_ASSERT(check_property(peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"waterzooi.test.google.be") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "foo.test.google.fr") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "bar.test.google.fr") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "waterzooi.test.google.be") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "foo.test.youtube.com") == 1);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "bar.foo.test.google.com") == 0);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "test.google.fr") == 0);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "tartines.test.google.be") == 0);
GPR_ASSERT(tsi_ssl_peer_matches_name(peer, "tartines.youtube.com") == 0);
ASSERT_EQ(
memcmp(property->value.data, expected_match, property->value.length), 0);
ASSERT_EQ(
check_property(peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"*.test.google.fr"),
1);
ASSERT_EQ(
check_property(peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"waterzooi.test.google.be"),
1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.test.google.fr"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.test.google.fr"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "waterzooi.test.google.be"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "foo.test.youtube.com"), 1);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "bar.foo.test.google.com"), 0);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "test.google.fr"), 0);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "tartines.test.google.be"), 0);
ASSERT_EQ(tsi_ssl_peer_matches_name(peer, "tartines.youtube.com"), 0);
tsi_peer_destruct(peer);
}
static void check_client_peer(ssl_tsi_test_fixture* ssl_fixture,
tsi_peer* peer) {
GPR_ASSERT(ssl_fixture != nullptr);
GPR_ASSERT(ssl_fixture->alpn_lib != nullptr);
ASSERT_NE(ssl_fixture, nullptr);
ASSERT_NE(ssl_fixture->alpn_lib, nullptr);
ssl_alpn_lib* alpn_lib = ssl_fixture->alpn_lib;
if (!ssl_fixture->force_client_auth) {
GPR_ASSERT(peer->property_count ==
(alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_OK ? 3 : 2));
ASSERT_EQ(peer->property_count,
(alpn_lib->alpn_mode == ALPN_CLIENT_SERVER_OK ? 3 : 2));
} else {
const tsi_peer_property* property =
check_basic_authenticated_peer_and_get_common_name(peer);
const char* expected_match = "testclient";
GPR_ASSERT(memcmp(property->value.data, expected_match,
property->value.length) == 0);
ASSERT_EQ(
memcmp(property->value.data, expected_match, property->value.length),
0);
}
tsi_peer_destruct(peer);
}
@ -318,8 +328,8 @@ static void check_client_peer(ssl_tsi_test_fixture* ssl_fixture,
static void ssl_test_check_handshaker_peers(tsi_test_fixture* fixture) {
ssl_tsi_test_fixture* ssl_fixture =
reinterpret_cast<ssl_tsi_test_fixture*>(fixture);
GPR_ASSERT(ssl_fixture != nullptr);
GPR_ASSERT(ssl_fixture->key_cert_lib != nullptr);
ASSERT_NE(ssl_fixture, nullptr);
ASSERT_NE(ssl_fixture->key_cert_lib, nullptr);
ssl_key_cert_lib* key_cert_lib = ssl_fixture->key_cert_lib;
tsi_peer peer;
// In TLS 1.3, the client-side handshake succeeds even if the client sends a
@ -342,8 +352,9 @@ static void ssl_test_check_handshaker_peers(tsi_test_fixture* fixture) {
bool expect_client_success = expect_server_success;
#endif
if (expect_client_success) {
GPR_ASSERT(tsi_handshaker_result_extract_peer(
ssl_fixture->base.client_result, &peer) == TSI_OK);
ASSERT_EQ(tsi_handshaker_result_extract_peer(
ssl_fixture->base.client_result, &peer),
TSI_OK);
check_session_reusage(ssl_fixture, &peer);
check_alpn(ssl_fixture, &peer);
check_security_level(&peer);
@ -357,17 +368,18 @@ static void ssl_test_check_handshaker_peers(tsi_test_fixture* fixture) {
check_server1_peer(&peer);
}
} else {
GPR_ASSERT(ssl_fixture->base.client_result == nullptr);
ASSERT_EQ(ssl_fixture->base.client_result, nullptr);
}
if (expect_server_success) {
GPR_ASSERT(tsi_handshaker_result_extract_peer(
ssl_fixture->base.server_result, &peer) == TSI_OK);
ASSERT_EQ(tsi_handshaker_result_extract_peer(
ssl_fixture->base.server_result, &peer),
TSI_OK);
check_session_reusage(ssl_fixture, &peer);
check_alpn(ssl_fixture, &peer);
check_security_level(&peer);
check_client_peer(ssl_fixture, &peer);
} else {
GPR_ASSERT(ssl_fixture->base.server_result == nullptr);
ASSERT_EQ(ssl_fixture->base.server_result, nullptr);
}
}
@ -432,7 +444,7 @@ static char* load_file(const char* dir_path, const char* file_name) {
memcpy(file_path, dir_path, strlen(dir_path));
memcpy(file_path + strlen(dir_path), file_name, strlen(file_name));
grpc_slice slice;
GPR_ASSERT(grpc_load_file(file_path, 1, &slice) == GRPC_ERROR_NONE);
EXPECT_EQ(grpc_load_file(file_path, 1, &slice), GRPC_ERROR_NONE);
char* data = grpc_slice_to_c_string(slice);
grpc_slice_unref(slice);
gpr_free(file_path);
@ -484,7 +496,7 @@ static tsi_test_fixture* ssl_tsi_test_fixture_create() {
key_cert_lib->root_cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "ca.pem");
key_cert_lib->root_store =
tsi_ssl_root_certs_store_create(key_cert_lib->root_cert);
GPR_ASSERT(key_cert_lib->root_store != nullptr);
EXPECT_NE(key_cert_lib->root_store, nullptr);
ssl_fixture->key_cert_lib = key_cert_lib;
/* Create ssl_alpn_lib. */
ssl_alpn_lib* alpn_lib = grpc_core::Zalloc<ssl_alpn_lib>();
@ -699,8 +711,8 @@ void ssl_tsi_test_do_round_trip_with_error_on_stack() {
gpr_log(GPR_INFO, "ssl_tsi_test_do_round_trip_with_error_on_stack");
// Invoke an SSL function that causes an error, and ensure the error
// makes it to the stack.
GPR_ASSERT(!EC_KEY_new_by_curve_name(NID_rsa));
GPR_ASSERT(ERR_peek_error() != 0);
ASSERT_FALSE(EC_KEY_new_by_curve_name(NID_rsa));
ASSERT_NE(ERR_peek_error(), 0);
tsi_test_fixture* fixture = ssl_tsi_test_fixture_create();
tsi_test_do_round_trip(fixture);
tsi_test_fixture_destroy(fixture);
@ -784,7 +796,7 @@ static bool handshaker_factory_destructor_called;
static void ssl_tsi_test_handshaker_factory_destructor(
tsi_ssl_handshaker_factory* factory) {
GPR_ASSERT(factory != nullptr);
ASSERT_NE(factory, nullptr);
handshaker_factory_destructor_called = true;
if (original_vtable != nullptr && original_vtable->destroy != nullptr) {
original_vtable->destroy(factory);
@ -801,8 +813,9 @@ void test_tsi_ssl_client_handshaker_factory_refcounting() {
tsi_ssl_client_handshaker_options options;
options.pem_root_certs = cert_chain;
tsi_ssl_client_handshaker_factory* client_handshaker_factory;
GPR_ASSERT(tsi_create_ssl_client_handshaker_factory_with_options(
&options, &client_handshaker_factory) == TSI_OK);
ASSERT_EQ(tsi_create_ssl_client_handshaker_factory_with_options(
&options, &client_handshaker_factory),
TSI_OK);
handshaker_factory_destructor_called = false;
original_vtable = tsi_ssl_handshaker_factory_swap_vtable(
@ -812,22 +825,23 @@ void test_tsi_ssl_client_handshaker_factory_refcounting() {
tsi_handshaker* handshaker[3];
for (i = 0; i < 3; ++i) {
GPR_ASSERT(tsi_ssl_client_handshaker_factory_create_handshaker(
client_handshaker_factory, "google.com", 0, 0,
&handshaker[i]) == TSI_OK);
ASSERT_EQ(
tsi_ssl_client_handshaker_factory_create_handshaker(
client_handshaker_factory, "google.com", 0, 0, &handshaker[i]),
TSI_OK);
}
tsi_handshaker_destroy(handshaker[1]);
GPR_ASSERT(!handshaker_factory_destructor_called);
ASSERT_FALSE(handshaker_factory_destructor_called);
tsi_handshaker_destroy(handshaker[0]);
GPR_ASSERT(!handshaker_factory_destructor_called);
ASSERT_FALSE(handshaker_factory_destructor_called);
tsi_ssl_client_handshaker_factory_unref(client_handshaker_factory);
GPR_ASSERT(!handshaker_factory_destructor_called);
ASSERT_FALSE(handshaker_factory_destructor_called);
tsi_handshaker_destroy(handshaker[2]);
GPR_ASSERT(handshaker_factory_destructor_called);
ASSERT_TRUE(handshaker_factory_destructor_called);
gpr_free(cert_chain);
}
@ -848,8 +862,9 @@ void test_tsi_ssl_server_handshaker_factory_refcounting() {
options.num_key_cert_pairs = 1;
options.pem_client_root_certs = cert_chain;
GPR_ASSERT(tsi_create_ssl_server_handshaker_factory_with_options(
&options, &server_handshaker_factory) == TSI_OK);
ASSERT_EQ(tsi_create_ssl_server_handshaker_factory_with_options(
&options, &server_handshaker_factory),
TSI_OK);
handshaker_factory_destructor_called = false;
original_vtable = tsi_ssl_handshaker_factory_swap_vtable(
@ -857,21 +872,22 @@ void test_tsi_ssl_server_handshaker_factory_refcounting() {
&test_handshaker_factory_vtable);
for (i = 0; i < 3; ++i) {
GPR_ASSERT(tsi_ssl_server_handshaker_factory_create_handshaker(
server_handshaker_factory, 0, 0, &handshaker[i]) == TSI_OK);
ASSERT_EQ(tsi_ssl_server_handshaker_factory_create_handshaker(
server_handshaker_factory, 0, 0, &handshaker[i]),
TSI_OK);
}
tsi_handshaker_destroy(handshaker[1]);
GPR_ASSERT(!handshaker_factory_destructor_called);
ASSERT_FALSE(handshaker_factory_destructor_called);
tsi_handshaker_destroy(handshaker[0]);
GPR_ASSERT(!handshaker_factory_destructor_called);
ASSERT_FALSE(handshaker_factory_destructor_called);
tsi_ssl_server_handshaker_factory_unref(server_handshaker_factory);
GPR_ASSERT(!handshaker_factory_destructor_called);
ASSERT_FALSE(handshaker_factory_destructor_called);
tsi_handshaker_destroy(handshaker[2]);
GPR_ASSERT(handshaker_factory_destructor_called);
ASSERT_TRUE(handshaker_factory_destructor_called);
ssl_test_pem_key_cert_pair_destroy(cert_pair);
}
@ -884,8 +900,9 @@ void test_tsi_ssl_client_handshaker_factory_bad_params() {
tsi_ssl_client_handshaker_factory* client_handshaker_factory;
tsi_ssl_client_handshaker_options options;
options.pem_root_certs = cert_chain;
GPR_ASSERT(tsi_create_ssl_client_handshaker_factory_with_options(
&options, &client_handshaker_factory) == TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_create_ssl_client_handshaker_factory_with_options(
&options, &client_handshaker_factory),
TSI_INVALID_ARGUMENT);
tsi_ssl_client_handshaker_factory_unref(client_handshaker_factory);
}
@ -905,7 +922,7 @@ void ssl_tsi_test_duplicate_root_certificates() {
memcpy(dup_root_cert + strlen(root_cert), root_cert, strlen(root_cert));
tsi_ssl_root_certs_store* root_store =
tsi_ssl_root_certs_store_create(dup_root_cert);
GPR_ASSERT(root_store != nullptr);
ASSERT_NE(root_store, nullptr);
// Free memory.
tsi_ssl_root_certs_store_destroy(root_store);
gpr_free(root_cert);
@ -916,84 +933,101 @@ void ssl_tsi_test_extract_x509_subject_names() {
gpr_log(GPR_INFO, "ssl_tsi_test_extract_x509_subject_names");
char* cert = load_file(SSL_TSI_TEST_CREDENTIALS_DIR, "multi-domain.pem");
tsi_peer peer;
GPR_ASSERT(tsi_ssl_extract_x509_subject_names_from_pem_cert(cert, &peer) ==
TSI_OK);
ASSERT_EQ(tsi_ssl_extract_x509_subject_names_from_pem_cert(cert, &peer),
TSI_OK);
// tsi_peer should include one subject, one common name, one certificate, one
// security level, ten SAN fields, two DNS SAN fields, three URI fields, two
// email addresses and two IP addresses.
size_t expected_property_count = 22;
GPR_ASSERT(peer.property_count == expected_property_count);
ASSERT_EQ(peer.property_count, expected_property_count);
// Check subject
const char* expected_subject = "CN=xpigors,OU=Google,L=SF,ST=CA,C=US";
const tsi_peer_property* property =
tsi_peer_get_property_by_name(&peer, TSI_X509_SUBJECT_PEER_PROPERTY);
GPR_ASSERT(property != nullptr);
GPR_ASSERT(memcmp(property->value.data, expected_subject,
property->value.length) == 0);
ASSERT_NE(property, nullptr);
ASSERT_EQ(
memcmp(property->value.data, expected_subject, property->value.length),
0);
// Check common name
const char* expected_cn = "xpigors";
property = tsi_peer_get_property_by_name(
&peer, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY);
GPR_ASSERT(property != nullptr);
GPR_ASSERT(
memcmp(property->value.data, expected_cn, property->value.length) == 0);
ASSERT_NE(property, nullptr);
ASSERT_EQ(memcmp(property->value.data, expected_cn, property->value.length),
0);
// Check certificate data
property = tsi_peer_get_property_by_name(&peer, TSI_X509_PEM_CERT_PROPERTY);
GPR_ASSERT(property != nullptr);
GPR_ASSERT(memcmp(property->value.data, cert, property->value.length) == 0);
ASSERT_NE(property, nullptr);
ASSERT_EQ(memcmp(property->value.data, cert, property->value.length), 0);
// Check DNS
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"foo.test.domain.com") == 1);
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"bar.test.domain.com") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_DNS_PEER_PROPERTY,
"foo.test.domain.com") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_DNS_PEER_PROPERTY,
"bar.test.domain.com") == 1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"foo.test.domain.com"),
1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"bar.test.domain.com"),
1);
ASSERT_EQ(
check_property(&peer, TSI_X509_DNS_PEER_PROPERTY, "foo.test.domain.com"),
1);
ASSERT_EQ(
check_property(&peer, TSI_X509_DNS_PEER_PROPERTY, "bar.test.domain.com"),
1);
// Check URI
// Note that a valid SPIFFE certificate should only have one URI.
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"spiffe://foo.com/bar/baz") == 1);
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"https://foo.test.domain.com/test") == 1);
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"https://bar.test.domain.com/test") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
"spiffe://foo.com/bar/baz") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
"https://foo.test.domain.com/test") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
"https://bar.test.domain.com/test") == 1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"spiffe://foo.com/bar/baz"),
1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"https://foo.test.domain.com/test"),
1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"https://bar.test.domain.com/test"),
1);
ASSERT_EQ(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
"spiffe://foo.com/bar/baz"),
1);
ASSERT_EQ(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
"https://foo.test.domain.com/test"),
1);
ASSERT_EQ(check_property(&peer, TSI_X509_URI_PEER_PROPERTY,
"https://bar.test.domain.com/test"),
1);
// Check email address
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"foo@test.domain.com") == 1);
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"bar@test.domain.com") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_EMAIL_PEER_PROPERTY,
"foo@test.domain.com") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_EMAIL_PEER_PROPERTY,
"bar@test.domain.com") == 1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"foo@test.domain.com"),
1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"bar@test.domain.com"),
1);
ASSERT_EQ(check_property(&peer, TSI_X509_EMAIL_PEER_PROPERTY,
"foo@test.domain.com"),
1);
ASSERT_EQ(check_property(&peer, TSI_X509_EMAIL_PEER_PROPERTY,
"bar@test.domain.com"),
1);
// Check ip address
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"192.168.7.1") == 1);
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"13::17") == 1);
GPR_ASSERT(check_property(&peer, TSI_X509_IP_PEER_PROPERTY, "192.168.7.1") ==
1);
GPR_ASSERT(check_property(&peer, TSI_X509_IP_PEER_PROPERTY, "13::17") == 1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"192.168.7.1"),
1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"13::17"),
1);
ASSERT_EQ(check_property(&peer, TSI_X509_IP_PEER_PROPERTY, "192.168.7.1"), 1);
ASSERT_EQ(check_property(&peer, TSI_X509_IP_PEER_PROPERTY, "13::17"), 1);
// Check other fields
GPR_ASSERT(check_property(&peer,
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"other types of SAN") == 1);
ASSERT_EQ(
check_property(&peer, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
"other types of SAN"),
1);
// Free memory
gpr_free(cert);
tsi_peer_destruct(&peer);
@ -1008,16 +1042,16 @@ void ssl_tsi_test_extract_cert_chain() {
memcpy(chain, cert, strlen(cert));
memcpy(chain + strlen(cert), ca, strlen(ca));
STACK_OF(X509)* cert_chain = sk_X509_new_null();
GPR_ASSERT(cert_chain != nullptr);
ASSERT_NE(cert_chain, nullptr);
BIO* bio = BIO_new_mem_buf(chain, strlen(chain));
GPR_ASSERT(bio != nullptr);
ASSERT_NE(bio, nullptr);
STACK_OF(X509_INFO)* certInfos =
PEM_X509_INFO_read_bio(bio, nullptr, nullptr, nullptr);
GPR_ASSERT(certInfos != nullptr);
ASSERT_NE(certInfos, nullptr);
for (size_t i = 0; i < sk_X509_INFO_num(certInfos); i++) {
X509_INFO* certInfo = sk_X509_INFO_value(certInfos, i);
if (certInfo->x509 != nullptr) {
GPR_ASSERT(sk_X509_push(cert_chain, certInfo->x509) != 0);
ASSERT_NE(sk_X509_push(cert_chain, certInfo->x509), 0);
#if OPENSSL_VERSION_NUMBER >= 0x10100000
X509_up_ref(certInfo->x509);
#else
@ -1026,10 +1060,10 @@ void ssl_tsi_test_extract_cert_chain() {
}
}
tsi_peer_property chain_property;
GPR_ASSERT(tsi_ssl_get_cert_chain_contents(cert_chain, &chain_property) ==
TSI_OK);
GPR_ASSERT(memcmp(chain, chain_property.value.data,
chain_property.value.length) == 0);
ASSERT_EQ(tsi_ssl_get_cert_chain_contents(cert_chain, &chain_property),
TSI_OK);
ASSERT_EQ(
memcmp(chain, chain_property.value.data, chain_property.value.length), 0);
BIO_free(bio);
gpr_free(chain);
gpr_free(cert);
@ -1053,8 +1087,7 @@ void ssl_tsi_test_do_handshake_with_custom_bio_pair() {
tsi_test_fixture_destroy(fixture);
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
TEST(SslTransportSecurityTest, MainTest) {
grpc_init();
const size_t number_tls_versions = 2;
const tsi_tls_version tls_versions[] = {tsi_tls_version::TSI_TLS1_2,
@ -1092,5 +1125,11 @@ int main(int argc, char** argv) {
ssl_tsi_test_do_handshake_with_custom_bio_pair();
}
grpc_shutdown();
return 0;
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
grpc::testing::TestGrpcScope grpc_scope;
return RUN_ALL_TESTS();
}

@ -22,6 +22,7 @@
#include <string>
#include <gtest/gtest.h>
#include <openssl/crypto.h>
#include "absl/strings/str_format.h"
@ -265,18 +266,20 @@ static tsi_peer peer_from_cert_name_test_entry(
parsed_names dns_entries = parse_names(entry->dns_names);
parsed_names ip_entries = parse_names(entry->ip_names);
nl = dns_entries.names;
GPR_ASSERT(tsi_construct_peer(
1 + dns_entries.name_count + ip_entries.name_count, &peer) ==
TSI_OK);
GPR_ASSERT(tsi_construct_string_peer_property_from_cstring(
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, entry->common_name,
&peer.properties[0]) == TSI_OK);
EXPECT_EQ(tsi_construct_peer(
1 + dns_entries.name_count + ip_entries.name_count, &peer),
TSI_OK);
EXPECT_EQ(tsi_construct_string_peer_property_from_cstring(
TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, entry->common_name,
&peer.properties[0]),
TSI_OK);
i = 1;
while (nl != nullptr) {
char* processed = processed_name(nl->name);
GPR_ASSERT(tsi_construct_string_peer_property(
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed,
strlen(nl->name), &peer.properties[i++]) == TSI_OK);
EXPECT_EQ(tsi_construct_string_peer_property(
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed,
strlen(nl->name), &peer.properties[i++]),
TSI_OK);
nl = nl->next;
gpr_free(processed);
}
@ -284,9 +287,10 @@ static tsi_peer peer_from_cert_name_test_entry(
nl = ip_entries.names;
while (nl != nullptr) {
char* processed = processed_name(nl->name);
GPR_ASSERT(tsi_construct_string_peer_property(
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed,
strlen(nl->name), &peer.properties[i++]) == TSI_OK);
EXPECT_EQ(tsi_construct_string_peer_property(
TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, processed,
strlen(nl->name), &peer.properties[i++]),
TSI_OK);
nl = nl->next;
gpr_free(processed);
}
@ -304,7 +308,7 @@ std::string cert_name_test_entry_to_string(const cert_name_test_entry* entry) {
entry->ip_names != nullptr ? entry->ip_names : "");
}
static void test_peer_matches_name(void) {
TEST(TransportSecurityTest, TestPeerMatchesName) {
size_t i = 0;
for (i = 0; i < GPR_ARRAY_SIZE(cert_name_test_entries); i++) {
const cert_name_test_entry* entry = &cert_name_test_entries[i];
@ -312,7 +316,7 @@ static void test_peer_matches_name(void) {
int result = tsi_ssl_peer_matches_name(&peer, entry->host_name);
if (result != entry->expected) {
gpr_log(GPR_ERROR, "%s", cert_name_test_entry_to_string(entry).c_str());
GPR_ASSERT(0); /* Unexpected result. */
ASSERT_TRUE(0); /* Unexpected result. */
}
tsi_peer_destruct(&peer);
}
@ -323,7 +327,7 @@ typedef struct {
const char* str;
} tsi_result_string_pair;
static void test_result_strings(void) {
TEST(TransportSecurityTest, TestResultStrings) {
const tsi_result_string_pair results[] = {
{TSI_OK, "TSI_OK"},
{TSI_UNKNOWN_ERROR, "TSI_UNKNOWN_ERROR"},
@ -340,51 +344,50 @@ static void test_result_strings(void) {
{TSI_OUT_OF_RESOURCES, "TSI_OUT_OF_RESOURCES"}};
size_t i;
for (i = 0; i < GPR_ARRAY_SIZE(results); i++) {
GPR_ASSERT(strcmp(results[i].str, tsi_result_to_string(results[i].res)) ==
0);
ASSERT_STREQ(results[i].str, tsi_result_to_string(results[i].res));
}
GPR_ASSERT(strcmp("UNKNOWN", tsi_result_to_string((tsi_result)42)) == 0);
ASSERT_STREQ("UNKNOWN", tsi_result_to_string((tsi_result)42));
}
static void test_protector_invalid_args(void) {
GPR_ASSERT(tsi_frame_protector_protect(nullptr, nullptr, nullptr, nullptr,
nullptr) == TSI_INVALID_ARGUMENT);
GPR_ASSERT(tsi_frame_protector_protect_flush(
nullptr, nullptr, nullptr, nullptr) == TSI_INVALID_ARGUMENT);
GPR_ASSERT(tsi_frame_protector_unprotect(nullptr, nullptr, nullptr, nullptr,
nullptr) == TSI_INVALID_ARGUMENT);
TEST(TransportSecurityTest, TestProtectorInvalidArgs) {
ASSERT_EQ(
tsi_frame_protector_protect(nullptr, nullptr, nullptr, nullptr, nullptr),
TSI_INVALID_ARGUMENT);
ASSERT_EQ(
tsi_frame_protector_protect_flush(nullptr, nullptr, nullptr, nullptr),
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_frame_protector_unprotect(nullptr, nullptr, nullptr, nullptr,
nullptr),
TSI_INVALID_ARGUMENT);
}
static void test_handshaker_invalid_args(void) {
GPR_ASSERT(tsi_handshaker_get_result(nullptr) == TSI_INVALID_ARGUMENT);
GPR_ASSERT(tsi_handshaker_extract_peer(nullptr, nullptr) ==
TSI_INVALID_ARGUMENT);
GPR_ASSERT(tsi_handshaker_create_frame_protector(nullptr, nullptr, nullptr) ==
TSI_INVALID_ARGUMENT);
GPR_ASSERT(tsi_handshaker_process_bytes_from_peer(
nullptr, nullptr, nullptr) == TSI_INVALID_ARGUMENT);
GPR_ASSERT(tsi_handshaker_get_bytes_to_send_to_peer(
nullptr, nullptr, nullptr) == TSI_INVALID_ARGUMENT);
GPR_ASSERT(tsi_handshaker_next(nullptr, nullptr, 0, nullptr, nullptr, nullptr,
nullptr, nullptr) == TSI_INVALID_ARGUMENT);
TEST(TransportSecurityTest, TestHandshakerInvalidArgs) {
ASSERT_EQ(tsi_handshaker_get_result(nullptr), TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_extract_peer(nullptr, nullptr),
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_create_frame_protector(nullptr, nullptr, nullptr),
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_process_bytes_from_peer(nullptr, nullptr, nullptr),
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_get_bytes_to_send_to_peer(nullptr, nullptr, nullptr),
TSI_INVALID_ARGUMENT);
ASSERT_EQ(tsi_handshaker_next(nullptr, nullptr, 0, nullptr, nullptr, nullptr,
nullptr, nullptr),
TSI_INVALID_ARGUMENT);
}
static void test_handshaker_invalid_state(void) {
TEST(TransportSecurityTest, TestHandshakerInvalidState) {
tsi_handshaker* h = tsi_create_fake_handshaker(0);
tsi_peer peer;
tsi_frame_protector* p;
GPR_ASSERT(tsi_handshaker_extract_peer(h, &peer) == TSI_FAILED_PRECONDITION);
GPR_ASSERT(tsi_handshaker_create_frame_protector(h, nullptr, &p) ==
TSI_FAILED_PRECONDITION);
ASSERT_EQ(tsi_handshaker_extract_peer(h, &peer), TSI_FAILED_PRECONDITION);
ASSERT_EQ(tsi_handshaker_create_frame_protector(h, nullptr, &p),
TSI_FAILED_PRECONDITION);
tsi_handshaker_destroy(h);
}
int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv);
test_peer_matches_name();
test_result_strings();
test_protector_invalid_args();
test_handshaker_invalid_args();
test_handshaker_invalid_state();
return 0;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save