"Fossies" - the Fresh Open Source Software Archive

Member "apr-1.7.0/test/testipsub.c" (19 Mar 2019, 9727 Bytes) of package /linux/www/apr-1.7.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "testipsub.c": 1.6.5_vs_1.7.0.

    1 /* Licensed to the Apache Software Foundation (ASF) under one or more
    2  * contributor license agreements.  See the NOTICE file distributed with
    3  * this work for additional information regarding copyright ownership.
    4  * The ASF licenses this file to You under the Apache License, Version 2.0
    5  * (the "License"); you may not use this file except in compliance with
    6  * the License.  You may obtain a copy of the License at
    7  *
    8  *     http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 #include "testutil.h"
   18 #include "apr_general.h"
   19 #include "apr_network_io.h"
   20 #include "apr_errno.h"
   21 
   22 static void test_bad_input(abts_case *tc, void *data)
   23 {
   24     struct {
   25         const char *ipstr;
   26         const char *mask;
   27         apr_status_t expected_rv;
   28     } testcases[] =
   29     {
   30         /* so we have a few good inputs in here; sue me */
   31         {"my.host.name",       NULL,               APR_EINVAL}
   32         ,{"127.0.0.256",       NULL,               APR_EBADIP}
   33         ,{"127.0.0.1",         NULL,               APR_SUCCESS}
   34         ,{"127.0.0.1",         "32",               APR_SUCCESS}
   35         ,{"127.0.0.1",         "1",                APR_SUCCESS}
   36         ,{"127.0.0.1",         "15",               APR_SUCCESS}
   37         ,{"127.0.0.1",         "-1",               APR_EBADMASK}
   38         ,{"127.0.0.1",         "0",                APR_EBADMASK}
   39         ,{"127.0.0.1",         "33",               APR_EBADMASK}
   40         ,{"127.0.0.1",         "255.0.0.0",        APR_SUCCESS}
   41         ,{"127.0.0.1",         "255.0",            APR_EBADMASK}
   42         ,{"127.0.0.1",         "255.255.256.0",    APR_EBADMASK}
   43         ,{"127.0.0.1",         "abc",              APR_EBADMASK}
   44         ,{"127",               NULL,               APR_SUCCESS}
   45         ,{"127.0.0.1.2",       NULL,               APR_EBADIP}
   46         ,{"127.0.0.1.2",       "8",                APR_EBADIP}
   47         ,{"127",               "255.0.0.0",        APR_EBADIP} /* either EBADIP or EBADMASK seems fine */
   48         ,{"",                  NULL,               APR_EINVAL}
   49 #if APR_HAVE_IPV6
   50         ,{"::1",               NULL,               APR_SUCCESS}
   51         ,{"::1",               "20",               APR_SUCCESS}
   52         ,{"::ffff:9.67.113.15", NULL,              APR_EBADIP} /* yes, this is goodness */
   53         ,{"fe80::",            "16",               APR_SUCCESS}
   54         ,{"fe80::",            "255.0.0.0",        APR_EBADMASK}
   55         ,{"fe80::1",           "0",                APR_EBADMASK}
   56         ,{"fe80::1",           "-1",               APR_EBADMASK}
   57         ,{"fe80::1",           "1",                APR_SUCCESS}
   58         ,{"fe80::1",           "33",               APR_SUCCESS}
   59         ,{"fe80::1",           "128",              APR_SUCCESS}
   60         ,{"fe80::1",           "129",              APR_EBADMASK}
   61 #else
   62         /* do some IPv6 stuff and verify that it fails with APR_EBADIP */
   63         ,{"::ffff:9.67.113.15", NULL,              APR_EBADIP}
   64 #endif
   65     };
   66     int i;
   67     apr_ipsubnet_t *ipsub;
   68     apr_status_t rv;
   69 
   70     for (i = 0; i < (sizeof testcases / sizeof testcases[0]); i++) {
   71         rv = apr_ipsubnet_create(&ipsub, testcases[i].ipstr, testcases[i].mask, p);
   72         ABTS_INT_EQUAL(tc, testcases[i].expected_rv, rv);
   73     }
   74 }
   75 
   76 static void test_singleton_subnets(abts_case *tc, void *data)
   77 {
   78     const char *v4addrs[] = {
   79         "127.0.0.1", "129.42.18.99", "63.161.155.20", "207.46.230.229", "64.208.42.36",
   80         "198.144.203.195", "192.18.97.241", "198.137.240.91", "62.156.179.119", 
   81         "204.177.92.181"
   82     };
   83     apr_ipsubnet_t *ipsub;
   84     apr_sockaddr_t *sa;
   85     apr_status_t rv;
   86     int i, j, rc;
   87 
   88     for (i = 0; i < sizeof v4addrs / sizeof v4addrs[0]; i++) {
   89         rv = apr_ipsubnet_create(&ipsub, v4addrs[i], NULL, p);
   90         ABTS_TRUE(tc, rv == APR_SUCCESS);
   91         for (j = 0; j < sizeof v4addrs / sizeof v4addrs[0]; j++) {
   92             rv = apr_sockaddr_info_get(&sa, v4addrs[j], APR_INET, 0, 0, p);
   93             ABTS_TRUE(tc, rv == APR_SUCCESS);
   94             rc = apr_ipsubnet_test(ipsub, sa);
   95             if (!strcmp(v4addrs[i], v4addrs[j])) {
   96                 ABTS_TRUE(tc, rc != 0);
   97             }
   98             else {
   99                 ABTS_TRUE(tc, rc == 0);
  100             }
  101         }
  102     }
  103 
  104     /* same for v6? */
  105 }
  106 
  107 static void test_interesting_subnets(abts_case *tc, void *data)
  108 {
  109     struct {
  110         const char *ipstr, *mask;
  111         int family;
  112         char *in_subnet, *not_in_subnet;
  113     } testcases[] =
  114     {
  115          {"9.67",             NULL,            APR_INET,  "9.67.113.15",         "10.1.2.3"}
  116         ,{"9.67.0.0",         "16",            APR_INET,  "9.67.113.15",         "10.1.2.3"}
  117         ,{"9.67.0.0",         "255.255.0.0",   APR_INET,  "9.67.113.15",         "10.1.2.3"}
  118         ,{"9.67.113.99",      "16",            APR_INET,  "9.67.113.15",         "10.1.2.3"}
  119         ,{"9.67.113.99",      "255.255.255.0", APR_INET,  "9.67.113.15",         "10.1.2.3"}
  120         ,{"127",              NULL,            APR_INET,  "127.0.0.1",           "10.1.2.3"}
  121         ,{"127.0.0.1",        "8",             APR_INET,  "127.0.0.1",           "10.1.2.3"}
  122 #if APR_HAVE_IPV6
  123         ,{"38.0.0.0",         "8",             APR_INET6, "::ffff:38.1.1.1",     "2600::1"} /* PR 54047 */
  124         ,{"fe80::",           "8",             APR_INET6, "fe80::1",             "ff01::1"}
  125         ,{"ff01::",           "8",             APR_INET6, "ff01::1",             "fe80::1"}
  126         ,{"3FFE:8160::",      "28",            APR_INET6, "3ffE:816e:abcd:1234::1", "3ffe:8170::1"}
  127         ,{"127.0.0.1",        NULL,            APR_INET6, "::ffff:127.0.0.1",    "fe80::1"}
  128         ,{"127.0.0.1",        "8",             APR_INET6, "::ffff:127.0.0.1",    "fe80::1"}
  129 #endif
  130     };
  131     apr_ipsubnet_t *ipsub;
  132     apr_sockaddr_t *sa;
  133     apr_status_t rv;
  134     int i, rc;
  135 
  136     for (i = 0; i < sizeof testcases / sizeof testcases[0]; i++) {
  137         rv = apr_ipsubnet_create(&ipsub, testcases[i].ipstr, testcases[i].mask, p);
  138         ABTS_TRUE(tc, rv == APR_SUCCESS);
  139         rv = apr_sockaddr_info_get(&sa, testcases[i].in_subnet, testcases[i].family, 0, 0, p);
  140         ABTS_TRUE(tc, rv == APR_SUCCESS);
  141         ABTS_TRUE(tc, sa != NULL);
  142         if (!sa) continue;
  143         rc = apr_ipsubnet_test(ipsub, sa);
  144         ABTS_TRUE(tc, rc != 0);
  145         rv = apr_sockaddr_info_get(&sa, testcases[i].not_in_subnet, testcases[i].family, 0, 0, p);
  146         ABTS_TRUE(tc, rv == APR_SUCCESS);
  147         rc = apr_ipsubnet_test(ipsub, sa);
  148         ABTS_TRUE(tc, rc == 0);
  149     }
  150 }
  151 
  152 static void test_badmask_str(abts_case *tc, void *data)
  153 {
  154     char buf[128];
  155 
  156     ABTS_STR_EQUAL(tc, apr_strerror(APR_EBADMASK, buf, sizeof buf),
  157                       "The specified network mask is invalid.");
  158 }
  159 
  160 static void test_badip_str(abts_case *tc, void *data)
  161 {
  162     char buf[128];
  163 
  164     ABTS_STR_EQUAL(tc, apr_strerror(APR_EBADIP, buf, sizeof buf),
  165                       "The specified IP address is invalid.");
  166 }
  167 
  168 static void test_parse_addr_port(abts_case *tc, void *data)
  169 {
  170     const struct {
  171         const char *input;
  172         apr_status_t rv;
  173         const char *addr, *scope_id;
  174         apr_port_t port;
  175     } *test, testcases[] = {
  176         /* Success cases */
  177         { "localhost:80", APR_SUCCESS, "localhost", NULL, 80 }
  178         ,{ "localhost", APR_SUCCESS, "localhost", NULL, 0 }
  179         ,{ "www.example.com:8080", APR_SUCCESS, "www.example.com", NULL, 8080 }
  180         ,{ "w:1", APR_SUCCESS, "w", NULL, 1 }
  181         ,{ "127.0.0.1:80", APR_SUCCESS, "127.0.0.1", NULL, 80 }
  182         ,{ "8080", APR_SUCCESS, NULL, NULL, 8080 } /* API doc has this case */
  183 #if APR_HAVE_IPV6
  184         ,{ "[::]:80", APR_SUCCESS, "::", NULL, 80 }
  185         ,{ "[::%eth0]:80", APR_SUCCESS, "::", "eth0", 80 }
  186         ,{ "[::%eth0]", APR_SUCCESS, "::", "eth0", 0 }
  187 #endif
  188 
  189         /* Failure cases */
  190         ,{ "localhost:999999", APR_EINVAL, NULL, NULL, 0 }
  191         ,{ "localhost:0", APR_EINVAL, NULL, NULL, 0 }
  192 #if APR_HAVE_IPV6
  193         ,{ "[abc]", APR_EINVAL, NULL, NULL, 0 }
  194         ,{ "[::]z:80", APR_EINVAL, NULL, NULL, 0 }
  195         ,{ "[:::80", APR_EINVAL, NULL, NULL, 0 }
  196         ,{ "[zzzz]:80", APR_EINVAL, NULL, NULL, 0 }
  197         ,{ "[::%]:80", APR_EINVAL, NULL, NULL, 0 }
  198 #endif
  199 /*        ,{ "127.0.0.1:80x", APR_EINVAL, NULL, NULL, 0 }  <- should fail, doesn't  */
  200 /*        ,{ "127.0.0.1x:80", APR_EINVAL, NULL, NULL, 0 }  <- maybe should fail?, doesn't  */
  201 /*        ,{ "localhost:-1", APR_EINVAL, NULL, NULL, 0 }   <- should fail, doesn't */
  202     };
  203     unsigned i;
  204         
  205     for (i = 0; i < (sizeof testcases / sizeof testcases[0]); i++) {
  206         char *addr, *scope_id;
  207         apr_port_t port;
  208         apr_status_t rv;
  209 
  210         test = &testcases[i];
  211         
  212         rv = apr_parse_addr_port(&addr, &scope_id, &port, test->input, p);
  213         ABTS_INT_EQUAL(tc, test->rv, rv);
  214 
  215         if (test->rv != APR_SUCCESS) continue;
  216 
  217         APR_ASSERT_SUCCESS(tc, "parse address", test->rv);
  218 
  219         ABTS_STR_EQUAL(tc, test->addr, addr);
  220         ABTS_STR_EQUAL(tc, test->scope_id, scope_id);
  221         ABTS_INT_EQUAL(tc, test->port, port);
  222     }
  223 }
  224 
  225 abts_suite *testipsub(abts_suite *suite)
  226 {
  227     suite = ADD_SUITE(suite)
  228 
  229     abts_run_test(suite, test_bad_input, NULL);
  230     abts_run_test(suite, test_singleton_subnets, NULL);
  231     abts_run_test(suite, test_interesting_subnets, NULL);
  232     abts_run_test(suite, test_badmask_str, NULL);
  233     abts_run_test(suite, test_badip_str, NULL);
  234     abts_run_test(suite, test_parse_addr_port, NULL);
  235     return suite;
  236 }
  237