/ev/external/grpc_old/test/core/end2end/fixtures/h2_http_proxy.cc

https://github.com/saaras-io/yastack · C++ · 134 lines · 94 code · 19 blank · 21 comment · 5 complexity · 3f19998dcb58d3f67162f4c51ff29235 MD5 · raw file

  1. /*
  2. *
  3. * Copyright 2016 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 "test/core/end2end/end2end_tests.h"
  19. #include <string.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include <grpc/support/string_util.h>
  23. #include <grpc/support/sync.h>
  24. #include "src/core/ext/filters/client_channel/client_channel.h"
  25. #include "src/core/ext/filters/http/server/http_server_filter.h"
  26. #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
  27. #include "src/core/lib/channel/connected_channel.h"
  28. #include "src/core/lib/gpr/env.h"
  29. #include "src/core/lib/gpr/host_port.h"
  30. #include "src/core/lib/surface/channel.h"
  31. #include "src/core/lib/surface/server.h"
  32. #include "test/core/end2end/fixtures/http_proxy_fixture.h"
  33. #include "test/core/util/port.h"
  34. #include "test/core/util/test_config.h"
  35. typedef struct fullstack_fixture_data {
  36. char* server_addr;
  37. grpc_end2end_http_proxy* proxy;
  38. } fullstack_fixture_data;
  39. static grpc_end2end_test_fixture chttp2_create_fixture_fullstack(
  40. grpc_channel_args* client_args, grpc_channel_args* server_args) {
  41. grpc_end2end_test_fixture f;
  42. memset(&f, 0, sizeof(f));
  43. fullstack_fixture_data* ffd = static_cast<fullstack_fixture_data*>(
  44. gpr_malloc(sizeof(fullstack_fixture_data)));
  45. const int server_port = grpc_pick_unused_port_or_die();
  46. gpr_join_host_port(&ffd->server_addr, "localhost", server_port);
  47. /* Passing client_args to proxy_create for the case of checking for proxy auth
  48. */
  49. ffd->proxy = grpc_end2end_http_proxy_create(client_args);
  50. f.fixture_data = ffd;
  51. f.cq = grpc_completion_queue_create_for_next(nullptr);
  52. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  53. return f;
  54. }
  55. void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f,
  56. grpc_channel_args* client_args) {
  57. fullstack_fixture_data* ffd =
  58. static_cast<fullstack_fixture_data*>(f->fixture_data);
  59. char* proxy_uri;
  60. /* If testing for proxy auth, add credentials to proxy uri */
  61. const grpc_arg* proxy_auth_arg =
  62. grpc_channel_args_find(client_args, GRPC_ARG_HTTP_PROXY_AUTH_CREDS);
  63. const char* proxy_auth_str = grpc_channel_arg_get_string(proxy_auth_arg);
  64. if (proxy_auth_str == nullptr) {
  65. gpr_asprintf(&proxy_uri, "http://%s",
  66. grpc_end2end_http_proxy_get_proxy_name(ffd->proxy));
  67. } else {
  68. gpr_asprintf(&proxy_uri, "http://%s@%s", proxy_auth_str,
  69. grpc_end2end_http_proxy_get_proxy_name(ffd->proxy));
  70. }
  71. gpr_setenv("http_proxy", proxy_uri);
  72. gpr_free(proxy_uri);
  73. f->client =
  74. grpc_insecure_channel_create(ffd->server_addr, client_args, nullptr);
  75. GPR_ASSERT(f->client);
  76. }
  77. void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f,
  78. grpc_channel_args* server_args) {
  79. fullstack_fixture_data* ffd =
  80. static_cast<fullstack_fixture_data*>(f->fixture_data);
  81. if (f->server) {
  82. grpc_server_destroy(f->server);
  83. }
  84. f->server = grpc_server_create(server_args, nullptr);
  85. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  86. GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->server_addr));
  87. grpc_server_start(f->server);
  88. }
  89. void chttp2_tear_down_fullstack(grpc_end2end_test_fixture* f) {
  90. fullstack_fixture_data* ffd =
  91. static_cast<fullstack_fixture_data*>(f->fixture_data);
  92. gpr_free(ffd->server_addr);
  93. grpc_end2end_http_proxy_destroy(ffd->proxy);
  94. gpr_free(ffd);
  95. }
  96. /* All test configurations */
  97. static grpc_end2end_test_config configs[] = {
  98. {"chttp2/fullstack",
  99. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  100. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  101. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
  102. chttp2_create_fixture_fullstack, chttp2_init_client_fullstack,
  103. chttp2_init_server_fullstack, chttp2_tear_down_fullstack},
  104. };
  105. int main(int argc, char** argv) {
  106. size_t i;
  107. grpc_test_init(argc, argv);
  108. grpc_end2end_tests_pre_init();
  109. grpc_init();
  110. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  111. grpc_end2end_tests(argc, argv, configs[i]);
  112. }
  113. grpc_shutdown();
  114. return 0;
  115. }