]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f4912f3a LP |
2 | /*** |
3 | This file is part of systemd | |
4 | ||
5 | Copyright 2017 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <netinet/in.h> | |
22 | ||
23 | #include "in-addr-util.h" | |
24 | ||
25 | static void test_in_addr_prefix_from_string(const char *p, int family, int ret, const union in_addr_union *u, unsigned char prefixlen) { | |
26 | union in_addr_union q; | |
27 | unsigned char l; | |
28 | int r; | |
29 | ||
30 | r = in_addr_prefix_from_string(p, family, &q, &l); | |
31 | assert_se(r == ret); | |
32 | ||
33 | if (r >= 0) { | |
34 | int f; | |
35 | ||
36 | assert_se(in_addr_equal(family, &q, u)); | |
37 | assert_se(l == prefixlen); | |
38 | ||
39 | r = in_addr_prefix_from_string_auto(p, &f, &q, &l); | |
40 | assert_se(r >= 0); | |
41 | ||
42 | assert_se(f == family); | |
43 | assert_se(in_addr_equal(family, &q, u)); | |
44 | assert_se(l == prefixlen); | |
45 | } | |
46 | } | |
47 | ||
48 | int main(int argc, char *argv[]) { | |
49 | test_in_addr_prefix_from_string("", AF_INET, -EINVAL, NULL, 0); | |
50 | test_in_addr_prefix_from_string("/", AF_INET, -EINVAL, NULL, 0); | |
51 | test_in_addr_prefix_from_string("/8", AF_INET, -EINVAL, NULL, 0); | |
52 | test_in_addr_prefix_from_string("1.2.3.4", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32); | |
53 | test_in_addr_prefix_from_string("1.2.3.4/0", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 0); | |
54 | test_in_addr_prefix_from_string("1.2.3.4/1", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 1); | |
55 | test_in_addr_prefix_from_string("1.2.3.4/2", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 2); | |
56 | test_in_addr_prefix_from_string("1.2.3.4/32", AF_INET, 0, &(union in_addr_union) { .in = (struct in_addr) { .s_addr = htobe32(0x01020304) } }, 32); | |
57 | test_in_addr_prefix_from_string("1.2.3.4/33", AF_INET, -ERANGE, NULL, 0); | |
58 | test_in_addr_prefix_from_string("1.2.3.4/-1", AF_INET, -ERANGE, NULL, 0); | |
59 | test_in_addr_prefix_from_string("::1", AF_INET, -EINVAL, NULL, 0); | |
60 | ||
61 | test_in_addr_prefix_from_string("", AF_INET6, -EINVAL, NULL, 0); | |
62 | test_in_addr_prefix_from_string("/", AF_INET6, -EINVAL, NULL, 0); | |
63 | test_in_addr_prefix_from_string("/8", AF_INET6, -EINVAL, NULL, 0); | |
64 | test_in_addr_prefix_from_string("::1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128); | |
65 | test_in_addr_prefix_from_string("::1/0", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 0); | |
66 | test_in_addr_prefix_from_string("::1/1", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 1); | |
67 | test_in_addr_prefix_from_string("::1/2", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 2); | |
68 | test_in_addr_prefix_from_string("::1/32", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 32); | |
69 | test_in_addr_prefix_from_string("::1/33", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 33); | |
70 | test_in_addr_prefix_from_string("::1/64", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 64); | |
71 | test_in_addr_prefix_from_string("::1/128", AF_INET6, 0, &(union in_addr_union) { .in6 = IN6ADDR_LOOPBACK_INIT }, 128); | |
72 | test_in_addr_prefix_from_string("::1/129", AF_INET6, -ERANGE, NULL, 0); | |
73 | test_in_addr_prefix_from_string("::1/-1", AF_INET6, -ERANGE, NULL, 0); | |
74 | ||
75 | return 0; | |
76 | } |