h2_compress.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <string.h>
  19. #include <grpc/grpc_security.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include <grpc/support/sync.h>
  23. #include "src/core/ext/filters/client_channel/client_channel.h"
  24. #include "src/core/ext/filters/http/server/http_server_filter.h"
  25. #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
  26. #include "src/core/lib/channel/channel_args.h"
  27. #include "src/core/lib/channel/connected_channel.h"
  28. #include "src/core/lib/gprpp/host_port.h"
  29. #include "src/core/lib/surface/channel.h"
  30. #include "src/core/lib/surface/server.h"
  31. #include "test/core/compression/args_utils.h"
  32. #include "test/core/end2end/end2end_tests.h"
  33. #include "test/core/util/port.h"
  34. #include "test/core/util/test_config.h"
  35. struct fullstack_compression_fixture_data {
  36. ~fullstack_compression_fixture_data() {
  37. grpc_channel_args_destroy(client_args_compression);
  38. grpc_channel_args_destroy(server_args_compression);
  39. }
  40. std::string localaddr;
  41. const grpc_channel_args* client_args_compression = nullptr;
  42. const grpc_channel_args* server_args_compression = nullptr;
  43. };
  44. static grpc_end2end_test_fixture chttp2_create_fixture_fullstack_compression(
  45. const grpc_channel_args* /*client_args*/,
  46. const grpc_channel_args* /*server_args*/) {
  47. grpc_end2end_test_fixture f;
  48. int port = grpc_pick_unused_port_or_die();
  49. fullstack_compression_fixture_data* ffd =
  50. new fullstack_compression_fixture_data();
  51. ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
  52. memset(&f, 0, sizeof(f));
  53. f.fixture_data = ffd;
  54. f.cq = grpc_completion_queue_create_for_next(nullptr);
  55. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  56. return f;
  57. }
  58. void chttp2_init_client_fullstack_compression(
  59. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
  60. fullstack_compression_fixture_data* ffd =
  61. static_cast<fullstack_compression_fixture_data*>(f->fixture_data);
  62. if (ffd->client_args_compression != nullptr) {
  63. grpc_core::ExecCtx exec_ctx;
  64. grpc_channel_args_destroy(ffd->client_args_compression);
  65. }
  66. ffd->client_args_compression =
  67. grpc_channel_args_set_channel_default_compression_algorithm(
  68. client_args, GRPC_COMPRESS_GZIP);
  69. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  70. f->client = grpc_channel_create(ffd->localaddr.c_str(), creds,
  71. ffd->client_args_compression);
  72. grpc_channel_credentials_release(creds);
  73. }
  74. void chttp2_init_server_fullstack_compression(
  75. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
  76. fullstack_compression_fixture_data* ffd =
  77. static_cast<fullstack_compression_fixture_data*>(f->fixture_data);
  78. if (ffd->server_args_compression != nullptr) {
  79. grpc_core::ExecCtx exec_ctx;
  80. grpc_channel_args_destroy(ffd->server_args_compression);
  81. }
  82. ffd->server_args_compression =
  83. grpc_channel_args_set_channel_default_compression_algorithm(
  84. server_args, GRPC_COMPRESS_GZIP);
  85. if (f->server) {
  86. grpc_server_destroy(f->server);
  87. }
  88. f->server = grpc_server_create(ffd->server_args_compression, nullptr);
  89. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  90. grpc_server_credentials* server_creds =
  91. grpc_insecure_server_credentials_create();
  92. GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(),
  93. server_creds));
  94. grpc_server_credentials_release(server_creds);
  95. grpc_server_start(f->server);
  96. }
  97. void chttp2_tear_down_fullstack_compression(grpc_end2end_test_fixture* f) {
  98. grpc_core::ExecCtx exec_ctx;
  99. fullstack_compression_fixture_data* ffd =
  100. static_cast<fullstack_compression_fixture_data*>(f->fixture_data);
  101. delete ffd;
  102. }
  103. /* All test configurations */
  104. static grpc_end2end_test_config configs[] = {
  105. {"chttp2/fullstack_compression",
  106. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  107. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  108. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
  109. nullptr, chttp2_create_fixture_fullstack_compression,
  110. chttp2_init_client_fullstack_compression,
  111. chttp2_init_server_fullstack_compression,
  112. chttp2_tear_down_fullstack_compression},
  113. };
  114. int main(int argc, char** argv) {
  115. size_t i;
  116. grpc::testing::TestEnvironment env(argc, argv);
  117. grpc_end2end_tests_pre_init();
  118. grpc_init();
  119. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  120. grpc_end2end_tests(argc, argv, configs[i]);
  121. }
  122. grpc_shutdown();
  123. return 0;
  124. }