compression_test.cc
Go to the documentation of this file.
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 
32 
34  size_t i;
35  const char* valid_names[] = {"identity", "gzip", "deflate"};
36  const grpc_compression_algorithm valid_algorithms[] = {
40  };
41  const char* invalid_names[] = {"gzip2", "foo", "", "2gzip"};
42 
43  gpr_log(GPR_DEBUG, "test_compression_algorithm_parse");
44 
45  for (i = 0; i < GPR_ARRAY_SIZE(valid_names); i++) {
46  const char* valid_name = valid_names[i];
48  const int success = grpc_compression_algorithm_parse(
49  grpc_slice_from_static_string(valid_name), &algorithm);
50  GPR_ASSERT(success != 0);
51  GPR_ASSERT(algorithm == valid_algorithms[i]);
52  }
53 
54  for (i = 0; i < GPR_ARRAY_SIZE(invalid_names); i++) {
55  const char* invalid_name = invalid_names[i];
57  int success;
59  grpc_slice_from_static_string(invalid_name), &algorithm);
60  GPR_ASSERT(success == 0);
61  /* the value of "algorithm" is undefined upon failure */
62  }
63 }
64 
66  int success;
67  const char* name;
68  size_t i;
69  const char* valid_names[] = {"identity", "gzip", "deflate"};
70  const grpc_compression_algorithm valid_algorithms[] = {
74  };
75 
76  gpr_log(GPR_DEBUG, "test_compression_algorithm_name");
77 
78  for (i = 0; i < GPR_ARRAY_SIZE(valid_algorithms); i++) {
79  success = grpc_compression_algorithm_name(valid_algorithms[i], &name);
80  GPR_ASSERT(success != 0);
81  GPR_ASSERT(strcmp(name, valid_names[i]) == 0);
82  }
83 
84  success =
86  GPR_ASSERT(success == 0);
87  /* the value of "name" is undefined upon failure */
88 }
89 
91  gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level");
92 
93  {
94  /* accept only identity (aka none) */
95  uint32_t accepted_encodings = 0;
96  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
97 
100  accepted_encodings));
101 
104  accepted_encodings));
105 
108  accepted_encodings));
109 
112  accepted_encodings));
113  }
114 
115  {
116  /* accept only gzip */
117  uint32_t accepted_encodings = 0;
118  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
119  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_GZIP);
120 
123  accepted_encodings));
124 
127  accepted_encodings));
128 
131  accepted_encodings));
132 
135  accepted_encodings));
136  }
137 
138  {
139  /* accept only deflate */
140  uint32_t accepted_encodings = 0;
141  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
142  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
143 
146  accepted_encodings));
147 
150  accepted_encodings));
151 
154  accepted_encodings));
155 
158  accepted_encodings));
159  }
160 
161  {
162  /* accept gzip and deflate */
163  uint32_t accepted_encodings = 0;
164  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
165  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_GZIP);
166  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
167 
170  accepted_encodings));
171 
174  accepted_encodings));
175 
178  accepted_encodings));
179 
182  accepted_encodings));
183  }
184 
185  {
186  /* accept all algorithms */
187  uint32_t accepted_encodings = 0;
188  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */
189  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_GZIP);
190  grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_DEFLATE);
191 
194  accepted_encodings));
195 
198  accepted_encodings));
199 
202  accepted_encodings));
203 
206  accepted_encodings));
207  }
208 }
209 
212  grpc_compression_algorithm algorithm;
213 
214  gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm");
215 
217  for (algorithm = GRPC_COMPRESS_NONE;
218  algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
219  algorithm = static_cast<grpc_compression_algorithm>(
220  static_cast<int>(algorithm) + 1)) {
221  /* all algorithms are enabled by default */
223  algorithm) != 0);
224  }
225  /* disable one by one */
226  for (algorithm = GRPC_COMPRESS_NONE;
227  algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
228  algorithm = static_cast<grpc_compression_algorithm>(
229  static_cast<int>(algorithm) + 1)) {
232  algorithm) == 0);
233  }
234  /* re-enable one by one */
235  for (algorithm = GRPC_COMPRESS_NONE;
236  algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT;
237  algorithm = static_cast<grpc_compression_algorithm>(
238  static_cast<int>(algorithm) + 1)) {
241  algorithm) != 0);
242  }
243 }
244 
247  const grpc_channel_args* ch_args;
248 
250  nullptr, GRPC_COMPRESS_GZIP);
251  GPR_ASSERT(ch_args->num_args == 1);
252  GPR_ASSERT(strcmp(ch_args->args[0].key,
254  GPR_ASSERT(ch_args->args[0].type == GRPC_ARG_INTEGER);
255 
256  grpc_channel_args_destroy(ch_args);
257 }
258 
262 
263  const grpc_channel_args* ch_args =
264  grpc_channel_args_copy_and_add(nullptr, nullptr, 0);
265  /* by default, all enabled */
267 
268  for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
269  GPR_ASSERT(states.IsSet(static_cast<grpc_compression_algorithm>(i)));
270  }
271 
272  /* disable gzip and deflate and stream/gzip */
273  const grpc_channel_args* ch_args_wo_gzip =
275  GRPC_COMPRESS_GZIP, 0);
276  GPR_ASSERT(ch_args == ch_args_wo_gzip);
277  const grpc_channel_args* ch_args_wo_gzip_deflate =
279  &ch_args_wo_gzip, GRPC_COMPRESS_DEFLATE, 0);
280  GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate);
281 
283  ch_args_wo_gzip_deflate);
284  for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
286  GPR_ASSERT(!states.IsSet(static_cast<grpc_compression_algorithm>(i)));
287  } else {
288  GPR_ASSERT(states.IsSet(static_cast<grpc_compression_algorithm>(i)));
289  }
290  }
291 
292  /* re-enabled gzip only */
294  &ch_args_wo_gzip_deflate, GRPC_COMPRESS_GZIP, 1);
295  GPR_ASSERT(ch_args_wo_gzip == ch_args_wo_gzip_deflate);
296 
297  states = grpc_core::CompressionAlgorithmSet::FromChannelArgs(ch_args_wo_gzip);
298  for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) {
299  if (i == GRPC_COMPRESS_DEFLATE) {
300  GPR_ASSERT(!states.IsSet(static_cast<grpc_compression_algorithm>(i)));
301  } else {
302  GPR_ASSERT(states.IsSet(static_cast<grpc_compression_algorithm>(i)));
303  }
304  }
305 
306  grpc_channel_args_destroy(ch_args);
307 }
308 
309 int main(int argc, char** argv) {
310  grpc::testing::TestEnvironment env(&argc, argv);
311  grpc_init();
318  grpc_shutdown();
319  return 0;
320 }
compression.h
GRPC_COMPRESS_LEVEL_MED
@ GRPC_COMPRESS_LEVEL_MED
Definition: compression_types.h:75
GRPC_COMPRESS_DEFLATE
@ GRPC_COMPRESS_DEFLATE
Definition: compression_types.h:62
log.h
grpc_channel_args_compression_algorithm_set_state
const grpc_channel_args * grpc_channel_args_compression_algorithm_set_state(const grpc_channel_args **a, grpc_compression_algorithm algorithm, int state)
Definition: args_utils.cc:58
generate.env
env
Definition: generate.py:37
GRPC_ARG_INTEGER
@ GRPC_ARG_INTEGER
Definition: grpc_types.h:81
args_utils.h
grpc_core::CompressionAlgorithmSet
Definition: compression_internal.h:52
string.h
options
double_dict options[]
Definition: capstone_test.c:55
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
useful.h
setup.name
name
Definition: setup.py:542
grpc_compression_options
Definition: compression_types.h:80
test_channel_args_set_compression_algorithm
static void test_channel_args_set_compression_algorithm(void)
Definition: compression_test.cc:245
grpc_compression_options_enable_algorithm
GRPCAPI void grpc_compression_options_enable_algorithm(grpc_compression_options *opts, grpc_compression_algorithm algorithm)
Definition: compression.cc:80
GRPC_COMPRESS_NONE
@ GRPC_COMPRESS_NONE
Definition: compression_types.h:61
grpc_channel_args
Definition: grpc_types.h:132
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_compression_algorithm_name
GRPCAPI int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm, const char **name)
Definition: compression.cc:56
grpc_core::CompressionAlgorithmSet::IsSet
bool IsSet(grpc_compression_algorithm algorithm) const
Definition: compression_internal.cc:190
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
GRPC_COMPRESS_LEVEL_NONE
@ GRPC_COMPRESS_LEVEL_NONE
Definition: compression_types.h:73
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
grpc_channel_args::num_args
size_t num_args
Definition: grpc_types.h:133
grpc_slice_from_static_string
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
Definition: slice/slice.cc:89
grpc_channel_args_set_channel_default_compression_algorithm
const grpc_channel_args * grpc_channel_args_set_channel_default_compression_algorithm(const grpc_channel_args *a, grpc_compression_algorithm algorithm)
Definition: args_utils.cc:26
test_compression_algorithm_for_level
static void test_compression_algorithm_for_level(void)
Definition: compression_test.cc:90
GRPC_COMPRESS_LEVEL_HIGH
@ GRPC_COMPRESS_LEVEL_HIGH
Definition: compression_types.h:76
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_compression_algorithm_parse
GRPCAPI int grpc_compression_algorithm_parse(grpc_slice name, grpc_compression_algorithm *algorithm)
Definition: compression.cc:44
grpc_compression_options_disable_algorithm
GRPCAPI void grpc_compression_options_disable_algorithm(grpc_compression_options *opts, grpc_compression_algorithm algorithm)
Definition: compression.cc:85
grpc_core::CompressionAlgorithmSet::FromChannelArgs
static CompressionAlgorithmSet FromChannelArgs(const grpc_channel_args *args)
Definition: compression_internal.cc:165
test_config.h
compression_internal.h
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
grpc_compression_options_is_algorithm_enabled
GRPCAPI int grpc_compression_options_is_algorithm_enabled(const grpc_compression_options *opts, grpc_compression_algorithm algorithm)
Definition: compression.cc:90
GRPC_COMPRESS_LEVEL_LOW
@ GRPC_COMPRESS_LEVEL_LOW
Definition: compression_types.h:74
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
test_compression_algorithm_name
static void test_compression_algorithm_name(void)
Definition: compression_test.cc:65
grpc_compression_options_init
GRPCAPI void grpc_compression_options_init(grpc_compression_options *opts)
Definition: compression.cc:74
test_channel_args_compression_algorithm_states
static void test_channel_args_compression_algorithm_states(void)
Definition: compression_test.cc:259
main
int main(int argc, char **argv)
Definition: compression_test.cc:309
GRPC_COMPRESS_ALGORITHMS_COUNT
@ GRPC_COMPRESS_ALGORITHMS_COUNT
Definition: compression_types.h:65
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
GRPC_COMPRESS_GZIP
@ GRPC_COMPRESS_GZIP
Definition: compression_types.h:63
grpc_arg::key
char * key
Definition: grpc_types.h:105
exec_ctx.h
grpc_core::SetBit
T SetBit(T *i, size_t n)
Definition: useful.h:49
grpc_compression_algorithm_for_level
GRPCAPI grpc_compression_algorithm grpc_compression_algorithm_for_level(grpc_compression_level level, uint32_t accepted_encodings)
Definition: compression.cc:68
channel_args.h
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
test_compression_algorithm_parse
static void test_compression_algorithm_parse(void)
Definition: compression_test.cc:33
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM
#define GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM
Definition: compression_types.h:42
grpc_arg::type
grpc_arg_type type
Definition: grpc_types.h:104
grpc_channel_args::args
grpc_arg * args
Definition: grpc_types.h:134
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
test_compression_enable_disable_algorithm
static void test_compression_enable_disable_algorithm(void)
Definition: compression_test.cc:210
grpc_channel_args_copy_and_add
grpc_channel_args * grpc_channel_args_copy_and_add(const grpc_channel_args *src, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:224
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:52