| 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 "gtest/gtest.h" |
| 23 |
|
| 24 |
#include <grpc/compression.h> |
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/slice.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
|
| 29 |
#include "src/core/lib/channel/channel_args.h" |
| 30 |
#include "src/core/lib/compression/compression_internal.h" |
| 31 |
#include "src/core/lib/gpr/useful.h" |
| 32 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 33 |
#include "test/core/compression/args_utils.h" |
| 34 |
#include "test/core/util/test_config.h" |
| 35 |
|
| 36 |
TEST(CompressionTest, CompressionAlgorithmParse) { |
| 37 |
size_t i; |
| 38 |
const char* valid_names[] = {"identity", "gzip", "deflate"}; |
| 39 |
const grpc_compression_algorithm valid_algorithms[] = { |
| 40 |
GRPC_COMPRESS_NONE, |
| 41 |
GRPC_COMPRESS_GZIP, |
| 42 |
GRPC_COMPRESS_DEFLATE, |
| 43 |
}; |
| 44 |
const char* invalid_names[] = {"gzip2", "foo", "", "2gzip"}; |
| 45 |
|
| 46 |
gpr_log(GPR_DEBUG, "test_compression_algorithm_parse"); |
| 47 |
|
| 48 |
for (i = 0; i < GPR_ARRAY_SIZE(valid_names); i++) { |
| 49 |
const char* valid_name = valid_names[i]; |
| 50 |
grpc_compression_algorithm algorithm; |
| 51 |
const int success = grpc_compression_algorithm_parse( |
| 52 |
grpc_slice_from_static_string(valid_name), &algorithm); |
| 53 |
ASSERT_NE(success, 0); |
| 54 |
ASSERT_EQ(algorithm, valid_algorithms[i]); |
| 55 |
} |
| 56 |
|
| 57 |
for (i = 0; i < GPR_ARRAY_SIZE(invalid_names); i++) { |
| 58 |
const char* invalid_name = invalid_names[i]; |
| 59 |
grpc_compression_algorithm algorithm; |
| 60 |
int success; |
| 61 |
success = grpc_compression_algorithm_parse( |
| 62 |
grpc_slice_from_static_string(invalid_name), &algorithm); |
| 63 |
ASSERT_EQ(success, 0); |
| 64 |
/* the value of "algorithm" is undefined upon failure */ |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
TEST(CompressionTest, CompressionAlgorithmName) { |
| 69 |
int success; |
| 70 |
const char* name; |
| 71 |
size_t i; |
| 72 |
const char* valid_names[] = {"identity", "gzip", "deflate"}; |
| 73 |
const grpc_compression_algorithm valid_algorithms[] = { |
| 74 |
GRPC_COMPRESS_NONE, |
| 75 |
GRPC_COMPRESS_GZIP, |
| 76 |
GRPC_COMPRESS_DEFLATE, |
| 77 |
}; |
| 78 |
|
| 79 |
gpr_log(GPR_DEBUG, "test_compression_algorithm_name"); |
| 80 |
|
| 81 |
for (i = 0; i < GPR_ARRAY_SIZE(valid_algorithms); i++) { |
| 82 |
success = grpc_compression_algorithm_name(valid_algorithms[i], &name); |
| 83 |
ASSERT_NE(success, 0); |
| 84 |
ASSERT_STREQ(name, valid_names[i]); |
| 85 |
} |
| 86 |
|
| 87 |
success = |
| 88 |
grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, &name); |
| 89 |
ASSERT_EQ(success, 0); |
| 90 |
/* the value of "name" is undefined upon failure */ |
| 91 |
} |
| 92 |
|
| 93 |
TEST(CompressionTest, CompressionAlgorithmForLevel) { |
| 94 |
gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level"); |
| 95 |
|
| 96 |
{ |
| 97 |
/* accept only identity (aka none) */ |
| 98 |
uint32_t accepted_encodings = 0; |
| 99 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ |
| 100 |
|
| 101 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 102 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, |
| 103 |
accepted_encodings)); |
| 104 |
|
| 105 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 106 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, |
| 107 |
accepted_encodings)); |
| 108 |
|
| 109 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 110 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, |
| 111 |
accepted_encodings)); |
| 112 |
|
| 113 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 114 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, |
| 115 |
accepted_encodings)); |
| 116 |
} |
| 117 |
|
| 118 |
{ |
| 119 |
/* accept only gzip */ |
| 120 |
uint32_t accepted_encodings = 0; |
| 121 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ |
| 122 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_GZIP); |
| 123 |
|
| 124 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 125 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, |
| 126 |
accepted_encodings)); |
| 127 |
|
| 128 |
ASSERT_EQ(GRPC_COMPRESS_GZIP, |
| 129 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, |
| 130 |
accepted_encodings)); |
| 131 |
|
| 132 |
ASSERT_EQ(GRPC_COMPRESS_GZIP, |
| 133 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, |
| 134 |
accepted_encodings)); |
| 135 |
|
| 136 |
ASSERT_EQ(GRPC_COMPRESS_GZIP, |
| 137 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, |
| 138 |
accepted_encodings)); |
| 139 |
} |
| 140 |
|
| 141 |
{ |
| 142 |
/* accept only deflate */ |
| 143 |
uint32_t accepted_encodings = 0; |
| 144 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ |
| 145 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_DEFLATE); |
| 146 |
|
| 147 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 148 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, |
| 149 |
accepted_encodings)); |
| 150 |
|
| 151 |
ASSERT_EQ(GRPC_COMPRESS_DEFLATE, |
| 152 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, |
| 153 |
accepted_encodings)); |
| 154 |
|
| 155 |
ASSERT_EQ(GRPC_COMPRESS_DEFLATE, |
| 156 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, |
| 157 |
accepted_encodings)); |
| 158 |
|
| 159 |
ASSERT_EQ(GRPC_COMPRESS_DEFLATE, |
| 160 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, |
| 161 |
accepted_encodings)); |
| 162 |
} |
| 163 |
|
| 164 |
{ |
| 165 |
/* accept gzip and deflate */ |
| 166 |
uint32_t accepted_encodings = 0; |
| 167 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ |
| 168 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_GZIP); |
| 169 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_DEFLATE); |
| 170 |
|
| 171 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 172 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, |
| 173 |
accepted_encodings)); |
| 174 |
|
| 175 |
ASSERT_EQ(GRPC_COMPRESS_GZIP, |
| 176 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, |
| 177 |
accepted_encodings)); |
| 178 |
|
| 179 |
ASSERT_EQ(GRPC_COMPRESS_DEFLATE, |
| 180 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, |
| 181 |
accepted_encodings)); |
| 182 |
|
| 183 |
ASSERT_EQ(GRPC_COMPRESS_DEFLATE, |
| 184 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, |
| 185 |
accepted_encodings)); |
| 186 |
} |
| 187 |
|
| 188 |
{ |
| 189 |
/* accept all algorithms */ |
| 190 |
uint32_t accepted_encodings = 0; |
| 191 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_NONE); /* always */ |
| 192 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_GZIP); |
| 193 |
grpc_core::SetBit(&accepted_encodings, GRPC_COMPRESS_DEFLATE); |
| 194 |
|
| 195 |
ASSERT_EQ(GRPC_COMPRESS_NONE, |
| 196 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_NONE, |
| 197 |
accepted_encodings)); |
| 198 |
|
| 199 |
ASSERT_EQ(GRPC_COMPRESS_GZIP, |
| 200 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_LOW, |
| 201 |
accepted_encodings)); |
| 202 |
|
| 203 |
ASSERT_EQ(GRPC_COMPRESS_DEFLATE, |
| 204 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_MED, |
| 205 |
accepted_encodings)); |
| 206 |
|
| 207 |
ASSERT_EQ(GRPC_COMPRESS_DEFLATE, |
| 208 |
grpc_compression_algorithm_for_level(GRPC_COMPRESS_LEVEL_HIGH, |
| 209 |
accepted_encodings)); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
TEST(CompressionTest, CompressionEnableDisableAlgorithm) { |
| 214 |
grpc_compression_options options; |
| 215 |
grpc_compression_algorithm algorithm; |
| 216 |
|
| 217 |
gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm"); |
| 218 |
|
| 219 |
grpc_compression_options_init(&options); |
| 220 |
for (algorithm = GRPC_COMPRESS_NONE; |
| 221 |
algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 222 |
algorithm = static_cast<grpc_compression_algorithm>( |
| 223 |
static_cast<int>(algorithm) + 1)) { |
| 224 |
/* all algorithms are enabled by default */ |
| 225 |
ASSERT_NE( |
| 226 |
grpc_compression_options_is_algorithm_enabled(&options, algorithm), 0); |
| 227 |
} |
| 228 |
/* disable one by one */ |
| 229 |
for (algorithm = GRPC_COMPRESS_NONE; |
| 230 |
algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 231 |
algorithm = static_cast<grpc_compression_algorithm>( |
| 232 |
static_cast<int>(algorithm) + 1)) { |
| 233 |
grpc_compression_options_disable_algorithm(&options, algorithm); |
| 234 |
ASSERT_EQ( |
| 235 |
grpc_compression_options_is_algorithm_enabled(&options, algorithm), 0); |
| 236 |
} |
| 237 |
/* re-enable one by one */ |
| 238 |
for (algorithm = GRPC_COMPRESS_NONE; |
| 239 |
algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; |
| 240 |
algorithm = static_cast<grpc_compression_algorithm>( |
| 241 |
static_cast<int>(algorithm) + 1)) { |
| 242 |
grpc_compression_options_enable_algorithm(&options, algorithm); |
| 243 |
ASSERT_NE( |
| 244 |
grpc_compression_options_is_algorithm_enabled(&options, algorithm), 0); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
TEST(CompressionTest, ChannelArgsSetCompressionAlgorithm) { |
| 249 |
grpc_core::ExecCtx exec_ctx; |
| 250 |
const grpc_channel_args* ch_args; |
| 251 |
|
| 252 |
ch_args = grpc_channel_args_set_channel_default_compression_algorithm( |
| 253 |
nullptr, GRPC_COMPRESS_GZIP); |
| 254 |
ASSERT_EQ(ch_args->num_args, 1); |
| 255 |
ASSERT_STREQ(ch_args->args[0].key, |
| 256 |
GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM); |
| 257 |
ASSERT_EQ(ch_args->args[0].type, GRPC_ARG_INTEGER); |
| 258 |
|
| 259 |
grpc_channel_args_destroy(ch_args); |
| 260 |
} |
| 261 |
|
| 262 |
TEST(CompressionTest, ChannelArgsCompressionAlgorithmStates) { |
| 263 |
grpc_core::ExecCtx exec_ctx; |
| 264 |
grpc_core::CompressionAlgorithmSet states; |
| 265 |
|
| 266 |
const grpc_channel_args* ch_args = |
| 267 |
grpc_channel_args_copy_and_add(nullptr, nullptr, 0); |
| 268 |
/* by default, all enabled */ |
| 269 |
states = grpc_core::CompressionAlgorithmSet::FromChannelArgs(ch_args); |
| 270 |
|
| 271 |
for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { |
| 272 |
ASSERT_TRUE(states.IsSet(static_cast<grpc_compression_algorithm>(i))); |
| 273 |
} |
| 274 |
|
| 275 |
/* disable gzip and deflate and stream/gzip */ |
| 276 |
const grpc_channel_args* ch_args_wo_gzip = |
| 277 |
grpc_channel_args_compression_algorithm_set_state(&ch_args, |
| 278 |
GRPC_COMPRESS_GZIP, 0); |
| 279 |
ASSERT_EQ(ch_args, ch_args_wo_gzip); |
| 280 |
const grpc_channel_args* ch_args_wo_gzip_deflate = |
| 281 |
grpc_channel_args_compression_algorithm_set_state( |
| 282 |
&ch_args_wo_gzip, GRPC_COMPRESS_DEFLATE, 0); |
| 283 |
ASSERT_EQ(ch_args_wo_gzip, ch_args_wo_gzip_deflate); |
| 284 |
|
| 285 |
states = grpc_core::CompressionAlgorithmSet::FromChannelArgs( |
| 286 |
ch_args_wo_gzip_deflate); |
| 287 |
for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { |
| 288 |
if (i == GRPC_COMPRESS_GZIP || i == GRPC_COMPRESS_DEFLATE) { |
| 289 |
ASSERT_FALSE(states.IsSet(static_cast<grpc_compression_algorithm>(i))); |
| 290 |
} else { |
| 291 |
ASSERT_TRUE(states.IsSet(static_cast<grpc_compression_algorithm>(i))); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/* re-enabled gzip only */ |
| 296 |
ch_args_wo_gzip = grpc_channel_args_compression_algorithm_set_state( |
| 297 |
&ch_args_wo_gzip_deflate, GRPC_COMPRESS_GZIP, 1); |
| 298 |
ASSERT_EQ(ch_args_wo_gzip, ch_args_wo_gzip_deflate); |
| 299 |
|
| 300 |
states = grpc_core::CompressionAlgorithmSet::FromChannelArgs(ch_args_wo_gzip); |
| 301 |
for (size_t i = 0; i < GRPC_COMPRESS_ALGORITHMS_COUNT; i++) { |
| 302 |
if (i == GRPC_COMPRESS_DEFLATE) { |
| 303 |
ASSERT_FALSE(states.IsSet(static_cast<grpc_compression_algorithm>(i))); |
| 304 |
} else { |
| 305 |
ASSERT_TRUE(states.IsSet(static_cast<grpc_compression_algorithm>(i))); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
grpc_channel_args_destroy(ch_args); |
| 310 |
} |
| 311 |
|
| 312 |
int main(int argc, char** argv) { |
| 313 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 314 |
::testing::InitGoogleTest(&argc, argv); |
| 315 |
grpc::testing::TestGrpcScope grpc_scope; |
| 316 |
return RUN_ALL_TESTS(); |
| 317 |
} |