1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/tsi/fake_transport_security.h"
20
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <gtest/gtest.h>
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30
31 #include "src/core/lib/gprpp/crash.h"
32 #include "src/core/lib/security/security_connector/security_connector.h"
33 #include "src/core/tsi/transport_security.h"
34 #include "test/core/tsi/transport_security_test_lib.h"
35 #include "test/core/util/test_config.h"
36
37 typedef struct fake_tsi_test_fixture {
38 tsi_test_fixture base;
39 } fake_tsi_test_fixture;
40
fake_test_setup_handshakers(tsi_test_fixture * fixture)41 static void fake_test_setup_handshakers(tsi_test_fixture* fixture) {
42 fixture->client_handshaker =
43 tsi_create_fake_handshaker(true /* is_client. */);
44 fixture->server_handshaker =
45 tsi_create_fake_handshaker(false /* is_client. */);
46 }
47
validate_handshaker_peers(tsi_handshaker_result * result)48 static void validate_handshaker_peers(tsi_handshaker_result* result) {
49 ASSERT_NE(result, nullptr);
50 tsi_peer peer;
51 ASSERT_EQ(tsi_handshaker_result_extract_peer(result, &peer), TSI_OK);
52 const tsi_peer_property* property =
53 tsi_peer_get_property_by_name(&peer, TSI_CERTIFICATE_TYPE_PEER_PROPERTY);
54 ASSERT_NE(property, nullptr);
55 ASSERT_EQ(memcmp(property->value.data, TSI_FAKE_CERTIFICATE_TYPE,
56 property->value.length),
57 0);
58 property =
59 tsi_peer_get_property_by_name(&peer, TSI_SECURITY_LEVEL_PEER_PROPERTY);
60 ASSERT_NE(property, nullptr);
61 ASSERT_EQ(memcmp(property->value.data, TSI_FAKE_SECURITY_LEVEL,
62 property->value.length),
63 0);
64 tsi_peer_destruct(&peer);
65 }
66
fake_test_check_handshaker_peers(tsi_test_fixture * fixture)67 static void fake_test_check_handshaker_peers(tsi_test_fixture* fixture) {
68 validate_handshaker_peers(fixture->client_result);
69 validate_handshaker_peers(fixture->server_result);
70 }
71
fake_test_destruct(tsi_test_fixture * fixture)72 static void fake_test_destruct(tsi_test_fixture* fixture) { gpr_free(fixture); }
73
74 static const struct tsi_test_fixture_vtable vtable = {
75 fake_test_setup_handshakers, fake_test_check_handshaker_peers,
76 fake_test_destruct};
77
fake_tsi_test_fixture_create()78 static tsi_test_fixture* fake_tsi_test_fixture_create() {
79 fake_tsi_test_fixture* fake_fixture =
80 static_cast<fake_tsi_test_fixture*>(gpr_zalloc(sizeof(*fake_fixture)));
81 tsi_test_fixture_init(&fake_fixture->base);
82 fake_fixture->base.vtable = &vtable;
83 return &fake_fixture->base;
84 }
85
TEST(FakeTransportSecurityTest,FakeTsiTestDoHandshakeTinyHandshakeBuffer)86 TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshakeTinyHandshakeBuffer) {
87 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
88 fixture->handshake_buffer_size = TSI_TEST_TINY_HANDSHAKE_BUFFER_SIZE;
89 tsi_test_do_handshake(fixture);
90 tsi_test_fixture_destroy(fixture);
91 }
92
TEST(FakeTransportSecurityTest,FakeTsiTestDoHandshakeSmallHandshakeBuffer)93 TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshakeSmallHandshakeBuffer) {
94 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
95 fixture->handshake_buffer_size = TSI_TEST_SMALL_HANDSHAKE_BUFFER_SIZE;
96 tsi_test_do_handshake(fixture);
97 tsi_test_fixture_destroy(fixture);
98 }
99
TEST(FakeTransportSecurityTest,FakeTsiTestDoHandshake)100 TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshake) {
101 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
102 tsi_test_do_handshake(fixture);
103 tsi_test_fixture_destroy(fixture);
104 }
105
TEST(FakeTransportSecurityTest,FakeTsiTestDoRoundTripForAllConfigs)106 TEST(FakeTransportSecurityTest, FakeTsiTestDoRoundTripForAllConfigs) {
107 unsigned int* bit_array = static_cast<unsigned int*>(
108 gpr_zalloc(sizeof(unsigned int) * TSI_TEST_NUM_OF_ARGUMENTS));
109 const unsigned int mask = 1U << (TSI_TEST_NUM_OF_ARGUMENTS - 1);
110 for (unsigned int val = 0; val < TSI_TEST_NUM_OF_COMBINATIONS; val++) {
111 unsigned int v = val;
112 for (unsigned int ind = 0; ind < TSI_TEST_NUM_OF_ARGUMENTS; ind++) {
113 bit_array[ind] = (v & mask) ? 1 : 0;
114 v <<= 1;
115 }
116 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
117 fake_tsi_test_fixture* fake_fixture =
118 reinterpret_cast<fake_tsi_test_fixture*>(fixture);
119 tsi_test_frame_protector_config_destroy(fake_fixture->base.config);
120 fake_fixture->base.config = tsi_test_frame_protector_config_create(
121 bit_array[0], bit_array[1], bit_array[2], bit_array[3], bit_array[4],
122 bit_array[5], bit_array[6]);
123 tsi_test_do_round_trip(&fake_fixture->base);
124 tsi_test_fixture_destroy(fixture);
125 }
126 gpr_free(bit_array);
127 }
128
TEST(FakeTransportSecurityTest,FakeTsiTestDoRoundTripOddBufferSize)129 TEST(FakeTransportSecurityTest, FakeTsiTestDoRoundTripOddBufferSize) {
130 const size_t odd_sizes[] = {1025, 2051, 4103, 8207, 16409};
131 const size_t size = sizeof(odd_sizes) / sizeof(size_t);
132 for (size_t ind1 = 0; ind1 < size; ind1++) {
133 for (size_t ind2 = 0; ind2 < size; ind2++) {
134 for (size_t ind3 = 0; ind3 < size; ind3++) {
135 for (size_t ind4 = 0; ind4 < size; ind4++) {
136 for (size_t ind5 = 0; ind5 < size; ind5++) {
137 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
138 fake_tsi_test_fixture* fake_fixture =
139 reinterpret_cast<fake_tsi_test_fixture*>(fixture);
140 tsi_test_frame_protector_config_set_buffer_size(
141 fake_fixture->base.config, odd_sizes[ind1], odd_sizes[ind2],
142 odd_sizes[ind3], odd_sizes[ind4], odd_sizes[ind5]);
143 tsi_test_do_round_trip(&fake_fixture->base);
144 tsi_test_fixture_destroy(fixture);
145 }
146 }
147 }
148 }
149 }
150 }
151
main(int argc,char ** argv)152 int main(int argc, char** argv) {
153 grpc::testing::TestEnvironment env(&argc, argv);
154 ::testing::InitGoogleTest(&argc, argv);
155 grpc::testing::TestGrpcScope grpc_scope;
156 return RUN_ALL_TESTS();
157 }
158