Debian Codegraph @

test/core/end2end/h2_ssl_session_reuse_test.cc

raw
1 /*
2 *
3 * Copyright 2018 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 <stdint.h>
20 #include <string.h>
21
22 #include <string>
23
24 #include <gtest/gtest.h>
25
26 #include <grpc/grpc.h>
27 #include <grpc/grpc_security.h>
28 #include <grpc/grpc_security_constants.h>
29 #include <grpc/impl/codegen/propagation_bits.h>
30 #include <grpc/slice.h>
31 #include <grpc/status.h>
32 #include <grpc/support/log.h>
33
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/gprpp/global_config_generic.h"
37 #include "src/core/lib/gprpp/host_port.h"
38 #include "src/core/lib/iomgr/error.h"
39 #include "src/core/lib/iomgr/exec_ctx.h"
40 #include "src/core/lib/iomgr/load_file.h"
41 #include "src/core/lib/security/security_connector/ssl_utils_config.h"
42 #include "test/core/end2end/cq_verifier.h"
43 #include "test/core/util/port.h"
44 #include "test/core/util/test_config.h"
45
46 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
47 #define CLIENT_CERT_PATH "src/core/tsi/test_creds/client.pem"
48 #define CLIENT_KEY_PATH "src/core/tsi/test_creds/client.key"
49 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
50 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
51
52 namespace grpc {
53 namespace testing {
54 namespace {
55
56 void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
57
58 gpr_timespec five_seconds_time() { return grpc_timeout_seconds_to_deadline(5); }
59
60 grpc_server* server_create(grpc_completion_queue* cq, const char* server_addr) {
61 grpc_slice ca_slice, cert_slice, key_slice;
62 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
63 grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
64 GPR_ASSERT(GRPC_LOG_IF_ERROR(
65 "load_file", grpc_load_file(SERVER_CERT_PATH, 1, &cert_slice)));
66 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
67 grpc_load_file(SERVER_KEY_PATH, 1, &key_slice)));
68 const char* ca_cert =
69 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
70 const char* server_cert =
71 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
72 const char* server_key =
73 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
74 grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {server_key, server_cert};
75 grpc_server_credentials* server_creds = grpc_ssl_server_credentials_create_ex(
76 ca_cert, &pem_cert_key_pair, 1,
77 GRPC_SSL_REQUEST_CLIENT_CERTIFICATE_AND_VERIFY, nullptr);
78
79 grpc_server* server = grpc_server_create(nullptr, nullptr);
80 grpc_server_register_completion_queue(server, cq, nullptr);
81 GPR_ASSERT(grpc_server_add_http2_port(server, server_addr, server_creds));
82 grpc_server_credentials_release(server_creds);
83 grpc_server_start(server);
84
85 grpc_slice_unref(cert_slice);
86 grpc_slice_unref(key_slice);
87 grpc_slice_unref(ca_slice);
88 return server;
89 }
90
91 grpc_channel* client_create(const char* server_addr,
92 grpc_ssl_session_cache* cache) {
93 grpc_slice ca_slice, cert_slice, key_slice;
94 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
95 grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
96 GPR_ASSERT(GRPC_LOG_IF_ERROR(
97 "load_file", grpc_load_file(CLIENT_CERT_PATH, 1, &cert_slice)));
98 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
99 grpc_load_file(CLIENT_KEY_PATH, 1, &key_slice)));
100 const char* ca_cert =
101 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
102 const char* client_cert =
103 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(cert_slice);
104 const char* client_key =
105 reinterpret_cast<const char*> GRPC_SLICE_START_PTR(key_slice);
106 grpc_ssl_pem_key_cert_pair signed_client_key_cert_pair = {client_key,
107 client_cert};
108 grpc_channel_credentials* client_creds = grpc_ssl_credentials_create(
109 ca_cert, &signed_client_key_cert_pair, nullptr, nullptr);
110
111 grpc_arg args[] = {
112 grpc_channel_arg_string_create(
113 const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
114 const_cast<char*>("waterzooi.test.google.be")),
115 grpc_ssl_session_cache_create_channel_arg(cache),
116 };
117
118 grpc_channel_args* client_args =
119 grpc_channel_args_copy_and_add(nullptr, args, GPR_ARRAY_SIZE(args));
120
121 grpc_channel* client =
122 grpc_channel_create(server_addr, client_creds, client_args);
123 GPR_ASSERT(client != nullptr);
124 grpc_channel_credentials_release(client_creds);
125
126 {
127 grpc_core::ExecCtx exec_ctx;
128 grpc_channel_args_destroy(client_args);
129 }
130
131 grpc_slice_unref(cert_slice);
132 grpc_slice_unref(key_slice);
133 grpc_slice_unref(ca_slice);
134 return client;
135 }
136
137 void do_round_trip(grpc_completion_queue* cq, grpc_server* server,
138 const char* server_addr, grpc_ssl_session_cache* cache,
139 bool expect_session_reuse) {
140 grpc_channel* client = client_create(server_addr, cache);
141
142 grpc_core::CqVerifier cqv(cq);
143 grpc_op ops[6];
144 grpc_op* op;
145 grpc_metadata_array initial_metadata_recv;
146 grpc_metadata_array trailing_metadata_recv;
147 grpc_metadata_array request_metadata_recv;
148 grpc_call_details call_details;
149 grpc_status_code status;
150 grpc_call_error error;
151 grpc_slice details;
152 int was_cancelled = 2;
153
154 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(60);
155 grpc_call* c = grpc_channel_create_call(
156 client, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
157 grpc_slice_from_static_string("/foo"), nullptr, deadline, nullptr);
158 GPR_ASSERT(c);
159
160 grpc_metadata_array_init(&initial_metadata_recv);
161 grpc_metadata_array_init(&trailing_metadata_recv);
162 grpc_metadata_array_init(&request_metadata_recv);
163 grpc_call_details_init(&call_details);
164
165 memset(ops, 0, sizeof(ops));
166 op = ops;
167 op->op = GRPC_OP_SEND_INITIAL_METADATA;
168 op->data.send_initial_metadata.count = 0;
169 op->flags = 0;
170 op->reserved = nullptr;
171 op++;
172 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
173 op->flags = 0;
174 op->reserved = nullptr;
175 op++;
176 op->op = GRPC_OP_RECV_INITIAL_METADATA;
177 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
178 op->flags = 0;
179 op->reserved = nullptr;
180 op++;
181 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
182 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
183 op->data.recv_status_on_client.status = &status;
184 op->data.recv_status_on_client.status_details = &details;
185 op->flags = 0;
186 op->reserved = nullptr;
187 op++;
188 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
189 nullptr);
190 GPR_ASSERT(GRPC_CALL_OK == error);
191
192 grpc_call* s;
193 error = grpc_server_request_call(server, &s, &call_details,
194 &request_metadata_recv, cq, cq, tag(101));
195 GPR_ASSERT(GRPC_CALL_OK == error);
196 cqv.Expect(tag(101), true);
197 cqv.Verify();
198
199 grpc_auth_context* auth = grpc_call_auth_context(s);
200 grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name(
201 auth, GRPC_SSL_SESSION_REUSED_PROPERTY);
202 const grpc_auth_property* property = grpc_auth_property_iterator_next(&it);
203 GPR_ASSERT(property != nullptr);
204 if (expect_session_reuse) {
205 GPR_ASSERT(strcmp(property->value, "true") == 0);
206 } else {
207 GPR_ASSERT(strcmp(property->value, "false") == 0);
208 }
209 grpc_auth_context_release(auth);
210
211 memset(ops, 0, sizeof(ops));
212 op = ops;
213 op->op = GRPC_OP_SEND_INITIAL_METADATA;
214 op->data.send_initial_metadata.count = 0;
215 op->flags = 0;
216 op->reserved = nullptr;
217 op++;
218 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
219 op->data.recv_close_on_server.cancelled = &was_cancelled;
220 op->flags = 0;
221 op->reserved = nullptr;
222 op++;
223 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
224 op->data.send_status_from_server.trailing_metadata_count = 0;
225 op->data.send_status_from_server.status = GRPC_STATUS_OK;
226 op->flags = 0;
227 op->reserved = nullptr;
228 op++;
229 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
230 nullptr);
231 GPR_ASSERT(GRPC_CALL_OK == error);
232
233 cqv.Expect(tag(103), true);
234 cqv.Expect(tag(1), true);
235 cqv.Verify();
236
237 grpc_metadata_array_destroy(&initial_metadata_recv);
238 grpc_metadata_array_destroy(&trailing_metadata_recv);
239 grpc_metadata_array_destroy(&request_metadata_recv);
240 grpc_call_details_destroy(&call_details);
241
242 grpc_call_unref(c);
243 grpc_call_unref(s);
244
245 grpc_channel_destroy(client);
246 }
247
248 void drain_cq(grpc_completion_queue* cq) {
249 grpc_event ev;
250 do {
251 ev = grpc_completion_queue_next(cq, five_seconds_time(), nullptr);
252 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
253 }
254
255 TEST(H2SessionReuseTest, SingleReuse) {
256 int port = grpc_pick_unused_port_or_die();
257
258 std::string server_addr = grpc_core::JoinHostPort("localhost", port);
259
260 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
261 grpc_ssl_session_cache* cache = grpc_ssl_session_cache_create_lru(16);
262
263 grpc_server* server = server_create(cq, server_addr.c_str());
264
265 do_round_trip(cq, server, server_addr.c_str(), cache, false);
266 do_round_trip(cq, server, server_addr.c_str(), cache, true);
267 do_round_trip(cq, server, server_addr.c_str(), cache, true);
268
269 grpc_ssl_session_cache_destroy(cache);
270
271 GPR_ASSERT(grpc_completion_queue_next(
272 cq, grpc_timeout_milliseconds_to_deadline(100), nullptr)
273 .type == GRPC_QUEUE_TIMEOUT);
274
275 grpc_server_shutdown_and_notify(server, cq, tag(1000));
276 grpc_event ev;
277 do {
278 ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
279 nullptr);
280 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
281 grpc_server_destroy(server);
282
283 grpc_completion_queue_shutdown(cq);
284 drain_cq(cq);
285 grpc_completion_queue_destroy(cq);
286 }
287
288 } // namespace
289 } // namespace testing
290 } // namespace grpc
291
292 int main(int argc, char** argv) {
293 grpc::testing::TestEnvironment env(&argc, argv);
294 GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, CA_CERT_PATH);
295
296 grpc_init();
297 ::testing::InitGoogleTest(&argc, argv);
298 int ret = RUN_ALL_TESTS();
299 grpc_shutdown();
300
301 return ret;
302 }