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