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