3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "test/core/end2end/end2end_tests.h"
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gpr/host_port.h"
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/gpr/tmpfile.h"
31 #include "src/core/lib/security/credentials/credentials.h"
32 #include "src/core/lib/security/security_connector/ssl_utils.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"
37 typedef struct fullstack_secure_fixture_data {
39 bool server_credential_reloaded;
40 } fullstack_secure_fixture_data;
42 static grpc_ssl_certificate_config_reload_status
43 ssl_server_certificate_config_callback(
44 void* user_data, grpc_ssl_server_certificate_config** config) {
45 if (config == nullptr) {
46 return GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_FAIL;
48 fullstack_secure_fixture_data* ffd =
49 static_cast<fullstack_secure_fixture_data*>(user_data);
50 if (!ffd->server_credential_reloaded) {
51 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
53 *config = grpc_ssl_server_certificate_config_create(test_root_cert,
54 &pem_key_cert_pair, 1);
55 ffd->server_credential_reloaded = true;
56 return GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_NEW;
58 return GRPC_SSL_CERTIFICATE_CONFIG_RELOAD_UNCHANGED;
62 static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
63 grpc_channel_args* client_args, grpc_channel_args* server_args) {
64 grpc_end2end_test_fixture f;
65 int port = grpc_pick_unused_port_or_die();
66 fullstack_secure_fixture_data* ffd =
67 static_cast<fullstack_secure_fixture_data*>(
68 gpr_malloc(sizeof(fullstack_secure_fixture_data)));
69 memset(&f, 0, sizeof(f));
70 gpr_join_host_port(&ffd->localaddr, "localhost", port);
73 f.cq = grpc_completion_queue_create_for_next(nullptr);
74 f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
79 static void process_auth_failure(void* state, grpc_auth_context* ctx,
80 const grpc_metadata* md, size_t md_count,
81 grpc_process_auth_metadata_done_cb cb,
83 GPR_ASSERT(state == nullptr);
84 cb(user_data, nullptr, 0, nullptr, 0, GRPC_STATUS_UNAUTHENTICATED, nullptr);
87 static void chttp2_init_client_secure_fullstack(
88 grpc_end2end_test_fixture* f, grpc_channel_args* client_args,
89 grpc_channel_credentials* creds) {
90 fullstack_secure_fixture_data* ffd =
91 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
93 grpc_secure_channel_create(creds, ffd->localaddr, client_args, nullptr);
94 GPR_ASSERT(f->client != nullptr);
95 grpc_channel_credentials_release(creds);
98 static void chttp2_init_server_secure_fullstack(
99 grpc_end2end_test_fixture* f, grpc_channel_args* server_args,
100 grpc_server_credentials* server_creds) {
101 fullstack_secure_fixture_data* ffd =
102 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
104 grpc_server_destroy(f->server);
106 ffd->server_credential_reloaded = false;
107 f->server = grpc_server_create(server_args, nullptr);
108 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
109 GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr,
111 grpc_server_credentials_release(server_creds);
112 grpc_server_start(f->server);
115 void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture* f) {
116 fullstack_secure_fixture_data* ffd =
117 static_cast<fullstack_secure_fixture_data*>(f->fixture_data);
118 gpr_free(ffd->localaddr);
122 static void chttp2_init_client_simple_ssl_secure_fullstack(
123 grpc_end2end_test_fixture* f, grpc_channel_args* client_args) {
124 grpc_channel_credentials* ssl_creds =
125 grpc_ssl_credentials_create(nullptr, nullptr, nullptr, nullptr);
126 grpc_arg ssl_name_override = {
128 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
129 {const_cast<char*>("foo.test.google.fr")}};
130 grpc_channel_args* new_client_args =
131 grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1);
132 chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds);
133 grpc_channel_args_destroy(new_client_args);
136 static int fail_server_auth_check(grpc_channel_args* server_args) {
138 if (server_args == nullptr) return 0;
139 for (i = 0; i < server_args->num_args; i++) {
140 if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
148 static void chttp2_init_server_simple_ssl_secure_fullstack(
149 grpc_end2end_test_fixture* f, grpc_channel_args* server_args) {
150 grpc_ssl_server_credentials_options* options =
151 grpc_ssl_server_credentials_create_options_using_config_fetcher(
152 GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE,
153 ssl_server_certificate_config_callback, f->fixture_data);
154 grpc_server_credentials* ssl_creds =
155 grpc_ssl_server_credentials_create_with_options(options);
156 if (fail_server_auth_check(server_args)) {
157 grpc_auth_metadata_processor processor = {process_auth_failure, nullptr,
159 grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor);
161 chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
164 /* All test configurations */
166 static grpc_end2end_test_config configs[] = {
167 {"chttp2/simple_ssl_fullstack",
168 FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
169 FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS |
170 FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL |
171 FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER,
172 "foo.test.google.fr", chttp2_create_fixture_secure_fullstack,
173 chttp2_init_client_simple_ssl_secure_fullstack,
174 chttp2_init_server_simple_ssl_secure_fullstack,
175 chttp2_tear_down_secure_fullstack},
178 int main(int argc, char** argv) {
181 size_t roots_size = strlen(test_root_cert);
182 char* roots_filename;
184 grpc::testing::TestEnvironment env(argc, argv);
185 grpc_end2end_tests_pre_init();
187 /* Set the SSL roots env var. */
188 roots_file = gpr_tmpfile("chttp2_simple_ssl_fullstack_test", &roots_filename);
189 GPR_ASSERT(roots_filename != nullptr);
190 GPR_ASSERT(roots_file != nullptr);
191 GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size);
193 GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, roots_filename);
197 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
198 grpc_end2end_tests(argc, argv, configs[i]);
204 remove(roots_filename);
205 gpr_free(roots_filename);