aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/tsi/alts/crypt/gsec_test_util.cc
blob: 992c91e6bc849a656c3a7282f70e2d3358a41226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "test/core/tsi/alts/crypt/gsec_test_util.h"

#include <grpc/support/alloc.h>

void gsec_test_random_bytes(uint8_t* bytes, size_t length) {
  if (!RAND_bytes(bytes, static_cast<int>(length))) {
    fprintf(stderr,
            "Random bytes generation failed in gsec_test_random_bytes().");
    abort();
  }
}

void gsec_test_random_array(uint8_t** bytes, size_t length) {
  if (bytes != nullptr) {
    *bytes = static_cast<uint8_t*>(gpr_malloc(length));
    gsec_test_random_bytes(*bytes, length);
  } else {
    fprintf(stderr, "bytes buffer is nullptr in gsec_test_random_array().");
    abort();
  }
}

uint32_t gsec_test_bias_random_uint32(uint32_t max_length) {
  uint32_t value;
  gsec_test_random_bytes((uint8_t*)(&value), sizeof(value));
  return value % max_length;
}

void gsec_test_copy(const uint8_t* src, uint8_t** des, size_t source_len) {
  if (src != nullptr && des != nullptr) {
    *des = static_cast<uint8_t*>(gpr_malloc(source_len));
    memcpy(*des, src, source_len);
  } else {
    fprintf(stderr, "Either src or des buffer is nullptr in gsec_test_copy().");
    abort();
  }
}

void gsec_test_copy_and_alter_random_byte(const uint8_t* src, uint8_t** des,
                                          size_t source_len) {
  if (src != nullptr && des != nullptr) {
    *des = static_cast<uint8_t*>(gpr_malloc(source_len));
    memcpy(*des, src, source_len);
    uint32_t offset;
    offset = gsec_test_bias_random_uint32(static_cast<uint32_t>(source_len));
    (*(*des + offset))++;
  } else {
    fprintf(stderr,
            "Either src or des is nullptr in "
            "gsec_test_copy_and_alter_random_byte().");
    abort();
  }
}

int gsec_test_expect_compare_code_and_substr(grpc_status_code status1,
                                             grpc_status_code status2,
                                             const char* msg1,
                                             const char* msg2) {
  int failure = 1;
  if (status1 != status2) {
    fprintf(stderr, "Status %d does not equal %d.\n", status1, status2);
    failure = 0;
  }
  if (strstr(msg1, msg2) == nullptr) {
    fprintf(stderr, "Status message <%s> does not contain <%s>.\n", msg1, msg2);
    failure = 0;
  }
  return failure;
}