Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "test/core/end2end/end2end_tests.h"
35 :
36 : #include <stdio.h>
37 : #include <string.h>
38 :
39 : #include "src/core/channel/channel_args.h"
40 : #include "src/core/security/credentials.h"
41 : #include "src/core/support/env.h"
42 : #include "src/core/support/file.h"
43 : #include "src/core/support/string.h"
44 : #include <grpc/support/alloc.h>
45 : #include <grpc/support/host_port.h>
46 : #include <grpc/support/log.h>
47 : #include "test/core/util/test_config.h"
48 : #include "test/core/util/port.h"
49 : #include "test/core/end2end/data/ssl_test_data.h"
50 :
51 : typedef struct fullstack_secure_fixture_data {
52 : char *localaddr;
53 : } fullstack_secure_fixture_data;
54 :
55 84 : static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
56 : grpc_channel_args *client_args, grpc_channel_args *server_args) {
57 : grpc_end2end_test_fixture f;
58 84 : int port = grpc_pick_unused_port_or_die();
59 84 : fullstack_secure_fixture_data *ffd =
60 : gpr_malloc(sizeof(fullstack_secure_fixture_data));
61 84 : memset(&f, 0, sizeof(f));
62 :
63 84 : gpr_join_host_port(&ffd->localaddr, "localhost", port);
64 :
65 84 : f.fixture_data = ffd;
66 84 : f.cq = grpc_completion_queue_create(NULL);
67 :
68 84 : return f;
69 : }
70 :
71 1 : static void process_auth_failure(void *state, grpc_auth_context *ctx,
72 : const grpc_metadata *md, size_t md_count,
73 : grpc_process_auth_metadata_done_cb cb,
74 : void *user_data) {
75 1 : GPR_ASSERT(state == NULL);
76 1 : cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL);
77 1 : }
78 :
79 84 : static void chttp2_init_client_secure_fullstack(grpc_end2end_test_fixture *f,
80 : grpc_channel_args *client_args,
81 : grpc_credentials *creds) {
82 84 : fullstack_secure_fixture_data *ffd = f->fixture_data;
83 84 : f->client =
84 84 : grpc_secure_channel_create(creds, ffd->localaddr, client_args, NULL);
85 84 : GPR_ASSERT(f->client != NULL);
86 84 : grpc_credentials_release(creds);
87 84 : }
88 :
89 85 : static void chttp2_init_server_secure_fullstack(
90 : grpc_end2end_test_fixture *f, grpc_channel_args *server_args,
91 : grpc_server_credentials *server_creds) {
92 85 : fullstack_secure_fixture_data *ffd = f->fixture_data;
93 85 : if (f->server) {
94 1 : grpc_server_destroy(f->server);
95 : }
96 85 : f->server = grpc_server_create(server_args, NULL);
97 85 : grpc_server_register_completion_queue(f->server, f->cq, NULL);
98 85 : GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr,
99 : server_creds));
100 85 : grpc_server_credentials_release(server_creds);
101 85 : grpc_server_start(f->server);
102 85 : }
103 :
104 84 : void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) {
105 84 : fullstack_secure_fixture_data *ffd = f->fixture_data;
106 84 : gpr_free(ffd->localaddr);
107 84 : gpr_free(ffd);
108 84 : }
109 :
110 84 : static void chttp2_init_client_simple_ssl_secure_fullstack(
111 : grpc_end2end_test_fixture *f, grpc_channel_args *client_args) {
112 84 : grpc_credentials *ssl_creds = grpc_ssl_credentials_create(NULL, NULL, NULL);
113 84 : grpc_arg ssl_name_override = {GRPC_ARG_STRING,
114 : GRPC_SSL_TARGET_NAME_OVERRIDE_ARG,
115 : {"foo.test.google.fr"}};
116 84 : grpc_channel_args *new_client_args =
117 : grpc_channel_args_copy_and_add(client_args, &ssl_name_override, 1);
118 84 : chttp2_init_client_secure_fullstack(f, new_client_args, ssl_creds);
119 84 : grpc_channel_args_destroy(new_client_args);
120 84 : }
121 :
122 85 : static int fail_server_auth_check(grpc_channel_args *server_args) {
123 : size_t i;
124 85 : if (server_args == NULL) return 0;
125 19 : for (i = 0; i < server_args->num_args; i++) {
126 10 : if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
127 : 0) {
128 1 : return 1;
129 : }
130 : }
131 9 : return 0;
132 : }
133 :
134 85 : static void chttp2_init_server_simple_ssl_secure_fullstack(
135 : grpc_end2end_test_fixture *f, grpc_channel_args *server_args) {
136 85 : grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key,
137 : test_server1_cert};
138 85 : grpc_server_credentials *ssl_creds =
139 : grpc_ssl_server_credentials_create(NULL, &pem_cert_key_pair, 1, 0, NULL);
140 85 : if (fail_server_auth_check(server_args)) {
141 1 : grpc_auth_metadata_processor processor = {process_auth_failure, NULL, NULL};
142 1 : grpc_server_credentials_set_auth_metadata_processor(ssl_creds, processor);
143 : }
144 85 : chttp2_init_server_secure_fullstack(f, server_args, ssl_creds);
145 85 : }
146 :
147 : /* All test configurations */
148 :
149 : static grpc_end2end_test_config configs[] = {
150 : {"chttp2/simple_ssl_fullstack",
151 : FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION |
152 : FEATURE_MASK_SUPPORTS_PER_CALL_CREDENTIALS,
153 : chttp2_create_fixture_secure_fullstack,
154 : chttp2_init_client_simple_ssl_secure_fullstack,
155 : chttp2_init_server_simple_ssl_secure_fullstack,
156 : chttp2_tear_down_secure_fullstack},
157 : };
158 :
159 33 : int main(int argc, char **argv) {
160 : size_t i;
161 : FILE *roots_file;
162 33 : size_t roots_size = strlen(test_root_cert);
163 : char *roots_filename;
164 :
165 33 : grpc_platform_become_multipoller = grpc_poll_become_multipoller;
166 :
167 33 : grpc_test_init(argc, argv);
168 :
169 : /* Set the SSL roots env var. */
170 33 : roots_file = gpr_tmpfile("chttp2_simple_ssl_with_poll_fullstack_test",
171 : &roots_filename);
172 33 : GPR_ASSERT(roots_filename != NULL);
173 33 : GPR_ASSERT(roots_file != NULL);
174 33 : GPR_ASSERT(fwrite(test_root_cert, 1, roots_size, roots_file) == roots_size);
175 33 : fclose(roots_file);
176 33 : gpr_setenv(GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR, roots_filename);
177 :
178 33 : grpc_init();
179 :
180 66 : for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
181 33 : grpc_end2end_tests(configs[i]);
182 : }
183 :
184 33 : grpc_shutdown();
185 :
186 : /* Cleanup. */
187 33 : remove(roots_filename);
188 33 : gpr_free(roots_filename);
189 :
190 33 : return 0;
191 : }
|