]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-in-addr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2017 Lennart Poettering
6 #include <netinet/in.h>
8 #include "in-addr-util.h"
10 static void test_in_addr_prefix_from_string(const char *p
, int family
, int ret
, const union in_addr_union
*u
, unsigned char prefixlen
) {
11 union in_addr_union q
;
15 r
= in_addr_prefix_from_string(p
, family
, &q
, &l
);
21 assert_se(in_addr_equal(family
, &q
, u
));
22 assert_se(l
== prefixlen
);
24 r
= in_addr_prefix_from_string_auto(p
, &f
, &q
, &l
);
27 assert_se(f
== family
);
28 assert_se(in_addr_equal(family
, &q
, u
));
29 assert_se(l
== prefixlen
);
33 int main(int argc
, char *argv
[]) {
34 test_in_addr_prefix_from_string("", AF_INET
, -EINVAL
, NULL
, 0);
35 test_in_addr_prefix_from_string("/", AF_INET
, -EINVAL
, NULL
, 0);
36 test_in_addr_prefix_from_string("/8", AF_INET
, -EINVAL
, NULL
, 0);
37 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);
38 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);
39 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);
40 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);
41 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);
42 test_in_addr_prefix_from_string("1.2.3.4/33", AF_INET
, -ERANGE
, NULL
, 0);
43 test_in_addr_prefix_from_string("1.2.3.4/-1", AF_INET
, -ERANGE
, NULL
, 0);
44 test_in_addr_prefix_from_string("::1", AF_INET
, -EINVAL
, NULL
, 0);
46 test_in_addr_prefix_from_string("", AF_INET6
, -EINVAL
, NULL
, 0);
47 test_in_addr_prefix_from_string("/", AF_INET6
, -EINVAL
, NULL
, 0);
48 test_in_addr_prefix_from_string("/8", AF_INET6
, -EINVAL
, NULL
, 0);
49 test_in_addr_prefix_from_string("::1", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 128);
50 test_in_addr_prefix_from_string("::1/0", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 0);
51 test_in_addr_prefix_from_string("::1/1", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 1);
52 test_in_addr_prefix_from_string("::1/2", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 2);
53 test_in_addr_prefix_from_string("::1/32", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 32);
54 test_in_addr_prefix_from_string("::1/33", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 33);
55 test_in_addr_prefix_from_string("::1/64", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 64);
56 test_in_addr_prefix_from_string("::1/128", AF_INET6
, 0, &(union in_addr_union
) { .in6
= IN6ADDR_LOOPBACK_INIT
}, 128);
57 test_in_addr_prefix_from_string("::1/129", AF_INET6
, -ERANGE
, NULL
, 0);
58 test_in_addr_prefix_from_string("::1/-1", AF_INET6
, -ERANGE
, NULL
, 0);