Add distribution specific package installation instructions
[tinc] / test / unit / test_netutl.c
1 #include "unittest.h"
2 #include "../../src/netutl.h"
3
4 static void test_service_to_port_invalid(void **state) {
5         (void)state;
6
7         assert_int_equal(0, service_to_port(NULL));
8         assert_int_equal(0, service_to_port(""));
9         assert_int_equal(0, service_to_port("-1"));
10         assert_int_equal(0, service_to_port("foobar"));
11 }
12
13 static void test_service_to_port_valid(void **state) {
14         (void)state;
15
16         assert_int_equal(22, service_to_port("ssh"));
17         assert_int_equal(80, service_to_port("http"));
18         assert_int_equal(443, service_to_port("https"));
19         assert_int_equal(1234, service_to_port("1234"));
20 }
21
22 static void test_is_local_connection_ipv4(void **state) {
23         (void)state;
24
25         sockaddr_t sa;
26
27         assert_true(inet_pton(AF_INET, "127.0.0.0", &sa.in.sin_addr));
28         sa.sa.sa_family = AF_INET;
29         assert_true(is_local_connection(&sa));
30
31         assert_true(inet_pton(AF_INET, "127.42.13.5", &sa.in.sin_addr));
32         sa.sa.sa_family = AF_INET;
33         assert_true(is_local_connection(&sa));
34
35         assert_true(inet_pton(AF_INET, "127.255.255.255", &sa.in.sin_addr));
36         sa.sa.sa_family = AF_INET;
37         assert_true(is_local_connection(&sa));
38
39         assert_true(inet_pton(AF_INET, "128.0.0.1", &sa.in.sin_addr));
40         sa.sa.sa_family = AF_INET;
41         assert_false(is_local_connection(&sa));
42 }
43
44 static void test_is_local_connection_ipv6(void **state) {
45         (void)state;
46
47         sockaddr_t sa;
48
49         assert_true(inet_pton(AF_INET6, "::1", &sa.in6.sin6_addr));
50         sa.sa.sa_family = AF_INET6;
51         assert_true(is_local_connection(&sa));
52
53         assert_true(inet_pton(AF_INET6, "::1:1", &sa.in6.sin6_addr));
54         sa.sa.sa_family = AF_INET6;
55         assert_false(is_local_connection(&sa));
56
57         assert_true(inet_pton(AF_INET6, "fe80::", &sa.in6.sin6_addr));
58         sa.sa.sa_family = AF_INET6;
59         assert_false(is_local_connection(&sa));
60 }
61
62 static void test_is_local_connection_unix(void **state) {
63         (void)state;
64
65         sockaddr_t sa = {.sa.sa_family = AF_UNIX};
66         assert_true(is_local_connection(&sa));
67 }
68
69 int main(void) {
70         const struct CMUnitTest tests[] = {
71                 cmocka_unit_test(test_service_to_port_invalid),
72                 cmocka_unit_test(test_service_to_port_valid),
73                 cmocka_unit_test(test_is_local_connection_ipv4),
74                 cmocka_unit_test(test_is_local_connection_ipv6),
75                 cmocka_unit_test(test_is_local_connection_unix),
76         };
77         return cmocka_run_group_tests(tests, NULL, NULL);
78 }