Debian Codegraph @

crt/s2n/tests/unit/s2n_ktls_io_sendfile_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <fcntl.h>
17 #include <sys/socket.h>
18
19 #include "crypto/s2n_sequence.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22 #include "tls/s2n_ktls.h"
23 #include "utils/s2n_random.h"
24
25 static S2N_RESULT s2n_test_get_seq_num(struct s2n_connection *conn, uint64_t *seq_num_out)
26 {
27 struct s2n_blob seq_num = { 0 };
28 RESULT_GUARD(s2n_connection_get_sequence_number(conn, conn->mode, &seq_num));
29 RESULT_GUARD_POSIX(s2n_sequence_number_to_uint64(&seq_num, seq_num_out));
30 return S2N_RESULT_OK;
31 }
32
33 int main(int argc, char **argv)
34 {
35 BEGIN_TEST();
36
37 #ifdef S2N_LINUX_SENDFILE
38 const bool sendfile_supported = true;
39 #else
40 const bool sendfile_supported = false;
41 #endif
42
43 /* Test feature probe */
44 {
45 #if defined(__linux__)
46 EXPECT_TRUE(sendfile_supported);
47 #endif
48 #if defined(__FreeBSD__)
49 EXPECT_FALSE(sendfile_supported);
50 #endif
51 };
52
53 /* Safety */
54 {
55 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
56 s2n_connection_ptr_free);
57 EXPECT_NOT_NULL(conn);
58 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
59 size_t bytes_written = 0;
60
61 EXPECT_FAILURE_WITH_ERRNO(
62 s2n_sendfile(NULL, 0, 0, 0, &bytes_written, &blocked),
63 S2N_ERR_NULL);
64 EXPECT_FAILURE_WITH_ERRNO(
65 s2n_sendfile(conn, 0, 0, 0, NULL, &blocked),
66 S2N_ERR_NULL);
67 EXPECT_FAILURE_WITH_ERRNO(
68 s2n_sendfile(conn, 0, 0, 0, &bytes_written, NULL),
69 S2N_ERR_NULL);
70 };
71
72 /* Test s2n_sendfile unsupported */
73 if (!sendfile_supported) {
74 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
75 s2n_connection_ptr_free);
76 EXPECT_NOT_NULL(conn);
77 conn->ktls_send_enabled = true;
78
79 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
80 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
81 EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair));
82
83 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
84 size_t bytes_written = 0;
85 int result = s2n_sendfile(conn, 1, 0, 1, &bytes_written, &blocked);
86 EXPECT_FAILURE_WITH_ERRNO(result, S2N_ERR_UNIMPLEMENTED);
87
88 /* We do not run any further tests */
89 END_TEST();
90 };
91
92 /* The one file we know definitely exists is our own executable */
93 int ro_file = open(argv[0], O_RDONLY);
94 EXPECT_TRUE(ro_file > 0);
95
96 /* use pread to read the beginning of the file without updating its offset.
97 * Careful: if any call to sendfile sets offset=NULL, the file's offset will
98 * be updated and different data will be read.
99 */
100 uint8_t test_data[100] = { 0 };
101 EXPECT_EQUAL(pread(ro_file, test_data, sizeof(test_data), 0), sizeof(test_data));
102
103 /* Test: successful send */
104 {
105 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
106 s2n_connection_ptr_free);
107 EXPECT_NOT_NULL(conn);
108 conn->ktls_send_enabled = true;
109
110 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
111 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
112 int write_fd = io_pair.server;
113 int read_fd = io_pair.client;
114 EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));
115
116 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
117 size_t bytes_written = 0;
118 EXPECT_SUCCESS(s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
119 &bytes_written, &blocked));
120 EXPECT_EQUAL(bytes_written, sizeof(test_data));
121 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
122
123 uint8_t written[sizeof(test_data)] = { 0 };
124 EXPECT_EQUAL(read(read_fd, written, sizeof(written)), sizeof(test_data));
125 EXPECT_BYTEARRAY_EQUAL(written, test_data, sizeof(test_data));
126 EXPECT_TRUE(read(read_fd, written, sizeof(written)) < 0);
127 };
128
129 /* Test: IO error */
130 {
131 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
132 s2n_connection_ptr_free);
133 EXPECT_NOT_NULL(conn);
134 conn->ktls_send_enabled = true;
135
136 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
137 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
138 int write_fd = io_pair.server;
139 EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));
140
141 /* Close one side of the stream to make the fds invalid */
142 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
143
144 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
145 size_t bytes_written = 0;
146 int ret = s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
147 &bytes_written, &blocked);
148 EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_IO);
149 EXPECT_EQUAL(bytes_written, 0);
150 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
151 };
152
153 /* Test: send blocks */
154 {
155 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
156 s2n_connection_ptr_free);
157 EXPECT_NOT_NULL(conn);
158 conn->ktls_send_enabled = true;
159
160 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
161 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
162 int write_fd = io_pair.server;
163 EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));
164
165 /* We can force the socket to block by filling up its send buffer. */
166 int buffer_size = 0;
167 socklen_t optlen = sizeof(buffer_size);
168 EXPECT_EQUAL(getsockopt(write_fd, SOL_SOCKET, SO_SNDBUF, &buffer_size, &optlen), 0);
169 EXPECT_TRUE(buffer_size > 0);
170
171 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
172 size_t bytes_written = 0;
173 size_t total_bytes_written = 0;
174 while (true) {
175 int result = s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
176 &bytes_written, &blocked);
177 if (result < 0) {
178 EXPECT_FAILURE_WITH_ERRNO(result, S2N_ERR_IO_BLOCKED);
179 EXPECT_EQUAL(bytes_written, 0);
180 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
181 break;
182 }
183
184 EXPECT_TRUE(bytes_written <= sizeof(test_data));
185 EXPECT_TRUE(bytes_written > 0);
186 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
187 total_bytes_written += bytes_written;
188
189 /* The socket will block before buffer_size bytes are written.
190 * If we successfully send buffer_size bytes, something is wrong.
191 */
192 EXPECT_TRUE(total_bytes_written < buffer_size);
193 }
194 };
195
196 /* Test: partial write */
197 {
198 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
199 s2n_connection_ptr_free);
200 EXPECT_NOT_NULL(conn);
201 conn->ktls_send_enabled = true;
202
203 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
204 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
205 int write_fd = io_pair.server;
206 EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));
207
208 int buffer_size = 0;
209 socklen_t optlen = sizeof(buffer_size);
210 EXPECT_EQUAL(getsockopt(write_fd, SOL_SOCKET, SO_SNDBUF, &buffer_size, &optlen), 0);
211 EXPECT_TRUE(buffer_size > 0);
212
213 /* Try to write more data than the buffer can hold in a single sendfile call */
214 size_t bytes_to_write = buffer_size * 2;
215 size_t bytes_written = 0;
216 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
217 EXPECT_SUCCESS(s2n_sendfile(conn, ro_file, 0, bytes_to_write,
218 &bytes_written, &blocked));
219 EXPECT_TRUE(bytes_written > 0);
220 EXPECT_TRUE(bytes_written < bytes_to_write);
221 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
222 };
223
224 /* Test: key encryption limit tracked */
225 {
226 struct s2n_record_algorithm test_record_alg = *s2n_tls13_aes_128_gcm_sha256.record_alg;
227 test_record_alg.encryption_limit = 0;
228 struct s2n_cipher_suite test_cipher_suite = s2n_tls13_aes_128_gcm_sha256;
229 test_cipher_suite.record_alg = &test_record_alg;
230
231 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
232 s2n_connection_ptr_free);
233 EXPECT_NOT_NULL(conn);
234 conn->ktls_send_enabled = true;
235 conn->actual_protocol_version = S2N_TLS13;
236
237 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
238 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
239 EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, io_pair.server));
240
241 /* Test: The sequence number starts at 0 */
242 uint64_t seq_num = 1;
243 EXPECT_OK(s2n_test_get_seq_num(conn, &seq_num));
244 EXPECT_EQUAL(seq_num, 0);
245
246 /* Test: The sequence number tracks data sent */
247 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
248 size_t bytes_written = 0;
249 EXPECT_SUCCESS(s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
250 &bytes_written, &blocked));
251 EXPECT_OK(s2n_test_get_seq_num(conn, &seq_num));
252 EXPECT_TRUE(seq_num > 0);
253
254 /* Test: Enforce the encryption limit */
255 EXPECT_NOT_NULL(conn->secure);
256 conn->secure->cipher_suite = &test_cipher_suite;
257 EXPECT_FAILURE_WITH_ERRNO(
258 s2n_sendfile(conn, ro_file, 0, sizeof(test_data), &bytes_written, &blocked),
259 S2N_ERR_KTLS_KEY_LIMIT);
260 };
261
262 EXPECT_EQUAL(close(ro_file), 0);
263 END_TEST();
264 }