Debian Codegraph @

test/core/end2end/fixtures/h2_sockpair.cc

raw
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 <string.h>
20
21 #include "absl/status/status.h"
22 #include "absl/status/statusor.h"
23
24 #include <grpc/grpc.h>
25 #include <grpc/status.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/channel/channel_args_preconditioning.h"
32 #include "src/core/lib/channel/channelz.h"
33 #include "src/core/lib/config/core_configuration.h"
34 #include "src/core/lib/gprpp/ref_counted_ptr.h"
35 #include "src/core/lib/iomgr/endpoint.h"
36 #include "src/core/lib/iomgr/endpoint_pair.h"
37 #include "src/core/lib/iomgr/error.h"
38 #include "src/core/lib/iomgr/exec_ctx.h"
39 #include "src/core/lib/surface/channel.h"
40 #include "src/core/lib/surface/channel_stack_type.h"
41 #include "src/core/lib/surface/completion_queue.h"
42 #include "src/core/lib/surface/server.h"
43 #include "src/core/lib/transport/transport.h"
44 #include "src/core/lib/transport/transport_fwd.h"
45 #include "test/core/end2end/end2end_tests.h"
46 #include "test/core/util/test_config.h"
47
48 /* chttp2 transport that is immediately available (used for testing
49 connected_channel without a client_channel */
50
51 struct custom_fixture_data {
52 grpc_endpoint_pair ep;
53 };
54
55 static void server_setup_transport(void* ts, grpc_transport* transport) {
56 grpc_end2end_test_fixture* f = static_cast<grpc_end2end_test_fixture*>(ts);
57 grpc_core::ExecCtx exec_ctx;
58 custom_fixture_data* fixture_data =
59 static_cast<custom_fixture_data*>(f->fixture_data);
60 grpc_endpoint_add_to_pollset(fixture_data->ep.server, grpc_cq_pollset(f->cq));
61 grpc_core::Server* core_server = grpc_core::Server::FromC(f->server);
62 grpc_error_handle error = core_server->SetupTransport(
63 transport, nullptr, core_server->channel_args(), nullptr);
64 if (error.ok()) {
65 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
66 } else {
67 grpc_transport_destroy(transport);
68 }
69 }
70
71 typedef struct {
72 grpc_end2end_test_fixture* f;
73 const grpc_channel_args* client_args;
74 } sp_client_setup;
75
76 static void client_setup_transport(void* ts, grpc_transport* transport) {
77 sp_client_setup* cs = static_cast<sp_client_setup*>(ts);
78
79 auto args = grpc_core::ChannelArgs::FromC(cs->client_args)
80 .Set(GRPC_ARG_DEFAULT_AUTHORITY, "test-authority");
81 auto channel = grpc_core::Channel::Create(
82 "socketpair-target", args, GRPC_CLIENT_DIRECT_CHANNEL, transport);
83 if (channel.ok()) {
84 cs->f->client = channel->release()->c_ptr();
85 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
86 } else {
87 cs->f->client = grpc_lame_client_channel_create(
88 nullptr, static_cast<grpc_status_code>(channel.status().code()),
89 "lame channel");
90 grpc_transport_destroy(transport);
91 }
92 }
93
94 static grpc_end2end_test_fixture chttp2_create_fixture_socketpair(
95 const grpc_channel_args* /*client_args*/,
96 const grpc_channel_args* /*server_args*/) {
97 custom_fixture_data* fixture_data = static_cast<custom_fixture_data*>(
98 gpr_malloc(sizeof(custom_fixture_data)));
99 grpc_end2end_test_fixture f;
100 memset(&f, 0, sizeof(f));
101 f.fixture_data = fixture_data;
102 f.cq = grpc_completion_queue_create_for_next(nullptr);
103 fixture_data->ep = grpc_iomgr_create_endpoint_pair("fixture", nullptr);
104 return f;
105 }
106
107 static void chttp2_init_client_socketpair(
108 grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
109 grpc_core::ExecCtx exec_ctx;
110 auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
111 grpc_transport* transport;
112 sp_client_setup cs;
113 auto client_channel_args = grpc_core::CoreConfiguration::Get()
114 .channel_args_preconditioning()
115 .PreconditionChannelArgs(client_args);
116 cs.client_args = client_channel_args.ToC().release();
117 cs.f = f;
118 transport = grpc_create_chttp2_transport(client_channel_args,
119 fixture_data->ep.client, true);
120 client_setup_transport(&cs, transport);
121 grpc_channel_args_destroy(cs.client_args);
122 GPR_ASSERT(f->client);
123 }
124
125 static void chttp2_init_server_socketpair(
126 grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
127 grpc_core::ExecCtx exec_ctx;
128 auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
129 grpc_transport* transport;
130 GPR_ASSERT(!f->server);
131 f->server = grpc_server_create(server_args, nullptr);
132 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
133 grpc_server_start(f->server);
134 auto server_channel_args = grpc_core::CoreConfiguration::Get()
135 .channel_args_preconditioning()
136 .PreconditionChannelArgs(server_args);
137 transport = grpc_create_chttp2_transport(server_channel_args,
138 fixture_data->ep.server, false);
139 server_setup_transport(f, transport);
140 }
141
142 static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) {
143 grpc_core::ExecCtx exec_ctx;
144 gpr_free(f->fixture_data);
145 }
146
147 /* All test configurations */
148 static grpc_end2end_test_config configs[] = {
149 {"chttp2/socketpair", FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
150 chttp2_create_fixture_socketpair, chttp2_init_client_socketpair,
151 chttp2_init_server_socketpair, chttp2_tear_down_socketpair},
152 };
153
154 int main(int argc, char** argv) {
155 size_t i;
156
157 grpc::testing::TestEnvironment env(&argc, argv);
158 grpc_end2end_tests_pre_init();
159 grpc_init();
160
161 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
162 grpc_end2end_tests(argc, argv, configs[i]);
163 }
164
165 grpc_shutdown();
166
167 return 0;
168 }