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