Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / test / core / compression / compression_test.cc
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 <stdlib.h>
20 #include <string.h>
21
22 #include <grpc/compression.h>
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25
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"
31
32 static void test_compression_algorithm_parse(void) {
33   size_t i;
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"};
39
40   gpr_log(GPR_DEBUG, "test_compression_algorithm_parse");
41
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]);
49   }
50
51   for (i = 0; i < GPR_ARRAY_SIZE(invalid_names); i++) {
52     const char* invalid_name = invalid_names[i];
53     grpc_compression_algorithm algorithm;
54     int success;
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 */
59   }
60 }
61
62 static void test_compression_algorithm_name(void) {
63   int success;
64   const char* name;
65   size_t i;
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};
70
71   gpr_log(GPR_DEBUG, "test_compression_algorithm_name");
72
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);
77   }
78
79   success =
80       grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, &name);
81   GPR_ASSERT(success == 0);
82   /* the value of "name" is undefined upon failure */
83 }
84
85 static void test_compression_algorithm_for_level(void) {
86   gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level");
87
88   {
89     /* accept only identity (aka none) */
90     uint32_t accepted_encodings = 0;
91     GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
92
93     GPR_ASSERT(GRPC_COMPRESS_NONE ==
94                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
95                                                     accepted_encodings));
96
97     GPR_ASSERT(GRPC_COMPRESS_NONE ==
98                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
99                                                     accepted_encodings));
100
101     GPR_ASSERT(GRPC_COMPRESS_NONE ==
102                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
103                                                     accepted_encodings));
104
105     GPR_ASSERT(GRPC_COMPRESS_NONE ==
106                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
107                                                     accepted_encodings));
108   }
109
110   {
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);
115
116     GPR_ASSERT(GRPC_COMPRESS_NONE ==
117                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
118                                                     accepted_encodings));
119
120     GPR_ASSERT(GRPC_COMPRESS_GZIP ==
121                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
122                                                     accepted_encodings));
123
124     GPR_ASSERT(GRPC_COMPRESS_GZIP ==
125                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
126                                                     accepted_encodings));
127
128     GPR_ASSERT(GRPC_COMPRESS_GZIP ==
129                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
130                                                     accepted_encodings));
131   }
132
133   {
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);
138
139     GPR_ASSERT(GRPC_COMPRESS_NONE ==
140                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
141                                                     accepted_encodings));
142
143     GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
144                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
145                                                     accepted_encodings));
146
147     GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
148                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
149                                                     accepted_encodings));
150
151     GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
152                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
153                                                     accepted_encodings));
154   }
155
156   {
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);
162
163     GPR_ASSERT(GRPC_COMPRESS_NONE ==
164                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
165                                                     accepted_encodings));
166
167     GPR_ASSERT(GRPC_COMPRESS_GZIP ==
168                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
169                                                     accepted_encodings));
170
171     GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
172                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
173                                                     accepted_encodings));
174
175     GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
176                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
177                                                     accepted_encodings));
178   }
179
180   {
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);
185
186     GPR_ASSERT(GRPC_COMPRESS_NONE ==
187                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
188                                                     accepted_encodings));
189
190     GPR_ASSERT(GRPC_COMPRESS_NONE ==
191                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
192                                                     accepted_encodings));
193
194     GPR_ASSERT(GRPC_COMPRESS_NONE ==
195                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
196                                                     accepted_encodings));
197
198     GPR_ASSERT(GRPC_COMPRESS_NONE ==
199                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
200                                                     accepted_encodings));
201   }
202
203   {
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);
210
211     GPR_ASSERT(GRPC_COMPRESS_NONE ==
212                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
213                                                     accepted_encodings));
214
215     GPR_ASSERT(GRPC_COMPRESS_GZIP ==
216                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
217                                                     accepted_encodings));
218
219     GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
220                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
221                                                     accepted_encodings));
222
223     GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
224                grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
225                                                     accepted_encodings));
226   }
227 }
228
229 static void test_compression_enable_disable_algorithm(void) {
230   grpc_compression_options options;
231   grpc_compression_algorithm algorithm;
232
233   gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm");
234
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,
242                                                              algorithm) != 0);
243   }
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,
251                                                              algorithm) == 0);
252   }
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,
260                                                              algorithm) != 0);
261   }
262 }
263
264 static void test_channel_args_set_compression_algorithm(void) {
265   grpc_core::ExecCtx exec_ctx;
266   grpc_channel_args* ch_args;
267
268   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);
274
275   grpc_channel_args_destroy(ch_args);
276 }
277
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;
283   size_t i;
284
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));
289
290   for (i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
291     GPR_ASSERT(GPR_BITGET(states_bitset, i));
292   }
293
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);
305
306   states_bitset =
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);
313     } else {
314       GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
315     }
316   }
317
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);
324
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);
330     } else {
331       GPR_ASSERT(GPR_BITGET(states_bitset, i) != 0);
332     }
333   }
334
335   grpc_channel_args_destroy(ch_args);
336 }
337
338 int main(int argc, char** argv) {
339   grpc_init();
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();
346   grpc_shutdown();
347
348   return 0;
349 }