• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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((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     memcpy(*des, src, source_len);
53   } else {
54     fprintf(stderr, "Either src or des buffer is nullptr in gsec_test_copy().");
55     abort();
56   }
57 }
58 
gsec_test_copy_and_alter_random_byte(const uint8_t * src,uint8_t ** des,size_t source_len)59 void gsec_test_copy_and_alter_random_byte(const uint8_t* src, uint8_t** des,
60                                           size_t source_len) {
61   if (src != nullptr && des != nullptr) {
62     *des = static_cast<uint8_t*>(gpr_malloc(source_len));
63     memcpy(*des, src, source_len);
64     uint32_t offset;
65     offset = gsec_test_bias_random_uint32(static_cast<uint32_t>(source_len));
66     (*(*des + offset))++;
67   } else {
68     fprintf(stderr,
69             "Either src or des is nullptr in "
70             "gsec_test_copy_and_alter_random_byte().");
71     abort();
72   }
73 }
74 
gsec_test_expect_compare_code_and_substr(grpc_status_code status1,grpc_status_code status2,const char * msg1,const char * msg2)75 int gsec_test_expect_compare_code_and_substr(grpc_status_code status1,
76                                              grpc_status_code status2,
77                                              const char* msg1,
78                                              const char* msg2) {
79   int failure = 1;
80   if (status1 != status2) {
81     fprintf(stderr, "Status %d does not equal %d.\n", status1, status2);
82     failure = 0;
83   }
84   if (strstr(msg1, msg2) == nullptr) {
85     fprintf(stderr, "Status message <%s> does not contain <%s>.\n", msg1, msg2);
86     failure = 0;
87   }
88   return failure;
89 }
90