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