alts_counter_test.cc
Go to the documentation of this file.
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 
20 
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 
25 
26 const size_t kSmallCounterSize = 4;
27 const size_t kSmallOverflowSize = 1;
28 const size_t kGcmCounterSize = 12;
29 const size_t kGcmOverflowSize = 5;
30 
32  unsigned char* /*counter*/, size_t size) {
33  return (ctr->counter[size - 1] & 0x80) == 0x80;
34 }
35 
36 static void alts_counter_test_input_sanity_check(size_t counter_size,
37  size_t overflow_size) {
38  alts_counter* ctr = nullptr;
39  char* error_details = nullptr;
40 
41  /* Input sanity check on alts_counter_create(). */
42  /* Invalid counter size. */
44  alts_counter_create(true, 0, overflow_size, &ctr, &error_details);
46  status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
47  "counter_size is invalid."));
48  gpr_free(error_details);
49 
50  /* Invalid overflow size. */
51  status = alts_counter_create(true, counter_size, 0, &ctr, &error_details);
53  status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
54  "overflow_size is invalid."));
55  gpr_free(error_details);
56 
57  /* alts_counter is nullptr. */
58  status = alts_counter_create(true, counter_size, overflow_size, nullptr,
59  &error_details);
61  status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
62  "crypter_counter is nullptr."));
63  gpr_free(error_details);
64 
65  status = alts_counter_create(true, counter_size, overflow_size, &ctr,
66  &error_details);
68 
69  /* Input sanity check on alts_counter_increment(). */
70  /* crypter_counter is nullptr. */
71  bool is_overflow = false;
72  status = alts_counter_increment(nullptr, &is_overflow, &error_details);
74  status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
75  "crypter_counter is nullptr."));
76  gpr_free(error_details);
77  /* is_overflow is nullptr. */
78  status = alts_counter_increment(ctr, nullptr, &error_details);
80  status, GRPC_STATUS_INVALID_ARGUMENT, error_details,
81  "is_overflow is nullptr."));
82  gpr_free(error_details);
84 }
85 
86 static void alts_counter_test_overflow_full_range(bool is_client,
87  size_t counter_size,
88  size_t overflow_size) {
89  alts_counter* ctr = nullptr;
90  char* error_details = nullptr;
92  is_client, counter_size, overflow_size, &ctr, &error_details);
94  unsigned char* expected =
95  static_cast<unsigned char*>(gpr_zalloc(counter_size));
96  if (is_client) {
97  expected[counter_size - 1] = 0x80;
98  }
99  /* Do a single iteration to ensure the counter is initialized as expected. */
101  counter_size) == is_client);
102  GPR_ASSERT(memcmp(alts_counter_get_counter(ctr), expected, counter_size) ==
103  0);
104  bool is_overflow = false;
105  GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
107  GPR_ASSERT(!is_overflow);
113  int iterations = 1 << (overflow_size * 8);
114  int ind = 1;
115  for (ind = 1; ind < iterations - 1; ind++) {
117  counter_size) == is_client);
118  GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
120  GPR_ASSERT(!is_overflow);
121  }
123  counter_size) == is_client);
124  GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
126  GPR_ASSERT(is_overflow);
127  gpr_free(expected);
129 }
130 
131 /* Set the counter manually and make sure it overflows as expected. */
133  size_t counter_size,
134  size_t overflow_size) {
135  alts_counter* ctr = nullptr;
136  char* error_details = nullptr;
138  is_client, counter_size, overflow_size, &ctr, &error_details);
140  unsigned char* expected =
141  static_cast<unsigned char*>(gpr_zalloc(counter_size));
142  memset(expected, 0xFF, overflow_size);
143  expected[0] = 0xFE;
144 
145  if (is_client) {
146  expected[counter_size - 1] = 0x80;
147  }
148  memcpy(ctr->counter, expected, counter_size);
150  counter_size) == is_client);
151  GPR_ASSERT(memcmp(expected, alts_counter_get_counter(ctr), counter_size) ==
152  0);
153  bool is_overflow = false;
154  GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
156  GPR_ASSERT(!is_overflow);
158  counter_size) == is_client);
159  expected[0] = static_cast<unsigned char>(expected[0] + 1);
160  GPR_ASSERT(memcmp(expected, alts_counter_get_counter(ctr), counter_size) ==
161  0);
162  GPR_ASSERT(alts_counter_increment(ctr, &is_overflow, &error_details) ==
164  GPR_ASSERT(is_overflow);
165  gpr_free(expected);
167 }
168 
169 int main(int /*argc*/, char** /*argv*/) {
179 
180  return 0;
181 }
log.h
alts_counter_get_counter
unsigned char * alts_counter_get_counter(alts_counter *crypter_counter)
Definition: alts_counter.cc:106
memset
return memset(p, 0, total)
alts_counter.h
ind
Definition: bloaty/third_party/zlib/examples/gun.c:81
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
main
int main(int, char **)
Definition: alts_counter_test.cc:169
status
absl::Status status
Definition: rls.cc:251
alts_counter_increment
grpc_status_code alts_counter_increment(alts_counter *crypter_counter, bool *is_overflow, char **error_details)
Definition: alts_counter.cc:66
GRPC_STATUS_INVALID_ARGUMENT
@ GRPC_STATUS_INVALID_ARGUMENT
Definition: include/grpc/impl/codegen/status.h:46
alts_counter_test_overflow_full_range
static void alts_counter_test_overflow_full_range(bool is_client, size_t counter_size, size_t overflow_size)
Definition: alts_counter_test.cc:86
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
kSmallOverflowSize
const size_t kSmallOverflowSize
Definition: alts_counter_test.cc:27
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
alts_counter::counter
unsigned char * counter
Definition: alts_counter.h:33
gsec_test_util.h
alts_counter_test_input_sanity_check
static void alts_counter_test_input_sanity_check(size_t counter_size, size_t overflow_size)
Definition: alts_counter_test.cc:36
do_bytes_represent_client
static bool do_bytes_represent_client(alts_counter *ctr, unsigned char *, size_t size)
Definition: alts_counter_test.cc:31
alloc.h
kGcmOverflowSize
const size_t kGcmOverflowSize
Definition: alts_counter_test.cc:29
kGcmCounterSize
const size_t kGcmCounterSize
Definition: alts_counter_test.cc:28
gsec_test_expect_compare_code_and_substr
int gsec_test_expect_compare_code_and_substr(grpc_status_code status1, grpc_status_code status2, const char *msg1, const char *msg2)
Definition: gsec_test_util.cc:77
GRPC_STATUS_FAILED_PRECONDITION
@ GRPC_STATUS_FAILED_PRECONDITION
Definition: include/grpc/impl/codegen/status.h:97
alts_counter_test_overflow_single_increment
static void alts_counter_test_overflow_single_increment(bool is_client, size_t counter_size, size_t overflow_size)
Definition: alts_counter_test.cc:132
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
alts_counter_create
grpc_status_code alts_counter_create(bool is_client, size_t counter_size, size_t overflow_size, alts_counter **crypter_counter, char **error_details)
Definition: alts_counter.cc:34
kSmallCounterSize
const size_t kSmallCounterSize
Definition: alts_counter_test.cc:26
alts_counter
Definition: alts_counter.h:30
alts_counter_destroy
void alts_counter_destroy(alts_counter *crypter_counter)
Definition: alts_counter.cc:113


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:40