2 * Copyright 2023 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
10 #include <openssl/ssl.h>
11 #include <openssl/quic.h>
12 #include <openssl/bio.h>
13 #include "internal/common.h"
14 #include "internal/sockets.h"
15 #include "internal/time.h"
18 static const char msg1[] = "GET LICENSE.txt\r\n";
19 static char msg2[16000];
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_quic_client(void)
30 int testresult = 0, ret;
31 int c_fd = INVALID_SOCKET;
32 BIO *c_net_bio = NULL, *c_net_bio_own = NULL;
33 BIO_ADDR *s_addr_ = NULL;
34 struct in_addr ina = {0};
35 SSL_CTX *c_ctx = NULL;
38 int c_connected = 0, c_write_done = 0, c_shutdown = 0;
39 size_t l = 0, c_total_read = 0;
41 unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '0', '.', '9' };
43 ina.s_addr = htonl(0x7f000001UL);
45 /* Setup test client. */
46 c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
47 if (!TEST_int_ne(c_fd, INVALID_SOCKET))
50 if (!TEST_true(BIO_socket_nbio(c_fd, 1)))
53 if (!TEST_ptr(s_addr_ = BIO_ADDR_new()))
56 if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina),
60 if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0)))
63 if (!BIO_dgram_set_peer(c_net_bio, s_addr_))
66 if (!TEST_ptr(c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
69 if (!TEST_ptr(c_ssl = SSL_new(c_ctx)))
72 /* 0 is a success for SSL_set_alpn_protos() */
73 if (!TEST_false(SSL_set_alpn_protos(c_ssl, alpn, sizeof(alpn))))
76 /* Takes ownership of our reference to the BIO. */
77 SSL_set0_rbio(c_ssl, c_net_bio);
79 /* Get another reference to be transferred in the SSL_set0_wbio call. */
80 if (!TEST_true(BIO_up_ref(c_net_bio))) {
81 c_net_bio_own = NULL; /* SSL_free will free the first reference. */
85 SSL_set0_wbio(c_ssl, c_net_bio);
88 if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0)))
91 start_time = ossl_time_now();
94 if (ossl_time_compare(ossl_time_subtract(ossl_time_now(), start_time),
95 ossl_ms2time(3000)) >= 0) {
96 TEST_error("timeout while attempting QUIC client test");
101 ret = SSL_connect(c_ssl);
102 if (!TEST_true(ret == 1 || is_want(c_ssl, ret)))
107 TEST_info("Connected!");
111 if (c_connected && !c_write_done) {
112 if (!TEST_int_eq(SSL_write(c_ssl, msg1, sizeof(msg1) - 1),
113 (int)sizeof(msg1) - 1))
116 if (!TEST_true(SSL_stream_conclude(c_ssl, 0)))
122 if (c_write_done && !c_shutdown && c_total_read < sizeof(msg2) - 1) {
123 ret = SSL_read_ex(c_ssl, msg2 + c_total_read,
124 sizeof(msg2) - 1 - c_total_read, &l);
126 if (SSL_get_error(c_ssl, ret) == SSL_ERROR_ZERO_RETURN) {
128 TEST_info("Message: \n%s\n", msg2);
129 } else if (!TEST_true(is_want(c_ssl, ret))) {
135 if (!TEST_size_t_lt(c_total_read, sizeof(msg2) - 1))
141 ret = SSL_shutdown(c_ssl);
147 * This is inefficient because we spin until things work without
148 * blocking but this is just a test.
151 SSL_handle_events(c_ssl);
158 BIO_ADDR_free(s_addr_);
159 BIO_free(c_net_bio_own);
160 if (c_fd != INVALID_SOCKET)
161 BIO_closesocket(c_fd);
165 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
167 int setup_tests(void)
169 if (!test_skip_common_options()) {
170 TEST_error("Error parsing test options\n");
174 ADD_TEST(test_quic_client);