2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
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/time.h"
18 static const char msg1[] = "The quick brown fox jumped over the lazy dogs.";
19 static char msg2[1024], msg3[1024];
21 static int is_want(SSL *s, int ret)
23 int ec = SSL_get_error(s, ret);
25 return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
28 static int test_tserver(void)
30 int testresult = 0, ret;
31 int s_fd = -1, c_fd = -1;
32 BIO *s_net_bio = NULL, *s_net_bio_own = NULL;
33 BIO *c_net_bio = NULL, *c_net_bio_own = NULL;
34 QUIC_TSERVER_ARGS tserver_args = {0};
35 QUIC_TSERVER *tserver = NULL;
36 BIO_ADDR *s_addr_ = NULL;
37 struct in_addr ina = {0};
38 union BIO_sock_info_u s_info = {0};
39 SSL_CTX *c_ctx = NULL;
42 int c_connected = 0, c_write_done = 0, c_begin_read = 0;
43 size_t l = 0, s_total_read = 0, s_total_written = 0, c_total_read = 0;
44 int s_begin_write = 0;
47 ina.s_addr = htonl(0x7f000001UL);
49 /* Setup test server. */
50 s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
51 if (!TEST_int_ge(s_fd, 0))
54 if (!TEST_true(BIO_socket_nbio(s_fd, 1)))
57 if (!TEST_ptr(s_addr_ = BIO_ADDR_new()))
60 if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina),
64 if (!TEST_true(BIO_bind(s_fd, s_addr_, 0)))
67 s_info.addr = s_addr_;
68 if (!TEST_true(BIO_sock_info(s_fd, BIO_SOCK_INFO_ADDRESS, &s_info)))
71 if (!TEST_int_gt(BIO_ADDR_rawport(s_addr_), 0))
74 if (!TEST_ptr(s_net_bio = s_net_bio_own = BIO_new_dgram(s_fd, 0)))
77 if (!BIO_up_ref(s_net_bio))
80 tserver_args.net_rbio = s_net_bio;
81 tserver_args.net_wbio = s_net_bio;
83 if (!TEST_ptr(tserver = ossl_quic_tserver_new(&tserver_args))) {
90 /* Setup test client. */
91 c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
92 if (!TEST_int_ge(c_fd, 0))
95 if (!TEST_true(BIO_socket_nbio(c_fd, 1)))
98 if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0)))
101 if (!BIO_dgram_set_peer(c_net_bio, s_addr_))
104 if (!TEST_ptr(c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
107 if (!TEST_ptr(c_ssl = SSL_new(c_ctx)))
110 /* Takes ownership of our reference to the BIO. */
111 SSL_set0_rbio(c_ssl, c_net_bio);
113 /* Get another reference to be transferred in the SSL_set0_wbio call. */
114 if (!TEST_true(BIO_up_ref(c_net_bio))) {
115 c_net_bio_own = NULL; /* SSL_free will free the first reference. */
119 SSL_set0_wbio(c_ssl, c_net_bio);
120 c_net_bio_own = NULL;
122 if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0)))
125 start_time = ossl_time_now();
128 if (ossl_time_compare(ossl_time_subtract(ossl_time_now(), start_time),
129 ossl_ms2time(1000)) >= 0) {
130 TEST_error("timeout while attempting QUIC server test");
134 ret = SSL_connect(c_ssl);
135 if (!TEST_true(ret == 1 || is_want(c_ssl, ret)))
141 if (c_connected && !c_write_done) {
142 if (!TEST_int_eq(SSL_write(c_ssl, msg1, sizeof(msg1) - 1),
143 (int)sizeof(msg1) - 1))
149 if (c_connected && c_write_done && s_total_read < sizeof(msg1) - 1) {
150 if (!TEST_true(ossl_quic_tserver_read(tserver,
151 (unsigned char *)msg2 + s_total_read,
152 sizeof(msg2) - s_total_read, &l)))
156 if (s_total_read == sizeof(msg1) - 1) {
157 if (!TEST_mem_eq(msg1, sizeof(msg1) - 1,
158 msg2, sizeof(msg1) - 1))
165 if (s_begin_write && s_total_written < sizeof(msg1) - 1) {
166 if (!TEST_true(ossl_quic_tserver_write(tserver,
167 (unsigned char *)msg2 + s_total_written,
168 sizeof(msg1) - 1 - s_total_written, &l)))
171 s_total_written += l;
173 if (s_total_written == sizeof(msg1) - 1)
177 if (c_begin_read && c_total_read < sizeof(msg1) - 1) {
178 ret = SSL_read_ex(c_ssl, msg3 + c_total_read,
179 sizeof(msg1) - 1 - c_total_read, &l);
180 if (!TEST_true(ret == 1 || is_want(c_ssl, ret)))
185 if (c_total_read == sizeof(msg1) - 1) {
186 if (!TEST_mem_eq(msg1, sizeof(msg1) - 1,
196 * This is inefficient because we spin until things work without
197 * blocking but this is just a test.
200 ossl_quic_tserver_tick(tserver);
207 ossl_quic_tserver_free(tserver);
208 BIO_ADDR_free(s_addr_);
209 BIO_free(s_net_bio_own);
210 BIO_free(c_net_bio_own);
212 BIO_closesocket(s_fd);
214 BIO_closesocket(c_fd);
218 int setup_tests(void)
220 ADD_TEST(test_tserver);