Debian Codegraph @

crt/s2n/tests/unit/s2n_rfc5952_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 #ifdef _WIN32
17 #define WIN32_LEAN_AND_MEAN
18 #include <ws2tcpip.h>
19 #else
20 #include <arpa/inet.h>
21 #endif
22
23 #include "s2n_test.h"
24
25 #if defined(__FreeBSD__) || defined(__OpenBSD__)
26 #include <netinet/in.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #endif
30
31 #include "utils/s2n_rfc5952.h"
32
33 int main(int argc, char **argv)
34 {
35 BEGIN_TEST();
36 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
37
38 uint8_t ipv4[4];
39 uint8_t ipv6[16];
40
41 uint8_t ipv4_buf[sizeof("255.255.255.255")];
42 uint8_t ipv6_buf[sizeof("1111:2222:3333:4444:5555:6666:7777:8888")];
43
44 struct s2n_blob ipv4_blob = { 0 };
45 EXPECT_SUCCESS(s2n_blob_init(&ipv4_blob, ipv4_buf, sizeof(ipv4_buf)));
46 struct s2n_blob ipv6_blob = { 0 };
47 EXPECT_SUCCESS(s2n_blob_init(&ipv6_blob, ipv6_buf, sizeof(ipv6_buf)));
48
49 EXPECT_SUCCESS(inet_pton(AF_INET, "111.222.111.111", ipv4));
50 EXPECT_OK(s2n_inet_ntop(AF_INET, ipv4, &ipv4_blob));
51 EXPECT_EQUAL(strcmp("111.222.111.111", (char *) ipv4_buf), 0);
52
53 EXPECT_SUCCESS(inet_pton(AF_INET, "0.0.0.0", ipv4));
54 EXPECT_OK(s2n_inet_ntop(AF_INET, ipv4, &ipv4_blob));
55 EXPECT_EQUAL(strcmp("0.0.0.0", (char *) ipv4_buf), 0);
56
57 EXPECT_SUCCESS(inet_pton(AF_INET, "100.104.123.1", ipv4));
58 EXPECT_OK(s2n_inet_ntop(AF_INET, ipv4, &ipv4_blob));
59 EXPECT_EQUAL(strcmp("100.104.123.1", (char *) ipv4_buf), 0);
60
61 EXPECT_SUCCESS(inet_pton(AF_INET, "255.255.255.255", ipv4));
62 EXPECT_OK(s2n_inet_ntop(AF_INET, ipv4, &ipv4_blob));
63 EXPECT_EQUAL(strcmp("255.255.255.255", (char *) ipv4_buf), 0);
64
65 EXPECT_SUCCESS(inet_pton(AF_INET6, "2001:db8:0:0:0:0:2:1", ipv6));
66 EXPECT_OK(s2n_inet_ntop(AF_INET6, ipv6, &ipv6_blob));
67 EXPECT_EQUAL(strcmp("2001:db8::2:1", (char *) ipv6_buf), 0);
68
69 EXPECT_SUCCESS(inet_pton(AF_INET6, "2001:db8::1", ipv6));
70 EXPECT_OK(s2n_inet_ntop(AF_INET6, ipv6, &ipv6_blob));
71 EXPECT_EQUAL(strcmp("2001:db8::1", (char *) ipv6_buf), 0);
72
73 EXPECT_SUCCESS(inet_pton(AF_INET6, "2001:db8:0:1:1:1:1:1", ipv6));
74 EXPECT_OK(s2n_inet_ntop(AF_INET6, ipv6, &ipv6_blob));
75 EXPECT_EQUAL(strcmp("2001:db8:0:1:1:1:1:1", (char *) ipv6_buf), 0);
76
77 EXPECT_SUCCESS(inet_pton(AF_INET6, "2001:db8::1:0:0:1", ipv6));
78 EXPECT_OK(s2n_inet_ntop(AF_INET6, ipv6, &ipv6_blob));
79 EXPECT_EQUAL(strcmp("2001:db8::1:0:0:1", (char *) ipv6_buf), 0);
80
81 EXPECT_SUCCESS(inet_pton(AF_INET6, "0:0:0:0:0:0:0:1", ipv6));
82 EXPECT_OK(s2n_inet_ntop(AF_INET6, ipv6, &ipv6_blob));
83 EXPECT_EQUAL(strcmp("::1", (char *) ipv6_buf), 0);
84
85 EXPECT_SUCCESS(inet_pton(AF_INET6, "0:0:0:0:0:0:0:0", ipv6));
86 EXPECT_OK(s2n_inet_ntop(AF_INET6, ipv6, &ipv6_blob));
87 EXPECT_EQUAL(strcmp("::", (char *) ipv6_buf), 0);
88
89 /* Prevents build failure on Mac */
90 #ifndef AF_BLUETOOTH
91 #define AF_BLUETOOTH 31
92 #endif
93
94 EXPECT_ERROR_WITH_ERRNO(s2n_inet_ntop(AF_BLUETOOTH, ipv6, &ipv6_blob), S2N_ERR_INVALID_ARGUMENT);
95 END_TEST();
96 }