Debian Codegraph @

crt/s2n/tests/unit/s2n_events_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 "utils/s2n_events.h"
17
18 #include <math.h>
19 #include <stdlib.h>
20
21 #include "error/s2n_errno.h"
22 #include "s2n_test.h"
23 #include "testlib/s2n_testlib.h"
24 #include "tls/s2n_connection.h"
25 #include "tls/s2n_post_handshake.h"
26 #include "tls/s2n_tls.h"
27 #include "utils/s2n_safety.h"
28
29 struct event_subscriber {
30 uint64_t invoked;
31 };
32
33 void subscriber_on_handshake_complete(
34 struct s2n_connection *conn,
35 void *subscriber,
36 struct s2n_event_handshake *event)
37 {
38 struct event_subscriber *sub = (struct event_subscriber *) subscriber;
39 sub->invoked++;
40 }
41
42 int main(int argc, char **argv)
43 {
44 BEGIN_TEST();
45
46 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new_minimal(), s2n_config_ptr_free);
47 struct event_subscriber subscriber = { 0 };
48 s2n_config_set_subscriber(config, &subscriber);
49 s2n_config_set_handshake_event(config, subscriber_on_handshake_complete);
50
51 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
52 s2n_connection_set_config(conn, config);
53
54 /* setup connection fields for s2n_event_handshake_populate */
55 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_chacha20_poly1305_sha256;
56 conn->kex_params.server_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp521r1;
57 conn->actual_protocol_version = S2N_TLS12;
58 EXPECT_OK(s2n_handshake_type_set_flag(conn, FULL_HANDSHAKE));
59
60 /* s2n_event_handshake_populate: group, cipher, and protocol version are complete */
61 {
62 struct s2n_event_handshake event = { 0 };
63 EXPECT_OK(s2n_event_handshake_populate(conn, &event));
64 EXPECT_EQUAL(strcmp(event.cipher, "ECDHE-ECDSA-CHACHA20-POLY1305"), 0);
65 EXPECT_EQUAL(strcmp(event.group, "secp521r1"), 0);
66 EXPECT_EQUAL(event.protocol_version, S2N_TLS12);
67 /* we don't expect handshake_populate to touch the time */
68 EXPECT_EQUAL(event.handshake_end_ns, 0);
69 EXPECT_EQUAL(event.handshake_start_ns, 0);
70 EXPECT_EQUAL(event.handshake_time_ns, 0);
71 /* no error on success */
72 EXPECT_EQUAL(event.error_code, 0);
73 };
74
75 /* s2n_event_handshake_send: callback is invoked */
76 {
77 struct s2n_event_handshake event = { 0 };
78 EXPECT_OK(s2n_event_handshake_send(conn, &event));
79 EXPECT_EQUAL(subscriber.invoked, 1);
80 EXPECT_EQUAL(event.handshake_start_ns, HANDSHAKE_EVENT_SENT);
81
82 /* idempotency: calling handshake_send again will not
83 * invoke the callback */
84 EXPECT_OK(s2n_event_handshake_send(conn, &event));
85 EXPECT_EQUAL(subscriber.invoked, 1);
86 }
87
88 END_TEST();
89 }