h2_fd.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "src/core/lib/iomgr/port.h"
  19. // This test won't work except with posix sockets enabled
  20. #ifdef GRPC_POSIX_SOCKET
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <grpc/grpc.h>
  24. #include <grpc/grpc_posix.h>
  25. #include <grpc/grpc_security.h>
  26. #include <grpc/support/alloc.h>
  27. #include <grpc/support/log.h>
  28. #include "src/core/lib/iomgr/exec_ctx.h"
  29. #include "src/core/lib/iomgr/socket_utils_posix.h"
  30. #include "src/core/lib/iomgr/unix_sockets_posix.h"
  31. #include "test/core/end2end/end2end_tests.h"
  32. #include "test/core/util/test_config.h"
  33. typedef struct {
  34. int fd_pair[2];
  35. } sp_fixture_data;
  36. static void create_sockets(int sv[2]) {
  37. int flags;
  38. grpc_create_socketpair_if_unix(sv);
  39. flags = fcntl(sv[0], F_GETFL, 0);
  40. GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
  41. flags = fcntl(sv[1], F_GETFL, 0);
  42. GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
  43. GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0]) == GRPC_ERROR_NONE);
  44. GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1]) == GRPC_ERROR_NONE);
  45. }
  46. static grpc_end2end_test_fixture chttp2_create_fixture_socketpair(
  47. const grpc_channel_args* /*client_args*/,
  48. const grpc_channel_args* /*server_args*/) {
  49. sp_fixture_data* fixture_data =
  50. static_cast<sp_fixture_data*>(gpr_malloc(sizeof(*fixture_data)));
  51. grpc_end2end_test_fixture f;
  52. memset(&f, 0, sizeof(f));
  53. f.fixture_data = fixture_data;
  54. f.cq = grpc_completion_queue_create_for_next(nullptr);
  55. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  56. create_sockets(fixture_data->fd_pair);
  57. return f;
  58. }
  59. static void chttp2_init_client_socketpair(
  60. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
  61. grpc_core::ExecCtx exec_ctx;
  62. sp_fixture_data* sfd = static_cast<sp_fixture_data*>(f->fixture_data);
  63. GPR_ASSERT(!f->client);
  64. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  65. f->client = grpc_channel_create_from_fd("fixture_client", sfd->fd_pair[0],
  66. creds, client_args);
  67. grpc_channel_credentials_release(creds);
  68. GPR_ASSERT(f->client);
  69. }
  70. static void chttp2_init_server_socketpair(
  71. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
  72. grpc_core::ExecCtx exec_ctx;
  73. sp_fixture_data* sfd = static_cast<sp_fixture_data*>(f->fixture_data);
  74. GPR_ASSERT(!f->server);
  75. f->server = grpc_server_create(server_args, nullptr);
  76. GPR_ASSERT(f->server);
  77. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  78. grpc_server_start(f->server);
  79. grpc_server_credentials* creds = grpc_insecure_server_credentials_create();
  80. grpc_server_add_channel_from_fd(f->server, sfd->fd_pair[1], creds);
  81. grpc_server_credentials_release(creds);
  82. }
  83. static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) {
  84. gpr_free(f->fixture_data);
  85. }
  86. /* All test configurations */
  87. static grpc_end2end_test_config configs[] = {
  88. {"chttp2/fd", FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
  89. chttp2_create_fixture_socketpair, chttp2_init_client_socketpair,
  90. chttp2_init_server_socketpair, chttp2_tear_down_socketpair},
  91. };
  92. int main(int argc, char** argv) {
  93. size_t i;
  94. grpc::testing::TestEnvironment env(argc, argv);
  95. grpc_end2end_tests_pre_init();
  96. grpc_init();
  97. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  98. grpc_end2end_tests(argc, argv, configs[i]);
  99. }
  100. grpc_shutdown();
  101. return 0;
  102. }
  103. #else /* GRPC_POSIX_SOCKET */
  104. int main(int /* argc */, char** /* argv */) { return 1; }
  105. #endif /* GRPC_POSIX_SOCKET */