Debian Codegraph @

crt/s2n/tests/unit/s2n_error_lookup_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 "api/s2n.h"
17 #include "error/s2n_errno.h"
18 #include "s2n_test.h"
19 #include "testlib/s2n_testlib.h"
20 #include "utils/s2n_safety.h"
21
22 /* constructs a debug string from a path */
23 #define EXAMPLE_DEBUG_STR(path) (_S2N_EXTRACT_BASENAME(_S2N_DEBUG_LINE_PREFIX path))
24
25 S2N_RESULT test_function(bool is_valid)
26 {
27 /* the line number of this check is important; tests below will fail if it changed */
28 RESULT_ENSURE(is_valid, S2N_ERR_SAFETY);
29 return S2N_RESULT_OK;
30 }
31
32 int main(void)
33 {
34 BEGIN_TEST();
35 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
36
37 /* First, test that we can find error message for all defined errors */
38 for (int err = S2N_ERR_T_OK_START; err < S2N_ERR_T_OK_END; err++) {
39 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
40 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
41 }
42 for (int err = S2N_ERR_T_IO_START; err < S2N_ERR_T_IO_END; err++) {
43 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
44 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
45 }
46 for (int err = S2N_ERR_T_CLOSED_START; err < S2N_ERR_T_CLOSED_END; err++) {
47 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
48 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
49 }
50 for (int err = S2N_ERR_T_BLOCKED_START; err < S2N_ERR_T_BLOCKED_END; err++) {
51 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
52 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
53 }
54 for (int err = S2N_ERR_T_ALERT_START; err < S2N_ERR_T_ALERT_END; err++) {
55 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
56 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
57 }
58 for (int err = S2N_ERR_T_PROTO_START; err < S2N_ERR_T_PROTO_END; err++) {
59 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
60 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
61 }
62 for (int err = S2N_ERR_T_INTERNAL_START; err < S2N_ERR_T_INTERNAL_END; err++) {
63 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
64 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
65 }
66 for (int err = S2N_ERR_T_USAGE_START; err < S2N_ERR_T_USAGE_END; err++) {
67 EXPECT_NOT_EQUAL(strcmp(s2n_strerror_name(err), "Internal s2n error"), 0);
68 EXPECT_NOT_EQUAL(strcmp(s2n_strerror(err, "EN"), "Internal s2n error"), 0);
69 }
70
71 /* Next, test that we get an error wen try to search for non-existing errors for each type */
72 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_OK_END), "Internal s2n error"), 0);
73 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_OK_END, "EN"), "Internal s2n error"), 0);
74
75 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_IO_END), "Internal s2n error"), 0);
76 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_IO_END, "EN"), "Internal s2n error"), 0);
77
78 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_CLOSED_END), "Internal s2n error"), 0);
79 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_CLOSED_END, "EN"), "Internal s2n error"), 0);
80
81 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_BLOCKED_END), "Internal s2n error"), 0);
82 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_BLOCKED_END, "EN"), "Internal s2n error"), 0);
83
84 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_ALERT_END), "Internal s2n error"), 0);
85 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_ALERT_END, "EN"), "Internal s2n error"), 0);
86
87 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_PROTO_END), "Internal s2n error"), 0);
88 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_PROTO_END, "EN"), "Internal s2n error"), 0);
89
90 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_INTERNAL_END), "Internal s2n error"), 0);
91 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_INTERNAL_END, "EN"), "Internal s2n error"), 0);
92
93 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_T_USAGE_END), "Internal s2n error"), 0);
94 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_T_USAGE_END, "EN"), "Internal s2n error"), 0);
95
96 /* And ensure that we get an error in non-existing classes of errors */
97 EXPECT_EQUAL(strcmp(s2n_strerror_name((S2N_ERR_T_USAGE + 1) << S2N_ERR_NUM_VALUE_BITS), "Internal s2n error"), 0);
98 EXPECT_EQUAL(strcmp(s2n_strerror((S2N_ERR_T_USAGE + 1) << S2N_ERR_NUM_VALUE_BITS, "EN"), "Internal s2n error"), 0);
99
100 /* Ensure the file/line information is returned as expected */
101 s2n_result_ignore(test_function(false));
102 EXPECT_EQUAL(strcmp(s2n_strerror_source(S2N_ERR_SAFETY), "s2n_error_lookup_test.c:28"), 0);
103
104 EXPECT_EQUAL(strcmp(EXAMPLE_DEBUG_STR("/absolute/path/to/file.c"), "file.c"), 0);
105 EXPECT_EQUAL(strcmp(EXAMPLE_DEBUG_STR("relative/path/to/file.c"), "file.c"), 0);
106 EXPECT_EQUAL(strcmp(EXAMPLE_DEBUG_STR("path / with / spaces /file.c"), "file.c"), 0);
107 EXPECT_EQUAL(strcmp(EXAMPLE_DEBUG_STR("file.c"), "file.c"), 0);
108
109 /* Test that lookup works even after s2n_cleanup */
110 EXPECT_SUCCESS(s2n_cleanup());
111
112 EXPECT_EQUAL(strcmp(s2n_strerror_name(S2N_ERR_OK), "S2N_ERR_OK"), 0);
113 EXPECT_EQUAL(strcmp(s2n_strerror(S2N_ERR_OK, "EN"), "no error"), 0);
114
115 END_TEST();
116 }