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/env.h"
29 #include "src/core/lib/gpr/host_port.h"
30 #include "src/core/lib/gpr/string.h"
31 #include "src/core/lib/gpr/tmpfile.h"
32 #include "src/core/lib/security/credentials/credentials.h"
33 #include "test/core/end2end/data/ssl_test_data.h"
34 #include "test/core/util/port.h"
35 #include "test/core/util/test_config.h"
36
37 typedef struct fullstack_secure_fixture_data {
38 char* localaddr;
39 } fullstack_secure_fixture_data;
40
chttp2_create_fixture_secure_fullstack(grpc_channel_args * client_args,grpc_channel_args * server_args)41 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
42 grpc_channel_args* client_args, grpc_channel_args* server_args) {
43 grpc_end2end_test_fixture f;
44 int port = grpc_pick_unused_port_or_die();
45 fullstack_secure_fixture_data* ffd =
46 static_cast<fullstack_secure_fixture_data*>(
47 gpr_malloc(sizeof(fullstack_secure_fixture_data)));
48 memset(&f, 0, sizeof(f));
49
50 gpr_join_host_port(&ffd->localaddr, "localhost", port);
51
52 f.fixture_data = ffd;
53 f.cq = grpc_completion_queue_create_for_next(nullptr);
54 f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
55
56 return f;
57 }
58
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)59 static void process_auth_failure(void* state, grpc_auth_context* ctx,
60 const grpc_metadata* md, size_t md_count,
61 grpc_process_auth_metadata_done_cb cb,
62 void* user_data) {
63 GPR_ASSERT(state == nullptr);
64 cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
65 }
66
chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * client_args,grpc_channel_credentials * creds)67 static void chttp2_init_client_secure_fullstack(
68 grpc_end2end_test_fixture* f, grpc_channel_args* client_args,
69 grpc_channel_credentials* creds) {
70 fullstack_secure_fixture_data* ffd =
71 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
72 f->client =
73 grpc_secure_channel_create(creds, ffd->localaddr, client_args, nullptr);
74 GPR_ASSERT(f->client != nullptr);
75 grpc_channel_credentials_release(creds);
76 }
77
chttp2_init_server_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * server_args,grpc_server_credentials * server_creds)78 static void chttp2_init_server_secure_fullstack(
79 grpc_end2end_test_fixture* f, grpc_channel_args* server_args,
80 grpc_server_credentials* server_creds) {
81 fullstack_secure_fixture_data* ffd =
82 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
83 if (f->server) {
84 grpc_server_destroy(f->server);
85 }
86 f->server = grpc_server_create(server_args, nullptr);
87 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
88 GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr,
89 server_creds));
90 grpc_server_credentials_release(server_creds);
91 grpc_server_start(f->server);
92 }
93
chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture * f)94 void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
95 fullstack_secure_fixture_data* ffd =
96 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
97 gpr_free(ffd->localaddr);
98 gpr_free(ffd);
99 }
100
chttp2_init_client_simple_ssl_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * client_args)101 static void chttp2_init_client_simple_ssl_secure_fullstack(
102 grpc_end2end_test_fixture* f, grpc_channel_args* client_args) {
103 grpc_channel_credentials* ssl_creds =
104 grpc_ssl_credentials_create(nullptr, nullptr, nullptr, nullptr);
105 grpc_arg ssl_name_override = {
106 GRPC_ARG_STRING,
107 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
108 {const_cast<char*>("foo.test.google.fr")}};
109 grpc_channel_args* new_client_args =
110 grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1);
111 chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds);
112 grpc_channel_args_destroy(new_client_args);
113 }
114
fail_server_auth_check(grpc_channel_args * server_args)115 static int fail_server_auth_check(grpc_channel_args* server_args) {
116 size_t i;
117 if (server_args == nullptr) return 0;
118 for (i = 0; i < server_args->num_args; i++) {
119 if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
120 0) {
121 return 1;
122 }
123 }
124 return 0;
125 }
126
chttp2_init_server_simple_ssl_secure_fullstack(grpc_end2end_test_fixture * f,grpc_channel_args * server_args)127 static void chttp2_init_server_simple_ssl_secure_fullstack(
128 grpc_end2end_test_fixture* f, grpc_channel_args* server_args) {
129 grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key,
130 test_server1_cert};
131 grpc_server_credentials* ssl_creds = grpc_ssl_server_credentials_create(
132 nullptr, &pem_cert_key_pair, 1, 0, nullptr);
133 if (fail_server_auth_check(server_args)) {
134 grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
135 nullptr};
136 grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor);
137 }
138 chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
139 }
140
141 /* All test configurations */
142
143 static grpc_end2end_test_config configs[] = {
144 {"chttp2/simple_ssl_fullstack",
145 FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
146 FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
147 FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
148 FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
149 "foo.test.google.fr", chttp2_create_fixture_secure_fullstack,
150 chttp2_init_client_simple_ssl_secure_fullstack,
151 chttp2_init_server_simple_ssl_secure_fullstack,
152 chttp2_tear_down_secure_fullstack},
153 };
154
main(int argc,char ** argv)155 int main(int argc, char** argv) {
156 size_t i;
157 FILE* roots_file;
158 size_t roots_size = strlen(test_root_cert);
159 char* roots_filename;
160
161 grpc_test_init(argc, argv);
162 grpc_end2end_tests_pre_init();
163
164 /* Set the SSL roots env var. */
165 roots_file = gpr_tmpfile("chttp2_simple_ssl_fullstack_test", &roots_filename);
166 GPR_ASSERT(roots_filename != nullptr);
167 GPR_ASSERT(roots_file != nullptr);
168 GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size);
169 fclose(roots_file);
170 gpr_setenv(GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR, roots_filename);
171
172 grpc_init();
173
174 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
175 grpc_end2end_tests(argc, argv, configs[i]);
176 }
177
178 grpc_shutdown();
179
180 /* Cleanup. */
181 remove(roots_filename);
182 gpr_free(roots_filename);
183
184 return 0;
185 }
186