h2_uds.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <inttypes.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <string>
  23. #include "absl/strings/str_format.h"
  24. #include <grpc/grpc_security.h>
  25. #include <grpc/support/alloc.h>
  26. #include <grpc/support/log.h>
  27. #include <grpc/support/sync.h>
  28. #include "src/core/ext/filters/client_channel/client_channel.h"
  29. #include "src/core/ext/filters/http/server/http_server_filter.h"
  30. #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
  31. #include "src/core/lib/channel/connected_channel.h"
  32. #include "src/core/lib/gpr/string.h"
  33. #include "src/core/lib/surface/channel.h"
  34. #include "src/core/lib/surface/server.h"
  35. #include "test/core/end2end/end2end_tests.h"
  36. #include "test/core/util/port.h"
  37. #include "test/core/util/test_config.h"
  38. struct fullstack_fixture_data {
  39. std::string localaddr;
  40. };
  41. static int unique = 1;
  42. static grpc_end2end_test_fixture chttp2_create_fixture_fullstack_base(
  43. std::string addr) {
  44. fullstack_fixture_data* ffd = new fullstack_fixture_data;
  45. ffd->localaddr = std::move(addr);
  46. grpc_end2end_test_fixture f;
  47. memset(&f, 0, sizeof(f));
  48. f.fixture_data = ffd;
  49. f.cq = grpc_completion_queue_create_for_next(nullptr);
  50. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  51. return f;
  52. }
  53. static grpc_end2end_test_fixture chttp2_create_fixture_fullstack(
  54. const grpc_channel_args* /*client_args*/,
  55. const grpc_channel_args* /*server_args*/) {
  56. gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
  57. const std::string localaddr = absl::StrFormat(
  58. "unix:/tmp/grpc_fullstack_test.%d.%" PRId64 ".%" PRId32 ".%d", getpid(),
  59. now.tv_sec, now.tv_nsec, unique++);
  60. return chttp2_create_fixture_fullstack_base(localaddr);
  61. }
  62. static grpc_end2end_test_fixture
  63. chttp2_create_fixture_fullstack_abstract_namespace(
  64. const grpc_channel_args* /*client_args*/,
  65. const grpc_channel_args* /*server_args*/) {
  66. gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
  67. const std::string localaddr = absl::StrFormat(
  68. "unix-abstract:grpc_fullstack_test.%d.%" PRId64 ".%" PRId32 ".%d",
  69. getpid(), now.tv_sec, now.tv_nsec, unique++);
  70. return chttp2_create_fixture_fullstack_base(localaddr);
  71. }
  72. void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f,
  73. const grpc_channel_args* client_args) {
  74. fullstack_fixture_data* ffd =
  75. static_cast<fullstack_fixture_data*>(f->fixture_data);
  76. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  77. f->client = grpc_channel_create(ffd->localaddr.c_str(), creds, client_args);
  78. grpc_channel_credentials_release(creds);
  79. }
  80. void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f,
  81. const grpc_channel_args* server_args) {
  82. fullstack_fixture_data* ffd =
  83. static_cast<fullstack_fixture_data*>(f->fixture_data);
  84. if (f->server) {
  85. grpc_server_destroy(f->server);
  86. }
  87. f->server = grpc_server_create(server_args, nullptr);
  88. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  89. grpc_server_credentials* server_creds =
  90. grpc_insecure_server_credentials_create();
  91. GPR_ASSERT(grpc_server_add_http2_port(f->server, ffd->localaddr.c_str(),
  92. server_creds));
  93. grpc_server_credentials_release(server_creds);
  94. grpc_server_start(f->server);
  95. }
  96. void chttp2_tear_down_fullstack(grpc_end2end_test_fixture* f) {
  97. fullstack_fixture_data* ffd =
  98. static_cast<fullstack_fixture_data*>(f->fixture_data);
  99. delete ffd;
  100. }
  101. /* All test configurations */
  102. static grpc_end2end_test_config configs[] = {
  103. {"chttp2/fullstack_uds",
  104. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  105. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  106. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
  107. nullptr, chttp2_create_fixture_fullstack, chttp2_init_client_fullstack,
  108. chttp2_init_server_fullstack, chttp2_tear_down_fullstack},
  109. #ifndef GPR_APPLE // Apple doesn't support an abstract socket
  110. {"chttp2/fullstack_uds_abstract_namespace",
  111. FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
  112. FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
  113. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
  114. nullptr, chttp2_create_fixture_fullstack_abstract_namespace,
  115. chttp2_init_client_fullstack, chttp2_init_server_fullstack,
  116. chttp2_tear_down_fullstack},
  117. #endif
  118. };
  119. int main(int argc, char** argv) {
  120. size_t i;
  121. grpc::testing::TestEnvironment env(argc, argv);
  122. grpc_end2end_tests_pre_init();
  123. grpc_init();
  124. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  125. grpc_end2end_tests(argc, argv, configs[i]);
  126. }
  127. grpc_shutdown();
  128. return 0;
  129. }