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/server/http_server_filter.h"
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/connected_channel.h"
32 #include "src/core/lib/gprpp/host_port.h"
33 #include "src/core/lib/surface/channel.h"
34 #include "src/core/lib/surface/server.h"
35 #include "test/core/util/port.h"
36 #include "test/core/util/test_config.h"
37
38 struct fullstack_fixture_data {
39 std::string localaddr;
40 };
41
chttp2_create_fixture_fullstack(grpc_channel_args *,grpc_channel_args *)42 static grpc_end2end_test_fixture chttp2_create_fixture_fullstack(
43 grpc_channel_args* /*client_args*/, grpc_channel_args* /*server_args*/) {
44 grpc_end2end_test_fixture f;
45 int port = grpc_pick_unused_port_or_die();
46 fullstack_fixture_data* ffd = new fullstack_fixture_data();
47 memset(&f, 0, sizeof(f));
48 ffd->localaddr = grpc_core::JoinHostPort("localhost", port);
49
50 f.fixture_data = ffd;
51 f.cq = grpc_completion_queue_create_for_next(nullptr);
52 f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
53
54 return f;
55 }
56
make_census_enable_arg(void)57 static grpc_arg make_census_enable_arg(void) {
58 grpc_arg arg;
59 arg.type = GRPC_ARG_INTEGER;
60 arg.key = const_cast<char*>(GRPC_ARG_ENABLE_CENSUS);
61 arg.value.integer = 1;
62 return arg;
63 }
64
chttp2_init_client_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * client_args)65 void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f,
66 grpc_channel_args* client_args) {
67 fullstack_fixture_data* ffd =
68 static_cast<fullstack_fixture_data*>(f->fixture_data);
69 grpc_arg arg = make_census_enable_arg();
70 client_args = grpc_channel_args_copy_and_add(client_args, &arg, 1);
71 f->client = grpc_insecure_channel_create(ffd->localaddr.c_str(), client_args,
72 nullptr);
73 GPR_ASSERT(f->client);
74 {
75 grpc_core::ExecCtx exec_ctx;
76 grpc_channel_args_destroy(client_args);
77 }
78 }
79
chttp2_init_server_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * server_args)80 void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f,
81 grpc_channel_args* server_args) {
82 fullstack_fixture_data* ffd =
83 static_cast<fullstack_fixture_data*>(f->fixture_data);
84 grpc_arg arg = make_census_enable_arg();
85 if (f->server) {
86 grpc_server_destroy(f->server);
87 }
88 server_args = grpc_channel_args_copy_and_add(server_args, &arg, 1);
89 f->server = grpc_server_create(server_args, nullptr);
90 {
91 grpc_core::ExecCtx exec_ctx;
92 grpc_channel_args_destroy(server_args);
93 }
94 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
95 GPR_ASSERT(
96 grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.c_str()));
97 grpc_server_start(f->server);
98 }
99
chttp2_tear_down_fullstack(grpc_end2end_test_fixture * f)100 void chttp2_tear_down_fullstack(grpc_end2end_test_fixture* f) {
101 fullstack_fixture_data* ffd =
102 static_cast<fullstack_fixture_data*>(f->fixture_data);
103 delete ffd;
104 }
105
106 /* All test configurations */
107 static grpc_end2end_test_config configs[] = {
108 {"chttp2/fullstack+census",
109 FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
110 FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
111 FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
112 nullptr, chttp2_create_fixture_fullstack, chttp2_init_client_fullstack,
113 chttp2_init_server_fullstack, chttp2_tear_down_fullstack},
114 };
115
main(int argc,char ** argv)116 int main(int argc, char** argv) {
117 size_t i;
118
119 grpc::testing::TestEnvironment env(argc, argv);
120 grpc_end2end_tests_pre_init();
121 grpc_init();
122
123 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
124 grpc_end2end_tests(argc, argv, configs[i]);
125 }
126
127 grpc_shutdown();
128
129 return 0;
130 }
131