quicapitest.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <stdio.h>
  10. #include <string.h>
  11. #include <openssl/opensslconf.h>
  12. #include <openssl/quic.h>
  13. #include "helpers/ssltestlib.h"
  14. #include "testutil.h"
  15. #include "testutil/output.h"
  16. static OSSL_LIB_CTX *libctx = NULL;
  17. static OSSL_PROVIDER *defctxnull = NULL;
  18. static int is_fips = 0;
  19. /*
  20. * Test that we read what we've written.
  21. */
  22. static int test_quic_write_read(void)
  23. {
  24. SSL_CTX *cctx = NULL, *sctx = NULL;
  25. SSL *clientquic = NULL, *serverquic = NULL;
  26. int j, ret = 0;
  27. char buf[20];
  28. static char *msg = "A test message";
  29. size_t msglen = strlen(msg);
  30. size_t numbytes = 0;
  31. if (!TEST_true(create_ssl_ctx_pair(libctx, OSSL_QUIC_server_method(),
  32. OSSL_QUIC_client_method(),
  33. 0,
  34. 0,
  35. &sctx, &cctx, NULL, NULL))
  36. || !TEST_true(create_ssl_objects(sctx, cctx, &serverquic, &clientquic,
  37. NULL, NULL))
  38. || !TEST_true(create_bare_ssl_connection(serverquic, clientquic,
  39. SSL_ERROR_NONE, 0, 0)))
  40. goto end;
  41. for (j = 0; j < 2; j++) {
  42. /* Check that sending and receiving app data is ok */
  43. if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
  44. || !TEST_true(SSL_read_ex(serverquic, buf, sizeof(buf),
  45. &numbytes))
  46. || !TEST_mem_eq(buf, numbytes, msg, msglen))
  47. goto end;
  48. if (!TEST_true(SSL_write_ex(serverquic, msg, msglen, &numbytes))
  49. || !TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf),
  50. &numbytes))
  51. || !TEST_mem_eq(buf, numbytes, msg, msglen))
  52. goto end;
  53. }
  54. ret = 1;
  55. end:
  56. SSL_free(serverquic);
  57. SSL_free(clientquic);
  58. SSL_CTX_free(sctx);
  59. SSL_CTX_free(cctx);
  60. return ret;
  61. }
  62. OPT_TEST_DECLARE_USAGE("provider config\n")
  63. int setup_tests(void)
  64. {
  65. char *modulename;
  66. char *configfile;
  67. libctx = OSSL_LIB_CTX_new();
  68. if (!TEST_ptr(libctx))
  69. return 0;
  70. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  71. /*
  72. * Verify that the default and fips providers in the default libctx are not
  73. * available
  74. */
  75. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  76. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  77. return 0;
  78. if (!test_skip_common_options()) {
  79. TEST_error("Error parsing test options\n");
  80. return 0;
  81. }
  82. if (!TEST_ptr(modulename = test_get_argument(0))
  83. || !TEST_ptr(configfile = test_get_argument(1)))
  84. return 0;
  85. if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
  86. return 0;
  87. /* Check we have the expected provider available */
  88. if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  89. return 0;
  90. /* Check the default provider is not available */
  91. if (strcmp(modulename, "default") != 0
  92. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  93. return 0;
  94. if (strcmp(modulename, "fips") == 0)
  95. is_fips = 1;
  96. ADD_TEST(test_quic_write_read);
  97. return 1;
  98. }
  99. void cleanup_tests(void)
  100. {
  101. OSSL_PROVIDER_unload(defctxnull);
  102. OSSL_LIB_CTX_free(libctx);
  103. }