Debian Codegraph @

crt/s2n/tests/unit/s2n_testlib_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 "testlib/s2n_testlib.h"
17
18 #include "s2n_test.h"
19
20 int main(int argc, char **argv)
21 {
22 BEGIN_TEST();
23 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
24
25 /* Test that s2n_negotiate_test_server_and_client produces useful errors.
26 * In the past, we failed to surface errors and instead reported io errors when
27 * the failed connection's peer couldn't read the next expected message.
28 *
29 * We should always report the actual error to allow better debugging of tests.
30 */
31 {
32 struct s2n_connection *server_conn = NULL;
33 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
34
35 /* Create nonblocking pipes */
36 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
37 POSIX_GUARD(s2n_io_pair_init_non_blocking(&io_pair));
38 POSIX_GUARD(s2n_connection_set_io_pair(server_conn, &io_pair));
39
40 /* This should NEVER fail with an error related to blocked IO. */
41 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, NULL), S2N_ERR_NULL);
42
43 EXPECT_SUCCESS(s2n_connection_free(server_conn));
44 };
45
46 /* Test s2n_blob_alloc_from_hex_with_whitespace */
47 {
48 struct {
49 const char *input;
50 size_t expected_output_size;
51 uint8_t expected_output[100];
52 } test_cases[] = {
53 { .input = "abcd", .expected_output = { 171, 205 }, .expected_output_size = 2 },
54 { .input = "ab cd", .expected_output = { 171, 205 }, .expected_output_size = 2 },
55 { .input = " abcd", .expected_output = { 171, 205 }, .expected_output_size = 2 },
56 { .input = "abcd ", .expected_output = { 171, 205 }, .expected_output_size = 2 },
57 { .input = " ab cd ", .expected_output = { 171, 205 }, .expected_output_size = 2 },
58 { .input = "", .expected_output = { 0 }, .expected_output_size = 0 },
59 { .input = " ", .expected_output = { 0 }, .expected_output_size = 0 },
60 { .input = "12 34 56 78 90", .expected_output = { 18, 52, 86, 120, 144 }, .expected_output_size = 5 },
61 { .input = "1234567890", .expected_output = { 18, 52, 86, 120, 144 }, .expected_output_size = 5 },
62 };
63 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
64 DEFER_CLEANUP(struct s2n_blob actual_output = { 0 }, s2n_free);
65 EXPECT_OK(s2n_blob_alloc_from_hex_with_whitespace(&actual_output, test_cases[i].input));
66 EXPECT_EQUAL(actual_output.size, test_cases[i].expected_output_size);
67 EXPECT_BYTEARRAY_EQUAL(actual_output.data, test_cases[i].expected_output, actual_output.size);
68 }
69 };
70
71 END_TEST();
72 }