• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
22 
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26 
27 #include "src/core/ext/filters/client_channel/client_channel.h"
28 #include "src/core/ext/filters/http/client/http_client_filter.h"
29 #include "src/core/ext/filters/http/message_compress/message_compress_filter.h"
30 #include "src/core/ext/filters/http/server/http_server_filter.h"
31 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
32 #include "src/core/lib/channel/connected_channel.h"
33 #include "src/core/lib/iomgr/endpoint_pair.h"
34 #include "src/core/lib/iomgr/iomgr.h"
35 #include "src/core/lib/surface/channel.h"
36 #include "src/core/lib/surface/completion_queue.h"
37 #include "src/core/lib/surface/server.h"
38 #include "test/core/util/port.h"
39 #include "test/core/util/test_config.h"
40 
41 /* chttp2 transport that is immediately available (used for testing
42    connected_channel without a client_channel */
43 
server_setup_transport(void * ts,grpc_transport * transport)44 static void server_setup_transport(void* ts, grpc_transport* transport) {
45   grpc_end2end_test_fixture* f = static_cast<grpc_end2end_test_fixture*>(ts);
46   grpc_core::ExecCtx exec_ctx;
47   grpc_endpoint_pair* sfd = static_cast<grpc_endpoint_pair*>(f->fixture_data);
48   grpc_endpoint_add_to_pollset(sfd->server, grpc_cq_pollset(f->cq));
49   grpc_server_setup_transport(f->server, transport, nullptr,
50                               grpc_server_get_channel_args(f->server), nullptr);
51 }
52 
53 typedef struct {
54   grpc_end2end_test_fixture* f;
55   grpc_channel_args* client_args;
56 } sp_client_setup;
57 
client_setup_transport(void * ts,grpc_transport * transport)58 static void client_setup_transport(void* ts, grpc_transport* transport) {
59   sp_client_setup* cs = static_cast<sp_client_setup*>(ts);
60 
61   grpc_arg authority_arg = grpc_channel_arg_string_create(
62       const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY),
63       const_cast<char*>("test-authority"));
64   grpc_channel_args* args =
65       grpc_channel_args_copy_and_add(cs->client_args, &authority_arg, 1);
66   cs->f->client = grpc_channel_create("socketpair-target", args,
67                                       GRPC_CLIENT_DIRECT_CHANNEL, transport);
68   grpc_channel_args_destroy(args);
69 }
70 
chttp2_create_fixture_socketpair(grpc_channel_args *,grpc_channel_args *)71 static grpc_end2end_test_fixture chttp2_create_fixture_socketpair(
72     grpc_channel_args* /*client_args*/, grpc_channel_args* /*server_args*/) {
73   grpc_endpoint_pair* sfd =
74       static_cast<grpc_endpoint_pair*>(gpr_malloc(sizeof(grpc_endpoint_pair)));
75 
76   grpc_end2end_test_fixture f;
77   memset(&f, 0, sizeof(f));
78   f.fixture_data = sfd;
79   f.cq = grpc_completion_queue_create_for_next(nullptr);
80   f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
81 
82   *sfd = grpc_iomgr_create_endpoint_pair("fixture", nullptr);
83 
84   return f;
85 }
86 
chttp2_init_client_socketpair(grpc_end2end_test_fixture * f,grpc_channel_args * client_args)87 static void chttp2_init_client_socketpair(grpc_end2end_test_fixture* f,
88                                           grpc_channel_args* client_args) {
89   grpc_core::ExecCtx exec_ctx;
90   grpc_endpoint_pair* sfd = static_cast<grpc_endpoint_pair*>(f->fixture_data);
91   grpc_transport* transport;
92   sp_client_setup cs;
93   cs.client_args = client_args;
94   cs.f = f;
95   transport = grpc_create_chttp2_transport(client_args, sfd->client, true);
96   client_setup_transport(&cs, transport);
97   GPR_ASSERT(f->client);
98   grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
99 }
100 
chttp2_init_server_socketpair(grpc_end2end_test_fixture * f,grpc_channel_args * server_args)101 static void chttp2_init_server_socketpair(grpc_end2end_test_fixture* f,
102                                           grpc_channel_args* server_args) {
103   grpc_core::ExecCtx exec_ctx;
104   grpc_endpoint_pair* sfd = static_cast<grpc_endpoint_pair*>(f->fixture_data);
105   grpc_transport* transport;
106   GPR_ASSERT(!f->server);
107   f->server = grpc_server_create(server_args, nullptr);
108   grpc_server_register_completion_queue(f->server, f->cq, nullptr);
109   grpc_server_start(f->server);
110   transport = grpc_create_chttp2_transport(server_args, sfd->server, false);
111   server_setup_transport(f, transport);
112   grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
113 }
114 
chttp2_tear_down_socketpair(grpc_end2end_test_fixture * f)115 static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) {
116   gpr_free(f->fixture_data);
117 }
118 
119 /* All test configurations */
120 static grpc_end2end_test_config configs[] = {
121     {"chttp2/socketpair", FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
122      chttp2_create_fixture_socketpair, chttp2_init_client_socketpair,
123      chttp2_init_server_socketpair, chttp2_tear_down_socketpair},
124 };
125 
main(int argc,char ** argv)126 int main(int argc, char** argv) {
127   size_t i;
128 
129   grpc::testing::TestEnvironment env(argc, argv);
130   grpc_end2end_tests_pre_init();
131   grpc_init();
132 
133   for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
134     grpc_end2end_tests(argc, argv, configs[i]);
135   }
136 
137   grpc_shutdown();
138 
139   return 0;
140 }
141