Debian Codegraph @

crt/s2n/tests/testlib/s2n_io_testlib.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 "testlib/s2n_testlib.h"
17
18 S2N_CLEANUP_RESULT s2n_test_iovecs_free(struct s2n_test_iovecs *in)
19 {
20 RESULT_ENSURE_REF(in);
21 for (size_t i = 0; i < in->iovecs_count; i++) {
22 RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &in->iovecs[i].iov_base,
23 in->iovecs[i].iov_len));
24 }
25 RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &in->iovecs,
26 sizeof(struct iovec) * in->iovecs_count));
27 return S2N_RESULT_OK;
28 }
29
30 static S2N_RESULT s2n_test_split_data(struct s2n_test_iovecs *iovecs, struct s2n_blob *data)
31 {
32 RESULT_ENSURE_REF(iovecs);
33 RESULT_ENSURE_REF(data);
34
35 struct s2n_stuffer in = { 0 };
36 RESULT_GUARD_POSIX(s2n_stuffer_init_written(&in, data));
37
38 for (size_t i = 0; i < iovecs->iovecs_count; i++) {
39 if (iovecs->iovecs[i].iov_len == 0) {
40 continue;
41 }
42 struct s2n_blob mem = { 0 };
43 RESULT_GUARD_POSIX(s2n_alloc(&mem, iovecs->iovecs[i].iov_len));
44 RESULT_GUARD_POSIX(s2n_stuffer_read(&in, &mem));
45 iovecs->iovecs[i].iov_base = mem.data;
46 }
47 RESULT_ENSURE_EQ(s2n_stuffer_data_available(&in), 0);
48 return S2N_RESULT_OK;
49 }
50
51 S2N_RESULT s2n_test_new_iovecs(struct s2n_test_iovecs *iovecs,
52 struct s2n_blob *data, const size_t *lens, size_t lens_count)
53 {
54 RESULT_ENSURE_REF(iovecs);
55 RESULT_ENSURE_REF(data);
56 RESULT_ENSURE_REF(lens);
57
58 size_t len_total = 0;
59 for (size_t i = 0; i < lens_count; i++) {
60 len_total += lens[i];
61 }
62 RESULT_ENSURE_LTE(len_total, data->size);
63
64 size_t iovecs_count = lens_count;
65 if (len_total < data->size) {
66 iovecs_count++;
67 }
68
69 struct s2n_blob iovecs_mem = { 0 };
70 RESULT_GUARD_POSIX(s2n_alloc(&iovecs_mem, sizeof(struct iovec) * iovecs_count));
71 RESULT_GUARD_POSIX(s2n_blob_zero(&iovecs_mem));
72 iovecs->iovecs = (struct iovec *) (void *) iovecs_mem.data;
73 iovecs->iovecs_count = iovecs_count;
74
75 for (size_t i = 0; i < lens_count; i++) {
76 iovecs->iovecs[i].iov_len = lens[i];
77 }
78 if (lens_count < iovecs_count) {
79 iovecs->iovecs[lens_count].iov_len = data->size - len_total;
80 }
81
82 RESULT_GUARD(s2n_test_split_data(iovecs, data));
83 return S2N_RESULT_OK;
84 }