quic_client_test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2023 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 <stdio.h>
  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"
  16. #include "testutil.h"
  17. static const char msg1[] = "GET LICENSE.txt\r\n";
  18. static char msg2[16000];
  19. #define DST_PORT 4433
  20. #define DST_ADDR 0x7f000001UL
  21. static int is_want(SSL *s, int ret)
  22. {
  23. int ec = SSL_get_error(s, ret);
  24. return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE;
  25. }
  26. static int test_quic_client_ex(int fd_arg)
  27. {
  28. int testresult = 0, ret;
  29. int c_fd;
  30. BIO *c_net_bio = NULL, *c_net_bio_own = NULL;
  31. BIO_ADDR *s_addr_ = NULL;
  32. struct in_addr ina = {0};
  33. SSL_CTX *c_ctx = NULL;
  34. SSL *c_ssl = NULL;
  35. short port = DST_PORT;
  36. int c_connected = 0, c_write_done = 0, c_shutdown = 0;
  37. size_t l = 0, c_total_read = 0;
  38. OSSL_TIME start_time;
  39. unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '0', '.', '9' };
  40. if (fd_arg == INVALID_SOCKET) {
  41. /* Setup test client. */
  42. c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0);
  43. if (!TEST_int_ne(c_fd, INVALID_SOCKET))
  44. goto err;
  45. if (!TEST_true(BIO_socket_nbio(c_fd, 1)))
  46. goto err;
  47. if (!TEST_ptr(s_addr_ = BIO_ADDR_new()))
  48. goto err;
  49. ina.s_addr = htonl(DST_ADDR);
  50. if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina),
  51. htons(port))))
  52. goto err;
  53. } else {
  54. c_fd = fd_arg;
  55. }
  56. if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0)))
  57. goto err;
  58. /* connected socket does not need to set peer */
  59. if (s_addr_ != NULL && !BIO_dgram_set_peer(c_net_bio, s_addr_))
  60. goto err;
  61. if (!TEST_ptr(c_ctx = SSL_CTX_new(OSSL_QUIC_client_method())))
  62. goto err;
  63. if (!TEST_ptr(c_ssl = SSL_new(c_ctx)))
  64. goto err;
  65. /* 0 is a success for SSL_set_alpn_protos() */
  66. if (!TEST_false(SSL_set_alpn_protos(c_ssl, alpn, sizeof(alpn))))
  67. goto err;
  68. /* Takes ownership of our reference to the BIO. */
  69. SSL_set0_rbio(c_ssl, c_net_bio);
  70. /* Get another reference to be transferred in the SSL_set0_wbio call. */
  71. if (!TEST_true(BIO_up_ref(c_net_bio))) {
  72. c_net_bio_own = NULL; /* SSL_free will free the first reference. */
  73. goto err;
  74. }
  75. SSL_set0_wbio(c_ssl, c_net_bio);
  76. c_net_bio_own = NULL;
  77. if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0)))
  78. goto err;
  79. start_time = ossl_time_now();
  80. for (;;) {
  81. if (ossl_time_compare(ossl_time_subtract(ossl_time_now(), start_time),
  82. ossl_ms2time(10000)) >= 0) {
  83. TEST_error("timeout while attempting QUIC client test");
  84. goto err;
  85. }
  86. if (!c_connected) {
  87. ret = SSL_connect(c_ssl);
  88. if (!TEST_true(ret == 1 || is_want(c_ssl, ret)))
  89. goto err;
  90. if (ret == 1) {
  91. c_connected = 1;
  92. TEST_info("Connected!");
  93. }
  94. }
  95. if (c_connected && !c_write_done) {
  96. if (!TEST_int_eq(SSL_write(c_ssl, msg1, sizeof(msg1) - 1),
  97. (int)sizeof(msg1) - 1))
  98. goto err;
  99. if (!TEST_true(SSL_stream_conclude(c_ssl, 0)))
  100. goto err;
  101. c_write_done = 1;
  102. }
  103. if (c_write_done && !c_shutdown && c_total_read < sizeof(msg2) - 1) {
  104. ret = SSL_read_ex(c_ssl, msg2 + c_total_read,
  105. sizeof(msg2) - 1 - c_total_read, &l);
  106. if (ret != 1) {
  107. if (SSL_get_error(c_ssl, ret) == SSL_ERROR_ZERO_RETURN) {
  108. c_shutdown = 1;
  109. TEST_info("Message: \n%s\n", msg2);
  110. } else if (!TEST_true(is_want(c_ssl, ret))) {
  111. goto err;
  112. }
  113. } else {
  114. c_total_read += l;
  115. if (!TEST_size_t_lt(c_total_read, sizeof(msg2) - 1))
  116. goto err;
  117. }
  118. }
  119. if (c_shutdown) {
  120. ret = SSL_shutdown(c_ssl);
  121. if (ret == 1)
  122. break;
  123. }
  124. /*
  125. * This is inefficient because we spin until things work without
  126. * blocking but this is just a test.
  127. */
  128. OSSL_sleep(0);
  129. SSL_handle_events(c_ssl);
  130. }
  131. testresult = 1;
  132. err:
  133. SSL_free(c_ssl);
  134. SSL_CTX_free(c_ctx);
  135. BIO_ADDR_free(s_addr_);
  136. BIO_free(c_net_bio_own);
  137. if (fd_arg == INVALID_SOCKET && c_fd != INVALID_SOCKET)
  138. BIO_closesocket(c_fd);
  139. return testresult;
  140. }
  141. static int test_quic_client(void)
  142. {
  143. return (test_quic_client_ex(INVALID_SOCKET));
  144. }
  145. static int test_quic_client_connect_first(void)
  146. {
  147. struct sockaddr_in sin = {0};
  148. int c_fd;
  149. int rv;
  150. #ifdef SA_LEN
  151. sin.sin_len = sizeof(struct sockaddr_in);
  152. #endif
  153. sin.sin_family = AF_INET;
  154. sin.sin_port = htons(DST_PORT);
  155. sin.sin_addr.s_addr = htonl(DST_ADDR);
  156. c_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  157. if (!TEST_int_ne(c_fd, INVALID_SOCKET))
  158. goto err;
  159. if (!TEST_int_eq(connect(c_fd, (const struct sockaddr *)&sin, sizeof(sin)), 0))
  160. goto err;
  161. if (!TEST_true(BIO_socket_nbio(c_fd, 1)))
  162. goto err;
  163. rv = test_quic_client_ex(c_fd);
  164. close(c_fd);
  165. return (rv);
  166. err:
  167. if (c_fd != INVALID_SOCKET)
  168. close(c_fd);
  169. return (0);
  170. }
  171. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  172. int setup_tests(void)
  173. {
  174. if (!test_skip_common_options()) {
  175. TEST_error("Error parsing test options\n");
  176. return 0;
  177. }
  178. ADD_TEST(test_quic_client);
  179. ADD_TEST(test_quic_client_connect_first);
  180. return 1;
  181. }