]>
Commit | Line | Data |
---|---|---|
a58eb06d | 1 | /* |
b0edda11 | 2 | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
a58eb06d | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"); |
a58eb06d TS |
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 | ||
11 | #include <string.h> | |
12 | #include <openssl/ssl.h> | |
13 | #include <openssl/bio.h> | |
14 | #include <openssl/err.h> | |
15 | ||
16 | #include "../ssl/packet_locl.h" | |
17 | ||
18 | #include "ssltestlib.h" | |
19 | #include "testutil.h" | |
20 | ||
21 | struct async_ctrs { | |
22 | unsigned int rctr; | |
23 | unsigned int wctr; | |
24 | }; | |
25 | ||
26 | static SSL_CTX *serverctx = NULL; | |
27 | static SSL_CTX *clientctx = NULL; | |
28 | ||
29 | #define MAX_ATTEMPTS 100 | |
30 | ||
31 | ||
32 | /* | |
33 | * There are 9 passes in the tests | |
34 | * 0 = control test | |
35 | * tests during writes | |
36 | * 1 = free buffers | |
37 | * 2 = + allocate buffers after free | |
38 | * 3 = + allocate buffers again | |
39 | * 4 = + free buffers after allocation | |
40 | * tests during reads | |
41 | * 5 = + free buffers | |
42 | * 6 = + free buffers again | |
43 | * 7 = + allocate buffers after free | |
44 | * 8 = + free buffers after allocation | |
45 | */ | |
46 | static int test_func(int test) | |
47 | { | |
48 | int result = 0; | |
49 | SSL *serverssl = NULL, *clientssl = NULL; | |
50 | int ret; | |
51 | size_t i, j; | |
52 | const char testdata[] = "Test data"; | |
53 | char buf[sizeof(testdata)]; | |
54 | ||
55 | if (!TEST_true(create_ssl_objects(serverctx, clientctx, &serverssl, &clientssl, | |
56 | NULL, NULL))) { | |
57 | TEST_error("Test %d failed: Create SSL objects failed\n", test); | |
58 | goto end; | |
59 | } | |
60 | ||
61 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) { | |
62 | TEST_error("Test %d failed: Create SSL connection failed\n", test); | |
63 | goto end; | |
64 | } | |
65 | ||
66 | /* | |
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 | |
69 | */ | |
70 | for (j = 0; j < 2; j++) { | |
71 | int len; | |
72 | ||
73 | /* | |
74 | ||
75 | * Write some test data. It should never take more than 2 attempts | |
76 | * (the first one might be a retryable fail). | |
77 | */ | |
78 | for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && i < 2; | |
79 | i++) { | |
80 | /* test == 0 mean to free/allocate = control */ | |
81 | if (test >= 1 && !TEST_true(SSL_free_buffers(clientssl))) | |
82 | goto end; | |
83 | if (test >= 2 && !TEST_true(SSL_alloc_buffers(clientssl))) | |
84 | goto end; | |
85 | /* allocate a second time */ | |
86 | if (test >= 3 && !TEST_true(SSL_alloc_buffers(clientssl))) | |
87 | goto end; | |
88 | if (test >= 4 && !TEST_true(SSL_free_buffers(clientssl))) | |
89 | goto end; | |
90 | ||
91 | ret = SSL_write(clientssl, testdata + len, | |
92 | sizeof(testdata) - len); | |
93 | if (ret > 0) { | |
94 | len += ret; | |
95 | } else { | |
96 | int ssl_error = SSL_get_error(clientssl, ret); | |
97 | ||
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); | |
101 | goto end; | |
102 | } | |
103 | } | |
104 | } | |
105 | if (!TEST_size_t_eq(len, sizeof(testdata))) | |
106 | goto end; | |
107 | /* | |
bdcacd93 | 108 | * Now read the test data. It may take more attempts here because |
a58eb06d TS |
109 | * it could fail once for each byte read, including all overhead |
110 | * bytes from the record header/padding etc. | |
111 | */ | |
112 | for (ret = -1, i = 0, len = 0; len != sizeof(testdata) && | |
113 | i < MAX_ATTEMPTS; i++) | |
114 | { | |
115 | if (test >= 5 && !TEST_true(SSL_free_buffers(serverssl))) | |
116 | goto end; | |
117 | /* free a second time */ | |
118 | if (test >= 6 && !TEST_true(SSL_free_buffers(serverssl))) | |
119 | goto end; | |
120 | if (test >= 7 && !TEST_true(SSL_alloc_buffers(serverssl))) | |
121 | goto end; | |
122 | if (test >= 8 && !TEST_true(SSL_free_buffers(serverssl))) | |
123 | goto end; | |
124 | ||
125 | ret = SSL_read(serverssl, buf + len, sizeof(buf) - len); | |
126 | if (ret > 0) { | |
127 | len += ret; | |
128 | } else { | |
129 | int ssl_error = SSL_get_error(serverssl, ret); | |
130 | ||
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); | |
134 | goto end; | |
135 | } | |
136 | } | |
137 | } | |
138 | if (!TEST_mem_eq(buf, len, testdata, sizeof(testdata))) | |
139 | goto end; | |
140 | } | |
141 | ||
142 | result = 1; | |
143 | end: | |
144 | if (!result) | |
145 | ERR_print_errors_fp(stderr); | |
146 | ||
147 | SSL_free(clientssl); | |
148 | SSL_free(serverssl); | |
149 | ||
150 | return result; | |
151 | } | |
152 | ||
43405889 | 153 | int global_init(void) |
a58eb06d | 154 | { |
a58eb06d TS |
155 | CRYPTO_set_mem_debug(1); |
156 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | |
43405889 P |
157 | return 1; |
158 | } | |
a58eb06d | 159 | |
43405889 P |
160 | int setup_tests(void) |
161 | { | |
162 | char *cert, *pkey; | |
163 | ||
164 | if (!TEST_ptr(cert = test_get_argument(0)) | |
165 | || !TEST_ptr(pkey = test_get_argument(1))) | |
166 | return 0; | |
a58eb06d TS |
167 | |
168 | if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), | |
5c587fb6 | 169 | TLS1_VERSION, 0, |
43405889 | 170 | &serverctx, &clientctx, cert, pkey)) { |
a58eb06d | 171 | TEST_error("Failed to create SSL_CTX pair\n"); |
43405889 | 172 | return 0; |
a58eb06d TS |
173 | } |
174 | ||
175 | ADD_ALL_TESTS(test_func, 9); | |
43405889 P |
176 | return 1; |
177 | } | |
a58eb06d | 178 | |
43405889 P |
179 | void cleanup_tests(void) |
180 | { | |
a58eb06d TS |
181 | SSL_CTX_free(clientctx); |
182 | SSL_CTX_free(serverctx); | |
a58eb06d | 183 | } |