Debian Codegraph @

test/core/compression/message_compress_test.cc

raw
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
19 #include "src/core/lib/compression/message_compress.h"
20
21 #include <inttypes.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "gtest/gtest.h"
26
27 #include <grpc/compression.h>
28 #include <grpc/slice_buffer.h>
29 #include <grpc/support/log.h>
30
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "test/core/util/slice_splitter.h"
34 #include "test/core/util/test_config.h"
35
36 typedef enum { ONE_A = 0, ONE_KB_A, ONE_MB_A, TEST_VALUE_COUNT } test_value;
37
38 typedef enum {
39 SHOULD_NOT_COMPRESS,
40 SHOULD_COMPRESS,
41 MAYBE_COMPRESSES
42 } compressability;
43
44 static void assert_passthrough(grpc_slice value,
45 grpc_compression_algorithm algorithm,
46 grpc_slice_split_mode uncompressed_split_mode,
47 grpc_slice_split_mode compressed_split_mode,
48 compressability compress_result_check) {
49 grpc_slice_buffer input;
50 grpc_slice_buffer compressed_raw;
51 grpc_slice_buffer compressed;
52 grpc_slice_buffer output;
53 grpc_slice final;
54 int was_compressed;
55 const char* algorithm_name;
56
57 ASSERT_NE(grpc_compression_algorithm_name(algorithm, &algorithm_name), 0);
58 gpr_log(GPR_INFO,
59 "assert_passthrough: value_length=%" PRIuPTR
60 " algorithm='%s' uncompressed_split='%s' compressed_split='%s'",
61 GRPC_SLICE_LENGTH(value), algorithm_name,
62 grpc_slice_split_mode_name(uncompressed_split_mode),
63 grpc_slice_split_mode_name(compressed_split_mode));
64
65 grpc_slice_buffer_init(&input);
66 grpc_slice_buffer_init(&compressed_raw);
67 grpc_slice_buffer_init(&compressed);
68 grpc_slice_buffer_init(&output);
69
70 grpc_split_slices_to_buffer(uncompressed_split_mode, &value, 1, &input);
71
72 {
73 grpc_core::ExecCtx exec_ctx;
74 was_compressed = grpc_msg_compress(algorithm, &input, &compressed_raw);
75 }
76 ASSERT_GT(input.count, 0);
77
78 switch (compress_result_check) {
79 case SHOULD_NOT_COMPRESS:
80 ASSERT_EQ(was_compressed, 0);
81 break;
82 case SHOULD_COMPRESS:
83 ASSERT_EQ(was_compressed, 1);
84 break;
85 case MAYBE_COMPRESSES:
86 /* no check */
87 break;
88 }
89
90 grpc_split_slice_buffer(compressed_split_mode, &compressed_raw, &compressed);
91
92 {
93 grpc_core::ExecCtx exec_ctx;
94 ASSERT_TRUE(grpc_msg_decompress(
95 was_compressed ? algorithm : GRPC_COMPRESS_NONE, &compressed, &output));
96 }
97
98 final = grpc_slice_merge(output.slices, output.count);
99 ASSERT_TRUE(grpc_slice_eq(value, final));
100
101 grpc_slice_buffer_destroy(&input);
102 grpc_slice_buffer_destroy(&compressed);
103 grpc_slice_buffer_destroy(&compressed_raw);
104 grpc_slice_buffer_destroy(&output);
105 grpc_slice_unref(final);
106 }
107
108 static grpc_slice repeated(char c, size_t length) {
109 grpc_slice out = grpc_slice_malloc(length);
110 memset(GRPC_SLICE_START_PTR(out), c, length);
111 return out;
112 }
113
114 static compressability get_compressability(
115 test_value id, grpc_compression_algorithm algorithm) {
116 if (algorithm == GRPC_COMPRESS_NONE) return SHOULD_NOT_COMPRESS;
117 switch (id) {
118 case ONE_A:
119 return SHOULD_NOT_COMPRESS;
120 case ONE_KB_A:
121 case ONE_MB_A:
122 return SHOULD_COMPRESS;
123 case TEST_VALUE_COUNT:
124 abort();
125 }
126 return MAYBE_COMPRESSES;
127 }
128
129 static grpc_slice create_test_value(test_value id) {
130 switch (id) {
131 case ONE_A:
132 return grpc_slice_from_copied_string("a");
133 case ONE_KB_A:
134 return repeated('a', 1024);
135 case ONE_MB_A:
136 return repeated('a', 1024 * 1024);
137 case TEST_VALUE_COUNT:
138 abort();
139 }
140 return grpc_slice_from_copied_string("bad value");
141 }
142
143 TEST(MessageCompressTest, TinyDataCompress) {
144 grpc_slice_buffer input;
145 grpc_slice_buffer output;
146
147 grpc_slice_buffer_init(&input);
148 grpc_slice_buffer_init(&output);
149 grpc_slice_buffer_add(&input, create_test_value(ONE_A));
150
151 for (int i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
152 if (i == GRPC_COMPRESS_NONE) continue;
153 grpc_core::ExecCtx exec_ctx;
154 ASSERT_EQ(0, grpc_msg_compress(static_cast<grpc_compression_algorithm>(i),
155 &input, &output));
156 ASSERT_EQ(1, output.count);
157 }
158
159 grpc_slice_buffer_destroy(&input);
160 grpc_slice_buffer_destroy(&output);
161 }
162
163 TEST(MessageCompressTest, BadDecompressionDataCrc) {
164 grpc_slice_buffer input;
165 grpc_slice_buffer corrupted;
166 grpc_slice_buffer output;
167 size_t idx;
168 const uint32_t bad = 0xdeadbeef;
169
170 grpc_slice_buffer_init(&input);
171 grpc_slice_buffer_init(&corrupted);
172 grpc_slice_buffer_init(&output);
173 grpc_slice_buffer_add(&input, create_test_value(ONE_MB_A));
174
175 grpc_core::ExecCtx exec_ctx;
176 /* compress it */
177 grpc_msg_compress(GRPC_COMPRESS_GZIP, &input, &corrupted);
178 /* corrupt the output by smashing the CRC */
179 ASSERT_GT(corrupted.count, 1);
180 ASSERT_GT(GRPC_SLICE_LENGTH(corrupted.slices[1]), 8);
181 idx = GRPC_SLICE_LENGTH(corrupted.slices[1]) - 8;
182 memcpy(GRPC_SLICE_START_PTR(corrupted.slices[1]) + idx, &bad, 4);
183
184 /* try (and fail) to decompress the corrupted compresed buffer */
185 ASSERT_EQ(0, grpc_msg_decompress(GRPC_COMPRESS_GZIP, &corrupted, &output));
186
187 grpc_slice_buffer_destroy(&input);
188 grpc_slice_buffer_destroy(&corrupted);
189 grpc_slice_buffer_destroy(&output);
190 }
191
192 TEST(MessageCompressTest, BadDecompressionDataMissingTrailer) {
193 grpc_slice_buffer input;
194 grpc_slice_buffer decompressed;
195 grpc_slice_buffer garbage;
196 grpc_slice_buffer output;
197
198 grpc_slice_buffer_init(&input);
199 grpc_slice_buffer_init(&decompressed);
200 grpc_slice_buffer_init(&garbage);
201 grpc_slice_buffer_init(&output);
202 grpc_slice_buffer_add(&input, create_test_value(ONE_MB_A));
203
204 grpc_core::ExecCtx exec_ctx;
205 /* compress it */
206 grpc_msg_compress(GRPC_COMPRESS_GZIP, &input, &decompressed);
207 ASSERT_GT(decompressed.length, 8);
208 /* Remove the footer from the decompressed message */
209 grpc_slice_buffer_trim_end(&decompressed, 8, &garbage);
210 /* try (and fail) to decompress the compressed buffer without the footer */
211 ASSERT_EQ(0, grpc_msg_decompress(GRPC_COMPRESS_GZIP, &decompressed, &output));
212
213 grpc_slice_buffer_destroy(&input);
214 grpc_slice_buffer_destroy(&decompressed);
215 grpc_slice_buffer_destroy(&garbage);
216 grpc_slice_buffer_destroy(&output);
217 }
218
219 TEST(MessageCompressTest, BadDecompressionDataTrailingGarbage) {
220 grpc_slice_buffer input;
221 grpc_slice_buffer output;
222
223 grpc_slice_buffer_init(&input);
224 grpc_slice_buffer_init(&output);
225 /* append 0x99 to the end of an otherwise valid stream */
226 grpc_slice_buffer_add(
227 &input, grpc_slice_from_copied_buffer(
228 "\x78\xda\x63\x60\x60\x60\x00\x00\x00\x04\x00\x01\x99", 13));
229
230 /* try (and fail) to decompress the invalid compresed buffer */
231 grpc_core::ExecCtx exec_ctx;
232 ASSERT_EQ(0, grpc_msg_decompress(GRPC_COMPRESS_DEFLATE, &input, &output));
233
234 grpc_slice_buffer_destroy(&input);
235 grpc_slice_buffer_destroy(&output);
236 }
237
238 TEST(MessageCompressTest, BadDecompressionDataStream) {
239 grpc_slice_buffer input;
240 grpc_slice_buffer output;
241
242 grpc_slice_buffer_init(&input);
243 grpc_slice_buffer_init(&output);
244 grpc_slice_buffer_add(&input,
245 grpc_slice_from_copied_buffer("\x78\xda\xff\xff", 4));
246
247 /* try (and fail) to decompress the invalid compresed buffer */
248 grpc_core::ExecCtx exec_ctx;
249 ASSERT_EQ(0, grpc_msg_decompress(GRPC_COMPRESS_DEFLATE, &input, &output));
250
251 grpc_slice_buffer_destroy(&input);
252 grpc_slice_buffer_destroy(&output);
253 }
254
255 TEST(MessageCompressTest, BadCompressionAlgorithm) {
256 grpc_slice_buffer input;
257 grpc_slice_buffer output;
258 int was_compressed;
259
260 grpc_slice_buffer_init(&input);
261 grpc_slice_buffer_init(&output);
262 grpc_slice_buffer_add(
263 &input, grpc_slice_from_copied_string("Never gonna give you up"));
264
265 grpc_core::ExecCtx exec_ctx;
266 was_compressed =
267 grpc_msg_compress(GRPC_COMPRESS_ALGORITHMS_COUNT, &input, &output);
268 ASSERT_EQ(0, was_compressed);
269
270 was_compressed = grpc_msg_compress(static_cast<grpc_compression_algorithm>(
271 GRPC_COMPRESS_ALGORITHMS_COUNT + 123),
272 &input, &output);
273 ASSERT_EQ(0, was_compressed);
274
275 grpc_slice_buffer_destroy(&input);
276 grpc_slice_buffer_destroy(&output);
277 }
278
279 TEST(MessageCompressTest, BadDecompressionAlgorithm) {
280 grpc_slice_buffer input;
281 grpc_slice_buffer output;
282 int was_decompressed;
283
284 grpc_slice_buffer_init(&input);
285 grpc_slice_buffer_init(&output);
286 grpc_slice_buffer_add(&input,
287 grpc_slice_from_copied_string(
288 "I'm not really compressed but it doesn't matter"));
289 grpc_core::ExecCtx exec_ctx;
290 was_decompressed =
291 grpc_msg_decompress(GRPC_COMPRESS_ALGORITHMS_COUNT, &input, &output);
292 ASSERT_EQ(0, was_decompressed);
293
294 was_decompressed =
295 grpc_msg_decompress(static_cast<grpc_compression_algorithm>(
296 GRPC_COMPRESS_ALGORITHMS_COUNT + 123),
297 &input, &output);
298 ASSERT_EQ(0, was_decompressed);
299
300 grpc_slice_buffer_destroy(&input);
301 grpc_slice_buffer_destroy(&output);
302 }
303
304 int main(int argc, char** argv) {
305 grpc::testing::TestEnvironment env(&argc, argv);
306 ::testing::InitGoogleTest(&argc, argv);
307 grpc::testing::TestGrpcScope grpc_scope;
308
309 unsigned i, j, k, m;
310 grpc_slice_split_mode uncompressed_split_modes[] = {
311 GRPC_SLICE_SPLIT_IDENTITY, GRPC_SLICE_SPLIT_ONE_BYTE};
312 grpc_slice_split_mode compressed_split_modes[] = {GRPC_SLICE_SPLIT_MERGE_ALL,
313 GRPC_SLICE_SPLIT_IDENTITY,
314 GRPC_SLICE_SPLIT_ONE_BYTE};
315 for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
316 for (j = 0; j < GPR_ARRAY_SIZE(uncompressed_split_modes); j++) {
317 for (k = 0; k < GPR_ARRAY_SIZE(compressed_split_modes); k++) {
318 for (m = 0; m < TEST_VALUE_COUNT; m++) {
319 grpc_slice slice = create_test_value(static_cast<test_value>(m));
320 assert_passthrough(
321 slice, static_cast<grpc_compression_algorithm>(i),
322 static_cast<grpc_slice_split_mode>(j),
323 static_cast<grpc_slice_split_mode>(k),
324 get_compressability(static_cast<test_value>(m),
325 static_cast<grpc_compression_algorithm>(i)));
326 grpc_slice_unref(slice);
327 }
328 }
329 }
330 }
331
332 return RUN_ALL_TESTS();
333 }