sslbuffertest.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <string.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/err.h>
  14. #include "../ssl/packet_locl.h"
  15. #include "ssltestlib.h"
  16. #include "testutil.h"
  17. struct async_ctrs {
  18. unsigned int rctr;
  19. unsigned int wctr;
  20. };
  21. static SSL_CTX *serverctx = NULL;
  22. static SSL_CTX *clientctx = NULL;
  23. #define MAX_ATTEMPTS 100
  24. /*
  25. * There are 9 passes in the tests
  26. * 0 = control test
  27. * tests during writes
  28. * 1 = free buffers
  29. * 2 = + allocate buffers after free
  30. * 3 = + allocate buffers again
  31. * 4 = + free buffers after allocation
  32. * tests during reads
  33. * 5 = + free buffers
  34. * 6 = + free buffers again
  35. * 7 = + allocate buffers after free
  36. * 8 = + free buffers after allocation
  37. */
  38. static int test_func(int test)
  39. {
  40. int result = 0;
  41. SSL *serverssl = NULL, *clientssl = NULL;
  42. int ret;
  43. size_t i, j;
  44. const char testdata[] = "Test data";
  45. char buf[sizeof(testdata)];
  46. if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl,
  47. NULL, NULL))) {
  48. TEST_error("Test %d failed: Create SSL objects failed\n", test);
  49. goto end;
  50. }
  51. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
  52. TEST_error("Test %d failed: Create SSL connection failed\n", test);
  53. goto end;
  54. }
  55. /*
  56. * Send and receive some test data. Do the whole thing twice to ensure
  57. * we hit at least one async event in both reading and writing
  58. */
  59. for (j = 0; j < 2; j++) {
  60. int len;
  61. /*
  62. * Write some test data. It should never take more than 2 attempts
  63. * (the first one might be a retryable fail).
  64. */
  65. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
  66. i++) {
  67. /* test == 0 mean to free/allocate = control */
  68. if (test >= 1 && !TEST_true(SSL_free_buffers(clientssl)))
  69. goto end;
  70. if (test >= 2 && !TEST_true(SSL_alloc_buffers(clientssl)))
  71. goto end;
  72. /* allocate a second time */
  73. if (test >= 3 && !TEST_true(SSL_alloc_buffers(clientssl)))
  74. goto end;
  75. if (test >= 4 && !TEST_true(SSL_free_buffers(clientssl)))
  76. goto end;
  77. ret = SSL_write(clientssl, testdata + len,
  78. sizeof(testdata) - len);
  79. if (ret > 0) {
  80. len += ret;
  81. } else {
  82. int ssl_error = SSL_get_error(clientssl, ret);
  83. if (ssl_error == SSL_ERROR_SYSCALL ||
  84. ssl_error == SSL_ERROR_SSL) {
  85. TEST_error("Test %d failed: Failed to write app data\n", test);
  86. goto end;
  87. }
  88. }
  89. }
  90. if (!TEST_size_t_eq(len, sizeof(testdata)))
  91. goto end;
  92. /*
  93. * Now read the test data. It may take more attempts here because
  94. * it could fail once for each byte read, including all overhead
  95. * bytes from the record header/padding etc.
  96. */
  97. for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
  98. i < MAX_ATTEMPTS; i++)
  99. {
  100. if (test >= 5 && !TEST_true(SSL_free_buffers(serverssl)))
  101. goto end;
  102. /* free a second time */
  103. if (test >= 6 && !TEST_true(SSL_free_buffers(serverssl)))
  104. goto end;
  105. if (test >= 7 && !TEST_true(SSL_alloc_buffers(serverssl)))
  106. goto end;
  107. if (test >= 8 && !TEST_true(SSL_free_buffers(serverssl)))
  108. goto end;
  109. ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
  110. if (ret > 0) {
  111. len += ret;
  112. } else {
  113. int ssl_error = SSL_get_error(serverssl, ret);
  114. if (ssl_error == SSL_ERROR_SYSCALL ||
  115. ssl_error == SSL_ERROR_SSL) {
  116. TEST_error("Test %d failed: Failed to read app data\n", test);
  117. goto end;
  118. }
  119. }
  120. }
  121. if (!TEST_mem_eq(buf, len, testdata, sizeof(testdata)))
  122. goto end;
  123. }
  124. result = 1;
  125. end:
  126. if (!result)
  127. ERR_print_errors_fp(stderr);
  128. SSL_free(clientssl);
  129. SSL_free(serverssl);
  130. return result;
  131. }
  132. int global_init(void)
  133. {
  134. CRYPTO_set_mem_debug(1);
  135. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  136. return 1;
  137. }
  138. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  139. int setup_tests(void)
  140. {
  141. char *cert, *pkey;
  142. if (!TEST_ptr(cert = test_get_argument(0))
  143. || !TEST_ptr(pkey = test_get_argument(1)))
  144. return 0;
  145. if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  146. TLS1_VERSION, 0,
  147. &serverctx, &clientctx, cert, pkey)) {
  148. TEST_error("Failed to create SSL_CTX pair\n");
  149. return 0;
  150. }
  151. ADD_ALL_TESTS(test_func, 9);
  152. return 1;
  153. }
  154. void cleanup_tests(void)
  155. {
  156. SSL_CTX_free(clientctx);
  157. SSL_CTX_free(serverctx);
  158. }