Debian Codegraph @

test/core/tsi/fake_transport_security_test.cc

raw
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/security/security_connector/security_connector.h"
32 #include "src/core/tsi/transport_security.h"
33 #include "test/core/tsi/transport_security_test_lib.h"
34 #include "test/core/util/test_config.h"
35
36 typedef struct fake_tsi_test_fixture {
37 tsi_test_fixture base;
38 } fake_tsi_test_fixture;
39
40 static void fake_test_setup_handshakers(tsi_test_fixture* fixture) {
41 fixture->client_handshaker =
42 tsi_create_fake_handshaker(true /* is_client. */);
43 fixture->server_handshaker =
44 tsi_create_fake_handshaker(false /* is_client. */);
45 }
46
47 static void validate_handshaker_peers(tsi_handshaker_result* result) {
48 ASSERT_NE(result, nullptr);
49 tsi_peer peer;
50 ASSERT_EQ(tsi_handshaker_result_extract_peer(result, &peer), TSI_OK);
51 const tsi_peer_property* property =
52 tsi_peer_get_property_by_name(&peer, TSI_CERTIFICATE_TYPE_PEER_PROPERTY);
53 ASSERT_NE(property, nullptr);
54 ASSERT_EQ(memcmp(property->value.data, TSI_FAKE_CERTIFICATE_TYPE,
55 property->value.length),
56 0);
57 property =
58 tsi_peer_get_property_by_name(&peer, TSI_SECURITY_LEVEL_PEER_PROPERTY);
59 ASSERT_NE(property, nullptr);
60 ASSERT_EQ(memcmp(property->value.data, TSI_FAKE_SECURITY_LEVEL,
61 property->value.length),
62 0);
63 tsi_peer_destruct(&peer);
64 }
65
66 static void fake_test_check_handshaker_peers(tsi_test_fixture* fixture) {
67 validate_handshaker_peers(fixture->client_result);
68 validate_handshaker_peers(fixture->server_result);
69 }
70
71 static void fake_test_destruct(tsi_test_fixture* fixture) { gpr_free(fixture); }
72
73 static const struct tsi_test_fixture_vtable vtable = {
74 fake_test_setup_handshakers, fake_test_check_handshaker_peers,
75 fake_test_destruct};
76
77 static tsi_test_fixture* fake_tsi_test_fixture_create() {
78 fake_tsi_test_fixture* fake_fixture =
79 static_cast<fake_tsi_test_fixture*>(gpr_zalloc(sizeof(*fake_fixture)));
80 tsi_test_fixture_init(&fake_fixture->base);
81 fake_fixture->base.vtable = &vtable;
82 return &fake_fixture->base;
83 }
84
85 TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshakeTinyHandshakeBuffer) {
86 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
87 fixture->handshake_buffer_size = TSI_TEST_TINY_HANDSHAKE_BUFFER_SIZE;
88 tsi_test_do_handshake(fixture);
89 tsi_test_fixture_destroy(fixture);
90 }
91
92 TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshakeSmallHandshakeBuffer) {
93 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
94 fixture->handshake_buffer_size = TSI_TEST_SMALL_HANDSHAKE_BUFFER_SIZE;
95 tsi_test_do_handshake(fixture);
96 tsi_test_fixture_destroy(fixture);
97 }
98
99 TEST(FakeTransportSecurityTest, FakeTsiTestDoHandshake) {
100 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
101 tsi_test_do_handshake(fixture);
102 tsi_test_fixture_destroy(fixture);
103 }
104
105 TEST(FakeTransportSecurityTest, FakeTsiTestDoRoundTripForAllConfigs) {
106 unsigned int* bit_array = static_cast<unsigned int*>(
107 gpr_zalloc(sizeof(unsigned int) * TSI_TEST_NUM_OF_ARGUMENTS));
108 const unsigned int mask = 1U << (TSI_TEST_NUM_OF_ARGUMENTS - 1);
109 for (unsigned int val = 0; val < TSI_TEST_NUM_OF_COMBINATIONS; val++) {
110 unsigned int v = val;
111 for (unsigned int ind = 0; ind < TSI_TEST_NUM_OF_ARGUMENTS; ind++) {
112 bit_array[ind] = (v & mask) ? 1 : 0;
113 v <<= 1;
114 }
115 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
116 fake_tsi_test_fixture* fake_fixture =
117 reinterpret_cast<fake_tsi_test_fixture*>(fixture);
118 tsi_test_frame_protector_config_destroy(fake_fixture->base.config);
119 fake_fixture->base.config = tsi_test_frame_protector_config_create(
120 bit_array[0], bit_array[1], bit_array[2], bit_array[3], bit_array[4],
121 bit_array[5], bit_array[6]);
122 tsi_test_do_round_trip(&fake_fixture->base);
123 tsi_test_fixture_destroy(fixture);
124 }
125 gpr_free(bit_array);
126 }
127
128 TEST(FakeTransportSecurityTest, FakeTsiTestDoRoundTripOddBufferSize) {
129 const size_t odd_sizes[] = {1025, 2051, 4103, 8207, 16409};
130 const size_t size = sizeof(odd_sizes) / sizeof(size_t);
131 for (size_t ind1 = 0; ind1 < size; ind1++) {
132 for (size_t ind2 = 0; ind2 < size; ind2++) {
133 for (size_t ind3 = 0; ind3 < size; ind3++) {
134 for (size_t ind4 = 0; ind4 < size; ind4++) {
135 for (size_t ind5 = 0; ind5 < size; ind5++) {
136 tsi_test_fixture* fixture = fake_tsi_test_fixture_create();
137 fake_tsi_test_fixture* fake_fixture =
138 reinterpret_cast<fake_tsi_test_fixture*>(fixture);
139 tsi_test_frame_protector_config_set_buffer_size(
140 fake_fixture->base.config, odd_sizes[ind1], odd_sizes[ind2],
141 odd_sizes[ind3], odd_sizes[ind4], odd_sizes[ind5]);
142 tsi_test_do_round_trip(&fake_fixture->base);
143 tsi_test_fixture_destroy(fixture);
144 }
145 }
146 }
147 }
148 }
149 }
150
151 int main(int argc, char** argv) {
152 grpc::testing::TestEnvironment env(&argc, argv);
153 ::testing::InitGoogleTest(&argc, argv);
154 grpc::testing::TestGrpcScope grpc_scope;
155 return RUN_ALL_TESTS();
156 }