Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_shutdown_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 "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18
19 int main(int argc, char **argv)
20 {
21 BEGIN_TEST();
22
23 const s2n_mode modes[] = { S2N_CLIENT, S2N_SERVER };
24
25 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
26 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
27 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
28
29 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
30 EXPECT_NOT_NULL(config);
31 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
32 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
33 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
34
35 /* Self-Talk: shutdown during handshake */
36 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
37 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
38 s2n_connection_ptr_free);
39 EXPECT_NOT_NULL(server_conn);
40 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
41
42 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
43 s2n_connection_ptr_free);
44 EXPECT_NOT_NULL(client_conn);
45 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
46
47 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
48 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
49 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
50
51 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn, SERVER_CERT));
52 EXPECT_FALSE(s2n_handshake_is_complete(client_conn));
53 EXPECT_FALSE(s2n_handshake_is_complete(server_conn));
54
55 /* Choose which connection will request the shutdown */
56 struct s2n_connection *request = server_conn;
57 struct s2n_connection *response = client_conn;
58 if (modes[mode_i] == S2N_CLIENT) {
59 request = client_conn;
60 response = server_conn;
61 }
62
63 /* Both shutdown attempts should succeed */
64 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
65 EXPECT_SUCCESS(s2n_shutdown(request, &blocked));
66 EXPECT_SUCCESS(s2n_shutdown(response, &blocked));
67
68 /* Both connections successfully closed */
69 EXPECT_TRUE(s2n_connection_check_io_status(server_conn, S2N_IO_CLOSED));
70 EXPECT_TRUE(s2n_connection_check_io_status(client_conn, S2N_IO_CLOSED));
71
72 /* Closed connections behave properly */
73 for (size_t i = 0; i < 5; i++) {
74 /* Future attempts to shutdown succeed (they are no-ops) */
75 EXPECT_SUCCESS(s2n_shutdown(server_conn, &blocked));
76 EXPECT_SUCCESS(s2n_shutdown(client_conn, &blocked));
77 }
78 };
79
80 /* Self-Talk: shutdown during application data */
81 for (size_t mode_i = 0; mode_i < s2n_array_len(modes); mode_i++) {
82 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
83 s2n_connection_ptr_free);
84 EXPECT_NOT_NULL(server_conn);
85 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
86
87 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
88 s2n_connection_ptr_free);
89 EXPECT_NOT_NULL(client_conn);
90 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
91
92 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
93 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
94 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
95
96 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
97
98 /* Both the client and server should send application data, but not read any.
99 * This leaves unread application data for both to handle.
100 */
101 uint8_t data[] = "hello world";
102 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
103 EXPECT_EQUAL(s2n_send(server_conn, data, sizeof(data), &blocked), sizeof(data));
104 EXPECT_EQUAL(s2n_send(client_conn, data, sizeof(data), &blocked), sizeof(data));
105
106 /* Choose which connection will request the shutdown, and which will respond */
107 struct s2n_connection *request = server_conn;
108 struct s2n_connection *response = client_conn;
109 if (modes[mode_i] == S2N_CLIENT) {
110 request = client_conn;
111 response = server_conn;
112 }
113
114 /* The requester's first shutdown attempt will send a close_notify
115 * but then block on receiving the response close_notify.
116 */
117 EXPECT_FAILURE_WITH_ERRNO(s2n_shutdown(request, &blocked), S2N_ERR_IO_BLOCKED);
118 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
119
120 /* The responder's first shutdown attempt will receive the request close_notify,
121 * send the response close_notify, and then consider the shutdown successful.
122 */
123 EXPECT_SUCCESS(s2n_shutdown(response, &blocked));
124
125 /* The requester's second shutdown attempt will receive the response close_notify
126 * and then consider the shutdown successful.
127 */
128 EXPECT_SUCCESS(s2n_shutdown(request, &blocked));
129
130 /* Both connections successfully closed */
131 EXPECT_TRUE(s2n_connection_check_io_status(server_conn, S2N_IO_CLOSED));
132 EXPECT_TRUE(s2n_connection_check_io_status(client_conn, S2N_IO_CLOSED));
133
134 /* Closed connections behave properly */
135 for (size_t i = 0; i < 5; i++) {
136 /* Future attempts to shutdown succeed (they are no-ops) */
137 EXPECT_SUCCESS(s2n_shutdown(server_conn, &blocked));
138 EXPECT_SUCCESS(s2n_shutdown(client_conn, &blocked));
139
140 /* Future attempts to write fail */
141 EXPECT_FAILURE_WITH_ERRNO(s2n_send(server_conn, data, sizeof(data), &blocked), S2N_ERR_CLOSED);
142 EXPECT_FAILURE_WITH_ERRNO(s2n_send(client_conn, data, sizeof(data), &blocked), S2N_ERR_CLOSED);
143
144 /* Future attempts to read indicate end of stream */
145 EXPECT_EQUAL(s2n_recv(server_conn, data, sizeof(data), &blocked), 0);
146 EXPECT_EQUAL(s2n_recv(client_conn, data, sizeof(data), &blocked), 0);
147 }
148 };
149
150 END_TEST();
151 }