File: s2n_ktls_io_sendfile_test.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (265 lines) | stat: -rw-r--r-- 10,142 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
/*
 * 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 <fcntl.h>
#include <sys/socket.h>

#include "crypto/s2n_sequence.h"
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_ktls.h"
#include "utils/s2n_random.h"

static S2N_RESULT s2n_test_get_seq_num(struct s2n_connection *conn, uint64_t *seq_num_out)
{
    struct s2n_blob seq_num = { 0 };
    RESULT_GUARD(s2n_connection_get_sequence_number(conn, conn->mode, &seq_num));
    RESULT_GUARD_POSIX(s2n_sequence_number_to_uint64(&seq_num, seq_num_out));
    return S2N_RESULT_OK;
}

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

#ifdef S2N_LINUX_SENDFILE
    const bool sendfile_supported = true;
#else
    const bool sendfile_supported = false;
#endif

    /* Test feature probe */
    {
#if defined(__linux__)
        EXPECT_TRUE(sendfile_supported);
#endif
#if defined(__FreeBSD__)
        EXPECT_FALSE(sendfile_supported);
#endif
    };

    /* Safety */
    {
        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        size_t bytes_written = 0;

        EXPECT_FAILURE_WITH_ERRNO(
                s2n_sendfile(NULL, 0, 0, 0, &bytes_written, &blocked),
                S2N_ERR_NULL);
        EXPECT_FAILURE_WITH_ERRNO(
                s2n_sendfile(conn, 0, 0, 0, NULL, &blocked),
                S2N_ERR_NULL);
        EXPECT_FAILURE_WITH_ERRNO(
                s2n_sendfile(conn, 0, 0, 0, &bytes_written, NULL),
                S2N_ERR_NULL);
    };

    /* Test s2n_sendfile unsupported */
    if (!sendfile_supported) {
        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        conn->ktls_send_enabled = true;

        DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        size_t bytes_written = 0;
        int result = s2n_sendfile(conn, 1, 0, 1, &bytes_written, &blocked);
        EXPECT_FAILURE_WITH_ERRNO(result, S2N_ERR_UNIMPLEMENTED);

        /* We do not run any further tests */
        END_TEST();
    };

    /* The one file we know definitely exists is our own executable */
    int ro_file = open(argv[0], O_RDONLY);
    EXPECT_TRUE(ro_file > 0);

    /* use pread to read the beginning of the file without updating its offset.
     * Careful: if any call to sendfile sets offset=NULL, the file's offset will
     * be updated and different data will be read.
     */
    uint8_t test_data[100] = { 0 };
    EXPECT_EQUAL(pread(ro_file, test_data, sizeof(test_data), 0), sizeof(test_data));

    /* Test: successful send */
    {
        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        conn->ktls_send_enabled = true;

        DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        int write_fd = io_pair.server;
        int read_fd = io_pair.client;
        EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        size_t bytes_written = 0;
        EXPECT_SUCCESS(s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
                &bytes_written, &blocked));
        EXPECT_EQUAL(bytes_written, sizeof(test_data));
        EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);

        uint8_t written[sizeof(test_data)] = { 0 };
        EXPECT_EQUAL(read(read_fd, written, sizeof(written)), sizeof(test_data));
        EXPECT_BYTEARRAY_EQUAL(written, test_data, sizeof(test_data));
        EXPECT_TRUE(read(read_fd, written, sizeof(written)) < 0);
    };

    /* Test: IO error */
    {
        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        conn->ktls_send_enabled = true;

        DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        int write_fd = io_pair.server;
        int read_fd = io_pair.client;
        EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));

        /* Close one side of the stream to make the fds invalid */
        close(read_fd);

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        size_t bytes_written = 0;
        int ret = s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
                &bytes_written, &blocked);
        EXPECT_FAILURE_WITH_ERRNO(ret, S2N_ERR_IO);
        EXPECT_EQUAL(bytes_written, 0);
        EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
    };

    /* Test: send blocks */
    {
        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        conn->ktls_send_enabled = true;

        DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        int write_fd = io_pair.server;
        EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));

        /* We can force the socket to block by filling up its send buffer. */
        int buffer_size = 0;
        socklen_t optlen = sizeof(buffer_size);
        EXPECT_EQUAL(getsockopt(write_fd, SOL_SOCKET, SO_SNDBUF, &buffer_size, &optlen), 0);
        EXPECT_TRUE(buffer_size > 0);

        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        size_t bytes_written = 0;
        size_t total_bytes_written = 0;
        while (true) {
            int result = s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
                    &bytes_written, &blocked);
            if (result < 0) {
                EXPECT_FAILURE_WITH_ERRNO(result, S2N_ERR_IO_BLOCKED);
                EXPECT_EQUAL(bytes_written, 0);
                EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_WRITE);
                break;
            }

            EXPECT_TRUE(bytes_written <= sizeof(test_data));
            EXPECT_TRUE(bytes_written > 0);
            EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
            total_bytes_written += bytes_written;

            /* The socket will block before buffer_size bytes are written.
             * If we successfully send buffer_size bytes, something is wrong.
             */
            EXPECT_TRUE(total_bytes_written < buffer_size);
        }
    };

    /* Test: partial write */
    {
        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        conn->ktls_send_enabled = true;

        DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        int write_fd = io_pair.server;
        EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, write_fd));

        int buffer_size = 0;
        socklen_t optlen = sizeof(buffer_size);
        EXPECT_EQUAL(getsockopt(write_fd, SOL_SOCKET, SO_SNDBUF, &buffer_size, &optlen), 0);
        EXPECT_TRUE(buffer_size > 0);

        /* Try to write more data than the buffer can hold in a single sendfile call */
        size_t bytes_to_write = buffer_size * 2;
        size_t bytes_written = 0;
        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        EXPECT_SUCCESS(s2n_sendfile(conn, ro_file, 0, bytes_to_write,
                &bytes_written, &blocked));
        EXPECT_TRUE(bytes_written > 0);
        EXPECT_TRUE(bytes_written < bytes_to_write);
        EXPECT_EQUAL(blocked, S2N_NOT_BLOCKED);
    };

    /* Test: key encryption limit tracked */
    {
        struct s2n_record_algorithm test_record_alg = *s2n_tls13_aes_128_gcm_sha256.record_alg;
        test_record_alg.encryption_limit = 0;
        struct s2n_cipher_suite test_cipher_suite = s2n_tls13_aes_128_gcm_sha256;
        test_cipher_suite.record_alg = &test_record_alg;

        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER),
                s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        conn->ktls_send_enabled = true;
        conn->actual_protocol_version = S2N_TLS13;

        DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
        EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
        EXPECT_SUCCESS(s2n_connection_set_write_fd(conn, io_pair.server));

        /* Test: The sequence number starts at 0 */
        uint64_t seq_num = 1;
        EXPECT_OK(s2n_test_get_seq_num(conn, &seq_num));
        EXPECT_EQUAL(seq_num, 0);

        /* Test: The sequence number tracks data sent */
        s2n_blocked_status blocked = S2N_NOT_BLOCKED;
        size_t bytes_written = 0;
        EXPECT_SUCCESS(s2n_sendfile(conn, ro_file, 0, sizeof(test_data),
                &bytes_written, &blocked));
        EXPECT_OK(s2n_test_get_seq_num(conn, &seq_num));
        EXPECT_TRUE(seq_num > 0);

        /* Test: Enforce the encryption limit */
        EXPECT_NOT_NULL(conn->secure);
        conn->secure->cipher_suite = &test_cipher_suite;
        EXPECT_FAILURE_WITH_ERRNO(
                s2n_sendfile(conn, ro_file, 0, sizeof(test_data), &bytes_written, &blocked),
                S2N_ERR_KTLS_KEY_LIMIT);
    };

    EXPECT_EQUAL(close(ro_file), 0);
    END_TEST();
}