message_compress_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 <stdlib.h>
22 #include <string.h>
23 
24 #include <grpc/compression.h>
25 #include <grpc/grpc.h>
26 #include <grpc/support/log.h>
27 
33 
35 
36 typedef enum {
41 
44  grpc_slice_split_mode uncompressed_split_mode,
45  grpc_slice_split_mode compressed_split_mode,
46  compressability compress_result_check) {
48  grpc_slice_buffer compressed_raw;
49  grpc_slice_buffer compressed;
51  grpc_slice final;
52  int was_compressed;
53  const char* algorithm_name;
54 
55  GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algorithm_name) != 0);
57  "assert_passthrough: value_length=%" PRIuPTR
58  " value_hash=0x%08x "
59  "algorithm='%s' uncompressed_split='%s' compressed_split='%s'",
63  algorithm_name, grpc_slice_split_mode_name(uncompressed_split_mode),
64  grpc_slice_split_mode_name(compressed_split_mode));
65 
67  grpc_slice_buffer_init(&compressed_raw);
68  grpc_slice_buffer_init(&compressed);
70 
71  grpc_split_slices_to_buffer(uncompressed_split_mode, &value, 1, &input);
72 
73  {
75  was_compressed = grpc_msg_compress(algorithm, &input, &compressed_raw);
76  }
77  GPR_ASSERT(input.count > 0);
78 
79  switch (compress_result_check) {
81  GPR_ASSERT(was_compressed == 0);
82  break;
83  case SHOULD_COMPRESS:
84  GPR_ASSERT(was_compressed == 1);
85  break;
86  case MAYBE_COMPRESSES:
87  /* no check */
88  break;
89  }
90 
91  grpc_split_slice_buffer(compressed_split_mode, &compressed_raw, &compressed);
92 
93  {
96  was_compressed ? algorithm : GRPC_COMPRESS_NONE, &compressed, &output));
97  }
98 
99  final = grpc_slice_merge(output.slices, output.count);
100  GPR_ASSERT(grpc_slice_eq(value, final));
101 
103  grpc_slice_buffer_destroy(&compressed);
104  grpc_slice_buffer_destroy(&compressed_raw);
106  grpc_slice_unref(final);
107 }
108 
109 static grpc_slice repeated(char c, size_t length) {
112  return out;
113 }
114 
116  test_value id, grpc_compression_algorithm algorithm) {
117  if (algorithm == GRPC_COMPRESS_NONE) return SHOULD_NOT_COMPRESS;
118  switch (id) {
119  case ONE_A:
120  return SHOULD_NOT_COMPRESS;
121  case ONE_KB_A:
122  case ONE_MB_A:
123  return SHOULD_COMPRESS;
124  case TEST_VALUE_COUNT:
125  abort();
126  }
127  return MAYBE_COMPRESSES;
128 }
129 
131  switch (id) {
132  case ONE_A:
133  return grpc_slice_from_copied_string("a");
134  case ONE_KB_A:
135  return repeated('a', 1024);
136  case ONE_MB_A:
137  return repeated('a', 1024 * 1024);
138  case TEST_VALUE_COUNT:
139  abort();
140  }
141  return grpc_slice_from_copied_string("bad value");
142 }
143 
144 static void test_tiny_data_compress(void) {
147 
151 
152  for (int i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
153  if (i == GRPC_COMPRESS_NONE) continue;
155  GPR_ASSERT(0 ==
157  &input, &output));
158  GPR_ASSERT(1 == output.count);
159  }
160 
163 }
164 
167  grpc_slice_buffer corrupted;
169  size_t idx;
170  const uint32_t bad = 0xdeadbeef;
171 
173  grpc_slice_buffer_init(&corrupted);
176 
178  /* compress it */
180  /* corrupt the output by smashing the CRC */
181  GPR_ASSERT(corrupted.count > 1);
182  GPR_ASSERT(GRPC_SLICE_LENGTH(corrupted.slices[1]) > 8);
183  idx = GRPC_SLICE_LENGTH(corrupted.slices[1]) - 8;
184  memcpy(GRPC_SLICE_START_PTR(corrupted.slices[1]) + idx, &bad, 4);
185 
186  /* try (and fail) to decompress the corrupted compresed buffer */
188 
190  grpc_slice_buffer_destroy(&corrupted);
192 }
193 
196  grpc_slice_buffer decompressed;
197  grpc_slice_buffer garbage;
199 
201  grpc_slice_buffer_init(&decompressed);
202  grpc_slice_buffer_init(&garbage);
205 
207  /* compress it */
208  grpc_msg_compress(GRPC_COMPRESS_GZIP, &input, &decompressed);
209  GPR_ASSERT(decompressed.length > 8);
210  /* Remove the footer from the decompressed message */
211  grpc_slice_buffer_trim_end(&decompressed, 8, &garbage);
212  /* try (and fail) to decompress the compressed buffer without the footer */
213  GPR_ASSERT(0 ==
214  grpc_msg_decompress(GRPC_COMPRESS_GZIP, &decompressed, &output));
215 
217  grpc_slice_buffer_destroy(&decompressed);
218  grpc_slice_buffer_destroy(&garbage);
220 }
221 
225 
228  /* append 0x99 to the end of an otherwise valid stream */
231  "\x78\xda\x63\x60\x60\x60\x00\x00\x00\x04\x00\x01\x99", 13));
232 
233  /* try (and fail) to decompress the invalid compresed buffer */
236 
239 }
240 
244 
248  grpc_slice_from_copied_buffer("\x78\xda\xff\xff", 4));
249 
250  /* try (and fail) to decompress the invalid compresed buffer */
253 
256 }
257 
261  int was_compressed;
262 
266  &input, grpc_slice_from_copied_string("Never gonna give you up"));
267 
269  was_compressed =
271  GPR_ASSERT(0 == was_compressed);
272 
273  was_compressed = grpc_msg_compress(static_cast<grpc_compression_algorithm>(
275  &input, &output);
276  GPR_ASSERT(0 == was_compressed);
277 
280 }
281 
285  int was_decompressed;
286 
291  "I'm not really compressed but it doesn't matter"));
293  was_decompressed =
295  GPR_ASSERT(0 == was_decompressed);
296 
297  was_decompressed =
300  &input, &output);
301  GPR_ASSERT(0 == was_decompressed);
302 
305 }
306 
307 int main(int argc, char** argv) {
308  unsigned i, j, k, m;
309  grpc_slice_split_mode uncompressed_split_modes[] = {
311  grpc_slice_split_mode compressed_split_modes[] = {GRPC_SLICE_SPLIT_MERGE_ALL,
314 
315  grpc::testing::TestEnvironment env(&argc, argv);
316  grpc_init();
317 
318  for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
319  for (j = 0; j < GPR_ARRAY_SIZE(uncompressed_split_modes); j++) {
320  for (k = 0; k < GPR_ARRAY_SIZE(compressed_split_modes); k++) {
321  for (m = 0; m < TEST_VALUE_COUNT; m++) {
322  grpc_slice slice = create_test_value(static_cast<test_value>(m));
324  slice, static_cast<grpc_compression_algorithm>(i),
325  static_cast<grpc_slice_split_mode>(j),
326  static_cast<grpc_slice_split_mode>(k),
327  get_compressability(static_cast<test_value>(m),
328  static_cast<grpc_compression_algorithm>(i)));
330  }
331  }
332  }
333  }
334 
342  grpc_shutdown();
343 
344  return 0;
345 }
grpc_slice_split_mode_name
const char * grpc_slice_split_mode_name(grpc_slice_split_mode mode)
Definition: slice_splitter.cc:29
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
compression.h
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
GRPC_COMPRESS_DEFLATE
@ GRPC_COMPRESS_DEFLATE
Definition: compression_types.h:62
check_banned_filenames.bad
bad
Definition: check_banned_filenames.py:26
grpc_slice_buffer_destroy
GPRAPI void grpc_slice_buffer_destroy(grpc_slice_buffer *sb)
Definition: slice_buffer_api.cc:27
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
log.h
GRPC_SLICE_SPLIT_IDENTITY
@ GRPC_SLICE_SPLIT_IDENTITY
Definition: slice_splitter.h:32
generate.env
env
Definition: generate.py:37
memset
return memset(p, 0, total)
grpc_slice_from_copied_string
GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source)
Definition: slice/slice.cc:177
SHOULD_COMPRESS
@ SHOULD_COMPRESS
Definition: message_compress_test.cc:38
test_bad_decompression_data_stream
static void test_bad_decompression_data_stream(void)
Definition: message_compress_test.cc:241
ONE_KB_A
@ ONE_KB_A
Definition: message_compress_test.cc:34
assert_passthrough
static void assert_passthrough(grpc_slice value, grpc_compression_algorithm algorithm, grpc_slice_split_mode uncompressed_split_mode, grpc_slice_split_mode compressed_split_mode, compressability compress_result_check)
Definition: message_compress_test.cc:42
string.h
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
grpc_msg_compress
int grpc_msg_compress(grpc_compression_algorithm algorithm, grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:171
grpc_slice_split_mode
grpc_slice_split_mode
Definition: slice_splitter.h:28
grpc_slice_buffer::slices
grpc_slice * slices
Definition: include/grpc/impl/codegen/slice.h:89
useful.h
ONE_A
@ ONE_A
Definition: message_compress_test.cc:34
get_compressability
static compressability get_compressability(test_value id, grpc_compression_algorithm algorithm)
Definition: message_compress_test.cc:115
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
GRPC_COMPRESS_NONE
@ GRPC_COMPRESS_NONE
Definition: compression_types.h:61
grpc_slice_malloc
GPRAPI grpc_slice grpc_slice_malloc(size_t length)
Definition: slice/slice.cc:227
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
GRPC_SLICE_SPLIT_MERGE_ALL
@ GRPC_SLICE_SPLIT_MERGE_ALL
Definition: slice_splitter.h:30
slice_splitter.h
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
test_tiny_data_compress
static void test_tiny_data_compress(void)
Definition: message_compress_test.cc:144
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc_compression_algorithm_name
GRPCAPI int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, const char **name)
Definition: compression.cc:56
grpc_slice_buffer::count
size_t count
Definition: include/grpc/impl/codegen/slice.h:91
grpc_split_slices_to_buffer
void grpc_split_slices_to_buffer(grpc_slice_split_mode mode, grpc_slice *src_slices, size_t src_slice_count, grpc_slice_buffer *dst)
Definition: slice_splitter.cc:92
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
test_bad_decompression_algorithm
static void test_bad_decompression_algorithm(void)
Definition: message_compress_test.cc:282
TEST_VALUE_COUNT
@ TEST_VALUE_COUNT
Definition: message_compress_test.cc:34
SHOULD_NOT_COMPRESS
@ SHOULD_NOT_COMPRESS
Definition: message_compress_test.cc:37
grpc.h
grpc_split_slice_buffer
void grpc_split_slice_buffer(grpc_slice_split_mode mode, grpc_slice_buffer *src, grpc_slice_buffer *dst)
Definition: slice_splitter.cc:106
test_value
test_value
Definition: message_compress_test.cc:34
grpc_slice_buffer::length
size_t length
Definition: include/grpc/impl/codegen/slice.h:96
create_test_value
static grpc_slice create_test_value(test_value id)
Definition: message_compress_test.cc:130
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
test_bad_decompression_data_missing_trailer
static void test_bad_decompression_data_missing_trailer(void)
Definition: message_compress_test.cc:194
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_slice_merge
grpc_slice grpc_slice_merge(grpc_slice *slices, size_t nslices)
Definition: slice_splitter.cc:111
grpc_msg_decompress
int grpc_msg_decompress(grpc_compression_algorithm algorithm, grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:180
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
setup.idx
idx
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:197
ONE_MB_A
@ ONE_MB_A
Definition: message_compress_test.cc:34
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
grpc_slice_buffer_add
GPRAPI void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:170
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
MAYBE_COMPRESSES
@ MAYBE_COMPRESSES
Definition: message_compress_test.cc:39
GRPC_COMPRESS_ALGORITHMS_COUNT
@ GRPC_COMPRESS_ALGORITHMS_COUNT
Definition: compression_types.h:65
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
GRPC_COMPRESS_GZIP
@ GRPC_COMPRESS_GZIP
Definition: compression_types.h:63
exec_ctx.h
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
grpc_slice_buffer_trim_end
GPRAPI void grpc_slice_buffer_trim_end(grpc_slice_buffer *sb, size_t n, grpc_slice_buffer *garbage)
Definition: slice/slice_buffer.cc:402
test_bad_decompression_data_crc
static void test_bad_decompression_data_crc(void)
Definition: message_compress_test.cc:165
compressability
compressability
Definition: message_compress_test.cc:36
murmur_hash.h
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
main
int main(int argc, char **argv)
Definition: message_compress_test.cc:307
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
message_compress.h
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
regress.m
m
Definition: regress/regress.py:25
gpr_murmur_hash3
uint32_t gpr_murmur_hash3(const void *key, size_t len, uint32_t seed)
Definition: murmur_hash.cc:36
GRPC_SLICE_SPLIT_ONE_BYTE
@ GRPC_SLICE_SPLIT_ONE_BYTE
Definition: slice_splitter.h:34
grpc_slice_eq
GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b)
Definition: slice/slice.cc:387
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
test_bad_compression_algorithm
static void test_bad_compression_algorithm(void)
Definition: message_compress_test.cc:258
repeated
static grpc_slice repeated(char c, size_t length)
Definition: message_compress_test.cc:109
test_bad_decompression_data_trailing_garbage
static void test_bad_decompression_data_trailing_garbage(void)
Definition: message_compress_test.cc:222
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:37