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