• 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));
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 * client_args,grpc_channel_args * server_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   grpc_arg a[3];
83   a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
84   a[0].type = GRPC_ARG_INTEGER;
85   a[0].value.integer = 1;
86   a[1].key = const_cast<char*>(GRPC_ARG_TCP_MIN_READ_CHUNK_SIZE);
87   a[1].type = GRPC_ARG_INTEGER;
88   a[1].value.integer = 1;
89   a[2].key = const_cast<char*>(GRPC_ARG_TCP_MAX_READ_CHUNK_SIZE);
90   a[2].type = GRPC_ARG_INTEGER;
91   a[2].value.integer = 1;
92   grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
93   *sfd = grpc_iomgr_create_endpoint_pair("fixture", &args);
94 
95   return f;
96 }
97 
chttp2_init_client_socketpair(grpc_end2end_test_fixture * f,grpc_channel_args * client_args)98 static void chttp2_init_client_socketpair(grpc_end2end_test_fixture* f,
99                                           grpc_channel_args* client_args) {
100   grpc_core::ExecCtx exec_ctx;
101   grpc_endpoint_pair* sfd = static_cast<grpc_endpoint_pair*>(f->fixture_data);
102   grpc_transport* transport;
103   sp_client_setup cs;
104   cs.client_args = client_args;
105   cs.f = f;
106   transport = grpc_create_chttp2_transport(client_args, sfd->client, true);
107   client_setup_transport(&cs, transport);
108   GPR_ASSERT(f->client);
109   grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
110 }
111 
chttp2_init_server_socketpair(grpc_end2end_test_fixture * f,grpc_channel_args * server_args)112 static void chttp2_init_server_socketpair(grpc_end2end_test_fixture* f,
113                                           grpc_channel_args* server_args) {
114   grpc_core::ExecCtx exec_ctx;
115   grpc_endpoint_pair* sfd = static_cast<grpc_endpoint_pair*>(f->fixture_data);
116   grpc_transport* transport;
117   GPR_ASSERT(!f->server);
118   f->server = grpc_server_create(server_args, nullptr);
119   grpc_server_register_completion_queue(f->server, f->cq, nullptr);
120   grpc_server_start(f->server);
121   transport = grpc_create_chttp2_transport(server_args, sfd->server, false);
122   server_setup_transport(f, transport);
123   grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
124 }
125 
chttp2_tear_down_socketpair(grpc_end2end_test_fixture * f)126 static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) {
127   gpr_free(f->fixture_data);
128 }
129 
130 /* All test configurations */
131 static grpc_end2end_test_config configs[] = {
132     {"chttp2/socketpair_one_byte_at_a_time",
133      FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
134      chttp2_create_fixture_socketpair, chttp2_init_client_socketpair,
135      chttp2_init_server_socketpair, chttp2_tear_down_socketpair},
136 };
137 
main(int argc,char ** argv)138 int main(int argc, char** argv) {
139   size_t i;
140 
141   g_fixture_slowdown_factor = 2;
142 
143   grpc_test_init(argc, argv);
144   grpc_end2end_tests_pre_init();
145   grpc_init();
146 
147   for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
148     grpc_end2end_tests(argc, argv, configs[i]);
149   }
150 
151   grpc_shutdown();
152 
153   return 0;
154 }
155