2 * Copyright 2016-2020 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 /* We include internal headers so we can check if the buffers are allocated */
17 #include "../ssl/ssl_local.h"
18 #include "../ssl/record/record_local.h"
19 #include "../ssl/record/recordmethod.h"
20 #include "../ssl/record/methods/recmethod_local.h"
22 #include "internal/packet.h"
24 #include "helpers/ssltestlib.h"
32 static SSL_CTX *serverctx = NULL;
33 static SSL_CTX *clientctx = NULL;
35 #define MAX_ATTEMPTS 100
37 static int checkbuffers(SSL *s, int isalloced)
39 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
40 OSSL_RECORD_LAYER *rrl = sc->rlayer.rrl;
41 OSSL_RECORD_LAYER *wrl = sc->rlayer.wrl;
44 return rrl->rbuf.buf != NULL && wrl->wbuf[0].buf != NULL;
46 return rrl->rbuf.buf == NULL && wrl->wbuf[0].buf == NULL;
50 * There are 9 passes in the tests
54 * 2 = + allocate buffers after free
55 * 3 = + allocate buffers again
56 * 4 = + free buffers after allocation
59 * 6 = + free buffers again
60 * 7 = + allocate buffers after free
61 * 8 = + free buffers after allocation
63 static int test_func(int test)
66 SSL *serverssl = NULL, *clientssl = NULL;
69 const char testdata[] = "Test data";
70 char buf[sizeof(testdata)];
72 if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl,
74 TEST_error("Test %d failed: Create SSL objects failed\n", test);
78 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
79 TEST_error("Test %d failed: Create SSL connection failed\n", test);
84 * Send and receive some test data. Do the whole thing twice to ensure
85 * we hit at least one async event in both reading and writing
87 for (j = 0; j < 2; j++) {
92 * Write some test data. It should never take more than 2 attempts
93 * (the first one might be a retryable fail).
95 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2;
97 /* test == 0 mean to free/allocate = control */
98 if (test >= 1 && (!TEST_true(SSL_free_buffers(clientssl))
99 || !TEST_true(checkbuffers(clientssl, 0))))
101 if (test >= 2 && (!TEST_true(SSL_alloc_buffers(clientssl))
102 || !TEST_true(checkbuffers(clientssl, 1))))
104 /* allocate a second time */
105 if (test >= 3 && (!TEST_true(SSL_alloc_buffers(clientssl))
106 || !TEST_true(checkbuffers(clientssl, 1))))
108 if (test >= 4 && (!TEST_true(SSL_free_buffers(clientssl))
109 || !TEST_true(checkbuffers(clientssl, 0))))
112 ret = SSL_write(clientssl, testdata + len,
113 sizeof(testdata) - len);
117 int ssl_error = SSL_get_error(clientssl, ret);
119 if (ssl_error == SSL_ERROR_SYSCALL ||
120 ssl_error == SSL_ERROR_SSL) {
121 TEST_error("Test %d failed: Failed to write app data\n", test);
126 if (!TEST_size_t_eq(len, sizeof(testdata)))
129 * Now read the test data. It may take more attempts here because
130 * it could fail once for each byte read, including all overhead
131 * bytes from the record header/padding etc.
133 for (ret = -1, i = 0, len = 0; len != sizeof(testdata) &&
134 i < MAX_ATTEMPTS; i++)
136 if (test >= 5 && (!TEST_true(SSL_free_buffers(serverssl))
137 || !TEST_true(checkbuffers(serverssl, 0))))
139 /* free a second time */
140 if (test >= 6 && (!TEST_true(SSL_free_buffers(serverssl))
141 || !TEST_true(checkbuffers(serverssl, 0))))
143 if (test >= 7 && (!TEST_true(SSL_alloc_buffers(serverssl))
144 || !TEST_true(checkbuffers(serverssl, 1))))
146 if (test >= 8 && (!TEST_true(SSL_free_buffers(serverssl))
147 || !TEST_true(checkbuffers(serverssl, 0))))
150 ret = SSL_read(serverssl, buf + len, sizeof(buf) - len);
154 int ssl_error = SSL_get_error(serverssl, ret);
156 if (ssl_error == SSL_ERROR_SYSCALL ||
157 ssl_error == SSL_ERROR_SSL) {
158 TEST_error("Test %d failed: Failed to read app data\n", test);
163 if (!TEST_mem_eq(buf, len, testdata, sizeof(testdata)))
170 ERR_print_errors_fp(stderr);
178 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
180 int setup_tests(void)
184 if (!test_skip_common_options()) {
185 TEST_error("Error parsing test options\n");
189 if (!TEST_ptr(cert = test_get_argument(0))
190 || !TEST_ptr(pkey = test_get_argument(1)))
193 if (!create_ssl_ctx_pair(NULL, TLS_server_method(), TLS_client_method(),
195 &serverctx, &clientctx, cert, pkey)) {
196 TEST_error("Failed to create SSL_CTX pair\n");
200 ADD_ALL_TESTS(test_func, 9);
204 void cleanup_tests(void)
206 SSL_CTX_free(clientctx);
207 SSL_CTX_free(serverctx);