Debian Codegraph @

crt/s2n/tests/unit/s2n_send_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 <math.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_tls.h"
22 #include "utils/s2n_random.h"
23
24 /* clang-format off */
25 #define CLOSED_SEND_RESULT { .result = -1, .error = EPIPE }
26 #define BLOCK_SEND_RESULT { .result = -1, .error = EAGAIN }
27 #define PARTIAL_SEND_RESULT(bytes) { .result = bytes, .error = EAGAIN }
28 #define OK_SEND_RESULT { .result = INT_MAX }
29 /* clang-format on */
30
31 enum s2n_test_mfl {
32 S2N_MFL_DEFAULT = 0,
33 S2N_MFL_LARGE,
34 S2N_MFL_SMALL,
35 S2N_MFL_MINIMUM,
36 S2N_MFL_COUNT,
37 };
38
39 static S2N_RESULT s2n_set_test_max_fragment_len(struct s2n_connection *conn, enum s2n_test_mfl mfl)
40 {
41 switch (mfl) {
42 case S2N_MFL_DEFAULT:
43 break;
44 case S2N_MFL_LARGE:
45 EXPECT_SUCCESS(s2n_connection_prefer_throughput(conn));
46 break;
47 case S2N_MFL_SMALL:
48 EXPECT_SUCCESS(s2n_connection_prefer_low_latency(conn));
49 break;
50 case S2N_MFL_MINIMUM:
51 conn->max_outgoing_fragment_length = mfl_code_to_length[1];
52 break;
53 case S2N_MFL_COUNT:
54 RESULT_BAIL(S2N_ERR_INVALID_ARGUMENT);
55 break;
56 }
57 return S2N_RESULT_OK;
58 }
59
60 struct s2n_send_result {
61 int result;
62 int error;
63 };
64
65 struct s2n_send_context {
66 size_t calls;
67 size_t bytes_sent;
68 const struct s2n_send_result *results;
69 const size_t results_len;
70 };
71
72 bool s2n_custom_send_fn_called = false;
73 int s2n_expect_concurrent_error_send_fn(void *io_context, const uint8_t *buf, uint32_t len)
74 {
75 struct s2n_connection *conn = (struct s2n_connection *) io_context;
76 s2n_custom_send_fn_called = true;
77
78 s2n_blocked_status blocked = 0;
79 ssize_t result = s2n_send(conn, buf, len, &blocked);
80 EXPECT_FAILURE_WITH_ERRNO(result, S2N_ERR_REENTRANCY);
81 return result;
82 }
83
84 static int s2n_test_send_cb(void *io_context, const uint8_t *buf, uint32_t len)
85 {
86 struct s2n_send_context *context = (struct s2n_send_context *) io_context;
87 POSIX_ENSURE_REF(context);
88
89 POSIX_ENSURE_LT(context->calls, context->results_len);
90 const struct s2n_send_result *result = &context->results[context->calls];
91
92 int retval = S2N_MIN((int) len, result->result);
93
94 context->calls++;
95 if (retval > 0) {
96 context->bytes_sent += retval;
97 }
98
99 errno = result->error;
100 return retval;
101 }
102
103 int main(int argc, char **argv)
104 {
105 BEGIN_TEST();
106
107 uint8_t test_data[] = "hello world";
108
109 uint8_t large_test_data[S2N_TLS_MAXIMUM_FRAGMENT_LENGTH + 10] = { 0 };
110 struct s2n_blob large_data_blob = { 0 };
111 EXPECT_SUCCESS(s2n_blob_init(&large_data_blob, large_test_data, sizeof(large_test_data)));
112 EXPECT_OK(s2n_get_public_random_data(&large_data_blob));
113
114 /* Small record sizes will require a LOT of calls to s2n_send.
115 * Use this context when they should all succeed.
116 */
117 struct s2n_send_result results_all_ok[50] = { 0 };
118 for (size_t i = 0; i < s2n_array_len(results_all_ok); i++) {
119 results_all_ok[i] = (struct s2n_send_result) OK_SEND_RESULT;
120 }
121 const struct s2n_send_context context_all_ok = {
122 .results = results_all_ok,
123 .results_len = s2n_array_len(results_all_ok)
124 };
125
126 /* Calculating the record size for given data can be tricky.
127 * Instead, let's set the values based on the results of tests.
128 */
129 ssize_t test_data_bytes_sent = 0;
130
131 /* s2n_send */
132 {
133 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
134 s2n_connection_ptr_free);
135 EXPECT_NOT_NULL(conn);
136 EXPECT_OK(s2n_connection_set_secrets(conn));
137
138 struct s2n_send_context context = context_all_ok;
139 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
140 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
141
142 s2n_blocked_status blocked = 0;
143 EXPECT_EQUAL(s2n_send(conn, test_data, sizeof(test_data), &blocked), sizeof(test_data));
144
145 EXPECT_EQUAL(context.calls, 1);
146 EXPECT_EQUAL(context.bytes_sent, conn->wire_bytes_out);
147
148 /* Set the expected record size for future tests */
149 test_data_bytes_sent = context.bytes_sent;
150 EXPECT_TRUE(test_data_bytes_sent > sizeof(test_data));
151 };
152
153 /* Calculating the max record size for a given max fragment length can be tricky.
154 * Instead, let's set the values based on the results of tests.
155 */
156 ssize_t max_frag_bytes_sent[S2N_MFL_COUNT] = { 0 };
157
158 /* Track the size of the output buffer.
159 * It should be constant across all tests with the same max fragment length.
160 */
161 uint32_t out_size[S2N_MFL_COUNT] = { 0 };
162
163 /* Send exactly the maximum fragment size */
164 for (size_t mfl = 0; mfl < S2N_MFL_COUNT; mfl++) {
165 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
166 s2n_connection_ptr_free);
167 EXPECT_NOT_NULL(conn);
168 EXPECT_OK(s2n_connection_set_secrets(conn));
169 EXPECT_OK(s2n_set_test_max_fragment_len(conn, mfl));
170 uint32_t fragment_len = conn->max_outgoing_fragment_length;
171
172 struct s2n_send_context context = context_all_ok;
173 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
174 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
175
176 s2n_blocked_status blocked = 0;
177
178 /* Send exactly the fragment length */
179 EXPECT_EQUAL(s2n_send(conn, large_test_data, fragment_len, &blocked), fragment_len);
180 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
181 EXPECT_EQUAL(context.calls, 1);
182
183 /* Set the expected record size for future tests */
184 max_frag_bytes_sent[mfl] = context.bytes_sent;
185 EXPECT_TRUE(max_frag_bytes_sent[mfl] > 0);
186
187 /* Set the expected output buffer size for future tests */
188 out_size[mfl] = conn->out.blob.size;
189 EXPECT_TRUE(out_size[mfl] > 0);
190
191 /* Sanity check: Send one byte more than the fragment length.
192 * If this is actually the maximum fragment length, one extra byte will
193 * lead to an extra record / extra call to send.
194 */
195 context.calls = 0;
196 EXPECT_EQUAL(s2n_send(conn, large_test_data, fragment_len + 1, &blocked), fragment_len + 1);
197 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
198 EXPECT_EQUAL(context.calls, 2);
199
200 /* Verify output buffer */
201 EXPECT_EQUAL(conn->out.blob.size, out_size[mfl]);
202 }
203
204 /* s2n_send cannot be called concurrently */
205 {
206 /* Setup connections */
207 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
208 s2n_connection_ptr_free);
209 EXPECT_NOT_NULL(conn);
210 EXPECT_OK(s2n_connection_set_secrets(conn));
211
212 /* Setup bad send callback */
213 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_expect_concurrent_error_send_fn));
214 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) conn));
215 EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING));
216
217 s2n_blocked_status blocked = 0;
218 s2n_custom_send_fn_called = false;
219 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, test_data, sizeof(test_data), &blocked),
220 S2N_ERR_IO);
221 EXPECT_TRUE(s2n_custom_send_fn_called);
222 EXPECT_EQUAL(0, conn->wire_bytes_out);
223 };
224
225 /* s2n_send tracks conn->wire_bytes_out on send */
226 {
227 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
228 s2n_connection_ptr_free);
229 EXPECT_NOT_NULL(conn);
230 EXPECT_OK(s2n_connection_set_secrets(conn));
231 EXPECT_EQUAL(0, conn->wire_bytes_out);
232
233 struct s2n_send_context context = context_all_ok;
234 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
235 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
236
237 s2n_blocked_status blocked = 0;
238 EXPECT_EQUAL(s2n_send(conn, test_data, sizeof(test_data), &blocked), sizeof(test_data));
239
240 EXPECT_EQUAL(context.calls, 1);
241 EXPECT_EQUAL(context.bytes_sent, test_data_bytes_sent);
242 EXPECT_EQUAL(context.bytes_sent, conn->wire_bytes_out);
243 EXPECT_EQUAL(context.bytes_sent, s2n_connection_get_wire_bytes_out(conn));
244 };
245
246 /* s2n_send tracks conn->wire_bytes_out on partial send */
247 {
248 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
249 s2n_connection_ptr_free);
250 EXPECT_NOT_NULL(conn);
251 EXPECT_OK(s2n_connection_set_secrets(conn));
252 EXPECT_EQUAL(0, conn->wire_bytes_out);
253
254 const uint32_t partial_send = 10;
255 const struct s2n_send_result results[] = {
256 PARTIAL_SEND_RESULT(partial_send),
257 CLOSED_SEND_RESULT,
258 };
259 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
260 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
261 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
262
263 s2n_blocked_status blocked = 0;
264 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, test_data, sizeof(test_data), &blocked), S2N_ERR_IO);
265
266 EXPECT_EQUAL(context.calls, 2);
267 EXPECT_EQUAL(context.bytes_sent, partial_send);
268 EXPECT_EQUAL(context.bytes_sent, conn->wire_bytes_out);
269 EXPECT_EQUAL(context.bytes_sent, s2n_connection_get_wire_bytes_out(conn));
270 };
271
272 /* s2n_send sends all data, despite partial writes */
273 {
274 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
275 s2n_connection_ptr_free);
276 EXPECT_NOT_NULL(conn);
277 EXPECT_OK(s2n_connection_set_secrets(conn));
278
279 const struct s2n_send_result results[] = {
280 PARTIAL_SEND_RESULT(1),
281 PARTIAL_SEND_RESULT(5),
282 PARTIAL_SEND_RESULT(2),
283 OK_SEND_RESULT,
284 };
285 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
286 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
287 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
288
289 s2n_blocked_status blocked = 0;
290 EXPECT_EQUAL(s2n_send(conn, test_data, sizeof(test_data), &blocked), sizeof(test_data));
291 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
292 EXPECT_EQUAL(context.calls, s2n_array_len(results));
293 EXPECT_EQUAL(context.bytes_sent, test_data_bytes_sent);
294
295 /* Verify output buffer */
296 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
297 };
298
299 /* s2n_send would block and must be retried */
300 {
301 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
302 s2n_connection_ptr_free);
303 EXPECT_NOT_NULL(conn);
304 EXPECT_OK(s2n_connection_set_secrets(conn));
305
306 const uint32_t partial_send = 10;
307 const struct s2n_send_result results[] = {
308 PARTIAL_SEND_RESULT(partial_send),
309 BLOCK_SEND_RESULT,
310 PARTIAL_SEND_RESULT(partial_send),
311 BLOCK_SEND_RESULT,
312 OK_SEND_RESULT
313 };
314 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
315 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
316 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
317
318 s2n_blocked_status blocked = 0;
319
320 /* First attempt blocks */
321 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, test_data, sizeof(test_data), &blocked), S2N_ERR_IO_BLOCKED);
322 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
323 EXPECT_EQUAL(context.bytes_sent, partial_send);
324
325 /* Second attempt blocks */
326 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, test_data, sizeof(test_data), &blocked), S2N_ERR_IO_BLOCKED);
327 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
328 EXPECT_EQUAL(context.bytes_sent, partial_send * 2);
329
330 /* Third attempt completes */
331 EXPECT_EQUAL(s2n_send(conn, test_data, sizeof(test_data), &blocked), sizeof(test_data));
332 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
333 EXPECT_EQUAL(context.bytes_sent, test_data_bytes_sent);
334
335 /* Verify output buffer */
336 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
337 };
338
339 /* Calculating the record size for given data can be tricky.
340 * Instead, let's set the values based on the results of tests.
341 */
342 ssize_t large_test_data_bytes_sent = 0;
343
344 /* s2n_send sends multiple records worth of data */
345 {
346 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
347 s2n_connection_ptr_free);
348 EXPECT_NOT_NULL(conn);
349 EXPECT_OK(s2n_connection_set_secrets(conn));
350
351 struct s2n_send_result results[] = { OK_SEND_RESULT, OK_SEND_RESULT, OK_SEND_RESULT };
352 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
353 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
354 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
355
356 s2n_blocked_status blocked = 0;
357 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), sizeof(large_test_data));
358 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
359 EXPECT_EQUAL(context.calls, s2n_array_len(results));
360
361 large_test_data_bytes_sent = context.bytes_sent;
362 EXPECT_TRUE(large_test_data_bytes_sent > sizeof(large_test_data));
363
364 /* Verify output buffer */
365 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
366 };
367
368 /* s2n_send sends all records and data, despite partial writes */
369 {
370 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
371 s2n_connection_ptr_free);
372 EXPECT_NOT_NULL(conn);
373 EXPECT_OK(s2n_connection_set_secrets(conn));
374
375 struct s2n_send_result results[] = {
376 PARTIAL_SEND_RESULT(10), OK_SEND_RESULT,
377 OK_SEND_RESULT,
378 PARTIAL_SEND_RESULT(5), PARTIAL_SEND_RESULT(1), OK_SEND_RESULT
379 };
380 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
381 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
382 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
383
384 s2n_blocked_status blocked = 0;
385 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), sizeof(large_test_data));
386 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
387 EXPECT_EQUAL(context.calls, s2n_array_len(results));
388 EXPECT_EQUAL(context.bytes_sent, large_test_data_bytes_sent);
389
390 /* Verify output buffer */
391 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
392 };
393
394 /* s2n_send would block while sending multiple records */
395 {
396 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
397 s2n_connection_ptr_free);
398 EXPECT_NOT_NULL(conn);
399 EXPECT_OK(s2n_connection_set_secrets(conn));
400
401 const uint32_t partial_send = 10;
402 struct s2n_send_result results[] = {
403 OK_SEND_RESULT,
404 PARTIAL_SEND_RESULT(partial_send),
405 BLOCK_SEND_RESULT,
406 PARTIAL_SEND_RESULT(partial_send),
407 BLOCK_SEND_RESULT,
408 OK_SEND_RESULT, OK_SEND_RESULT, OK_SEND_RESULT
409 };
410 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
411 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
412 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
413
414 s2n_blocked_status blocked = 0;
415 ssize_t record_size = max_frag_bytes_sent[S2N_MFL_DEFAULT];
416
417 /* First attempt blocks after writing one record */
418 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked),
419 S2N_DEFAULT_FRAGMENT_LENGTH);
420 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
421 EXPECT_EQUAL(context.bytes_sent, record_size + partial_send);
422
423 /* Don't re-send the data already sent. */
424 const uint32_t offset = S2N_DEFAULT_FRAGMENT_LENGTH;
425
426 /* Second attempt blocks without writing another record */
427 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, large_test_data + offset, sizeof(large_test_data) - offset, &blocked),
428 S2N_ERR_IO_BLOCKED);
429 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
430 EXPECT_EQUAL(context.bytes_sent, record_size + partial_send + partial_send);
431
432 /* Third attempt completes */
433 EXPECT_EQUAL(s2n_send(conn, large_test_data + offset, sizeof(large_test_data) - offset, &blocked),
434 sizeof(large_test_data) - S2N_DEFAULT_FRAGMENT_LENGTH);
435 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
436 EXPECT_EQUAL(context.bytes_sent, large_test_data_bytes_sent);
437
438 /* Verify output buffer */
439 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
440 };
441
442 /* s2n_send would block after sending multiple records.
443 * ALL flushed records must be reported to the caller.
444 */
445 {
446 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
447 s2n_connection_ptr_free);
448 EXPECT_NOT_NULL(conn);
449 EXPECT_OK(s2n_connection_set_secrets(conn));
450
451 struct s2n_send_result results[] = {
452 OK_SEND_RESULT,
453 OK_SEND_RESULT,
454 BLOCK_SEND_RESULT,
455 OK_SEND_RESULT,
456 };
457 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
458 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
459 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
460
461 s2n_blocked_status blocked = 0;
462 ssize_t record_size = max_frag_bytes_sent[S2N_MFL_DEFAULT];
463
464 /* First attempt blocks after writing two records */
465 ssize_t expected_send = S2N_DEFAULT_FRAGMENT_LENGTH * 2;
466 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), expected_send);
467 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
468 EXPECT_EQUAL(context.bytes_sent, record_size * 2);
469
470 /* Don't re-send the data already sent. */
471 const uint32_t offset = expected_send;
472
473 /* Second attempt completes */
474 EXPECT_EQUAL(s2n_send(conn, large_test_data + offset, sizeof(large_test_data) - offset, &blocked),
475 sizeof(large_test_data) - offset);
476 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
477 EXPECT_EQUAL(context.bytes_sent, large_test_data_bytes_sent);
478
479 /* Verify output buffer */
480 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
481 };
482
483 /* Sending multiple records supports different maximum fragment lengths */
484 for (size_t mfl = 0; mfl < S2N_MFL_COUNT; mfl++) {
485 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
486 s2n_connection_ptr_free);
487 EXPECT_NOT_NULL(conn);
488 EXPECT_OK(s2n_connection_set_secrets(conn));
489 EXPECT_OK(s2n_set_test_max_fragment_len(conn, mfl));
490
491 struct s2n_send_context context = context_all_ok;
492 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
493 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
494
495 s2n_blocked_status blocked = 0;
496 EXPECT_EQUAL(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked), sizeof(large_test_data));
497 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
498
499 /* We expect enough calls to send to split the payload into records */
500 size_t expected_calls = ceil(sizeof(large_test_data) / (double) conn->max_outgoing_fragment_length);
501 EXPECT_EQUAL(context.calls, expected_calls);
502
503 /* Verify output buffer */
504 EXPECT_EQUAL(conn->out.blob.size, out_size[mfl]);
505 }
506
507 /* Test dynamic record threshold record fragmentation */
508 {
509 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
510 s2n_connection_ptr_free);
511 EXPECT_NOT_NULL(conn);
512 EXPECT_OK(s2n_connection_set_secrets(conn));
513
514 /* Retrieve the fragment size to expect */
515 uint16_t single_mtu_mfl = 0;
516 EXPECT_OK(s2n_record_min_write_payload_size(conn, &single_mtu_mfl));
517
518 /* Set the dynamic record threshold large enough for two small records */
519 const uint32_t resize_threshold = single_mtu_mfl * 2;
520 EXPECT_SUCCESS(s2n_connection_set_dynamic_record_threshold(conn, resize_threshold, UINT16_MAX));
521
522 struct s2n_send_result results[] = {
523 /* Block before sending the first record so that we can examine
524 * the connection state after buffering the first record.
525 */
526 BLOCK_SEND_RESULT, OK_SEND_RESULT,
527 /* Send the second record */
528 BLOCK_SEND_RESULT, OK_SEND_RESULT,
529 /* Send the third record */
530 OK_SEND_RESULT,
531 BLOCK_SEND_RESULT
532 };
533 struct s2n_send_context context = { .results = results, .results_len = s2n_array_len(results) };
534 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
535 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
536
537 s2n_blocked_status blocked = 0;
538 const size_t send_size = single_mtu_mfl * 2;
539
540 /* The first call to s2n_send blocks before sending the first record. */
541 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, large_test_data, send_size, &blocked),
542 S2N_ERR_IO_BLOCKED);
543 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
544 /* No records have been sent yet. */
545 EXPECT_EQUAL(context.bytes_sent, 0);
546 /* The first record is buffered,
547 * so its bytes still count towards the resize_threshold.
548 * We have NOT passed the threshold.
549 */
550 EXPECT_EQUAL(conn->active_application_bytes_consumed, single_mtu_mfl);
551 EXPECT_TRUE(conn->active_application_bytes_consumed < resize_threshold);
552 /* Output buffer should be able to handle the default size, not the single MTU size.
553 * Otherwise, the output buffer would need to resize later.
554 */
555 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
556
557 /* The second call to s2n_send flushes the buffered first record,
558 * but blocks before sending the second record.
559 */
560 ssize_t result = s2n_send(conn, large_test_data, send_size, &blocked);
561 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
562 /* First small, single-MTU record was sent. */
563 EXPECT_EQUAL(result, single_mtu_mfl);
564 EXPECT_TRUE(context.bytes_sent < ETH_MTU);
565 /* The second record is buffered,
566 * so its bytes count towards the resize_threshold.
567 * We have therefore hit the threshold.
568 */
569 EXPECT_EQUAL(conn->active_application_bytes_consumed, resize_threshold);
570 /* Output buffer should be able to handle the default size, not the single MTU size.
571 * Otherwise, the output buffer would need to resize later.
572 */
573 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
574
575 /* The third call to s2n_send flushes the second record. */
576 result = s2n_send(conn, large_test_data, send_size - single_mtu_mfl, &blocked);
577 EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
578 /* Second small, single-MTU record was sent. */
579 EXPECT_EQUAL(result, single_mtu_mfl);
580 /* There should be no change regarding the resize_threshold,
581 * since we did not construct any new records.
582 */
583 EXPECT_EQUAL(conn->active_application_bytes_consumed, resize_threshold);
584 /* Output buffer should be able to handle the default size, not the single MTU size.
585 * Otherwise, the output buffer would need to resize later.
586 */
587 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
588
589 /* The fourth call to s2n_send sends the third record. */
590 result = s2n_send(conn, large_test_data, conn->max_outgoing_fragment_length * 2, &blocked);
591 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
592 /* We have passed the resize_threshold, so records are no longer small.
593 * Instead they use the standard connection fragment length.
594 */
595 EXPECT_TRUE(result > single_mtu_mfl);
596 EXPECT_EQUAL(result, conn->max_outgoing_fragment_length);
597
598 /* Verify output buffer */
599 EXPECT_EQUAL(conn->out.blob.size, out_size[S2N_MFL_DEFAULT]);
600 };
601
602 /* Test: s2n_sendv_with_offset_total_size */
603 {
604 const struct iovec test_multiple_bufs[] = {
605 { .iov_len = 0 },
606 { .iov_len = 1 },
607 { .iov_len = 2 },
608 { .iov_len = 0 },
609 { .iov_len = 14 },
610 { .iov_len = 0 },
611 { .iov_len = 3 },
612 { .iov_len = 0 },
613 };
614 const ssize_t test_multiple_bufs_total_size = 20;
615
616 /* Safety */
617 {
618 const struct iovec test_buf = { 0 };
619 ssize_t out = 0;
620
621 /* Check null safety */
622 EXPECT_ERROR_WITH_ERRNO(
623 s2n_sendv_with_offset_total_size(&test_buf, 1, 0, NULL),
624 S2N_ERR_NULL);
625 EXPECT_ERROR_WITH_ERRNO(
626 s2n_sendv_with_offset_total_size(NULL, 1, 0, &out),
627 S2N_ERR_NULL);
628
629 /* Check negative safety */
630 EXPECT_OK(s2n_sendv_with_offset_total_size(NULL, -1, 0, &out));
631 EXPECT_EQUAL(out, 0);
632 EXPECT_OK(s2n_sendv_with_offset_total_size(&test_buf, -1, 0, &out));
633 EXPECT_EQUAL(out, 0);
634 EXPECT_ERROR_WITH_ERRNO(
635 s2n_sendv_with_offset_total_size(NULL, 0, -1, &out),
636 S2N_ERR_INVALID_ARGUMENT);
637 EXPECT_ERROR_WITH_ERRNO(
638 s2n_sendv_with_offset_total_size(&test_buf, 1, -1, &out),
639 S2N_ERR_INVALID_ARGUMENT);
640 }
641
642 /* No iovecs */
643 {
644 ssize_t out = 0;
645 EXPECT_OK(s2n_sendv_with_offset_total_size(NULL, 0, 0, &out));
646 EXPECT_EQUAL(out, 0);
647 }
648
649 /* Array of zero-length iovecs */
650 {
651 const struct iovec test_bufs[10] = { 0 };
652 ssize_t out = 0;
653 EXPECT_OK(s2n_sendv_with_offset_total_size(
654 test_bufs, s2n_array_len(test_bufs), 0, &out));
655 EXPECT_EQUAL(out, 0);
656 }
657
658 /* Single iovec */
659 {
660 const ssize_t expected_size = 10;
661 const struct iovec test_buf = { .iov_len = expected_size };
662 ssize_t out = 0;
663 EXPECT_OK(s2n_sendv_with_offset_total_size(&test_buf, 1, 0, &out));
664 EXPECT_EQUAL(out, expected_size);
665 }
666
667 /* Single iovec with offset */
668 {
669 const struct iovec test_buf = { .iov_len = 10 };
670 const ssize_t offset = 5;
671 ssize_t out = 0;
672 EXPECT_OK(s2n_sendv_with_offset_total_size(&test_buf, 1, offset, &out));
673 EXPECT_EQUAL(out, test_buf.iov_len - offset);
674 }
675
676 /* Multiple iovecs */
677 {
678 ssize_t out = 0;
679 EXPECT_OK(s2n_sendv_with_offset_total_size(
680 test_multiple_bufs, s2n_array_len(test_multiple_bufs), 0, &out));
681 EXPECT_EQUAL(out, test_multiple_bufs_total_size);
682 }
683
684 /* Multiple iovecs with offset */
685 {
686 const ssize_t offset = 10;
687 ssize_t out = 0;
688 EXPECT_OK(s2n_sendv_with_offset_total_size(
689 test_multiple_bufs, s2n_array_len(test_multiple_bufs), offset, &out));
690 EXPECT_EQUAL(out, test_multiple_bufs_total_size - offset);
691 }
692
693 /* Offset with no data */
694 {
695 const struct iovec test_bufs[10] = { 0 };
696 ssize_t out = 0;
697 EXPECT_ERROR_WITH_ERRNO(
698 s2n_sendv_with_offset_total_size(NULL, 0, 1, &out),
699 S2N_ERR_INVALID_ARGUMENT);
700 EXPECT_ERROR_WITH_ERRNO(
701 s2n_sendv_with_offset_total_size(test_bufs, 0, 1, &out),
702 S2N_ERR_INVALID_ARGUMENT);
703 EXPECT_ERROR_WITH_ERRNO(
704 s2n_sendv_with_offset_total_size(test_bufs, s2n_array_len(test_bufs), 1, &out),
705 S2N_ERR_INVALID_ARGUMENT);
706 }
707
708 /* Offset larger than available data */
709 {
710 const struct iovec test_buf = { .iov_len = 10 };
711 ssize_t out = 0;
712
713 ssize_t test_buf_offset = test_buf.iov_len + 1;
714 EXPECT_ERROR_WITH_ERRNO(
715 s2n_sendv_with_offset_total_size(&test_buf, 1, test_buf_offset, &out),
716 S2N_ERR_INVALID_ARGUMENT);
717
718 ssize_t test_multiple_bufs_offset = test_multiple_bufs_total_size + 1;
719 EXPECT_ERROR_WITH_ERRNO(
720 s2n_sendv_with_offset_total_size(test_multiple_bufs,
721 s2n_array_len(test_multiple_bufs), test_multiple_bufs_offset, &out),
722 S2N_ERR_INVALID_ARGUMENT);
723 }
724
725 /* Too much data to count
726 *
727 * This isn't really practically possible since an application would need
728 * to allocate more than SIZE_MAX memory for the iovec buffers, but we
729 * should ensure that the inputs don't cause unexpected behavior.
730 */
731 {
732 ssize_t out = 0;
733
734 const struct iovec test_bufs_ssize[] = {
735 { .iov_len = SSIZE_MAX },
736 { .iov_len = 1 },
737 };
738 EXPECT_ERROR_WITH_ERRNO(
739 s2n_sendv_with_offset_total_size(test_bufs_ssize, s2n_array_len(test_bufs_ssize), 0, &out),
740 S2N_ERR_INVALID_ARGUMENT);
741
742 const struct iovec test_bufs_size[] = {
743 { .iov_len = SIZE_MAX },
744 { .iov_len = 1 },
745 };
746 EXPECT_ERROR_WITH_ERRNO(
747 s2n_sendv_with_offset_total_size(test_bufs_size, s2n_array_len(test_bufs_size), 0, &out),
748 S2N_ERR_INVALID_ARGUMENT);
749 }
750 };
751
752 /* Test: s2n_flush is necessary and not achievable with s2n_send */
753 {
754 bool use_send[] = { true, false };
755
756 /* To reproduce the problematic scenario, we need to block on
757 * sending a record during a multi-record write.
758 */
759 struct s2n_send_result results[] = {
760 BLOCK_SEND_RESULT,
761 OK_SEND_RESULT,
762 OK_SEND_RESULT,
763 OK_SEND_RESULT,
764 };
765
766 for (size_t i = 0; i < s2n_array_len(use_send); i++) {
767 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
768 s2n_connection_ptr_free);
769 EXPECT_NOT_NULL(conn);
770 EXPECT_OK(s2n_connection_set_secrets(conn));
771
772 struct s2n_send_context context = {
773 .results = results,
774 .results_len = s2n_array_len(results)
775 };
776 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, s2n_test_send_cb));
777 EXPECT_SUCCESS(s2n_connection_set_send_ctx(conn, (void *) &context));
778
779 s2n_blocked_status blocked = 0;
780
781 /* First attempt blocks */
782 EXPECT_FAILURE_WITH_ERRNO(s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked),
783 S2N_ERR_IO_BLOCKED);
784 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
785 EXPECT_EQUAL(context.bytes_sent, 0);
786
787 /* For our control case, we attempt to use a zero-length send as flush */
788 if (use_send[i]) {
789 EXPECT_FAILURE_WITH_ERRNO(
790 s2n_send(conn, large_test_data, 0, &blocked),
791 S2N_ERR_SEND_SIZE);
792 continue;
793 }
794
795 /* Unlike the zero-length send, s2n_flush succeeds */
796 EXPECT_SUCCESS(s2n_flush(conn, &blocked));
797 EXPECT_EQUAL(context.bytes_sent, max_frag_bytes_sent[S2N_MFL_DEFAULT]);
798
799 /* We can also successfully finish sending */
800 EXPECT_EQUAL(
801 s2n_send(conn, large_test_data, sizeof(large_test_data), &blocked),
802 sizeof(large_test_data));
803 EXPECT_EQUAL(context.bytes_sent, large_test_data_bytes_sent);
804 }
805 };
806
807 END_TEST();
808 }