]> code.delx.au - pulseaudio/blob - src/tests/ipacl-test.c
74bafd75470b938317eed523cd39ed17d637045a
[pulseaudio] / 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/socket.h>
21 #include <pulsecore/macro.h>
22 #include <pulsecore/ipacl.h>
23 #include <pulsecore/arpa-inet.h>
24
25 static void do_ip_acl_check(const char *s, int fd, int expected) {
26 pa_ip_acl *acl;
27 int result;
28
29 pa_assert_se(acl = pa_ip_acl_new(s));
30 result = pa_ip_acl_check(acl, fd);
31 pa_ip_acl_free(acl);
32
33 printf("%-20s result=%u (should be %u)\n", s, result, expected);
34 pa_assert(result == expected);
35 }
36
37 int main(int argc, char *argv[]) {
38 struct sockaddr_in sa;
39 #ifdef HAVE_IPV6
40 struct sockaddr_in6 sa6;
41 #endif
42 int fd;
43 int r;
44
45 fd = socket(PF_INET, SOCK_STREAM, 0);
46 pa_assert(fd >= 0);
47
48 sa.sin_family = AF_INET;
49 sa.sin_port = htons(22);
50 sa.sin_addr.s_addr = inet_addr("127.0.0.1");
51
52 r = connect(fd, (struct sockaddr*) &sa, sizeof(sa));
53 pa_assert(r >= 0);
54
55 do_ip_acl_check("127.0.0.1", fd, 1);
56 do_ip_acl_check("127.0.0.2/0", fd, 1);
57 do_ip_acl_check("127.0.0.1/32", fd, 1);
58 do_ip_acl_check("127.0.0.1/7", fd, 1);
59 do_ip_acl_check("127.0.0.2", fd, 0);
60 do_ip_acl_check("127.0.0.0/8;0.0.0.0/32", fd, 1);
61 do_ip_acl_check("128.0.0.2/9", fd, 0);
62 do_ip_acl_check("::1/9", fd, 0);
63
64 close(fd);
65
66 #ifdef HAVE_IPV6
67 if ( (fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0 ) {
68 printf("Unable to open IPv6 socket, IPv6 tests ignored");
69 return 0;
70 }
71
72 memset(&sa6, 0, sizeof(sa6));
73 sa6.sin6_family = AF_INET6;
74 sa6.sin6_port = htons(22);
75 pa_assert_se(inet_pton(AF_INET6, "::1", &sa6.sin6_addr) == 1);
76
77 r = connect(fd, (struct sockaddr*) &sa6, sizeof(sa6));
78 pa_assert(r >= 0);
79
80 do_ip_acl_check("::1", fd, 1);
81 do_ip_acl_check("::1/9", fd, 1);
82 do_ip_acl_check("::/0", fd, 1);
83 do_ip_acl_check("::2/128", fd, 0);
84 do_ip_acl_check("::2/127", fd, 0);
85 do_ip_acl_check("::2/126", fd, 1);
86
87 close(fd);
88 #endif
89
90 return 0;
91 }