summaryrefslogtreecommitdiff
path: root/src/tests/ipacl-test.c
blob: d1bcb3e3c2679f7ad9102c0375271a91ff3cd134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* $Id$ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#endif
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

#include "../pulsecore/winsock.h"

#include <pulsecore/ipacl.h>

int main(int argc, char *argv[]) {
    struct sockaddr_in sa;
    struct sockaddr_in6 sa6;
    int fd;
    int r;
    pa_ip_acl *acl;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    assert(fd >= 0);

    sa.sin_family = AF_INET;
    sa.sin_port = htons(22);
    sa.sin_addr.s_addr = inet_addr("127.0.0.1");

    r = connect(fd, (struct sockaddr*) &sa, sizeof(sa));
    assert(r >= 0);

    acl = pa_ip_acl_new("127.0.0.1");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("127.0.0.2/0");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("127.0.0.1/32");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("127.0.0.1/7");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("127.0.0.2");
    assert(acl);
    printf("result=%u (should be 0)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("127.0.0.0/8;0.0.0.0/32");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("128.0.0.2/9");
    assert(acl);
    printf("result=%u (should be 0)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("::1/9");
    assert(acl);
    printf("result=%u (should be 0)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    close(fd);

    fd = socket(PF_INET6, SOCK_STREAM, 0);
    assert(fd >= 0);

    memset(&sa6, 0, sizeof(sa6));
    sa6.sin6_family = AF_INET6;
    sa6.sin6_port = htons(22);
    inet_pton(AF_INET6, "::1", &sa6.sin6_addr);

    r = connect(fd, (struct sockaddr*) &sa6, sizeof(sa6));
    assert(r >= 0);

    acl = pa_ip_acl_new("::1");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("::1/9");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("::/0");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("::2/128");
    assert(acl);
    printf("result=%u (should be 0)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("::2/127");
    assert(acl);
    printf("result=%u (should be 0)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    acl = pa_ip_acl_new("::2/126");
    assert(acl);
    printf("result=%u (should be 1)\n", pa_ip_acl_check(acl, fd));
    pa_ip_acl_free(acl);

    close(fd);

    return 0;
}