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