File: s2n_recv_buffering_test.c

package info (click to toggle)
aws-crt-python 0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,932 kB
  • sloc: ansic: 418,984; python: 23,626; makefile: 6,035; sh: 4,075; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (619 lines) | stat: -rw-r--r-- 29,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include "api/s2n.h"
#include "api/unstable/renegotiate.h"
#include "s2n_test.h"
#include "testlib/s2n_ktls_test_utils.h"
#include "testlib/s2n_testlib.h"
#include "utils/s2n_random.h"

struct s2n_recv_wrapper {
    size_t count;
    s2n_recv_fn *inner_recv;
    void *inner_recv_ctx;
};

static int s2n_counting_read(void *io_context, uint8_t *buf, uint32_t len)
{
    struct s2n_recv_wrapper *context = (struct s2n_recv_wrapper *) io_context;
    context->count++;
    return context->inner_recv(context->inner_recv_ctx, buf, len);
}

static S2N_RESULT s2n_connection_set_counting_read(struct s2n_connection *reader,
        struct s2n_recv_wrapper *wrapper)
{
    /* We'd need to handle cleanup for managed IO */
    RESULT_ENSURE(!reader->managed_recv_io, S2N_ERR_SAFETY);

    wrapper->inner_recv = reader->recv;
    reader->recv = s2n_counting_read;
    wrapper->inner_recv_ctx = reader->recv_io_context;
    reader->recv_io_context = wrapper;
    wrapper->count = 0;
    return S2N_RESULT_OK;
}

int main(int argc, char **argv)
{
    BEGIN_TEST();

    const uint8_t test_data[20] = "hello world";
    const size_t buffer_in_size = S2N_LARGE_FRAGMENT_LENGTH;

    DEFER_CLEANUP(struct s2n_cert_chain_and_key * chain_and_key,
            s2n_cert_chain_and_key_ptr_free);
    EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
            S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));

    DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(),
            s2n_config_ptr_free);
    EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
    EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
    EXPECT_SUCCESS(s2n_config_disable_x509_verification(config));

    DEFER_CLEANUP(struct s2n_config *multi_config = s2n_config_new(),
            s2n_config_ptr_free);
    EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(multi_config, chain_and_key));
    EXPECT_SUCCESS(s2n_config_set_cipher_preferences(multi_config, "default_tls13"));
    EXPECT_SUCCESS(s2n_config_disable_x509_verification(multi_config));
    EXPECT_SUCCESS(s2n_config_set_recv_multi_record(multi_config, true));

    /* Test: Read a single record */
    uint32_t test_record_size_val = 0;
    {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));

        struct s2n_recv_wrapper counter = { 0 };
        EXPECT_OK(s2n_connection_set_counting_read(server, &counter));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
        test_record_size_val = s2n_stuffer_data_available(&io_pair.server_in);
        EXPECT_TRUE(test_record_size_val > sizeof(test_data));

        uint8_t buffer[sizeof(test_data)] = { 0 };
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));
        EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(test_data), &blocked), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
        EXPECT_EQUAL(counter.count, 1);
    }
    const uint32_t test_record_size = test_record_size_val;

    /* Test: Read the handshake */
    {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
    }

    /* Test: Read a record larger than the input buffer */
    {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        client->max_outgoing_fragment_length = UINT16_MAX;
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));

        struct s2n_recv_wrapper counter = { 0 };
        EXPECT_OK(s2n_connection_set_counting_read(server, &counter));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        DEFER_CLEANUP(struct s2n_blob max_fragment_buffer = { 0 }, s2n_free);
        EXPECT_SUCCESS(s2n_alloc(&max_fragment_buffer, S2N_LARGE_FRAGMENT_LENGTH));

        /* Send a record that won't fit in the default input buffer */
        EXPECT_EQUAL(
                s2n_send(client, max_fragment_buffer.data, max_fragment_buffer.size, &blocked),
                max_fragment_buffer.size);
        size_t record_size = s2n_stuffer_data_available(&io_pair.server_in);
        size_t fragment_size = record_size - S2N_TLS_RECORD_HEADER_LENGTH;
        EXPECT_TRUE(fragment_size > buffer_in_size);

        /* Test that the record can be received and the input buffer resized */
        EXPECT_EQUAL(
                s2n_recv(server, max_fragment_buffer.data, max_fragment_buffer.size, &blocked),
                max_fragment_buffer.size);
        EXPECT_TRUE(s2n_stuffer_space_remaining(&server->buffer_in) > fragment_size);
        /* The header fits on the first read, but the rest of the data doesn't.
         * We need a (large) shift + read to get the rest of the data.
         */
        EXPECT_EQUAL(counter.count, 2);

        /* Check that another record can be received afterwards */
        uint8_t buffer[sizeof(test_data)] = { 0 };
        EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
        EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(test_data), &blocked), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
        EXPECT_EQUAL(counter.count, 3);
    }

    /* Test: Read multiple small records */
    for (size_t greedy = 0; greedy <= 1; greedy++) {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        if (greedy) {
            EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
            EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));
        }

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));

        struct s2n_recv_wrapper counter = { 0 };
        EXPECT_OK(s2n_connection_set_counting_read(server, &counter));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        for (size_t i = 1; i <= sizeof(test_data); i++) {
            EXPECT_EQUAL(s2n_send(client, test_data, i, &blocked), i);
        }

        uint8_t buffer[sizeof(test_data)] = { 0 };
        for (size_t i = 1; i <= sizeof(test_data); i++) {
            EXPECT_EQUAL(s2n_recv(server, buffer, i, &blocked), i);
            EXPECT_BYTEARRAY_EQUAL(buffer, test_data, i);

            if (greedy) {
                /* All our small records combined are smaller than the maximum
                 * TLS record size, so they should all be buffered immediately.
                 * Only one read is ever necessary.
                 */
                EXPECT_EQUAL(counter.count, 1);
            } else {
                /* We call recv twice for every record */
                EXPECT_EQUAL(counter.count, i * 2);
            }
        }

        /* The input buffer size does not change with greedy vs not greedy */
        EXPECT_EQUAL(server->buffer_in.blob.allocated, buffer_in_size);

        /* If all data is consumed, the input buffer can be released */
        EXPECT_SUCCESS(s2n_connection_release_buffers(server));
        EXPECT_EQUAL(server->buffer_in.blob.allocated, 0);
    }

    /* Test: Read multiple small records with "multi_record" enabled */
    {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, multi_config));

        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, multi_config));

        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));

        struct s2n_recv_wrapper counter = { 0 };
        EXPECT_OK(s2n_connection_set_counting_read(server, &counter));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        for (size_t i = 0; i < sizeof(test_data); i++) {
            EXPECT_EQUAL(s2n_send(client, test_data + i, 1, &blocked), 1);
        }

        uint8_t buffer[sizeof(test_data)] = { 0 };
        EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
        EXPECT_EQUAL(counter.count, 1);
    }

    /* Test: Read the rest of a partial record */
    for (size_t i = 0; i < test_record_size; i++) {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));

        struct s2n_recv_wrapper counter = { 0 };
        EXPECT_OK(s2n_connection_set_counting_read(server, &counter));

        size_t expected_count = 0;

        /* Test: manually copy some of the record into the read buffer */
        {
            s2n_blocked_status blocked = S2N_NOT_BLOCKED;
            EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
            EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), test_record_size);
            EXPECT_SUCCESS(s2n_stuffer_copy(&io_pair.server_in, &server->buffer_in, i));

            uint8_t buffer[sizeof(test_data)] = { 0 };
            EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
            EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
            expected_count++;
            EXPECT_EQUAL(counter.count, expected_count);
        }

        /* Test: force the first recv to return partial data */
        {
            s2n_blocked_status blocked = S2N_NOT_BLOCKED;
            EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), 0);
            EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
            EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), test_record_size);

            io_pair.server_in.write_cursor -= (test_record_size - i);

            uint8_t buffer[sizeof(test_data)] = { 0 };
            EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, buffer, sizeof(buffer), &blocked),
                    S2N_ERR_IO_BLOCKED);
            expected_count++;
            /* If the first call returns any data, then a second call is made.
             * The second call blocks. */
            if (i != 0) {
                expected_count++;
            }
            EXPECT_EQUAL(counter.count, expected_count);

            io_pair.server_in.write_cursor += (test_record_size - i);

            EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
            EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
            expected_count++;
            EXPECT_EQUAL(counter.count, expected_count);
        }
    }

    /* Test: read a single record one byte at a time */
    {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));

        struct s2n_recv_wrapper counter = { 0 };
        EXPECT_OK(s2n_connection_set_counting_read(server, &counter));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), 0);
        EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
        EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), test_record_size);
        io_pair.server_in.write_cursor -= test_record_size;

        size_t expected_count = 0;
        uint8_t buffer[sizeof(test_data)] = { 0 };
        for (size_t i = 1; i < test_record_size; i++) {
            /* Reads no additional data-- just blocks */
            EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, buffer, sizeof(buffer), &blocked),
                    S2N_ERR_IO_BLOCKED);
            expected_count++;
            EXPECT_EQUAL(counter.count, expected_count);

            /* Reads the next byte, then blocks again */
            io_pair.server_in.write_cursor++;
            EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server, buffer, sizeof(buffer), &blocked),
                    S2N_ERR_IO_BLOCKED);
            expected_count += 2;
            EXPECT_EQUAL(counter.count, expected_count);
        }

        /* Reads the final byte and succeeds */
        io_pair.server_in.write_cursor++;
        EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
        expected_count++;
        EXPECT_EQUAL(counter.count, expected_count);
    }

    /* Test: Read into a buffer that already contains data from a previous read */
    const struct {
        /* The offset the current record should begin at */
        uint16_t offset;
        /* Assert that shifting occurred if necessary */
        uint16_t final_offset;
        /* Most offsets result in a single read */
        uint8_t reads;
    } test_offsets[] = {
        /* Basic small offsets: single read, no shifting */
        { .offset = 0, .final_offset = test_record_size, .reads = 1 },
        { .offset = 10, .final_offset = 10 + test_record_size, .reads = 1 },
        { .offset = 1000, .final_offset = 1000 + test_record_size, .reads = 1 },
        /* Exactly enough space remaining in the buffer, so no shift or second read.
         * We wipe the buffer after: the extra byte we add to avoid the wipe isn't
         * read because we read exactly as much data as we need.
         */
        {
                .offset = buffer_in_size - test_record_size,
                .final_offset = 0,
                .reads = 1,
        },
        /* If we have enough space in the buffer for the next header,
         * but not enough for the next fragment, then we must still read twice.
         */
        {
                .offset = buffer_in_size - S2N_TLS_RECORD_HEADER_LENGTH,
                .final_offset = test_record_size - S2N_TLS_RECORD_HEADER_LENGTH,
                .reads = 2,
        },
        {
                .offset = buffer_in_size - S2N_TLS_RECORD_HEADER_LENGTH - 1,
                .final_offset = test_record_size - S2N_TLS_RECORD_HEADER_LENGTH,
                .reads = 2,
        },
        /* Not enough space in the buffer for the header or the fragment.
         * We have to shift but don't need a second read.
         */
        { .offset = buffer_in_size - 3, .final_offset = test_record_size, .reads = 1 },
        { .offset = buffer_in_size - 1, .final_offset = test_record_size, .reads = 1 },
        { .offset = buffer_in_size, .final_offset = test_record_size, .reads = 1 },
    };
    for (size_t i = 0; i < s2n_array_len(test_offsets); i++) {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));

        struct s2n_recv_wrapper counter = { 0 };
        EXPECT_OK(s2n_connection_set_counting_read(server, &counter));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
        EXPECT_EQUAL(s2n_stuffer_data_available(&io_pair.server_in), test_record_size);
        /* Write one more byte so that we won't wipe buffer_in after the read.
         * This will let us better examine the state of the buffer.
         */
        EXPECT_SUCCESS(s2n_stuffer_write_uint8(&io_pair.server_in, 0));

        uint16_t offset = test_offsets[i].offset;
        EXPECT_SUCCESS(s2n_stuffer_wipe(&server->buffer_in));
        EXPECT_SUCCESS(s2n_stuffer_skip_write(&server->buffer_in, offset));
        EXPECT_SUCCESS(s2n_stuffer_skip_read(&server->buffer_in, offset));
        if (offset < buffer_in_size) {
            /* Preemptively copy one byte of the next record into buffer_in.
             * If we don't do this, we just wipe buffer_in before the read,
             * making this test trivial.
             */
            EXPECT_SUCCESS(s2n_stuffer_copy(&io_pair.server_in, &server->buffer_in, 1));
        }

        uint8_t buffer[sizeof(test_data)] = { 0 };

        EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
        EXPECT_EQUAL(counter.count, test_offsets[i].reads);
        uint32_t expected_final_offset = test_offsets[i].final_offset;
        /* If there is an offset, consider the extra byte we added to avoid the final wipe. */
        if (expected_final_offset != 0) {
            expected_final_offset++;
        }
        EXPECT_EQUAL(server->buffer_in.write_cursor, expected_final_offset);
    }

    /* Test: Toggle recv_greedy while reading */
    {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
        EXPECT_SUCCESS(s2n_stuffer_wipe(&io_pair.server_in));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        uint8_t buffer[sizeof(test_data)] = { 0 };

        /* Send many records */
        const size_t records_count = 100;
        for (size_t i = 0; i < records_count; i++) {
            EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
            EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
        }

        for (size_t i = 0; i < records_count / 2; i++) {
            EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));
            EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
            EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
            EXPECT_TRUE(s2n_stuffer_data_available(&server->buffer_in));

            EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, false));
            EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
            EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
        }
    }

    /* Test: s2n_connection_release_buffers with data remaining in buffer_in */
    {
        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, config));
        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, config));

        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(client, true));
        EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));

        DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
        EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
        EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
        EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
        EXPECT_SUCCESS(s2n_stuffer_wipe(&io_pair.server_in));

        /* Send two records */
        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
        EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));

        /* Only consume a partial record */
        io_pair.server_in.write_cursor = test_record_size / 2;
        uint8_t buffer[sizeof(test_data)] = { 0 };
        EXPECT_FAILURE_WITH_ERRNO(
                s2n_recv(server, buffer, sizeof(test_data), &blocked),
                S2N_ERR_IO_BLOCKED);
        EXPECT_TRUE(s2n_stuffer_data_available(&server->in));
        EXPECT_FAILURE_WITH_ERRNO(
                s2n_connection_release_buffers(server),
                S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA);

        /* Consume the full first record */
        /* cppcheck-suppress redundantAssignment */
        io_pair.server_in.write_cursor = test_record_size * 2;
        EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));

        /* Release buffers */
        EXPECT_TRUE(s2n_stuffer_data_available(&server->buffer_in));
        EXPECT_SUCCESS(s2n_connection_release_buffers(server));
        EXPECT_TRUE(s2n_stuffer_data_available(&server->buffer_in));

        /* Consume the full second record */
        EXPECT_EQUAL(s2n_recv(server, buffer, sizeof(buffer), &blocked), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(buffer, test_data, sizeof(test_data));
    }

    /* Test: s2n_peek_buffered */
    {
        EXPECT_EQUAL(s2n_peek_buffered(NULL), 0);

        DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(client, multi_config));

        DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_SUCCESS(s2n_connection_set_config(server, multi_config));

        struct {
            uint32_t read_size;
            uint32_t expect_available;
            uint32_t expect_buffered;
        } test_cases[] = {
            {
                    .read_size = 1,
                    .expect_available = sizeof(test_data) - 1,
                    .expect_buffered = test_record_size,
            },
            {
                    .read_size = sizeof(test_data) - 1,
                    .expect_available = 1,
                    .expect_buffered = test_record_size,
            },
            {
                    .read_size = sizeof(test_data),
                    .expect_available = 0,
                    .expect_buffered = test_record_size,
            },
            {
                    .read_size = sizeof(test_data) + 1,
                    .expect_available = sizeof(test_data) - 1,
                    .expect_buffered = 0,
            },
        };
        for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
            DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
            EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
            EXPECT_OK(s2n_connections_set_io_stuffer_pair(client, server, &io_pair));
            EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client));
            EXPECT_SUCCESS(s2n_stuffer_wipe(&io_pair.server_in));

            s2n_blocked_status blocked = S2N_NOT_BLOCKED;
            uint8_t buffer[sizeof(test_data) * 2] = { 0 };

            EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));
            EXPECT_EQUAL(s2n_send(client, test_data, sizeof(test_data), &blocked), sizeof(test_data));

            uint32_t read_size = test_cases[i].read_size;
            EXPECT_SUCCESS(s2n_connection_set_recv_buffering(server, true));
            EXPECT_EQUAL(s2n_recv(server, buffer, read_size, &blocked), read_size);
            EXPECT_EQUAL(s2n_peek_buffered(server), test_cases[i].expect_buffered);
            EXPECT_EQUAL(s2n_peek(server), test_cases[i].expect_available);

            EXPECT_SUCCESS(s2n_connection_wipe(client));
            EXPECT_SUCCESS(s2n_connection_wipe(server));
        }
    }

    END_TEST();
}