Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/compression/message_compress.h"
35 :
36 : #include <stdlib.h>
37 : #include <string.h>
38 :
39 : #include "test/core/util/test_config.h"
40 : #include "src/core/support/murmur_hash.h"
41 : #include <grpc/support/log.h>
42 : #include <grpc/support/useful.h>
43 : #include "test/core/util/slice_splitter.h"
44 :
45 : typedef enum { ONE_A = 0, ONE_KB_A, ONE_MB_A, TEST_VALUE_COUNT } test_value;
46 :
47 : typedef enum {
48 : SHOULD_NOT_COMPRESS,
49 : SHOULD_COMPRESS,
50 : MAYBE_COMPRESSES
51 : } compressability;
52 :
53 54 : static void assert_passthrough(gpr_slice value,
54 : grpc_compression_algorithm algorithm,
55 : grpc_slice_split_mode uncompressed_split_mode,
56 : grpc_slice_split_mode compressed_split_mode,
57 : compressability compress_result_check) {
58 : gpr_slice_buffer input;
59 : gpr_slice_buffer compressed_raw;
60 : gpr_slice_buffer compressed;
61 : gpr_slice_buffer output;
62 : gpr_slice final;
63 : int was_compressed;
64 : char *algorithm_name;
65 :
66 54 : GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algorithm_name) != 0);
67 252 : gpr_log(GPR_INFO,
68 : "assert_passthrough: value_length=%d value_hash=0x%08x "
69 : "algorithm='%s' uncompressed_split='%s' compressed_split='%s'",
70 126 : GPR_SLICE_LENGTH(value), gpr_murmur_hash3(GPR_SLICE_START_PTR(value),
71 72 : GPR_SLICE_LENGTH(value), 0),
72 : algorithm_name, grpc_slice_split_mode_name(uncompressed_split_mode),
73 : grpc_slice_split_mode_name(compressed_split_mode));
74 :
75 54 : gpr_slice_buffer_init(&input);
76 54 : gpr_slice_buffer_init(&compressed_raw);
77 54 : gpr_slice_buffer_init(&compressed);
78 54 : gpr_slice_buffer_init(&output);
79 :
80 54 : grpc_split_slices_to_buffer(uncompressed_split_mode, &value, 1, &input);
81 :
82 54 : was_compressed = grpc_msg_compress(algorithm, &input, &compressed_raw);
83 54 : GPR_ASSERT(input.count > 0);
84 :
85 54 : switch (compress_result_check) {
86 : case SHOULD_NOT_COMPRESS:
87 30 : GPR_ASSERT(was_compressed == 0);
88 30 : break;
89 : case SHOULD_COMPRESS:
90 24 : GPR_ASSERT(was_compressed == 1);
91 24 : break;
92 : case MAYBE_COMPRESSES:
93 : /* no check */
94 0 : break;
95 : }
96 :
97 54 : grpc_split_slice_buffer(compressed_split_mode, &compressed_raw, &compressed);
98 :
99 54 : GPR_ASSERT(grpc_msg_decompress(
100 : was_compressed ? algorithm : GRPC_COMPRESS_NONE, &compressed, &output));
101 :
102 54 : final = grpc_slice_merge(output.slices, output.count);
103 54 : GPR_ASSERT(0 == gpr_slice_cmp(value, final));
104 :
105 54 : gpr_slice_buffer_destroy(&input);
106 54 : gpr_slice_buffer_destroy(&compressed);
107 54 : gpr_slice_buffer_destroy(&compressed_raw);
108 54 : gpr_slice_buffer_destroy(&output);
109 54 : gpr_slice_unref(final);
110 54 : }
111 :
112 36 : static gpr_slice repeated(char c, size_t length) {
113 36 : gpr_slice out = gpr_slice_malloc(length);
114 36 : memset(GPR_SLICE_START_PTR(out), c, length);
115 36 : return out;
116 : }
117 :
118 54 : static compressability get_compressability(
119 : test_value id, grpc_compression_algorithm algorithm) {
120 54 : if (algorithm == GRPC_COMPRESS_NONE) return SHOULD_NOT_COMPRESS;
121 36 : switch (id) {
122 : case ONE_A:
123 12 : return SHOULD_NOT_COMPRESS;
124 : case ONE_KB_A:
125 : case ONE_MB_A:
126 24 : return SHOULD_COMPRESS;
127 : case TEST_VALUE_COUNT:
128 0 : abort();
129 : break;
130 : }
131 0 : return MAYBE_COMPRESSES;
132 : }
133 :
134 54 : static gpr_slice create_test_value(test_value id) {
135 54 : switch (id) {
136 : case ONE_A:
137 18 : return gpr_slice_from_copied_string("a");
138 : case ONE_KB_A:
139 18 : return repeated('a', 1024);
140 : case ONE_MB_A:
141 18 : return repeated('a', 1024 * 1024);
142 : case TEST_VALUE_COUNT:
143 0 : abort();
144 : break;
145 : }
146 0 : return gpr_slice_from_copied_string("bad value");
147 : }
148 :
149 1 : static void test_bad_data(void) {
150 : gpr_slice_buffer input;
151 : gpr_slice_buffer output;
152 : grpc_compression_algorithm i;
153 :
154 1 : gpr_slice_buffer_init(&input);
155 1 : gpr_slice_buffer_init(&output);
156 1 : gpr_slice_buffer_add(&input, gpr_slice_from_copied_string(
157 : "this is not valid compressed input"));
158 :
159 4 : for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
160 3 : if (i == GRPC_COMPRESS_NONE) continue;
161 2 : GPR_ASSERT(0 == grpc_msg_decompress(i, &input, &output));
162 2 : GPR_ASSERT(0 == output.count);
163 : }
164 :
165 1 : gpr_slice_buffer_destroy(&input);
166 1 : gpr_slice_buffer_destroy(&output);
167 1 : }
168 :
169 1 : int main(int argc, char **argv) {
170 : unsigned i, j, k, m;
171 1 : grpc_slice_split_mode uncompressed_split_modes[] = {
172 : GRPC_SLICE_SPLIT_IDENTITY, GRPC_SLICE_SPLIT_ONE_BYTE};
173 1 : grpc_slice_split_mode compressed_split_modes[] = {GRPC_SLICE_SPLIT_MERGE_ALL,
174 : GRPC_SLICE_SPLIT_IDENTITY,
175 : GRPC_SLICE_SPLIT_ONE_BYTE};
176 :
177 1 : grpc_test_init(argc, argv);
178 :
179 4 : for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
180 9 : for (j = 0; j < GPR_ARRAY_SIZE(uncompressed_split_modes); j++) {
181 24 : for (k = 0; k < GPR_ARRAY_SIZE(compressed_split_modes); k++) {
182 72 : for (m = 0; m < TEST_VALUE_COUNT; m++) {
183 54 : gpr_slice slice = create_test_value(m);
184 54 : assert_passthrough(slice, i, j, k, get_compressability(m, i));
185 54 : gpr_slice_unref(slice);
186 : }
187 : }
188 : }
189 : }
190 :
191 1 : test_bad_data();
192 :
193 1 : return 0;
194 : }
|