Add a test for public variants of bn2bin()
[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/time.h"
16 #include "testutil.h"
17
18 static const char msg1[] = "The quick brown fox jumped over the lazy dogs.";
19 static char msg2[1024], msg3[1024];
20
21 static int is_want(SSL *s, int ret)
22 {
23     int ec = SSL_get_error(s, ret);
24
25     return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
26 }
27
28 static int test_tserver(void)
29 {
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;
40     SSL *c_ssl = NULL;
41     short port = 8186;
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;
45     OSSL_TIME start_time;
46
47     ina.s_addr = htonl(0x7f000001UL);
48
49     /* Setup test server. */
50     s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
51     if (!TEST_int_ge(s_fd, 0))
52         goto err;
53
54     if (!TEST_true(BIO_socket_nbio(s_fd, 1)))
55         goto err;
56
57     if (!TEST_ptr(s_addr_ = BIO_ADDR_new()))
58         goto err;
59
60     if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina),
61                                     htons(port))))
62         goto err;
63
64     if (!TEST_true(BIO_bind(s_fd, s_addr_, 0)))
65         goto err;
66
67     s_info.addr = s_addr_;
68     if (!TEST_true(BIO_sock_info(s_fd, BIO_SOCK_INFO_ADDRESS, &s_info)))
69         goto err;
70
71     if (!TEST_int_gt(BIO_ADDR_rawport(s_addr_), 0))
72         goto err;
73
74     if (!TEST_ptr(s_net_bio = s_net_bio_own = BIO_new_dgram(s_fd, 0)))
75         goto err;
76
77     if (!BIO_up_ref(s_net_bio))
78         goto err;
79
80     tserver_args.net_rbio = s_net_bio;
81     tserver_args.net_wbio = s_net_bio;
82
83     if (!TEST_ptr(tserver = ossl_quic_tserver_new(&tserver_args))) {
84         BIO_free(s_net_bio);
85         goto err;
86     }
87
88     s_net_bio_own = NULL;
89
90     /* Setup test client. */
91     c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
92     if (!TEST_int_ge(c_fd, 0))
93         goto err;
94
95     if (!TEST_true(BIO_socket_nbio(c_fd, 1)))
96         goto err;
97
98     if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0)))
99         goto err;
100
101     if (!BIO_dgram_set_peer(c_net_bio, s_addr_))
102         goto err;
103
104     if (!TEST_ptr(c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
105         goto err;
106
107     if (!TEST_ptr(c_ssl = SSL_new(c_ctx)))
108         goto err;
109
110     /* Takes ownership of our reference to the BIO. */
111     SSL_set0_rbio(c_ssl, c_net_bio);
112
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. */
116         goto err;
117     }
118
119     SSL_set0_wbio(c_ssl, c_net_bio);
120     c_net_bio_own = NULL;
121
122     if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0)))
123         goto err;
124
125     start_time = ossl_time_now();
126
127     for (;;) {
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");
131             goto err;
132         }
133
134         ret = SSL_connect(c_ssl);
135         if (!TEST_true(ret == 1 || is_want(c_ssl, ret)))
136             goto err;
137
138         if (ret == 1)
139             c_connected = 1;
140
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))
144                 goto err;
145
146             c_write_done = 1;
147         }
148
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)))
153                 goto err;
154
155             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))
159                     goto err;
160
161                 s_begin_write = 1;
162             }
163         }
164
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)))
169                 goto err;
170
171             s_total_written += l;
172
173             if (s_total_written == sizeof(msg1) - 1)
174                 c_begin_read = 1;
175         }
176
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)))
181                 goto err;
182
183             c_total_read += l;
184
185             if (c_total_read == sizeof(msg1) - 1) {
186                 if (!TEST_mem_eq(msg1, sizeof(msg1) - 1,
187                                  msg3, c_total_read))
188                     goto err;
189
190                 /* MATCH */
191                 break;
192             }
193         }
194
195         /*
196          * This is inefficient because we spin until things work without
197          * blocking but this is just a test.
198          */
199         SSL_tick(c_ssl);
200         ossl_quic_tserver_tick(tserver);
201     }
202
203     testresult = 1;
204 err:
205     SSL_free(c_ssl);
206     SSL_CTX_free(c_ctx);
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);
211     if (s_fd >= 0)
212         BIO_closesocket(s_fd);
213     if (c_fd >= 0)
214         BIO_closesocket(c_fd);
215     return testresult;
216 }
217
218 int setup_tests(void)
219 {
220     ADD_TEST(test_tserver);
221     return 1;
222 }