3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <grpc/compression.h>
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/compression/compression_args.h"
28 #include "src/core/lib/gpr/useful.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "test/core/util/test_config.h"
32 static void test_compression_algorithm_parse(void) {
34 const char* valid_names[] = {"identity", "gzip", "deflate", "stream/gzip"};
35 const grpc_compression_algorithm valid_algorithms[] = {
36 GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE,
37 GRPC_COMPRESS_STREAM_GZIP};
38 const char* invalid_names[] = {"gzip2", "foo", "", "2gzip"};
40 gpr_log(GPR_DEBUG, "test_compression_algorithm_parse");
42 for (i = 0; i < GPR_ARRAY_SIZE(valid_names); i++) {
43 const char* valid_name = valid_names[i];
44 grpc_compression_algorithm algorithm;
45 const int success = grpc_compression_algorithm_parse(
46 grpc_slice_from_static_string(valid_name), &algorithm);
47 GPR_ASSERT(success != 0);
48 GPR_ASSERT(algorithm == valid_algorithms[i]);
51 for (i = 0; i < GPR_ARRAY_SIZE(invalid_names); i++) {
52 const char* invalid_name = invalid_names[i];
53 grpc_compression_algorithm algorithm;
55 success = grpc_compression_algorithm_parse(
56 grpc_slice_from_static_string(invalid_name), &algorithm);
57 GPR_ASSERT(success == 0);
58 /* the value of "algorithm" is undefined upon failure */
62 static void test_compression_algorithm_name(void) {
66 const char* valid_names[] = {"identity", "gzip", "deflate", "stream/gzip"};
67 const grpc_compression_algorithm valid_algorithms[] = {
68 GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE,
69 GRPC_COMPRESS_STREAM_GZIP};
71 gpr_log(GPR_DEBUG, "test_compression_algorithm_name");
73 for (i = 0; i < GPR_ARRAY_SIZE(valid_algorithms); i++) {
74 success = grpc_compression_algorithm_name(valid_algorithms[i], &name);
75 GPR_ASSERT(success != 0);
76 GPR_ASSERT(strcmp(name, valid_names[i]) == 0);
80 grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, &name);
81 GPR_ASSERT(success == 0);
82 /* the value of "name" is undefined upon failure */
85 static void test_compression_algorithm_for_level(void) {
86 gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level");
89 /* accept only identity (aka none) */
90 uint32_t accepted_encodings = 0;
91 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
93 GPR_ASSERT(GRPC_COMPRESS_NONE ==
94 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
97 GPR_ASSERT(GRPC_COMPRESS_NONE ==
98 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
101 GPR_ASSERT(GRPC_COMPRESS_NONE ==
102 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
103 accepted_encodings));
105 GPR_ASSERT(GRPC_COMPRESS_NONE ==
106 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
107 accepted_encodings));
111 /* accept only gzip */
112 uint32_t accepted_encodings = 0;
113 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
114 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP);
116 GPR_ASSERT(GRPC_COMPRESS_NONE ==
117 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
118 accepted_encodings));
120 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
121 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
122 accepted_encodings));
124 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
125 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
126 accepted_encodings));
128 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
129 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
130 accepted_encodings));
134 /* accept only deflate */
135 uint32_t accepted_encodings = 0;
136 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
137 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
139 GPR_ASSERT(GRPC_COMPRESS_NONE ==
140 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
141 accepted_encodings));
143 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
144 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
145 accepted_encodings));
147 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
148 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
149 accepted_encodings));
151 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
152 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
153 accepted_encodings));
157 /* accept gzip and deflate */
158 uint32_t accepted_encodings = 0;
159 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
160 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP);
161 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
163 GPR_ASSERT(GRPC_COMPRESS_NONE ==
164 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
165 accepted_encodings));
167 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
168 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
169 accepted_encodings));
171 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
172 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
173 accepted_encodings));
175 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
176 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
177 accepted_encodings));
181 /* accept stream gzip */
182 uint32_t accepted_encodings = 0;
183 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
184 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_STREAM_GZIP);
186 GPR_ASSERT(GRPC_COMPRESS_NONE ==
187 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
188 accepted_encodings));
190 GPR_ASSERT(GRPC_COMPRESS_NONE ==
191 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
192 accepted_encodings));
194 GPR_ASSERT(GRPC_COMPRESS_NONE ==
195 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
196 accepted_encodings));
198 GPR_ASSERT(GRPC_COMPRESS_NONE ==
199 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
200 accepted_encodings));
204 /* accept all algorithms */
205 uint32_t accepted_encodings = 0;
206 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
207 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP);
208 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
209 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_STREAM_GZIP);
211 GPR_ASSERT(GRPC_COMPRESS_NONE ==
212 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
213 accepted_encodings));
215 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
216 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
217 accepted_encodings));
219 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
220 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
221 accepted_encodings));
223 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
224 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
225 accepted_encodings));
229 static void test_compression_enable_disable_algorithm(void) {
230 grpc_compression_options options;
231 grpc_compression_algorithm algorithm;
233 gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm");
235 grpc_compression_options_init(&options);
236 for (algorithm = GRPC_COMPRESS_NONE;
237 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
238 algorithm = static_cast<grpc_compression_algorithm>(
239 static_cast<int>(algorithm) + 1)) {
240 /* all algorithms are enabled by default */
241 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
244 /* disable one by one */
245 for (algorithm = GRPC_COMPRESS_NONE;
246 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
247 algorithm = static_cast<grpc_compression_algorithm>(
248 static_cast<int>(algorithm) + 1)) {
249 grpc_compression_options_disable_algorithm(&options, algorithm);
250 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
253 /* re-enable one by one */
254 for (algorithm = GRPC_COMPRESS_NONE;
255 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
256 algorithm = static_cast<grpc_compression_algorithm>(
257 static_cast<int>(algorithm) + 1)) {
258 grpc_compression_options_enable_algorithm(&options, algorithm);
259 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
264 static void test_channel_args_set_compression_algorithm(void) {
265 grpc_core::ExecCtx exec_ctx;
266 grpc_channel_args* ch_args;
269 grpc_channel_args_set_compression_algorithm(nullptr, GRPC_COMPRESS_GZIP);
270 GPR_ASSERT(ch_args->num_args == 1);
271 GPR_ASSERT(strcmp(ch_args->args[0].key,
272 GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM) == 0);
273 GPR_ASSERT(ch_args->args[0].type == GRPC_ARG_INTEGER);
275 grpc_channel_args_destroy(ch_args);
278 static void test_channel_args_compression_algorithm_states(void) {
279 grpc_core::ExecCtx exec_ctx;
280 grpc_channel_args *ch_args, *ch_args_wo_gzip, *ch_args_wo_gzip_deflate,
281 *ch_args_wo_gzip_deflate_gzip;
282 unsigned states_bitset;
285 ch_args = grpc_channel_args_copy_and_add(nullptr, nullptr, 0);
286 /* by default, all enabled */
287 states_bitset = static_cast<unsigned>(
288 grpc_channel_args_compression_algorithm_get_states(ch_args));
290 for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
291 GPR_ASSERT(GPR_BITGET(states_bitset, i));
294 /* disable gzip and deflate and stream/gzip */
295 ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state(
296 &ch_args, GRPC_COMPRESS_GZIP, 0);
297 GPR_ASSERT(ch_args == ch_args_wo_gzip);
298 ch_args_wo_gzip_deflate = grpc_channel_args_compression_algorithm_set_state(
299 &ch_args_wo_gzip, GRPC_COMPRESS_DEFLATE, 0);
300 GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate);
301 ch_args_wo_gzip_deflate_gzip =
302 grpc_channel_args_compression_algorithm_set_state(
303 &ch_args_wo_gzip_deflate, GRPC_COMPRESS_STREAM_GZIP, 0);
304 GPR_ASSERT(ch_args_wo_gzip_deflate == ch_args_wo_gzip_deflate_gzip);
307 static_cast<unsigned>(grpc_channel_args_compression_algorithm_get_states(
308 ch_args_wo_gzip_deflate));
309 for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
310 if (i == GRPC_COMPRESS_GZIP || i == GRPC_COMPRESS_DEFLATE ||
311 i == GRPC_COMPRESS_STREAM_GZIP) {
312 GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0);
314 GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
318 /* re-enabled gzip and stream/gzip only */
319 ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state(
320 &ch_args_wo_gzip_deflate_gzip, GRPC_COMPRESS_GZIP, 1);
321 ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state(
322 &ch_args_wo_gzip, GRPC_COMPRESS_STREAM_GZIP, 1);
323 GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate_gzip);
325 states_bitset = static_cast<unsigned>(
326 grpc_channel_args_compression_algorithm_get_states(ch_args_wo_gzip));
327 for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
328 if (i == GRPC_COMPRESS_DEFLATE) {
329 GPR_ASSERT(GPR_BITGET(states_bitset, i) == 0);
331 GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
335 grpc_channel_args_destroy(ch_args);
338 int main(int argc, char** argv) {
340 test_compression_algorithm_parse();
341 test_compression_algorithm_name();
342 test_compression_algorithm_for_level();
343 test_compression_enable_disable_algorithm();
344 test_channel_args_set_compression_algorithm();
345 test_channel_args_compression_algorithm_states();