| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 89 |
END_TEST(); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
int ro_file = open(argv[0], O_RDONLY); |
| 94 |
EXPECT_TRUE(ro_file > 0); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 190 |
|
| 191 |
|
| 192 |
EXPECT_TRUE(total_bytes_written < buffer_size); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |