]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/quic_tserver_test.c
eefb5ae743b400e2807b8c319a5ee540deda7a35
[thirdparty/openssl.git] / test / quic_tserver_test.c
1 /*
2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9 #include <openssl/ssl.h>
10 #include <openssl/quic.h>
11 #include <openssl/bio.h>
12 #include "internal/common.h"
13 #include "internal/sockets.h"
14 #include "internal/quic_tserver.h"
15 #include "internal/quic_thread_assist.h"
16 #include "internal/quic_ssl.h"
17 #include "internal/time.h"
18 #include "testutil.h"
19
20 static const char msg1[] = "The quick brown fox jumped over the lazy dogs.";
21 static char msg2[1024], msg3[1024];
22 static OSSL_TIME fake_time;
23 static CRYPTO_RWLOCK *fake_time_lock;
24
25 static const char *certfile, *keyfile;
26
27 static int is_want(SSL *s, int ret)
28 {
29 int ec = SSL_get_error(s, ret);
30
31 return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
32 }
33
34 static unsigned char scratch_buf[2048];
35
36 static OSSL_TIME fake_now(void *arg)
37 {
38 OSSL_TIME t;
39
40 if (!CRYPTO_THREAD_read_lock(fake_time_lock))
41 return ossl_time_zero();
42
43 t = fake_time;
44
45 CRYPTO_THREAD_unlock(fake_time_lock);
46 return t;
47 }
48
49 static OSSL_TIME real_now(void *arg)
50 {
51 return ossl_time_now();
52 }
53
54 static int do_test(int use_thread_assist, int use_fake_time, int use_inject)
55 {
56 int testresult = 0, ret;
57 int s_fd = -1, c_fd = -1;
58 BIO *s_net_bio = NULL, *s_net_bio_own = NULL;
59 BIO *c_net_bio = NULL, *c_net_bio_own = NULL;
60 BIO *c_pair_own = NULL, *s_pair_own = NULL;
61 QUIC_TSERVER_ARGS tserver_args = {0};
62 QUIC_TSERVER *tserver = NULL;
63 BIO_ADDR *s_addr_ = NULL;
64 struct in_addr ina = {0};
65 union BIO_sock_info_u s_info = {0};
66 SSL_CTX *c_ctx = NULL;
67 SSL *c_ssl = NULL;
68 int c_connected = 0, c_write_done = 0, c_begin_read = 0, s_read_done = 0;
69 int c_wait_eos = 0, c_done_eos = 0;
70 int c_start_idle_test = 0, c_done_idle_test = 0;
71 size_t l = 0, s_total_read = 0, s_total_written = 0, c_total_read = 0;
72 size_t idle_units_done = 0;
73 int s_begin_write = 0;
74 OSSL_TIME start_time;
75 unsigned char alpn[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' };
76 OSSL_TIME (*now_cb)(void *arg) = use_fake_time ? fake_now : real_now;
77 size_t limit_ms = 1000;
78
79 #if defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
80 if (use_thread_assist) {
81 TEST_skip("thread assisted mode not enabled");
82 return 1;
83 }
84 #endif
85
86 ina.s_addr = htonl(0x7f000001UL);
87
88 /* Setup test server. */
89 s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
90 if (!TEST_int_ge(s_fd, 0))
91 goto err;
92
93 if (!TEST_true(BIO_socket_nbio(s_fd, 1)))
94 goto err;
95
96 if (!TEST_ptr(s_addr_ = BIO_ADDR_new()))
97 goto err;
98
99 if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina), 0)))
100 goto err;
101
102 if (!TEST_true(BIO_bind(s_fd, s_addr_, 0)))
103 goto err;
104
105 s_info.addr = s_addr_;
106 if (!TEST_true(BIO_sock_info(s_fd, BIO_SOCK_INFO_ADDRESS, &s_info)))
107 goto err;
108
109 if (!TEST_int_gt(BIO_ADDR_rawport(s_addr_), 0))
110 goto err;
111
112 if (!TEST_ptr(s_net_bio = s_net_bio_own = BIO_new_dgram(s_fd, 0)))
113 goto err;
114
115 if (!BIO_up_ref(s_net_bio))
116 goto err;
117
118 fake_time = ossl_ms2time(1000);
119
120 tserver_args.net_rbio = s_net_bio;
121 tserver_args.net_wbio = s_net_bio;
122 if (use_fake_time)
123 tserver_args.now_cb = fake_now;
124
125 if (!TEST_ptr(tserver = ossl_quic_tserver_new(&tserver_args, certfile,
126 keyfile))) {
127 BIO_free(s_net_bio);
128 goto err;
129 }
130
131 s_net_bio_own = NULL;
132
133 if (use_inject) {
134 /*
135 * In inject mode we create a dgram pair to feed to the QUIC client on
136 * the read side. We don't feed anything to this, it is just a
137 * placeholder to give the client something which never returns any
138 * datagrams.
139 */
140 if (!TEST_true(BIO_new_bio_dgram_pair(&c_pair_own, 5000,
141 &s_pair_own, 5000)))
142 goto err;
143 }
144
145 /* Setup test client. */
146 c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
147 if (!TEST_int_ge(c_fd, 0))
148 goto err;
149
150 if (!TEST_true(BIO_socket_nbio(c_fd, 1)))
151 goto err;
152
153 if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0)))
154 goto err;
155
156 if (!BIO_dgram_set_peer(c_net_bio, s_addr_))
157 goto err;
158
159 if (!TEST_ptr(c_ctx = SSL_CTX_new(use_thread_assist
160 ? OSSL_QUIC_client_thread_method()
161 : OSSL_QUIC_client_method())))
162 goto err;
163
164 if (!TEST_ptr(c_ssl = SSL_new(c_ctx)))
165 goto err;
166
167 if (use_fake_time)
168 ossl_quic_conn_set_override_now_cb(c_ssl, fake_now, NULL);
169
170 /* 0 is a success for SSL_set_alpn_protos() */
171 if (!TEST_false(SSL_set_alpn_protos(c_ssl, alpn, sizeof(alpn))))
172 goto err;
173
174 /* Takes ownership of our reference to the BIO. */
175 if (use_inject) {
176 SSL_set0_rbio(c_ssl, c_pair_own);
177 c_pair_own = NULL;
178 } else {
179 SSL_set0_rbio(c_ssl, c_net_bio);
180
181 /* Get another reference to be transferred in the SSL_set0_wbio call. */
182 if (!TEST_true(BIO_up_ref(c_net_bio))) {
183 c_net_bio_own = NULL; /* SSL_free will free the first reference. */
184 goto err;
185 }
186 }
187
188 SSL_set0_wbio(c_ssl, c_net_bio);
189 c_net_bio_own = NULL;
190
191 if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0)))
192 goto err;
193
194 start_time = now_cb(NULL);
195
196 for (;;) {
197 if (ossl_time_compare(ossl_time_subtract(now_cb(NULL), start_time),
198 ossl_ms2time(limit_ms)) >= 0) {
199 TEST_error("timeout while attempting QUIC server test");
200 goto err;
201 }
202
203 if (!c_start_idle_test) {
204 ret = SSL_connect(c_ssl);
205 if (!TEST_true(ret == 1 || is_want(c_ssl, ret)))
206 goto err;
207
208 if (ret == 1)
209 c_connected = 1;
210 }
211
212 if (c_connected && !c_write_done) {
213 if (!TEST_int_eq(SSL_write(c_ssl, msg1, sizeof(msg1) - 1),
214 (int)sizeof(msg1) - 1))
215 goto err;
216
217 if (!TEST_true(SSL_stream_conclude(c_ssl, 0)))
218 goto err;
219
220 c_write_done = 1;
221 }
222
223 if (c_connected && c_write_done && !s_read_done) {
224 if (!ossl_quic_tserver_read(tserver, 0,
225 (unsigned char *)msg2 + s_total_read,
226 sizeof(msg2) - s_total_read, &l)) {
227 if (!TEST_true(ossl_quic_tserver_has_read_ended(tserver, 0)))
228 goto err;
229
230 if (!TEST_mem_eq(msg1, sizeof(msg1) - 1, msg2, s_total_read))
231 goto err;
232
233 s_begin_write = 1;
234 s_read_done = 1;
235 } else {
236 s_total_read += l;
237 if (!TEST_size_t_le(s_total_read, sizeof(msg1) - 1))
238 goto err;
239 }
240 }
241
242 if (s_begin_write && s_total_written < sizeof(msg1) - 1) {
243 if (!TEST_true(ossl_quic_tserver_write(tserver, 0,
244 (unsigned char *)msg2 + s_total_written,
245 sizeof(msg1) - 1 - s_total_written, &l)))
246 goto err;
247
248 s_total_written += l;
249
250 if (s_total_written == sizeof(msg1) - 1) {
251 ossl_quic_tserver_conclude(tserver, 0);
252 c_begin_read = 1;
253 }
254 }
255
256 if (c_begin_read && c_total_read < sizeof(msg1) - 1) {
257 ret = SSL_read_ex(c_ssl, msg3 + c_total_read,
258 sizeof(msg1) - 1 - c_total_read, &l);
259 if (!TEST_true(ret == 1 || is_want(c_ssl, ret)))
260 goto err;
261
262 c_total_read += l;
263
264 if (c_total_read == sizeof(msg1) - 1) {
265 if (!TEST_mem_eq(msg1, sizeof(msg1) - 1,
266 msg3, c_total_read))
267 goto err;
268
269 c_wait_eos = 1;
270 }
271 }
272
273 if (c_wait_eos && !c_done_eos) {
274 unsigned char c;
275
276 ret = SSL_read_ex(c_ssl, &c, sizeof(c), &l);
277 if (!TEST_false(ret))
278 goto err;
279
280 /*
281 * Allow the implementation to take as long as it wants to finally
282 * notice EOS. Account for varied timings in OS networking stacks.
283 */
284 if (SSL_get_error(c_ssl, ret) != SSL_ERROR_WANT_READ) {
285 if (!TEST_int_eq(SSL_get_error(c_ssl, ret),
286 SSL_ERROR_ZERO_RETURN))
287 goto err;
288
289 c_done_eos = 1;
290 if (use_thread_assist && use_fake_time) {
291 if (!TEST_true(ossl_quic_tserver_is_connected(tserver)))
292 goto err;
293 c_start_idle_test = 1;
294 limit_ms = 120000; /* extend time limit */
295 } else {
296 /* DONE */
297 break;
298 }
299 }
300 }
301
302 if (c_start_idle_test && !c_done_idle_test) {
303 /* This is more than our default idle timeout of 30s. */
304 if (idle_units_done < 600) {
305 if (!TEST_true(CRYPTO_THREAD_write_lock(fake_time_lock)))
306 goto err;
307 fake_time = ossl_time_add(fake_time, ossl_ms2time(100));
308 CRYPTO_THREAD_unlock(fake_time_lock);
309
310 ++idle_units_done;
311 ossl_quic_conn_force_assist_thread_wake(c_ssl);
312 OSSL_sleep(1); /* Ensure CPU scheduling for test purposes */
313 } else {
314 c_done_idle_test = 1;
315 }
316 }
317
318 if (c_done_idle_test) {
319 /*
320 * If we have finished the fake idling duration, the connection
321 * should still be healthy in TA mode.
322 */
323 if (!TEST_true(ossl_quic_tserver_is_connected(tserver)))
324 goto err;
325
326 /* DONE */
327 break;
328 }
329
330 /*
331 * This is inefficient because we spin until things work without
332 * blocking but this is just a test.
333 */
334 if (!c_start_idle_test || c_done_idle_test) {
335 /* Inhibit manual ticking during idle test to test TA mode. */
336 SSL_handle_events(c_ssl);
337 }
338
339 ossl_quic_tserver_tick(tserver);
340
341 if (use_inject) {
342 BIO_MSG rmsg = {0};
343 size_t msgs_processed = 0;
344
345 for (;;) {
346 /*
347 * Manually spoonfeed received datagrams from the real BIO_dgram
348 * into QUIC via the injection interface, thereby testing the
349 * injection interface.
350 */
351 rmsg.data = scratch_buf;
352 rmsg.data_len = sizeof(scratch_buf);
353
354 if (!BIO_recvmmsg(c_net_bio, &rmsg, sizeof(rmsg), 1, 0, &msgs_processed)
355 || msgs_processed == 0 || rmsg.data_len == 0)
356 break;
357
358 if (!TEST_true(SSL_inject_net_dgram(c_ssl, rmsg.data, rmsg.data_len,
359 NULL, NULL)))
360 goto err;
361 }
362 }
363 }
364
365 testresult = 1;
366 err:
367 SSL_free(c_ssl);
368 SSL_CTX_free(c_ctx);
369 ossl_quic_tserver_free(tserver);
370 BIO_ADDR_free(s_addr_);
371 BIO_free(s_net_bio_own);
372 BIO_free(c_net_bio_own);
373 BIO_free(c_pair_own);
374 BIO_free(s_pair_own);
375 if (s_fd >= 0)
376 BIO_closesocket(s_fd);
377 if (c_fd >= 0)
378 BIO_closesocket(c_fd);
379 return testresult;
380 }
381
382 static int test_tserver(int idx)
383 {
384 int thread_assisted, use_fake_time, use_inject;
385
386 thread_assisted = idx % 2;
387 idx /= 2;
388
389 use_inject = idx % 2;
390 idx /= 2;
391
392 use_fake_time = idx % 2;
393
394 if (use_fake_time && !thread_assisted)
395 return 1;
396
397 return do_test(thread_assisted, use_fake_time, use_inject);
398 }
399
400 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
401
402 int setup_tests(void)
403 {
404 if (!test_skip_common_options()) {
405 TEST_error("Error parsing test options\n");
406 return 0;
407 }
408
409 if (!TEST_ptr(certfile = test_get_argument(0))
410 || !TEST_ptr(keyfile = test_get_argument(1)))
411 return 0;
412
413 if ((fake_time_lock = CRYPTO_THREAD_lock_new()) == NULL)
414 return 0;
415
416 ADD_ALL_TESTS(test_tserver, 2 * 2 * 2);
417 return 1;
418 }