Debian Codegraph @

test/core/tsi/transport_security_test_lib.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 "test/core/tsi/transport_security_test_lib.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gprpp/memory.h"
30 #include "src/core/lib/security/transport/tsi_error.h"
31
32 static void notification_signal(tsi_test_fixture* fixture) {
33 gpr_mu_lock(&fixture->mu);
34 fixture->notified = true;
35 gpr_cv_signal(&fixture->cv);
36 gpr_mu_unlock(&fixture->mu);
37 }
38
39 static void notification_wait(tsi_test_fixture* fixture) {
40 gpr_mu_lock(&fixture->mu);
41 while (!fixture->notified) {
42 gpr_cv_wait(&fixture->cv, &fixture->mu, gpr_inf_future(GPR_CLOCK_REALTIME));
43 }
44 fixture->notified = false;
45 gpr_mu_unlock(&fixture->mu);
46 }
47
48 typedef struct handshaker_args {
49 tsi_test_fixture* fixture;
50 unsigned char* handshake_buffer;
51 size_t handshake_buffer_size;
52 bool is_client;
53 bool transferred_data;
54 bool appended_unused_bytes;
55 grpc_error_handle error;
56 } handshaker_args;
57
58 static handshaker_args* handshaker_args_create(tsi_test_fixture* fixture,
59 bool is_client) {
60 GPR_ASSERT(fixture != nullptr);
61 GPR_ASSERT(fixture->config != nullptr);
62 handshaker_args* args = new handshaker_args();
63 args->fixture = fixture;
64 args->handshake_buffer_size = fixture->handshake_buffer_size;
65 args->handshake_buffer =
66 static_cast<unsigned char*>(gpr_zalloc(args->handshake_buffer_size));
67 args->is_client = is_client;
68 args->error = absl::OkStatus();
69 return args;
70 }
71
72 static void handshaker_args_destroy(handshaker_args* args) {
73 gpr_free(args->handshake_buffer);
74 delete args;
75 }
76
77 static void do_handshaker_next(handshaker_args* args);
78
79 static void setup_handshakers(tsi_test_fixture* fixture) {
80 GPR_ASSERT(fixture != nullptr);
81 GPR_ASSERT(fixture->vtable != nullptr);
82 GPR_ASSERT(fixture->vtable->setup_handshakers != nullptr);
83 fixture->vtable->setup_handshakers(fixture);
84 }
85
86 static void check_unused_bytes(tsi_test_fixture* fixture) {
87 tsi_handshaker_result* result_with_unused_bytes =
88 fixture->has_client_finished_first ? fixture->server_result
89 : fixture->client_result;
90 tsi_handshaker_result* result_without_unused_bytes =
91 fixture->has_client_finished_first ? fixture->client_result
92 : fixture->server_result;
93 const unsigned char* bytes = nullptr;
94 size_t bytes_size = 0;
95 GPR_ASSERT(tsi_handshaker_result_get_unused_bytes(
96 result_with_unused_bytes, &bytes, &bytes_size) == TSI_OK);
97 GPR_ASSERT(bytes_size == strlen(TSI_TEST_UNUSED_BYTES));
98 GPR_ASSERT(memcmp(bytes, TSI_TEST_UNUSED_BYTES, bytes_size) == 0);
99 GPR_ASSERT(tsi_handshaker_result_get_unused_bytes(
100 result_without_unused_bytes, &bytes, &bytes_size) == TSI_OK);
101 GPR_ASSERT(bytes_size == 0);
102 GPR_ASSERT(bytes == nullptr);
103 }
104
105 static void check_handshake_results(tsi_test_fixture* fixture) {
106 GPR_ASSERT(fixture != nullptr);
107 GPR_ASSERT(fixture->vtable != nullptr);
108 GPR_ASSERT(fixture->vtable->check_handshaker_peers != nullptr);
109 /* Check handshaker peers. */
110 fixture->vtable->check_handshaker_peers(fixture);
111 /* Check unused bytes. */
112 if (fixture->test_unused_bytes) {
113 tsi_test_channel* channel = fixture->channel;
114 if (fixture->server_result != nullptr &&
115 fixture->client_result != nullptr) {
116 check_unused_bytes(fixture);
117 }
118 channel->bytes_written_to_server_channel = 0;
119 channel->bytes_written_to_client_channel = 0;
120 channel->bytes_read_from_client_channel = 0;
121 channel->bytes_read_from_server_channel = 0;
122 }
123 }
124
125 static void send_bytes_to_peer(tsi_test_channel* test_channel,
126 const unsigned char* buf, size_t buf_size,
127 bool is_client) {
128 GPR_ASSERT(test_channel != nullptr);
129 GPR_ASSERT(buf != nullptr);
130 uint8_t* channel =
131 is_client ? test_channel->server_channel : test_channel->client_channel;
132 GPR_ASSERT(channel != nullptr);
133 size_t* bytes_written = is_client
134 ? &test_channel->bytes_written_to_server_channel
135 : &test_channel->bytes_written_to_client_channel;
136 GPR_ASSERT(bytes_written != nullptr);
137 GPR_ASSERT(*bytes_written + buf_size <= TSI_TEST_DEFAULT_CHANNEL_SIZE);
138 /* Write data to channel. */
139 memcpy(channel + *bytes_written, buf, buf_size);
140 *bytes_written += buf_size;
141 }
142
143 static void maybe_append_unused_bytes(handshaker_args* args) {
144 GPR_ASSERT(args != nullptr);
145 GPR_ASSERT(args->fixture != nullptr);
146 tsi_test_fixture* fixture = args->fixture;
147 if (fixture->test_unused_bytes && !args->appended_unused_bytes) {
148 args->appended_unused_bytes = true;
149 send_bytes_to_peer(
150 fixture->channel,
151 reinterpret_cast<const unsigned char*>(TSI_TEST_UNUSED_BYTES),
152 strlen(TSI_TEST_UNUSED_BYTES), args->is_client);
153 if (fixture->client_result != nullptr &&
154 fixture->server_result == nullptr) {
155 fixture->has_client_finished_first = true;
156 }
157 }
158 }
159
160 static void receive_bytes_from_peer(tsi_test_channel* test_channel,
161 unsigned char** buf, size_t* buf_size,
162 bool is_client) {
163 GPR_ASSERT(test_channel != nullptr);
164 GPR_ASSERT(*buf != nullptr);
165 GPR_ASSERT(buf_size != nullptr);
166 uint8_t* channel =
167 is_client ? test_channel->client_channel : test_channel->server_channel;
168 GPR_ASSERT(channel != nullptr);
169 size_t* bytes_read = is_client
170 ? &test_channel->bytes_read_from_client_channel
171 : &test_channel->bytes_read_from_server_channel;
172 size_t* bytes_written = is_client
173 ? &test_channel->bytes_written_to_client_channel
174 : &test_channel->bytes_written_to_server_channel;
175 GPR_ASSERT(bytes_read != nullptr);
176 GPR_ASSERT(bytes_written != nullptr);
177 size_t to_read = *buf_size < *bytes_written - *bytes_read
178 ? *buf_size
179 : *bytes_written - *bytes_read;
180 /* Read data from channel. */
181 memcpy(*buf, channel + *bytes_read, to_read);
182 *buf_size = to_read;
183 *bytes_read += to_read;
184 }
185
186 void tsi_test_frame_protector_send_message_to_peer(
187 tsi_test_frame_protector_config* config, tsi_test_channel* channel,
188 tsi_frame_protector* protector, bool is_client) {
189 /* Initialization. */
190 GPR_ASSERT(config != nullptr);
191 GPR_ASSERT(channel != nullptr);
192 GPR_ASSERT(protector != nullptr);
193 unsigned char* protected_buffer =
194 static_cast<unsigned char*>(gpr_zalloc(config->protected_buffer_size));
195 size_t message_size =
196 is_client ? config->client_message_size : config->server_message_size;
197 uint8_t* message =
198 is_client ? config->client_message : config->server_message;
199 GPR_ASSERT(message != nullptr);
200 const unsigned char* message_bytes =
201 reinterpret_cast<unsigned char*>(message);
202 tsi_result result = TSI_OK;
203 /* Do protect and send protected data to peer. */
204 while (message_size > 0 && result == TSI_OK) {
205 size_t protected_buffer_size_to_send = config->protected_buffer_size;
206 size_t processed_message_size = message_size;
207 /* Do protect. */
208 result = tsi_frame_protector_protect(
209 protector, message_bytes, &processed_message_size, protected_buffer,
210 &protected_buffer_size_to_send);
211 GPR_ASSERT(result == TSI_OK);
212 /* Send protected data to peer. */
213 send_bytes_to_peer(channel, protected_buffer, protected_buffer_size_to_send,
214 is_client);
215 message_bytes += processed_message_size;
216 message_size -= processed_message_size;
217 /* Flush if we're done. */
218 if (message_size == 0) {
219 size_t still_pending_size;
220 do {
221 protected_buffer_size_to_send = config->protected_buffer_size;
222 result = tsi_frame_protector_protect_flush(
223 protector, protected_buffer, &protected_buffer_size_to_send,
224 &still_pending_size);
225 GPR_ASSERT(result == TSI_OK);
226 send_bytes_to_peer(channel, protected_buffer,
227 protected_buffer_size_to_send, is_client);
228 } while (still_pending_size > 0 && result == TSI_OK);
229 GPR_ASSERT(result == TSI_OK);
230 }
231 }
232 GPR_ASSERT(result == TSI_OK);
233 gpr_free(protected_buffer);
234 }
235
236 void tsi_test_frame_protector_receive_message_from_peer(
237 tsi_test_frame_protector_config* config, tsi_test_channel* channel,
238 tsi_frame_protector* protector, unsigned char* message,
239 size_t* bytes_received, bool is_client) {
240 /* Initialization. */
241 GPR_ASSERT(config != nullptr);
242 GPR_ASSERT(channel != nullptr);
243 GPR_ASSERT(protector != nullptr);
244 GPR_ASSERT(message != nullptr);
245 GPR_ASSERT(bytes_received != nullptr);
246 size_t read_offset = 0;
247 size_t message_offset = 0;
248 size_t read_from_peer_size = 0;
249 tsi_result result = TSI_OK;
250 bool done = false;
251 unsigned char* read_buffer = static_cast<unsigned char*>(
252 gpr_zalloc(config->read_buffer_allocated_size));
253 unsigned char* message_buffer = static_cast<unsigned char*>(
254 gpr_zalloc(config->message_buffer_allocated_size));
255 /* Do unprotect on data received from peer. */
256 while (!done && result == TSI_OK) {
257 /* Receive data from peer. */
258 if (read_from_peer_size == 0) {
259 read_from_peer_size = config->read_buffer_allocated_size;
260 receive_bytes_from_peer(channel, &read_buffer, &read_from_peer_size,
261 is_client);
262 read_offset = 0;
263 }
264 if (read_from_peer_size == 0) {
265 done = true;
266 }
267 /* Do unprotect. */
268 size_t message_buffer_size;
269 do {
270 message_buffer_size = config->message_buffer_allocated_size;
271 size_t processed_size = read_from_peer_size;
272 result = tsi_frame_protector_unprotect(
273 protector, read_buffer + read_offset, &processed_size, message_buffer,
274 &message_buffer_size);
275 GPR_ASSERT(result == TSI_OK);
276 if (message_buffer_size > 0) {
277 memcpy(message + message_offset, message_buffer, message_buffer_size);
278 message_offset += message_buffer_size;
279 }
280 read_offset += processed_size;
281 read_from_peer_size -= processed_size;
282 } while ((read_from_peer_size > 0 || message_buffer_size > 0) &&
283 result == TSI_OK);
284 GPR_ASSERT(result == TSI_OK);
285 }
286 GPR_ASSERT(result == TSI_OK);
287 *bytes_received = message_offset;
288 gpr_free(read_buffer);
289 gpr_free(message_buffer);
290 }
291
292 grpc_error_handle on_handshake_next_done(
293 tsi_result result, void* user_data, const unsigned char* bytes_to_send,
294 size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result) {
295 handshaker_args* args = static_cast<handshaker_args*>(user_data);
296 GPR_ASSERT(args != nullptr);
297 GPR_ASSERT(args->fixture != nullptr);
298 tsi_test_fixture* fixture = args->fixture;
299 grpc_error_handle error;
300 /* Read more data if we need to. */
301 if (result == TSI_INCOMPLETE_DATA) {
302 GPR_ASSERT(bytes_to_send_size == 0);
303 notification_signal(fixture);
304 return error;
305 }
306 if (result != TSI_OK) {
307 notification_signal(fixture);
308 return grpc_set_tsi_error_result(GRPC_ERROR_CREATE("Handshake failed"),
309 result);
310 }
311 /* Update handshaker result. */
312 if (handshaker_result != nullptr) {
313 tsi_handshaker_result** result_to_write =
314 args->is_client ? &fixture->client_result : &fixture->server_result;
315 GPR_ASSERT(*result_to_write == nullptr);
316 *result_to_write = handshaker_result;
317 }
318 /* Send data to peer, if needed. */
319 if (bytes_to_send_size > 0) {
320 send_bytes_to_peer(fixture->channel, bytes_to_send, bytes_to_send_size,
321 args->is_client);
322 args->transferred_data = true;
323 }
324 if (handshaker_result != nullptr) {
325 maybe_append_unused_bytes(args);
326 }
327 notification_signal(fixture);
328 return error;
329 }
330
331 static void on_handshake_next_done_wrapper(
332 tsi_result result, void* user_data, const unsigned char* bytes_to_send,
333 size_t bytes_to_send_size, tsi_handshaker_result* handshaker_result) {
334 handshaker_args* args = static_cast<handshaker_args*>(user_data);
335 args->error = on_handshake_next_done(result, user_data, bytes_to_send,
336 bytes_to_send_size, handshaker_result);
337 }
338
339 static bool is_handshake_finished_properly(handshaker_args* args) {
340 GPR_ASSERT(args != nullptr);
341 GPR_ASSERT(args->fixture != nullptr);
342 tsi_test_fixture* fixture = args->fixture;
343 return (args->is_client && fixture->client_result != nullptr) ||
344 (!args->is_client && fixture->server_result != nullptr);
345 }
346
347 static void do_handshaker_next(handshaker_args* args) {
348 /* Initialization. */
349 GPR_ASSERT(args != nullptr);
350 GPR_ASSERT(args->fixture != nullptr);
351 tsi_test_fixture* fixture = args->fixture;
352 tsi_handshaker* handshaker =
353 args->is_client ? fixture->client_handshaker : fixture->server_handshaker;
354 if (is_handshake_finished_properly(args)) {
355 return;
356 }
357 tsi_handshaker_result* handshaker_result = nullptr;
358 unsigned char* bytes_to_send = nullptr;
359 size_t bytes_to_send_size = 0;
360 tsi_result result = TSI_OK;
361 /* Receive data from peer, if available. */
362 do {
363 size_t buf_size = args->handshake_buffer_size;
364 receive_bytes_from_peer(fixture->channel, &args->handshake_buffer,
365 &buf_size, args->is_client);
366 if (buf_size > 0) {
367 args->transferred_data = true;
368 }
369 /* Peform handshaker next. */
370 result = tsi_handshaker_next(
371 handshaker, args->handshake_buffer, buf_size,
372 const_cast<const unsigned char**>(&bytes_to_send), &bytes_to_send_size,
373 &handshaker_result, &on_handshake_next_done_wrapper, args);
374 if (result != TSI_ASYNC) {
375 args->error = on_handshake_next_done(
376 result, args, bytes_to_send, bytes_to_send_size, handshaker_result);
377 if (!args->error.ok()) {
378 return;
379 }
380 }
381 } while (result == TSI_INCOMPLETE_DATA);
382 notification_wait(fixture);
383 }
384
385 void tsi_test_do_handshake(tsi_test_fixture* fixture) {
386 /* Initializaiton. */
387 setup_handshakers(fixture);
388 handshaker_args* client_args =
389 handshaker_args_create(fixture, true /* is_client */);
390 handshaker_args* server_args =
391 handshaker_args_create(fixture, false /* is_client */);
392 /* Do handshake. */
393 do {
394 client_args->transferred_data = false;
395 server_args->transferred_data = false;
396 do_handshaker_next(client_args);
397 if (!client_args->error.ok()) {
398 break;
399 }
400 do_handshaker_next(server_args);
401 if (!server_args->error.ok()) {
402 break;
403 }
404 GPR_ASSERT(client_args->transferred_data || server_args->transferred_data);
405 } while (fixture->client_result == nullptr ||
406 fixture->server_result == nullptr);
407 /* Verify handshake results. */
408 check_handshake_results(fixture);
409 /* Cleanup. */
410 handshaker_args_destroy(client_args);
411 handshaker_args_destroy(server_args);
412 }
413
414 static void tsi_test_do_ping_pong(tsi_test_frame_protector_config* config,
415 tsi_test_channel* channel,
416 tsi_frame_protector* client_frame_protector,
417 tsi_frame_protector* server_frame_protector) {
418 GPR_ASSERT(config != nullptr);
419 GPR_ASSERT(channel != nullptr);
420 GPR_ASSERT(client_frame_protector != nullptr);
421 GPR_ASSERT(server_frame_protector != nullptr);
422 /* Client sends a message to server. */
423 tsi_test_frame_protector_send_message_to_peer(
424 config, channel, client_frame_protector, true /* is_client */);
425 unsigned char* server_received_message =
426 static_cast<unsigned char*>(gpr_zalloc(TSI_TEST_DEFAULT_CHANNEL_SIZE));
427 size_t server_received_message_size = 0;
428 tsi_test_frame_protector_receive_message_from_peer(
429 config, channel, server_frame_protector, server_received_message,
430 &server_received_message_size, false /* is_client */);
431 GPR_ASSERT(config->client_message_size == server_received_message_size);
432 GPR_ASSERT(memcmp(config->client_message, server_received_message,
433 server_received_message_size) == 0);
434 /* Server sends a message to client. */
435 tsi_test_frame_protector_send_message_to_peer(
436 config, channel, server_frame_protector, false /* is_client */);
437 unsigned char* client_received_message =
438 static_cast<unsigned char*>(gpr_zalloc(TSI_TEST_DEFAULT_CHANNEL_SIZE));
439 size_t client_received_message_size = 0;
440 tsi_test_frame_protector_receive_message_from_peer(
441 config, channel, client_frame_protector, client_received_message,
442 &client_received_message_size, true /* is_client */);
443 GPR_ASSERT(config->server_message_size == client_received_message_size);
444 GPR_ASSERT(memcmp(config->server_message, client_received_message,
445 client_received_message_size) == 0);
446 gpr_free(server_received_message);
447 gpr_free(client_received_message);
448 }
449
450 void tsi_test_frame_protector_do_round_trip_no_handshake(
451 tsi_test_frame_protector_fixture* fixture) {
452 GPR_ASSERT(fixture != nullptr);
453 tsi_test_do_ping_pong(fixture->config, fixture->channel,
454 fixture->client_frame_protector,
455 fixture->server_frame_protector);
456 }
457
458 void tsi_test_do_round_trip(tsi_test_fixture* fixture) {
459 /* Initialization. */
460 GPR_ASSERT(fixture != nullptr);
461 GPR_ASSERT(fixture->config != nullptr);
462 tsi_test_frame_protector_config* config = fixture->config;
463 tsi_frame_protector* client_frame_protector = nullptr;
464 tsi_frame_protector* server_frame_protector = nullptr;
465 /* Perform handshake. */
466 tsi_test_do_handshake(fixture);
467 /* Create frame protectors.*/
468 size_t client_max_output_protected_frame_size =
469 config->client_max_output_protected_frame_size;
470 GPR_ASSERT(tsi_handshaker_result_create_frame_protector(
471 fixture->client_result,
472 client_max_output_protected_frame_size == 0
473 ? nullptr
474 : &client_max_output_protected_frame_size,
475 &client_frame_protector) == TSI_OK);
476 size_t server_max_output_protected_frame_size =
477 config->server_max_output_protected_frame_size;
478 GPR_ASSERT(tsi_handshaker_result_create_frame_protector(
479 fixture->server_result,
480 server_max_output_protected_frame_size == 0
481 ? nullptr
482 : &server_max_output_protected_frame_size,
483 &server_frame_protector) == TSI_OK);
484 tsi_test_do_ping_pong(config, fixture->channel, client_frame_protector,
485 server_frame_protector);
486 /* Destroy server and client frame protectors. */
487 tsi_frame_protector_destroy(client_frame_protector);
488 tsi_frame_protector_destroy(server_frame_protector);
489 }
490
491 static unsigned char* generate_random_message(size_t size) {
492 size_t i;
493 unsigned char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890";
494 unsigned char* output =
495 static_cast<unsigned char*>(gpr_zalloc(sizeof(unsigned char) * size));
496 for (i = 0; i < size - 1; ++i) {
497 output[i] = chars[rand() % static_cast<int>(sizeof(chars) - 1)];
498 }
499 return output;
500 }
501
502 tsi_test_frame_protector_config* tsi_test_frame_protector_config_create(
503 bool use_default_read_buffer_allocated_size,
504 bool use_default_message_buffer_allocated_size,
505 bool use_default_protected_buffer_size, bool use_default_client_message,
506 bool use_default_server_message,
507 bool use_default_client_max_output_protected_frame_size,
508 bool use_default_server_max_output_protected_frame_size) {
509 tsi_test_frame_protector_config* config =
510 static_cast<tsi_test_frame_protector_config*>(
511 gpr_zalloc(sizeof(*config)));
512 /* Set the value for read_buffer_allocated_size. */
513 config->read_buffer_allocated_size =
514 use_default_read_buffer_allocated_size
515 ? TSI_TEST_DEFAULT_BUFFER_SIZE
516 : TSI_TEST_SMALL_READ_BUFFER_ALLOCATED_SIZE;
517 /* Set the value for message_buffer_allocated_size. */
518 config->message_buffer_allocated_size =
519 use_default_message_buffer_allocated_size
520 ? TSI_TEST_DEFAULT_BUFFER_SIZE
521 : TSI_TEST_SMALL_MESSAGE_BUFFER_ALLOCATED_SIZE;
522 /* Set the value for protected_buffer_size. */
523 config->protected_buffer_size = use_default_protected_buffer_size
524 ? TSI_TEST_DEFAULT_PROTECTED_BUFFER_SIZE
525 : TSI_TEST_SMALL_PROTECTED_BUFFER_SIZE;
526 /* Set the value for client message. */
527 config->client_message_size = use_default_client_message
528 ? TSI_TEST_BIG_MESSAGE_SIZE
529 : TSI_TEST_SMALL_MESSAGE_SIZE;
530 config->client_message =
531 use_default_client_message
532 ? generate_random_message(TSI_TEST_BIG_MESSAGE_SIZE)
533 : generate_random_message(TSI_TEST_SMALL_MESSAGE_SIZE);
534 /* Set the value for server message. */
535 config->server_message_size = use_default_server_message
536 ? TSI_TEST_BIG_MESSAGE_SIZE
537 : TSI_TEST_SMALL_MESSAGE_SIZE;
538 config->server_message =
539 use_default_server_message
540 ? generate_random_message(TSI_TEST_BIG_MESSAGE_SIZE)
541 : generate_random_message(TSI_TEST_SMALL_MESSAGE_SIZE);
542 /* Set the value for client max_output_protected_frame_size.
543 If it is 0, we pass NULL to tsi_handshaker_result_create_frame_protector(),
544 which then uses default protected frame size for it. */
545 config->client_max_output_protected_frame_size =
546 use_default_client_max_output_protected_frame_size
547 ? 0
548 : TSI_TEST_SMALL_CLIENT_MAX_OUTPUT_PROTECTED_FRAME_SIZE;
549 /* Set the value for server max_output_protected_frame_size.
550 If it is 0, we pass NULL to tsi_handshaker_result_create_frame_protector(),
551 which then uses default protected frame size for it. */
552 config->server_max_output_protected_frame_size =
553 use_default_server_max_output_protected_frame_size
554 ? 0
555 : TSI_TEST_SMALL_SERVER_MAX_OUTPUT_PROTECTED_FRAME_SIZE;
556 return config;
557 }
558
559 void tsi_test_frame_protector_config_set_buffer_size(
560 tsi_test_frame_protector_config* config, size_t read_buffer_allocated_size,
561 size_t message_buffer_allocated_size, size_t protected_buffer_size,
562 size_t client_max_output_protected_frame_size,
563 size_t server_max_output_protected_frame_size) {
564 GPR_ASSERT(config != nullptr);
565 config->read_buffer_allocated_size = read_buffer_allocated_size;
566 config->message_buffer_allocated_size = message_buffer_allocated_size;
567 config->protected_buffer_size = protected_buffer_size;
568 config->client_max_output_protected_frame_size =
569 client_max_output_protected_frame_size;
570 config->server_max_output_protected_frame_size =
571 server_max_output_protected_frame_size;
572 }
573
574 void tsi_test_frame_protector_config_destroy(
575 tsi_test_frame_protector_config* config) {
576 if (config == nullptr) {
577 return;
578 }
579 gpr_free(config->client_message);
580 gpr_free(config->server_message);
581 gpr_free(config);
582 }
583
584 static tsi_test_channel* tsi_test_channel_create() {
585 tsi_test_channel* channel = grpc_core::Zalloc<tsi_test_channel>();
586 channel->client_channel =
587 static_cast<uint8_t*>(gpr_zalloc(TSI_TEST_DEFAULT_CHANNEL_SIZE));
588 channel->server_channel =
589 static_cast<uint8_t*>(gpr_zalloc(TSI_TEST_DEFAULT_CHANNEL_SIZE));
590 channel->bytes_written_to_client_channel = 0;
591 channel->bytes_written_to_server_channel = 0;
592 channel->bytes_read_from_client_channel = 0;
593 channel->bytes_read_from_server_channel = 0;
594 return channel;
595 }
596
597 static void tsi_test_channel_destroy(tsi_test_channel* channel) {
598 if (channel == nullptr) {
599 return;
600 }
601 gpr_free(channel->client_channel);
602 gpr_free(channel->server_channel);
603 gpr_free(channel);
604 }
605
606 void tsi_test_fixture_init(tsi_test_fixture* fixture) {
607 memset(fixture, 0, sizeof(tsi_test_fixture));
608 fixture->config = tsi_test_frame_protector_config_create(
609 true, true, true, true, true, true, true);
610 fixture->handshake_buffer_size = TSI_TEST_DEFAULT_BUFFER_SIZE;
611 fixture->channel = tsi_test_channel_create();
612 fixture->test_unused_bytes = true;
613 fixture->has_client_finished_first = false;
614 gpr_mu_init(&fixture->mu);
615 gpr_cv_init(&fixture->cv);
616 fixture->notified = false;
617 }
618
619 void tsi_test_fixture_destroy(tsi_test_fixture* fixture) {
620 if (fixture == nullptr) {
621 return;
622 }
623 tsi_test_frame_protector_config_destroy(fixture->config);
624 tsi_handshaker_destroy(fixture->client_handshaker);
625 tsi_handshaker_destroy(fixture->server_handshaker);
626 tsi_handshaker_result_destroy(fixture->client_result);
627 tsi_handshaker_result_destroy(fixture->server_result);
628 tsi_test_channel_destroy(fixture->channel);
629 GPR_ASSERT(fixture->vtable != nullptr);
630 GPR_ASSERT(fixture->vtable->destruct != nullptr);
631 gpr_mu_destroy(&fixture->mu);
632 gpr_cv_destroy(&fixture->cv);
633 fixture->vtable->destruct(fixture);
634 }
635
636 tsi_test_frame_protector_fixture* tsi_test_frame_protector_fixture_create() {
637 tsi_test_frame_protector_fixture* fixture =
638 static_cast<tsi_test_frame_protector_fixture*>(
639 gpr_zalloc(sizeof(*fixture)));
640 fixture->config = tsi_test_frame_protector_config_create(
641 true, true, true, true, true, true, true);
642 fixture->channel = tsi_test_channel_create();
643 return fixture;
644 }
645
646 void tsi_test_frame_protector_fixture_init(
647 tsi_test_frame_protector_fixture* fixture,
648 tsi_frame_protector* client_frame_protector,
649 tsi_frame_protector* server_frame_protector) {
650 GPR_ASSERT(fixture != nullptr);
651 fixture->client_frame_protector = client_frame_protector;
652 fixture->server_frame_protector = server_frame_protector;
653 }
654
655 void tsi_test_frame_protector_fixture_destroy(
656 tsi_test_frame_protector_fixture* fixture) {
657 if (fixture == nullptr) {
658 return;
659 }
660 tsi_test_frame_protector_config_destroy(fixture->config);
661 tsi_test_channel_destroy(fixture->channel);
662 tsi_frame_protector_destroy(fixture->client_frame_protector);
663 tsi_frame_protector_destroy(fixture->server_frame_protector);
664 gpr_free(fixture);
665 }