compression_test.cc 12 KB

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