1 /*
2 *
3 * Copyright 2015 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 "test/core/iomgr/endpoint_tests.h"
20
21 #include <fcntl.h>
22 #include <sys/types.h>
23
24 #include <grpc/grpc.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include "src/core/lib/gpr/useful.h"
28 #include "src/core/lib/iomgr/endpoint_pair.h"
29 #include "src/core/lib/iomgr/iomgr.h"
30 #include "src/core/lib/security/transport/secure_endpoint.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "src/core/tsi/fake_transport_security.h"
33 #include "test/core/util/test_config.h"
34
35 static gpr_mu* g_mu;
36 static grpc_pollset* g_pollset;
37
secure_endpoint_create_fixture_tcp_socketpair(size_t slice_size,grpc_slice * leftover_slices,size_t leftover_nslices,bool use_zero_copy_protector)38 static grpc_endpoint_test_fixture secure_endpoint_create_fixture_tcp_socketpair(
39 size_t slice_size, grpc_slice* leftover_slices, size_t leftover_nslices,
40 bool use_zero_copy_protector) {
41 grpc_core::ExecCtx exec_ctx;
42 tsi_frame_protector* fake_read_protector =
43 tsi_create_fake_frame_protector(nullptr);
44 tsi_frame_protector* fake_write_protector =
45 tsi_create_fake_frame_protector(nullptr);
46 tsi_zero_copy_grpc_protector* fake_read_zero_copy_protector =
47 use_zero_copy_protector
48 ? tsi_create_fake_zero_copy_grpc_protector(nullptr)
49 : nullptr;
50 tsi_zero_copy_grpc_protector* fake_write_zero_copy_protector =
51 use_zero_copy_protector
52 ? tsi_create_fake_zero_copy_grpc_protector(nullptr)
53 : nullptr;
54 grpc_endpoint_test_fixture f;
55 grpc_endpoint_pair tcp;
56
57 grpc_arg a[1];
58 a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
59 a[0].type = GRPC_ARG_INTEGER;
60 a[0].value.integer = static_cast<int>(slice_size);
61 grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
62 tcp = grpc_iomgr_create_endpoint_pair("fixture", &args);
63 grpc_endpoint_add_to_pollset(tcp.client, g_pollset);
64 grpc_endpoint_add_to_pollset(tcp.server, g_pollset);
65
66 if (leftover_nslices == 0) {
67 f.client_ep = grpc_secure_endpoint_create(fake_read_protector,
68 fake_read_zero_copy_protector,
69 tcp.client, nullptr, 0);
70 } else {
71 unsigned i;
72 tsi_result result;
73 size_t still_pending_size;
74 size_t total_buffer_size = 8192;
75 size_t buffer_size = total_buffer_size;
76 uint8_t* encrypted_buffer = static_cast<uint8_t*>(gpr_malloc(buffer_size));
77 uint8_t* cur = encrypted_buffer;
78 grpc_slice encrypted_leftover;
79 for (i = 0; i < leftover_nslices; i++) {
80 grpc_slice plain = leftover_slices[i];
81 uint8_t* message_bytes = GRPC_SLICE_START_PTR(plain);
82 size_t message_size = GRPC_SLICE_LENGTH(plain);
83 while (message_size > 0) {
84 size_t protected_buffer_size_to_send = buffer_size;
85 size_t processed_message_size = message_size;
86 result = tsi_frame_protector_protect(
87 fake_write_protector, message_bytes, &processed_message_size, cur,
88 &protected_buffer_size_to_send);
89 GPR_ASSERT(result == TSI_OK);
90 message_bytes += processed_message_size;
91 message_size -= processed_message_size;
92 cur += protected_buffer_size_to_send;
93 GPR_ASSERT(buffer_size >= protected_buffer_size_to_send);
94 buffer_size -= protected_buffer_size_to_send;
95 }
96 grpc_slice_unref(plain);
97 }
98 do {
99 size_t protected_buffer_size_to_send = buffer_size;
100 result = tsi_frame_protector_protect_flush(fake_write_protector, cur,
101 &protected_buffer_size_to_send,
102 &still_pending_size);
103 GPR_ASSERT(result == TSI_OK);
104 cur += protected_buffer_size_to_send;
105 GPR_ASSERT(buffer_size >= protected_buffer_size_to_send);
106 buffer_size -= protected_buffer_size_to_send;
107 } while (still_pending_size > 0);
108 encrypted_leftover = grpc_slice_from_copied_buffer(
109 reinterpret_cast<const char*>(encrypted_buffer),
110 total_buffer_size - buffer_size);
111 f.client_ep = grpc_secure_endpoint_create(
112 fake_read_protector, fake_read_zero_copy_protector, tcp.client,
113 &encrypted_leftover, 1);
114 grpc_slice_unref(encrypted_leftover);
115 gpr_free(encrypted_buffer);
116 }
117
118 f.server_ep = grpc_secure_endpoint_create(fake_write_protector,
119 fake_write_zero_copy_protector,
120 tcp.server, nullptr, 0);
121
122 return f;
123 }
124
125 static grpc_endpoint_test_fixture
secure_endpoint_create_fixture_tcp_socketpair_noleftover(size_t slice_size)126 secure_endpoint_create_fixture_tcp_socketpair_noleftover(size_t slice_size) {
127 return secure_endpoint_create_fixture_tcp_socketpair(slice_size, nullptr, 0,
128 false);
129 }
130
131 static grpc_endpoint_test_fixture
secure_endpoint_create_fixture_tcp_socketpair_noleftover_zero_copy(size_t slice_size)132 secure_endpoint_create_fixture_tcp_socketpair_noleftover_zero_copy(
133 size_t slice_size) {
134 return secure_endpoint_create_fixture_tcp_socketpair(slice_size, nullptr, 0,
135 true);
136 }
137
138 static grpc_endpoint_test_fixture
secure_endpoint_create_fixture_tcp_socketpair_leftover(size_t slice_size)139 secure_endpoint_create_fixture_tcp_socketpair_leftover(size_t slice_size) {
140 grpc_slice s =
141 grpc_slice_from_copied_string("hello world 12345678900987654321");
142 return secure_endpoint_create_fixture_tcp_socketpair(slice_size, &s, 1,
143 false);
144 }
145
146 static grpc_endpoint_test_fixture
secure_endpoint_create_fixture_tcp_socketpair_leftover_zero_copy(size_t slice_size)147 secure_endpoint_create_fixture_tcp_socketpair_leftover_zero_copy(
148 size_t slice_size) {
149 grpc_slice s =
150 grpc_slice_from_copied_string("hello world 12345678900987654321");
151 return secure_endpoint_create_fixture_tcp_socketpair(slice_size, &s, 1, true);
152 }
153
clean_up(void)154 static void clean_up(void) {}
155
156 static grpc_endpoint_test_config configs[] = {
157 {"secure_ep/tcp_socketpair",
158 secure_endpoint_create_fixture_tcp_socketpair_noleftover, clean_up},
159 {"secure_ep/tcp_socketpair_zero_copy",
160 secure_endpoint_create_fixture_tcp_socketpair_noleftover_zero_copy,
161 clean_up},
162 {"secure_ep/tcp_socketpair_leftover",
163 secure_endpoint_create_fixture_tcp_socketpair_leftover, clean_up},
164 {"secure_ep/tcp_socketpair_leftover_zero_copy",
165 secure_endpoint_create_fixture_tcp_socketpair_leftover_zero_copy,
166 clean_up},
167 };
168
inc_call_ctr(void * arg,grpc_error * error)169 static void inc_call_ctr(void* arg, grpc_error* error) {
170 ++*static_cast<int*>(arg);
171 }
172
test_leftover(grpc_endpoint_test_config config,size_t slice_size)173 static void test_leftover(grpc_endpoint_test_config config, size_t slice_size) {
174 grpc_endpoint_test_fixture f = config.create_fixture(slice_size);
175 grpc_slice_buffer incoming;
176 grpc_slice s =
177 grpc_slice_from_copied_string("hello world 12345678900987654321");
178 grpc_core::ExecCtx exec_ctx;
179 int n = 0;
180 grpc_closure done_closure;
181 gpr_log(GPR_INFO, "Start test left over");
182
183 grpc_slice_buffer_init(&incoming);
184 GRPC_CLOSURE_INIT(&done_closure, inc_call_ctr, &n, grpc_schedule_on_exec_ctx);
185 grpc_endpoint_read(f.client_ep, &incoming, &done_closure);
186
187 grpc_core::ExecCtx::Get()->Flush();
188 GPR_ASSERT(n == 1);
189 GPR_ASSERT(incoming.count == 1);
190 GPR_ASSERT(grpc_slice_eq(s, incoming.slices[0]));
191
192 grpc_endpoint_shutdown(
193 f.client_ep, GRPC_ERROR_CREATE_FROM_STATIC_STRING("test_leftover end"));
194 grpc_endpoint_shutdown(
195 f.server_ep, GRPC_ERROR_CREATE_FROM_STATIC_STRING("test_leftover end"));
196 grpc_endpoint_destroy(f.client_ep);
197 grpc_endpoint_destroy(f.server_ep);
198
199 grpc_slice_unref_internal(s);
200 grpc_slice_buffer_destroy_internal(&incoming);
201
202 clean_up();
203 }
204
destroy_pollset(void * p,grpc_error * error)205 static void destroy_pollset(void* p, grpc_error* error) {
206 grpc_pollset_destroy(static_cast<grpc_pollset*>(p));
207 }
208
main(int argc,char ** argv)209 int main(int argc, char** argv) {
210 grpc_closure destroyed;
211 grpc_test_init(argc, argv);
212 grpc_init();
213
214 {
215 grpc_core::ExecCtx exec_ctx;
216 g_pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
217 grpc_pollset_init(g_pollset, &g_mu);
218 grpc_endpoint_tests(configs[0], g_pollset, g_mu);
219 grpc_endpoint_tests(configs[1], g_pollset, g_mu);
220 test_leftover(configs[2], 1);
221 test_leftover(configs[3], 1);
222 GRPC_CLOSURE_INIT(&destroyed, destroy_pollset, g_pollset,
223 grpc_schedule_on_exec_ctx);
224 grpc_pollset_shutdown(g_pollset, &destroyed);
225 }
226
227 grpc_shutdown();
228
229 gpr_free(g_pollset);
230
231 return 0;
232 }
233