Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_errno_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 <errno.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22
23 #define RANDOM_ERRNO 150
24
25 int fake_recv(void *io_context, uint8_t *buf, uint32_t len)
26 {
27 /* Pretend that we have no data available to read for alert lookup. */
28 errno = EAGAIN;
29 return -1;
30 }
31
32 int fake_send(void *io_context, const uint8_t *buf, uint32_t len)
33 {
34 /* Fail the write with non-retriable error. */
35 errno = ENOENT;
36 return -1;
37 }
38
39 int main(int argc, char **argv)
40 {
41 struct s2n_connection *conn = NULL;
42 s2n_blocked_status blocked;
43
44 BEGIN_TEST();
45 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
46
47 /* Non-retriable errnos in io are not overwritten by retriable errnos */
48 {
49 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
50 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
51
52 /* Set custom recv/send callbacks. */
53 EXPECT_SUCCESS(s2n_connection_set_recv_cb(conn, &fake_recv));
54 EXPECT_SUCCESS(s2n_connection_set_send_cb(conn, &fake_send));
55
56 /* Perform the handshake and expect an error from write, instead of error from alert read. */
57 EXPECT_EQUAL(s2n_negotiate(conn, &blocked), -1);
58 EXPECT_EQUAL(errno, ENOENT);
59 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO);
60 s2n_errno = 0;
61
62 EXPECT_SUCCESS(s2n_connection_free(conn));
63 };
64
65 /* s2n_negotiate should not overwrite a non-retriable s2n_error with a retriable s2n_error.
66 * This previously happened if errno was set to a retriable error before s2n_negotiate
67 * was called, and a non-retriable error occurred in an io function before errno was
68 * reset for the system call. */
69 {
70 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
71 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
72
73 /* Set errno to a random value */
74 errno = RANDOM_ERRNO;
75
76 /* Break io setup. Will fail a non-null check before io system call. */
77 conn->recv = NULL;
78
79 /* Perform the handshake and expect an s2n_error */
80 /* Do not use EXPECT_FAILURE here -- it resets s2n_errno */
81 EXPECT_EQUAL(s2n_negotiate(conn, &blocked), -1);
82 EXPECT_EQUAL(errno, 0);
83 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO);
84 s2n_errno = 0;
85
86 EXPECT_SUCCESS(s2n_connection_free(conn));
87 };
88
89 END_TEST();
90 }