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