2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
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.
12 #include <openssl/ssl.h>
13 #include <openssl/bio.h>
14 #include <openssl/err.h>
16 #include "../ssl/packet_locl.h"
18 #include "ssltestlib.h"
26 static SSL_CTX *serverctx = NULL;
27 static SSL_CTX *clientctx = NULL;
29 #define MAX_ATTEMPTS 100
33 * There are 9 passes in the tests
37 * 2 = + allocate buffers after free
38 * 3 = + allocate buffers again
39 * 4 = + free buffers after allocation
42 * 6 = + free buffers again
43 * 7 = + allocate buffers after free
44 * 8 = + free buffers after allocation
46 static int test_func(int test)
49 SSL *serverssl = NULL, *clientssl = NULL;
52 const char testdata[] = "Test data";
53 char buf[sizeof(testdata)];
55 if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl,
57 TEST_error("Test %d failed: Create SSL objects failed\n", test);
61 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
62 TEST_error("Test %d failed: Create SSL connection failed\n", test);
67 * Send and receive some test data. Do the whole thing twice to ensure
68 * we hit at least one async event in both reading and writing
70 for (j = 0; j < 2; j++) {
75 * Write some test data. It should never take more than 2 attempts
76 * (the first one might be a retryable fail).
78 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
80 /* test == 0 mean to free/allocate = control */
81 if (test >= 1 && !TEST_true(SSL_free_buffers(clientssl)))
83 if (test >= 2 && !TEST_true(SSL_alloc_buffers(clientssl)))
85 /* allocate a second time */
86 if (test >= 3 && !TEST_true(SSL_alloc_buffers(clientssl)))
88 if (test >= 4 && !TEST_true(SSL_free_buffers(clientssl)))
91 ret = SSL_write(clientssl, testdata + len,
92 sizeof(testdata) - len);
96 int ssl_error = SSL_get_error(clientssl, ret);
98 if (ssl_error == SSL_ERROR_SYSCALL ||
99 ssl_error == SSL_ERROR_SSL) {
100 TEST_error("Test %d failed: Failed to write app data\n", test);
105 if (!TEST_size_t_eq(len, sizeof(testdata)))
108 * Now read the test data. It may take more attempts here because
109 * it could fail once for each byte read, including all overhead
110 * bytes from the record header/padding etc.
112 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
113 i < MAX_ATTEMPTS; i++)
115 if (test >= 5 && !TEST_true(SSL_free_buffers(serverssl)))
117 /* free a second time */
118 if (test >= 6 && !TEST_true(SSL_free_buffers(serverssl)))
120 if (test >= 7 && !TEST_true(SSL_alloc_buffers(serverssl)))
122 if (test >= 8 && !TEST_true(SSL_free_buffers(serverssl)))
125 ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
129 int ssl_error = SSL_get_error(serverssl, ret);
131 if (ssl_error == SSL_ERROR_SYSCALL ||
132 ssl_error == SSL_ERROR_SSL) {
133 TEST_error("Test %d failed: Failed to read app data\n", test);
138 if (!TEST_mem_eq(buf, len, testdata, sizeof(testdata)))
145 ERR_print_errors_fp(stderr);
153 int global_init(void)
155 CRYPTO_set_mem_debug(1);
156 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
160 int setup_tests(void)
164 if (!TEST_ptr(cert = test_get_argument(0))
165 || !TEST_ptr(pkey = test_get_argument(1)))
168 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
169 TLS1_VERSION, TLS_MAX_VERSION,
170 &serverctx, &clientctx, cert, pkey)) {
171 TEST_error("Failed to create SSL_CTX pair\n");
175 ADD_ALL_TESTS(test_func, 9);
179 void cleanup_tests(void)
181 SSL_CTX_free(clientctx);
182 SSL_CTX_free(serverctx);