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/gpr/useful.h"
27 #include "test/core/util/test_config.h"
28
test_compression_algorithm_parse(void)29 static void test_compression_algorithm_parse(void) {
30 size_t i;
31 const char* valid_names[] = {"identity", "gzip", "deflate", "stream/gzip"};
32 const grpc_compression_algorithm valid_algorithms[] = {
33 GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE,
34 GRPC_COMPRESS_STREAM_GZIP};
35 const char* invalid_names[] = {"gzip2", "foo", "", "2gzip"};
36
37 gpr_log(GPR_DEBUG, "test_compression_algorithm_parse");
38
39 for (i = 0; i < GPR_ARRAY_SIZE(valid_names); i++) {
40 const char* valid_name = valid_names[i];
41 grpc_compression_algorithm algorithm;
42 const int success = grpc_compression_algorithm_parse(
43 grpc_slice_from_static_string(valid_name), &algorithm);
44 GPR_ASSERT(success != 0);
45 GPR_ASSERT(algorithm == valid_algorithms[i]);
46 }
47
48 for (i = 0; i < GPR_ARRAY_SIZE(invalid_names); i++) {
49 const char* invalid_name = invalid_names[i];
50 grpc_compression_algorithm algorithm;
51 int success;
52 success = grpc_compression_algorithm_parse(
53 grpc_slice_from_static_string(invalid_name), &algorithm);
54 GPR_ASSERT(success == 0);
55 /* the value of "algorithm" is undefined upon failure */
56 }
57 }
58
test_compression_algorithm_name(void)59 static void test_compression_algorithm_name(void) {
60 int success;
61 const char* name;
62 size_t i;
63 const char* valid_names[] = {"identity", "gzip", "deflate", "stream/gzip"};
64 const grpc_compression_algorithm valid_algorithms[] = {
65 GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE,
66 GRPC_COMPRESS_STREAM_GZIP};
67
68 gpr_log(GPR_DEBUG, "test_compression_algorithm_name");
69
70 for (i = 0; i < GPR_ARRAY_SIZE(valid_algorithms); i++) {
71 success = grpc_compression_algorithm_name(valid_algorithms[i], &name);
72 GPR_ASSERT(success != 0);
73 GPR_ASSERT(strcmp(name, valid_names[i]) == 0);
74 }
75
76 success =
77 grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, &name);
78 GPR_ASSERT(success == 0);
79 /* the value of "name" is undefined upon failure */
80 }
81
test_compression_algorithm_for_level(void)82 static void test_compression_algorithm_for_level(void) {
83 gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level");
84
85 {
86 /* accept only identity (aka none) */
87 uint32_t accepted_encodings = 0;
88 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
89
90 GPR_ASSERT(GRPC_COMPRESS_NONE ==
91 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
92 accepted_encodings));
93
94 GPR_ASSERT(GRPC_COMPRESS_NONE ==
95 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
96 accepted_encodings));
97
98 GPR_ASSERT(GRPC_COMPRESS_NONE ==
99 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
100 accepted_encodings));
101
102 GPR_ASSERT(GRPC_COMPRESS_NONE ==
103 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
104 accepted_encodings));
105 }
106
107 {
108 /* accept only gzip */
109 uint32_t accepted_encodings = 0;
110 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
111 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP);
112
113 GPR_ASSERT(GRPC_COMPRESS_NONE ==
114 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
115 accepted_encodings));
116
117 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
118 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
119 accepted_encodings));
120
121 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
122 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
123 accepted_encodings));
124
125 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
126 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
127 accepted_encodings));
128 }
129
130 {
131 /* accept only deflate */
132 uint32_t accepted_encodings = 0;
133 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
134 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
135
136 GPR_ASSERT(GRPC_COMPRESS_NONE ==
137 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
138 accepted_encodings));
139
140 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
141 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
142 accepted_encodings));
143
144 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
145 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
146 accepted_encodings));
147
148 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
149 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
150 accepted_encodings));
151 }
152
153 {
154 /* accept gzip and deflate */
155 uint32_t accepted_encodings = 0;
156 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
157 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP);
158 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
159
160 GPR_ASSERT(GRPC_COMPRESS_NONE ==
161 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
162 accepted_encodings));
163
164 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
165 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
166 accepted_encodings));
167
168 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
169 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
170 accepted_encodings));
171
172 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
173 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
174 accepted_encodings));
175 }
176
177 {
178 /* accept stream gzip */
179 uint32_t accepted_encodings = 0;
180 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
181 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_STREAM_GZIP);
182
183 GPR_ASSERT(GRPC_COMPRESS_NONE ==
184 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
185 accepted_encodings));
186
187 GPR_ASSERT(GRPC_COMPRESS_NONE ==
188 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
189 accepted_encodings));
190
191 GPR_ASSERT(GRPC_COMPRESS_NONE ==
192 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
193 accepted_encodings));
194
195 GPR_ASSERT(GRPC_COMPRESS_NONE ==
196 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
197 accepted_encodings));
198 }
199
200 {
201 /* accept all algorithms */
202 uint32_t accepted_encodings = 0;
203 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
204 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_GZIP);
205 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
206 GPR_BITSET(&accepted_encodings, GRPC_COMPRESS_STREAM_GZIP);
207
208 GPR_ASSERT(GRPC_COMPRESS_NONE ==
209 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
210 accepted_encodings));
211
212 GPR_ASSERT(GRPC_COMPRESS_GZIP ==
213 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
214 accepted_encodings));
215
216 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
217 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
218 accepted_encodings));
219
220 GPR_ASSERT(GRPC_COMPRESS_DEFLATE ==
221 grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
222 accepted_encodings));
223 }
224 }
225
test_compression_enable_disable_algorithm(void)226 static void test_compression_enable_disable_algorithm(void) {
227 grpc_compression_options options;
228 grpc_compression_algorithm algorithm;
229
230 gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm");
231
232 grpc_compression_options_init(&options);
233 for (algorithm = GRPC_COMPRESS_NONE;
234 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
235 algorithm = static_cast<grpc_compression_algorithm>(
236 static_cast<int>(algorithm) + 1)) {
237 /* all algorithms are enabled by default */
238 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
239 algorithm) != 0);
240 }
241 /* disable one by one */
242 for (algorithm = GRPC_COMPRESS_NONE;
243 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
244 algorithm = static_cast<grpc_compression_algorithm>(
245 static_cast<int>(algorithm) + 1)) {
246 grpc_compression_options_disable_algorithm(&options, algorithm);
247 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
248 algorithm) == 0);
249 }
250 /* re-enable one by one */
251 for (algorithm = GRPC_COMPRESS_NONE;
252 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
253 algorithm = static_cast<grpc_compression_algorithm>(
254 static_cast<int>(algorithm) + 1)) {
255 grpc_compression_options_enable_algorithm(&options, algorithm);
256 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
257 algorithm) != 0);
258 }
259 }
260
main(int argc,char ** argv)261 int main(int argc, char** argv) {
262 grpc_init();
263 test_compression_algorithm_parse();
264 test_compression_algorithm_name();
265 test_compression_algorithm_for_level();
266 test_compression_enable_disable_algorithm();
267 grpc_shutdown();
268
269 return 0;
270 }
271