bluetooth: Remove commented out code.
[profile/ivi/pulseaudio-panda.git] / src / tests / ipacl-test.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <string.h>
9
10 #ifdef HAVE_NETINET_IN_H
11 #include <netinet/in.h>
12 #endif
13 #ifdef HAVE_NETINET_IN_SYSTM_H
14 #include <netinet/in_systm.h>
15 #endif
16 #ifdef HAVE_NETINET_IP_H
17 #include <netinet/ip.h>
18 #endif
19
20 #include <pulsecore/log.h>
21 #include <pulsecore/macro.h>
22 #include <pulsecore/socket.h>
23 #include <pulsecore/ipacl.h>
24 #include <pulsecore/arpa-inet.h>
25
26 static void do_ip_acl_check(const char *s, int fd, int expected) {
27     pa_ip_acl *acl;
28     int result;
29
30     pa_assert_se(acl = pa_ip_acl_new(s));
31     result = pa_ip_acl_check(acl, fd);
32     pa_ip_acl_free(acl);
33
34     pa_log_info("%-20s result=%u (should be %u)", s, result, expected);
35     pa_assert(result == expected);
36 }
37
38 int main(int argc, char *argv[]) {
39     struct sockaddr_in sa;
40 #ifdef HAVE_IPV6
41     struct sockaddr_in6 sa6;
42 #endif
43     int fd;
44     int r;
45
46     if (!getenv("MAKE_CHECK"))
47         pa_log_set_level(PA_LOG_DEBUG);
48
49     fd = socket(PF_INET, SOCK_STREAM, 0);
50     pa_assert(fd >= 0);
51
52     sa.sin_family = AF_INET;
53     sa.sin_port = htons(22);
54     sa.sin_addr.s_addr = inet_addr("127.0.0.1");
55
56     r = connect(fd, (struct sockaddr*) &sa, sizeof(sa));
57     pa_assert(r >= 0);
58
59     do_ip_acl_check("127.0.0.1", fd, 1);
60     do_ip_acl_check("127.0.0.2/0", fd, 1);
61     do_ip_acl_check("127.0.0.1/32", fd, 1);
62     do_ip_acl_check("127.0.0.1/7", fd, 1);
63     do_ip_acl_check("127.0.0.2", fd, 0);
64     do_ip_acl_check("127.0.0.0/8;0.0.0.0/32", fd, 1);
65     do_ip_acl_check("128.0.0.2/9", fd, 0);
66     do_ip_acl_check("::1/9", fd, 0);
67
68     close(fd);
69
70 #ifdef HAVE_IPV6
71     if ( (fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0 ) {
72       pa_log_error("Unable to open IPv6 socket, IPv6 tests ignored");
73       return 0;
74     }
75
76     memset(&sa6, 0, sizeof(sa6));
77     sa6.sin6_family = AF_INET6;
78     sa6.sin6_port = htons(22);
79     pa_assert_se(inet_pton(AF_INET6, "::1", &sa6.sin6_addr) == 1);
80
81     r = connect(fd, (struct sockaddr*) &sa6, sizeof(sa6));
82     pa_assert(r >= 0);
83
84     do_ip_acl_check("::1", fd, 1);
85     do_ip_acl_check("::1/9", fd, 1);
86     do_ip_acl_check("::/0", fd, 1);
87     do_ip_acl_check("::2/128", fd, 0);
88     do_ip_acl_check("::2/127", fd, 0);
89     do_ip_acl_check("::2/126", fd, 1);
90
91     close(fd);
92 #endif
93
94     return 0;
95 }