2 #include "../../src/netutl.h"
4 static void test_service_to_port_invalid(void **state) {
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"));
13 static void test_service_to_port_valid(void **state) {
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"));
22 static void test_is_local_connection_ipv4(void **state) {
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));
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));
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));
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));
44 static void test_is_local_connection_ipv6(void **state) {
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));
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));
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));
62 static void test_is_local_connection_unix(void **state) {
65 sockaddr_t sa = {.sa.sa_family = AF_UNIX};
66 assert_true(is_local_connection(&sa));
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),
77 return cmocka_run_group_tests(tests, NULL, NULL);