summaryrefslogtreecommitdiffstats
path: root/test/testipsub.c
blob: 1182d7a476ad04d387ad020325bc65a0fd792090 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "testutil.h"
#include "apr_general.h"
#include "apr_network_io.h"
#include "apr_errno.h"

static void test_bad_input(abts_case *tc, void *data)
{
    struct {
        const char *ipstr;
        const char *mask;
        apr_status_t expected_rv;
    } testcases[] =
    {
        /* so we have a few good inputs in here; sue me */
        {"my.host.name",       NULL,               APR_EINVAL}
        ,{"127.0.0.256",       NULL,               APR_EBADIP}
        ,{"127.0.0.1",         NULL,               APR_SUCCESS}
        ,{"127.0.0.1",         "32",               APR_SUCCESS}
        ,{"127.0.0.1",         "1",                APR_SUCCESS}
        ,{"127.0.0.1",         "15",               APR_SUCCESS}
        ,{"127.0.0.1",         "-1",               APR_EBADMASK}
        ,{"127.0.0.1",         "0",                APR_EBADMASK}
        ,{"127.0.0.1",         "33",               APR_EBADMASK}
        ,{"127.0.0.1",         "255.0.0.0",        APR_SUCCESS}
        ,{"127.0.0.1",         "255.0",            APR_EBADMASK}
        ,{"127.0.0.1",         "255.255.256.0",    APR_EBADMASK}
        ,{"127.0.0.1",         "abc",              APR_EBADMASK}
        ,{"127",               NULL,               APR_SUCCESS}
        ,{"127.0.0.1.2",       NULL,               APR_EBADIP}
        ,{"127.0.0.1.2",       "8",                APR_EBADIP}
        ,{"127",               "255.0.0.0",        APR_EBADIP} /* either EBADIP or EBADMASK seems fine */
        ,{"",                  NULL,               APR_EINVAL}
#if APR_HAVE_IPV6
        ,{"::1",               NULL,               APR_SUCCESS}
        ,{"::1",               "20",               APR_SUCCESS}
        ,{"::ffff:9.67.113.15", NULL,              APR_EBADIP} /* yes, this is goodness */
        ,{"fe80::",            "16",               APR_SUCCESS}
        ,{"fe80::",            "255.0.0.0",        APR_EBADMASK}
        ,{"fe80::1",           "0",                APR_EBADMASK}
        ,{"fe80::1",           "-1",               APR_EBADMASK}
        ,{"fe80::1",           "1",                APR_SUCCESS}
        ,{"fe80::1",           "33",               APR_SUCCESS}
        ,{"fe80::1",           "128",              APR_SUCCESS}
        ,{"fe80::1",           "129",              APR_EBADMASK}
#else
        /* do some IPv6 stuff and verify that it fails with APR_EBADIP */
        ,{"::ffff:9.67.113.15", NULL,              APR_EBADIP}
#endif
    };
    int i;
    apr_ipsubnet_t *ipsub;
    apr_status_t rv;

    for (i = 0; i < (sizeof testcases / sizeof testcases[0]); i++) {
        rv = apr_ipsubnet_create(&ipsub, testcases[i].ipstr, testcases[i].mask, p);
        ABTS_INT_EQUAL(tc, testcases[i].expected_rv, rv);
    }
}

static void test_singleton_subnets(abts_case *tc, void *data)
{
    const char *v4addrs[] = {
        "127.0.0.1", "129.42.18.99", "63.161.155.20", "207.46.230.229", "64.208.42.36",
        "198.144.203.195", "192.18.97.241", "198.137.240.91", "62.156.179.119", 
        "204.177.92.181"
    };
    apr_ipsubnet_t *ipsub;
    apr_sockaddr_t *sa;
    apr_status_t rv;
    int i, j, rc;

    for (i = 0; i < sizeof v4addrs / sizeof v4addrs[0]; i++) {
        rv = apr_ipsubnet_create(&ipsub, v4addrs[i], NULL, p);
        ABTS_TRUE(tc, rv == APR_SUCCESS);
        for (j = 0; j < sizeof v4addrs / sizeof v4addrs[0]; j++) {
            rv = apr_sockaddr_info_get(&sa, v4addrs[j], APR_INET, 0, 0, p);
            ABTS_TRUE(tc, rv == APR_SUCCESS);
            rc = apr_ipsubnet_test(ipsub, sa);
            if (!strcmp(v4addrs[i], v4addrs[j])) {
                ABTS_TRUE(tc, rc != 0);
            }
            else {
                ABTS_TRUE(tc, rc == 0);
            }
        }
    }

    /* same for v6? */
}

static void test_interesting_subnets(abts_case *tc, void *data)
{
    struct {
        const char *ipstr, *mask;
        int family;
        char *in_subnet, *not_in_subnet;
    } testcases[] =
    {
         {"9.67",             NULL,            APR_INET,  "9.67.113.15",         "10.1.2.3"}
        ,{"9.67.0.0",         "16",            APR_INET,  "9.67.113.15",         "10.1.2.3"}
        ,{"9.67.0.0",         "255.255.0.0",   APR_INET,  "9.67.113.15",         "10.1.2.3"}
        ,{"9.67.113.99",      "16",            APR_INET,  "9.67.113.15",         "10.1.2.3"}
        ,{"9.67.113.99",      "255.255.255.0", APR_INET,  "9.67.113.15",         "10.1.2.3"}
        ,{"127",              NULL,            APR_INET,  "127.0.0.1",           "10.1.2.3"}
        ,{"127.0.0.1",        "8",             APR_INET,  "127.0.0.1",           "10.1.2.3"}
#if APR_HAVE_IPV6
        ,{"38.0.0.0",         "8",             APR_INET6, "::ffff:38.1.1.1",     "2600::1"} /* PR 54047 */
        ,{"fe80::",           "8",             APR_INET6, "fe80::1",             "ff01::1"}
        ,{"ff01::",           "8",             APR_INET6, "ff01::1",             "fe80::1"}
        ,{"3FFE:8160::",      "28",            APR_INET6, "3ffE:816e:abcd:1234::1", "3ffe:8170::1"}
        ,{"127.0.0.1",        NULL,            APR_INET6, "::ffff:127.0.0.1",    "fe80::1"}
        ,{"127.0.0.1",        "8",             APR_INET6, "::ffff:127.0.0.1",    "fe80::1"}
#endif
    };
    apr_ipsubnet_t *ipsub;
    apr_sockaddr_t *sa;
    apr_status_t rv;
    int i, rc;

    for (i = 0; i < sizeof testcases / sizeof testcases[0]; i++) {
        rv = apr_ipsubnet_create(&ipsub, testcases[i].ipstr, testcases[i].mask, p);
        ABTS_TRUE(tc, rv == APR_SUCCESS);
        rv = apr_sockaddr_info_get(&sa, testcases[i].in_subnet, testcases[i].family, 0, 0, p);
        ABTS_TRUE(tc, rv == APR_SUCCESS);
        ABTS_TRUE(tc, sa != NULL);
        if (!sa) continue;
        rc = apr_ipsubnet_test(ipsub, sa);
        ABTS_TRUE(tc, rc != 0);
        rv = apr_sockaddr_info_get(&sa, testcases[i].not_in_subnet, testcases[i].family, 0, 0, p);
        ABTS_TRUE(tc, rv == APR_SUCCESS);
        rc = apr_ipsubnet_test(ipsub, sa);
        ABTS_TRUE(tc, rc == 0);
    }
}

static void test_badmask_str(abts_case *tc, void *data)
{
    char buf[128];

    ABTS_STR_EQUAL(tc, apr_strerror(APR_EBADMASK, buf, sizeof buf),
                      "The specified network mask is invalid.");
}

static void test_badip_str(abts_case *tc, void *data)
{
    char buf[128];

    ABTS_STR_EQUAL(tc, apr_strerror(APR_EBADIP, buf, sizeof buf),
                      "The specified IP address is invalid.");
}

static void test_parse_addr_port(abts_case *tc, void *data)
{
    const struct {
        const char *input;
        apr_status_t rv;
        const char *addr, *scope_id;
        apr_port_t port;
    } *test, testcases[] = {
        /* Success cases */
        { "localhost:80", APR_SUCCESS, "localhost", NULL, 80 }
        ,{ "localhost", APR_SUCCESS, "localhost", NULL, 0 }
        ,{ "www.example.com:8080", APR_SUCCESS, "www.example.com", NULL, 8080 }
        ,{ "w:1", APR_SUCCESS, "w", NULL, 1 }
        ,{ "127.0.0.1:80", APR_SUCCESS, "127.0.0.1", NULL, 80 }
        ,{ "8080", APR_SUCCESS, NULL, NULL, 8080 } /* API doc has this case */
#if APR_HAVE_IPV6
        ,{ "[::]:80", APR_SUCCESS, "::", NULL, 80 }
        ,{ "[::%eth0]:80", APR_SUCCESS, "::", "eth0", 80 }
        ,{ "[::%eth0]", APR_SUCCESS, "::", "eth0", 0 }
#endif

        /* Failure cases */
        ,{ "localhost:999999", APR_EINVAL, NULL, NULL, 0 }
        ,{ "localhost:0", APR_EINVAL, NULL, NULL, 0 }
#if APR_HAVE_IPV6
        ,{ "[abc]", APR_EINVAL, NULL, NULL, 0 }
        ,{ "[::]z:80", APR_EINVAL, NULL, NULL, 0 }
        ,{ "[:::80", APR_EINVAL, NULL, NULL, 0 }
        ,{ "[zzzz]:80", APR_EINVAL, NULL, NULL, 0 }
        ,{ "[::%]:80", APR_EINVAL, NULL, NULL, 0 }
#endif
/*        ,{ "127.0.0.1:80x", APR_EINVAL, NULL, NULL, 0 }  <- should fail, doesn't  */
/*        ,{ "127.0.0.1x:80", APR_EINVAL, NULL, NULL, 0 }  <- maybe should fail?, doesn't  */
/*        ,{ "localhost:-1", APR_EINVAL, NULL, NULL, 0 }   <- should fail, doesn't */
    };
    unsigned i;
        
    for (i = 0; i < (sizeof testcases / sizeof testcases[0]); i++) {
        char *addr, *scope_id;
        apr_port_t port;
        apr_status_t rv;

        test = &testcases[i];
        
        rv = apr_parse_addr_port(&addr, &scope_id, &port, test->input, p);
        ABTS_INT_EQUAL(tc, test->rv, rv);

        if (test->rv != APR_SUCCESS) continue;

        APR_ASSERT_SUCCESS(tc, "parse address", test->rv);

        ABTS_STR_EQUAL(tc, test->addr, addr);
        ABTS_STR_EQUAL(tc, test->scope_id, scope_id);
        ABTS_INT_EQUAL(tc, test->port, port);
    }
}

abts_suite *testipsub(abts_suite *suite)
{
    suite = ADD_SUITE(suite)

    abts_run_test(suite, test_bad_input, NULL);
    abts_run_test(suite, test_singleton_subnets, NULL);
    abts_run_test(suite, test_interesting_subnets, NULL);
    abts_run_test(suite, test_badmask_str, NULL);
    abts_run_test(suite, test_badip_str, NULL);
    abts_run_test(suite, test_parse_addr_port, NULL);
    return suite;
}