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 <stdio.h>
22 #include <string.h>
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gpr/host_port.h"
29 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
30 #include "test/core/end2end/data/ssl_test_data.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_config.h"
33
34 typedef struct fullstack_secure_fixture_data {
35 char* localaddr;
36 } fullstack_secure_fixture_data;
37
chttp2_create_fixture_secure_fullstack(grpc_channel_args * client_args,grpc_channel_args * server_args)38 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
39 grpc_channel_args* client_args, grpc_channel_args* server_args) {
40 grpc_end2end_test_fixture f;
41 int port = grpc_pick_unused_port_or_die();
42 fullstack_secure_fixture_data* ffd =
43 static_cast<fullstack_secure_fixture_data*>(
44 gpr_malloc(sizeof(fullstack_secure_fixture_data)));
45
46 memset(&f, 0, sizeof(f));
47 gpr_join_host_port(&ffd->localaddr, "localhost", port);
48
49 f.fixture_data = ffd;
50 f.cq = grpc_completion_queue_create_for_next(nullptr);
51 f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
52
53 return f;
54 }
55
process_auth_failure(void * state,grpc_auth_context * ctx,const grpc_metadata * md,size_t md_count,grpc_process_auth_metadata_done_cb cb,void * user_data)56 static void process_auth_failure(void* state, grpc_auth_context* ctx,
57 const grpc_metadata* md, size_t md_count,
58 grpc_process_auth_metadata_done_cb cb,
59 void* user_data) {
60 GPR_ASSERT(state == nullptr);
61 cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
62 }
63
chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * client_args,grpc_channel_credentials * creds)64 static void chttp2_init_client_secure_fullstack(
65 grpc_end2end_test_fixture* f, grpc_channel_args* client_args,
66 grpc_channel_credentials* creds) {
67 fullstack_secure_fixture_data* ffd =
68 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
69 f->client =
70 grpc_secure_channel_create(creds, ffd->localaddr, client_args, nullptr);
71 GPR_ASSERT(f->client != nullptr);
72 grpc_channel_credentials_release(creds);
73 }
74
chttp2_init_server_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * server_args,grpc_server_credentials * server_creds)75 static void chttp2_init_server_secure_fullstack(
76 grpc_end2end_test_fixture* f, grpc_channel_args* server_args,
77 grpc_server_credentials* server_creds) {
78 fullstack_secure_fixture_data* ffd =
79 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
80 if (f->server) {
81 grpc_server_destroy(f->server);
82 }
83 f->server = grpc_server_create(server_args, nullptr);
84 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
85 GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr,
86 server_creds));
87 grpc_server_credentials_release(server_creds);
88 grpc_server_start(f->server);
89 }
90
chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture * f)91 void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
92 fullstack_secure_fixture_data* ffd =
93 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
94 gpr_free(ffd->localaddr);
95 gpr_free(ffd);
96 }
97
chttp2_init_client_fake_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * client_args)98 static void chttp2_init_client_fake_secure_fullstack(
99 grpc_end2end_test_fixture* f, grpc_channel_args* client_args) {
100 grpc_channel_credentials* fake_ts_creds =
101 grpc_fake_transport_security_credentials_create();
102 chttp2_init_client_secure_fullstack(f, client_args, fake_ts_creds);
103 }
104
fail_server_auth_check(grpc_channel_args * server_args)105 static int fail_server_auth_check(grpc_channel_args* server_args) {
106 size_t i;
107 if (server_args == nullptr) return 0;
108 for (i = 0; i < server_args->num_args; i++) {
109 if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
110 0) {
111 return 1;
112 }
113 }
114 return 0;
115 }
116
chttp2_init_server_fake_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * server_args)117 static void chttp2_init_server_fake_secure_fullstack(
118 grpc_end2end_test_fixture* f, grpc_channel_args* server_args) {
119 grpc_server_credentials* fake_ts_creds =
120 grpc_fake_transport_security_server_credentials_create();
121 if (fail_server_auth_check(server_args)) {
122 grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
123 nullptr};
124 grpc_server_credentials_set_auth_metadata_processor(fake_ts_creds,
125 processor);
126 }
127 chttp2_init_server_secure_fullstack(f, server_args, fake_ts_creds);
128 }
129
130 /* All test configurations */
131
132 static grpc_end2end_test_config configs[] = {
133 {"chttp2/fake_secure_fullstack",
134 FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
135 FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
136 FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
137 nullptr, chttp2_create_fixture_secure_fullstack,
138 chttp2_init_client_fake_secure_fullstack,
139 chttp2_init_server_fake_secure_fullstack,
140 chttp2_tear_down_secure_fullstack},
141 };
142
main(int argc,char ** argv)143 int main(int argc, char** argv) {
144 size_t i;
145 grpc_test_init(argc, argv);
146 grpc_end2end_tests_pre_init();
147 grpc_init();
148
149 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
150 grpc_end2end_tests(argc, argv, configs[i]);
151 }
152
153 grpc_shutdown();
154
155 return 0;
156 }
157