xref: /aosp_15_r20/external/grpc-grpc/test/core/compression/compression_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <stdint.h>
20 #include <stdlib.h>
21 
22 #include <memory>
23 
24 #include "gtest/gtest.h"
25 
26 #include <grpc/compression.h>
27 #include <grpc/slice.h>
28 #include <grpc/support/log.h>
29 
30 #include "src/core/lib/gpr/useful.h"
31 #include "test/core/util/test_config.h"
32 
TEST(CompressionTest,CompressionAlgorithmParse)33 TEST(CompressionTest, CompressionAlgorithmParse) {
34   size_t i;
35   const char* valid_names[] = {"identity", "gzip", "deflate"};
36   const grpc_compression_algorithm valid_algorithms[] = {
37       GRPC_COMPRESS_NONE,
38       GRPC_COMPRESS_GZIP,
39       GRPC_COMPRESS_DEFLATE,
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];
47     grpc_compression_algorithm algorithm;
48     const int success = grpc_compression_algorithm_parse(
49         grpc_slice_from_static_string(valid_name), &algorithm);
50     ASSERT_NE(success, 0);
51     ASSERT_EQ(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];
56     grpc_compression_algorithm algorithm;
57     int success;
58     success = grpc_compression_algorithm_parse(
59         grpc_slice_from_static_string(invalid_name), &algorithm);
60     ASSERT_EQ(success, 0);
61     // the value of "algorithm" is undefined upon failure
62   }
63 }
64 
TEST(CompressionTest,CompressionAlgorithmName)65 TEST(CompressionTest, CompressionAlgorithmName) {
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[] = {
71       GRPC_COMPRESS_NONE,
72       GRPC_COMPRESS_GZIP,
73       GRPC_COMPRESS_DEFLATE,
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     ASSERT_NE(success, 0);
81     ASSERT_STREQ(name, valid_names[i]);
82   }
83 
84   success =
85       grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, &name);
86   ASSERT_EQ(success, 0);
87   // the value of "name" is undefined upon failure
88 }
89 
TEST(CompressionTest,CompressionAlgorithmForLevel)90 TEST(CompressionTest, CompressionAlgorithmForLevel) {
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 
98     ASSERT_EQ(GRPC_COMPRESS_NONE,
99               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
100                                                    accepted_encodings));
101 
102     ASSERT_EQ(GRPC_COMPRESS_NONE,
103               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
104                                                    accepted_encodings));
105 
106     ASSERT_EQ(GRPC_COMPRESS_NONE,
107               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
108                                                    accepted_encodings));
109 
110     ASSERT_EQ(GRPC_COMPRESS_NONE,
111               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
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 
121     ASSERT_EQ(GRPC_COMPRESS_NONE,
122               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
123                                                    accepted_encodings));
124 
125     ASSERT_EQ(GRPC_COMPRESS_GZIP,
126               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
127                                                    accepted_encodings));
128 
129     ASSERT_EQ(GRPC_COMPRESS_GZIP,
130               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
131                                                    accepted_encodings));
132 
133     ASSERT_EQ(GRPC_COMPRESS_GZIP,
134               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
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 
144     ASSERT_EQ(GRPC_COMPRESS_NONE,
145               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
146                                                    accepted_encodings));
147 
148     ASSERT_EQ(GRPC_COMPRESS_DEFLATE,
149               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
150                                                    accepted_encodings));
151 
152     ASSERT_EQ(GRPC_COMPRESS_DEFLATE,
153               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
154                                                    accepted_encodings));
155 
156     ASSERT_EQ(GRPC_COMPRESS_DEFLATE,
157               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
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 
168     ASSERT_EQ(GRPC_COMPRESS_NONE,
169               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
170                                                    accepted_encodings));
171 
172     ASSERT_EQ(GRPC_COMPRESS_GZIP,
173               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
174                                                    accepted_encodings));
175 
176     ASSERT_EQ(GRPC_COMPRESS_DEFLATE,
177               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
178                                                    accepted_encodings));
179 
180     ASSERT_EQ(GRPC_COMPRESS_DEFLATE,
181               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
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 
192     ASSERT_EQ(GRPC_COMPRESS_NONE,
193               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE,
194                                                    accepted_encodings));
195 
196     ASSERT_EQ(GRPC_COMPRESS_GZIP,
197               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW,
198                                                    accepted_encodings));
199 
200     ASSERT_EQ(GRPC_COMPRESS_DEFLATE,
201               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED,
202                                                    accepted_encodings));
203 
204     ASSERT_EQ(GRPC_COMPRESS_DEFLATE,
205               grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH,
206                                                    accepted_encodings));
207   }
208 }
209 
TEST(CompressionTest,CompressionEnableDisableAlgorithm)210 TEST(CompressionTest, CompressionEnableDisableAlgorithm) {
211   grpc_compression_options options;
212   grpc_compression_algorithm algorithm;
213 
214   gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm");
215 
216   grpc_compression_options_init(&options);
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
222     ASSERT_NE(
223         grpc_compression_options_is_algorithm_enabled(&options, 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)) {
230     grpc_compression_options_disable_algorithm(&options, algorithm);
231     ASSERT_EQ(
232         grpc_compression_options_is_algorithm_enabled(&options, 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)) {
239     grpc_compression_options_enable_algorithm(&options, algorithm);
240     ASSERT_NE(
241         grpc_compression_options_is_algorithm_enabled(&options, algorithm), 0);
242   }
243 }
244 
main(int argc,char ** argv)245 int main(int argc, char** argv) {
246   grpc::testing::TestEnvironment env(&argc, argv);
247   ::testing::InitGoogleTest(&argc, argv);
248   grpc::testing::TestGrpcScope grpc_scope;
249   return RUN_ALL_TESTS();
250 }
251