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