alts_zero_copy_grpc_protector_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/slice_buffer.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 
32 
33 /* TODO: tests zero_copy_grpc_protector under TSI test library, which
34  * has more comprehensive tests. */
35 
36 constexpr size_t kSealRepeatTimes = 50;
37 constexpr size_t kSmallBufferSize = 16;
38 constexpr size_t kLargeBufferSize = 16384;
39 constexpr size_t kChannelMaxSize = 2048;
40 constexpr size_t kChannelMinSize = 128;
41 
42 /* Test fixtures for each test cases. */
46 };
47 
48 /* Test input variables for protect/unprotect operations. */
55 };
56 
57 /* --- Test utility functions. --- */
58 
60  grpc_slice_buffer* dup_sb,
61  size_t length) {
62  GPR_ASSERT(sb != nullptr);
63  GPR_ASSERT(dup_sb != nullptr);
64  GPR_ASSERT(length > 0);
69 }
70 
72  GPR_ASSERT(sb != nullptr);
73  GPR_ASSERT(index < sb->length);
74  for (size_t i = 0; i < sb->count; i++) {
75  if (index < GRPC_SLICE_LENGTH(sb->slices[i])) {
76  return GRPC_SLICE_START_PTR(sb->slices[i]) + index;
77  } else {
79  }
80  }
81  return nullptr;
82 }
83 
84 /* Checks if two slice buffer contents are the same. It is not super efficient,
85  * but OK for testing. */
88  GPR_ASSERT(first != nullptr);
89  GPR_ASSERT(second != nullptr);
90  if (first->length != second->length) {
91  return false;
92  }
93  for (size_t i = 0; i < first->length; i++) {
94  uint8_t* first_ptr = pointer_to_nth_byte(first, i);
95  uint8_t* second_ptr = pointer_to_nth_byte(second, i);
96  GPR_ASSERT(first_ptr != nullptr && second_ptr != nullptr);
97  if ((*first_ptr) != (*second_ptr)) {
98  return false;
99  }
100  }
101  return true;
102 }
103 
106  bool integrity_only,
107  bool enable_extra_copy) {
112  size_t key_length = rekey ? kAes128GcmRekeyKeyLength : kAes128GcmKeyLength;
113  uint8_t* key;
114  size_t max_protected_frame_size = 1024;
115  size_t actual_max_protected_frame_size;
116  gsec_test_random_array(&key, key_length);
118  key, key_length, rekey, /*is_client=*/true, integrity_only,
119  enable_extra_copy, &max_protected_frame_size,
120  &fixture->client) == TSI_OK);
122  fixture->client, &actual_max_protected_frame_size) == TSI_OK);
123  GPR_ASSERT(actual_max_protected_frame_size == max_protected_frame_size);
125  key, key_length, rekey, /*is_client=*/false, integrity_only,
126  enable_extra_copy, &max_protected_frame_size,
127  &fixture->server) == TSI_OK);
129  fixture->server, &actual_max_protected_frame_size) == TSI_OK);
130  GPR_ASSERT(actual_max_protected_frame_size == max_protected_frame_size);
131  gpr_free(key);
133  return fixture;
134 }
135 
138  if (fixture == nullptr) {
139  return;
140  }
145  gpr_free(fixture);
146 }
147 
158  return var;
159 }
160 
163  if (var == nullptr) {
164  return;
165  }
171  gpr_free(var);
172 }
173 
174 /* --- ALTS zero-copy protector tests. --- */
175 
177  tsi_zero_copy_grpc_protector* receiver) {
179  for (size_t i = 0; i < kSealRepeatTimes; i++) {
180  int min_progress_size;
183  /* Creates a random small slice buffer and calls protect(). */
187  sender, &var->original_sb, &var->protected_sb) == TSI_OK);
188  /* Splits protected slice buffer into two: first one is staging_sb, and
189  * second one is protected_sb. */
190  uint32_t staging_sb_size =
192  static_cast<uint32_t>(var->protected_sb.length - 1)) +
193  1;
194  grpc_slice_buffer_move_first(&var->protected_sb, staging_sb_size,
195  &var->staging_sb);
196  /* Unprotects one by one. */
198  receiver, &var->staging_sb, &var->unprotected_sb,
199  &min_progress_size) == TSI_OK);
200  if (staging_sb_size >= kZeroCopyFrameLengthFieldSize) {
201  GPR_ASSERT(min_progress_size ==
202  static_cast<int>(var->protected_sb.length));
203  } else {
204  GPR_ASSERT(min_progress_size == 1);
205  }
206  GPR_ASSERT(var->unprotected_sb.length == 0);
208  receiver, &var->protected_sb, &var->unprotected_sb,
209  &min_progress_size) == TSI_OK);
210  GPR_ASSERT(
212  GPR_ASSERT(min_progress_size == 1);
214  }
216 }
217 
219  tsi_zero_copy_grpc_protector* receiver) {
221  for (size_t i = 0; i < kSealRepeatTimes; i++) {
224  /* Creates a random large slice buffer and calls protect(). */
228  sender, &var->original_sb, &var->protected_sb) == TSI_OK);
229  /* Splits protected slice buffer into multiple pieces. Receiver unprotects
230  * each slice buffer one by one. */
231  uint32_t channel_size = gsec_test_bias_random_uint32(static_cast<uint32_t>(
233  static_cast<uint32_t>(kChannelMinSize);
234  while (var->protected_sb.length > channel_size) {
236  grpc_slice_buffer_move_first(&var->protected_sb, channel_size,
237  &var->staging_sb);
239  receiver, &var->staging_sb, &var->unprotected_sb,
240  nullptr) == TSI_OK);
241  }
243  receiver, &var->protected_sb, &var->unprotected_sb,
244  nullptr) == TSI_OK);
245  GPR_ASSERT(
248  }
250 }
251 
252 /* --- Test cases. --- */
253 
255  bool enable_extra_copy) {
258  /*rekey=*/false, /*integrity_only=*/true, enable_extra_copy);
259  seal_unseal_small_buffer(fixture->client, fixture->server);
260  seal_unseal_small_buffer(fixture->server, fixture->client);
262 
264  /*rekey=*/false, /*integrity_only=*/false, enable_extra_copy);
265  seal_unseal_small_buffer(fixture->client, fixture->server);
266  seal_unseal_small_buffer(fixture->server, fixture->client);
268 
270  /*rekey=*/true, /*integrity_only=*/true, enable_extra_copy);
271  seal_unseal_small_buffer(fixture->client, fixture->server);
272  seal_unseal_small_buffer(fixture->server, fixture->client);
274 
276  /*rekey=*/true, /*integrity_only=*/false, enable_extra_copy);
277  seal_unseal_small_buffer(fixture->client, fixture->server);
278  seal_unseal_small_buffer(fixture->server, fixture->client);
280 }
281 
283  bool enable_extra_copy) {
286  /*rekey=*/false, /*integrity_only=*/true, enable_extra_copy);
287  seal_unseal_large_buffer(fixture->client, fixture->server);
288  seal_unseal_large_buffer(fixture->server, fixture->client);
290 
292  /*rekey=*/false, /*integrity_only=*/false, enable_extra_copy);
293  seal_unseal_large_buffer(fixture->client, fixture->server);
294  seal_unseal_large_buffer(fixture->server, fixture->client);
296 
298  /*rekey=*/true, /*integrity_only=*/true, enable_extra_copy);
299  seal_unseal_large_buffer(fixture->client, fixture->server);
300  seal_unseal_large_buffer(fixture->server, fixture->client);
302 
304  /*rekey=*/true, /*integrity_only=*/false, enable_extra_copy);
305  seal_unseal_large_buffer(fixture->client, fixture->server);
306  seal_unseal_large_buffer(fixture->server, fixture->client);
308 }
309 
310 int main(int argc, char** argv) {
311  grpc::testing::TestEnvironment env(&argc, argv);
312  grpc_init();
314  /*enable_extra_copy=*/false);
316  /*enable_extra_copy=*/true);
318  /*enable_extra_copy=*/false);
320  /*enable_extra_copy=*/true);
321  grpc_shutdown();
322  return 0;
323 }
tsi_zero_copy_grpc_protector_unprotect
tsi_result tsi_zero_copy_grpc_protector_unprotect(tsi_zero_copy_grpc_protector *self, grpc_slice_buffer *protected_slices, grpc_slice_buffer *unprotected_slices, int *min_progress_size)
Definition: transport_security_grpc.cc:52
log.h
gsec_test_bias_random_uint32
uint32_t gsec_test_bias_random_uint32(uint32_t max_length)
Definition: gsec_test_util.cc:43
generate.env
env
Definition: generate.py:37
tsi_zero_copy_grpc_protector_destroy
void tsi_zero_copy_grpc_protector_destroy(tsi_zero_copy_grpc_protector *self)
Definition: transport_security_grpc.cc:64
kSealRepeatTimes
constexpr size_t kSealRepeatTimes
Definition: alts_zero_copy_grpc_protector_test.cc:36
alts_zero_copy_protector_seal_unseal_large_buffer_tests
static void alts_zero_copy_protector_seal_unseal_large_buffer_tests(bool enable_extra_copy)
Definition: alts_zero_copy_grpc_protector_test.cc:282
alts_zero_copy_grpc_protector_test_var::original_sb
grpc_slice_buffer original_sb
Definition: alts_zero_copy_grpc_protector_test.cc:50
tsi_zero_copy_grpc_protector_protect
tsi_result tsi_zero_copy_grpc_protector_protect(tsi_zero_copy_grpc_protector *self, grpc_slice_buffer *unprotected_slices, grpc_slice_buffer *protected_slices)
Definition: transport_security_grpc.cc:41
pointer_to_nth_byte
static uint8_t * pointer_to_nth_byte(grpc_slice_buffer *sb, size_t index)
Definition: alts_zero_copy_grpc_protector_test.cc:71
grpc_slice_buffer::slices
grpc_slice * slices
Definition: include/grpc/impl/codegen/slice.h:89
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
GRPC_SLICE_MALLOC
#define GRPC_SLICE_MALLOC(len)
Definition: include/grpc/slice.h:70
alts_zero_copy_grpc_protector_test_fixture::client
tsi_zero_copy_grpc_protector * client
Definition: alts_zero_copy_grpc_protector_test.cc:44
gsec_test_random_array
void gsec_test_random_array(uint8_t **bytes, size_t length)
Definition: gsec_test_util.cc:33
alts_zero_copy_grpc_protector_test_var_destroy
static void alts_zero_copy_grpc_protector_test_var_destroy(alts_zero_copy_grpc_protector_test_var *var)
Definition: alts_zero_copy_grpc_protector_test.cc:161
second
StrT second
Definition: cxa_demangle.cpp:4885
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
alts_zero_copy_grpc_protector_test_fixture
Definition: alts_zero_copy_grpc_protector_test.cc:43
alts_iovec_record_protocol.h
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
TSI_OK
@ TSI_OK
Definition: transport_security_interface.h:32
kSmallBufferSize
constexpr size_t kSmallBufferSize
Definition: alts_zero_copy_grpc_protector_test.cc:37
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
main
int main(int argc, char **argv)
Definition: alts_zero_copy_grpc_protector_test.cc:310
kChannelMinSize
constexpr size_t kChannelMinSize
Definition: alts_zero_copy_grpc_protector_test.cc:40
alts_zero_copy_grpc_protector_test_var::protected_sb
grpc_slice_buffer protected_sb
Definition: alts_zero_copy_grpc_protector_test.cc:53
fixture
static const char fixture[]
Definition: test-fs-copyfile.c:36
kAes128GcmRekeyKeyLength
const size_t kAes128GcmRekeyKeyLength
Definition: gsec.h:54
gsec.h
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
grpc_slice_buffer::count
size_t count
Definition: include/grpc/impl/codegen/slice.h:91
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
gsec_test_util.h
are_slice_buffers_equal
static bool are_slice_buffers_equal(grpc_slice_buffer *first, grpc_slice_buffer *second)
Definition: alts_zero_copy_grpc_protector_test.cc:86
alts_zero_copy_grpc_protector_test_var::unprotected_sb
grpc_slice_buffer unprotected_sb
Definition: alts_zero_copy_grpc_protector_test.cc:54
grpc_slice_buffer::length
size_t length
Definition: include/grpc/impl/codegen/slice.h:96
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
slice_buffer.h
alts_zero_copy_grpc_protector_test_fixture::server
tsi_zero_copy_grpc_protector * server
Definition: alts_zero_copy_grpc_protector_test.cc:45
seal_unseal_small_buffer
static void seal_unseal_small_buffer(tsi_zero_copy_grpc_protector *sender, tsi_zero_copy_grpc_protector *receiver)
Definition: alts_zero_copy_grpc_protector_test.cc:176
alts_zero_copy_grpc_protector_create
tsi_result alts_zero_copy_grpc_protector_create(const uint8_t *key, size_t key_size, bool is_rekey, bool is_client, bool is_integrity_only, bool enable_extra_copy, size_t *max_protected_frame_size, tsi_zero_copy_grpc_protector **protector)
Definition: alts_zero_copy_grpc_protector.cc:260
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
create_random_slice_buffer
static void create_random_slice_buffer(grpc_slice_buffer *sb, grpc_slice_buffer *dup_sb, size_t length)
Definition: alts_zero_copy_grpc_protector_test.cc:59
slice_internal.h
kLargeBufferSize
constexpr size_t kLargeBufferSize
Definition: alts_zero_copy_grpc_protector_test.cc:38
alts_zero_copy_grpc_protector_test_var_create
static alts_zero_copy_grpc_protector_test_var * alts_zero_copy_grpc_protector_test_var_create()
Definition: alts_zero_copy_grpc_protector_test.cc:149
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_slice_buffer_init
GPRAPI void grpc_slice_buffer_init(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:116
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
alts_zero_copy_grpc_protector.h
alts_zero_copy_grpc_protector_test_var
Definition: alts_zero_copy_grpc_protector_test.cc:49
test_config.h
grpc_slice_buffer_add
GPRAPI void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:170
key
const char * key
Definition: hpack_parser_table.cc:164
seal_unseal_large_buffer
static void seal_unseal_large_buffer(tsi_zero_copy_grpc_protector *sender, tsi_zero_copy_grpc_protector *receiver)
Definition: alts_zero_copy_grpc_protector_test.cc:218
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
alloc.h
first
StrT first
Definition: cxa_demangle.cpp:4884
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
exec_ctx.h
alts_zero_copy_grpc_protector_test_var::duplicate_sb
grpc_slice_buffer duplicate_sb
Definition: alts_zero_copy_grpc_protector_test.cc:51
grpc_slice_buffer_move_first
GPRAPI void grpc_slice_buffer_move_first(grpc_slice_buffer *src, size_t n, grpc_slice_buffer *dst)
Definition: slice/slice_buffer.cc:348
alts_zero_copy_grpc_protector_test_var::staging_sb
grpc_slice_buffer staging_sb
Definition: alts_zero_copy_grpc_protector_test.cc:52
alts_zero_copy_grpc_protector_test_fixture_create
static alts_zero_copy_grpc_protector_test_fixture * alts_zero_copy_grpc_protector_test_fixture_create(bool rekey, bool integrity_only, bool enable_extra_copy)
Definition: alts_zero_copy_grpc_protector_test.cc:105
grpc_slice_buffer_destroy_internal
void grpc_slice_buffer_destroy_internal(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:123
tsi_zero_copy_grpc_protector_max_frame_size
tsi_result tsi_zero_copy_grpc_protector_max_frame_size(tsi_zero_copy_grpc_protector *self, size_t *max_frame_size)
Definition: transport_security_grpc.cc:69
gsec_test_random_bytes
void gsec_test_random_bytes(uint8_t *bytes, size_t length)
Definition: gsec_test_util.cc:25
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
tsi_zero_copy_grpc_protector
Definition: transport_security_grpc.h:79
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
alts_zero_copy_protector_seal_unseal_small_buffer_tests
static void alts_zero_copy_protector_seal_unseal_small_buffer_tests(bool enable_extra_copy)
Definition: alts_zero_copy_grpc_protector_test.cc:254
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
kZeroCopyFrameLengthFieldSize
constexpr size_t kZeroCopyFrameLengthFieldSize
Definition: alts_iovec_record_protocol.h:29
kAes128GcmKeyLength
const size_t kAes128GcmKeyLength
Definition: gsec.h:49
kChannelMaxSize
constexpr size_t kChannelMaxSize
Definition: alts_zero_copy_grpc_protector_test.cc:39
alts_zero_copy_grpc_protector_test_fixture_destroy
static void alts_zero_copy_grpc_protector_test_fixture_destroy(alts_zero_copy_grpc_protector_test_fixture *fixture)
Definition: alts_zero_copy_grpc_protector_test.cc:136
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_slice_buffer_reset_and_unref_internal
void grpc_slice_buffer_reset_and_unref_internal(grpc_slice_buffer *sb)
Definition: slice/slice_buffer.cc:238
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
transport_security_grpc.h
grpc_slice_ref
GPRAPI grpc_slice grpc_slice_ref(grpc_slice s)
Definition: slice_api.cc:27


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