1 //
2 //
3 // Copyright 2018 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/alts/crypt/gsec_test_util.h"
20
21 #include <grpc/support/alloc.h>
22 #include <time.h>
23
gsec_test_random_bytes(uint8_t * bytes,size_t length)24 void gsec_test_random_bytes(uint8_t* bytes, size_t length) {
25 srand(time(nullptr));
26 size_t ind;
27 for (ind = 0; ind < length; ind++) {
28 bytes[ind] = static_cast<uint8_t>((rand() % 255) + 1);
29 }
30 }
31
gsec_test_random_array(uint8_t ** bytes,size_t length)32 void gsec_test_random_array(uint8_t** bytes, size_t length) {
33 if (bytes != nullptr) {
34 *bytes = static_cast<uint8_t*>(gpr_malloc(length));
35 gsec_test_random_bytes(*bytes, length);
36 } else {
37 fprintf(stderr, "bytes buffer is nullptr in gsec_test_random_array().");
38 abort();
39 }
40 }
41
gsec_test_bias_random_uint32(uint32_t max_length)42 uint32_t gsec_test_bias_random_uint32(uint32_t max_length) {
43 uint32_t value;
44 gsec_test_random_bytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
45 return value % max_length;
46 }
47
gsec_test_copy(const uint8_t * src,uint8_t ** des,size_t source_len)48 void gsec_test_copy(const uint8_t* src, uint8_t** des, size_t source_len) {
49 if (src != nullptr && des != nullptr) {
50 *des = static_cast<uint8_t*>(gpr_malloc(source_len));
51 if (*des != nullptr) {
52 memcpy(*des, src, source_len);
53 }
54 } else {
55 fprintf(stderr, "Either src or des buffer is nullptr in gsec_test_copy().");
56 abort();
57 }
58 }
59
gsec_test_copy_and_alter_random_byte(const uint8_t * src,uint8_t ** des,size_t source_len)60 void gsec_test_copy_and_alter_random_byte(const uint8_t* src, uint8_t** des,
61 size_t source_len) {
62 if (src != nullptr && des != nullptr) {
63 *des = static_cast<uint8_t*>(gpr_malloc(source_len));
64 memcpy(*des, src, source_len);
65 uint32_t offset;
66 offset = gsec_test_bias_random_uint32(static_cast<uint32_t>(source_len));
67 (*(*des + offset))++;
68 } else {
69 fprintf(stderr,
70 "Either src or des is nullptr in "
71 "gsec_test_copy_and_alter_random_byte().");
72 abort();
73 }
74 }
75
gsec_test_expect_compare_code_and_substr(grpc_status_code status1,grpc_status_code status2,const char * msg1,const char * msg2)76 int gsec_test_expect_compare_code_and_substr(grpc_status_code status1,
77 grpc_status_code status2,
78 const char* msg1,
79 const char* msg2) {
80 int failure = 1;
81 if (status1 != status2) {
82 fprintf(stderr, "Status %d does not equal %d.\n", status1, status2);
83 failure = 0;
84 }
85 if (strstr(msg1, msg2) == nullptr) {
86 fprintf(stderr, "Status message <%s> does not contain <%s>.\n", msg1, msg2);
87 failure = 0;
88 }
89 return failure;
90 }
91