"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/tests/contrib/test_sockaddr.c" (16 Jul 2019, 9247 Bytes) of package /linux/misc/dns/knot-2.8.3.tar.xz:


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 last Fossies "Diffs" side-by-side code changes report for "test_sockaddr.c": 2.7.6_vs_2.8.0.

    1 /*  Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
    2 
    3     This program is free software: you can redistribute it and/or modify
    4     it under the terms of the GNU General Public License as published by
    5     the Free Software Foundation, either version 3 of the License, or
    6     (at your option) any later version.
    7 
    8     This program is distributed in the hope that it will be useful,
    9     but WITHOUT ANY WARRANTY; without even the implied warranty of
   10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11     GNU General Public License for more details.
   12 
   13     You should have received a copy of the GNU General Public License
   14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15  */
   16 
   17 #include <tap/basic.h>
   18 
   19 #include "contrib/sockaddr.h"
   20 #include "libknot/errcode.h"
   21 
   22 struct sockaddr *SA(struct sockaddr_storage *ss)
   23 {
   24     return (struct sockaddr *)ss;
   25 }
   26 
   27 static void test_sockaddr_is_any(void)
   28 {
   29     struct sockaddr_storage invalid = { 0 };
   30     ok(!sockaddr_is_any(SA(&invalid)), "sockaddr_is_any: invalid");
   31 
   32     struct sockaddr_storage path = { 0 };
   33     path.ss_family = AF_UNIX;
   34     ok(!sockaddr_is_any(SA(&path)), "sockaddr_is_any: unix");
   35 
   36     struct sockaddr_storage ipv4_local = { 0 };
   37     sockaddr_set(&ipv4_local, AF_INET, "127.0.0.1", 0);
   38     ok(!sockaddr_is_any(SA(&ipv4_local)), "sockaddr_is_any: IPv4 local");
   39 
   40     struct sockaddr_storage ipv4_any = { 0 };
   41     sockaddr_set(&ipv4_any, AF_INET, "0.0.0.0", 0);
   42     ok(sockaddr_is_any(SA(&ipv4_any)), "sockaddr_is_any: IPv4 any");
   43 
   44     struct sockaddr_storage ipv6_local = { 0 };
   45     sockaddr_set(&ipv6_local, AF_INET6, "::1", 0);
   46     ok(!sockaddr_is_any(SA(&ipv6_local)), "sockaddr_is_any: IPv6 local");
   47 
   48     struct sockaddr_storage ipv6_any = { 0 };
   49     sockaddr_set(&ipv6_any, AF_INET6, "::", 0);
   50     ok(sockaddr_is_any(SA(&ipv6_any)), "sockaddr_is_any: IPv6 any");
   51 }
   52 
   53 static void check_sockaddr_set(struct sockaddr_storage *ss, int family,
   54                                const char *straddr, int port)
   55 {
   56     int ret = sockaddr_set(ss, family, straddr, port);
   57     is_int(KNOT_EOK, ret, "set address '%s'", straddr);
   58 }
   59 
   60 static void test_net_match(void)
   61 {
   62     int ret;
   63     struct sockaddr_storage t = { 0 };
   64 
   65     // 127 dec ~ 01111111 bin
   66     // 170 dec ~ 10101010 bin
   67     struct sockaddr_storage ref4 = { 0 };
   68     check_sockaddr_set(&ref4, AF_INET, "127.170.170.127", 0);
   69 
   70     // 7F hex ~ 01111111 bin
   71     // AA hex ~ 10101010 bin
   72     struct sockaddr_storage ref6 = { 0 };
   73     check_sockaddr_set(&ref6, AF_INET6, "7FAA::AA7F", 0);
   74 
   75     ret = sockaddr_net_match(SA(&ref4), SA(&ref6), 32);
   76     ok(ret == false, "match: family mismatch");
   77 
   78     ret = sockaddr_net_match(NULL, SA(&ref4), 32);
   79     ok(ret == false, "match: NULL first parameter");
   80     ret = sockaddr_net_match(SA(&ref4), NULL, 32);
   81     ok(ret == false, "match: NULL second parameter");
   82 
   83     ret = sockaddr_net_match(SA(&ref4), SA(&ref4), -1);
   84     ok(ret == true, "match: ipv4 - identity, auto full prefix");
   85     ret = sockaddr_net_match(SA(&ref4), SA(&ref4), 31);
   86     ok(ret == true, "match: ipv4 - identity, subnet");
   87     ret = sockaddr_net_match(SA(&ref4), SA(&ref4), 32);
   88     ok(ret == true, "match: ipv4 - identity, full prefix");
   89     ret = sockaddr_net_match(SA(&ref4), SA(&ref4), 33);
   90     ok(ret == true, "match: ipv4 - identity, prefix overflow");
   91 
   92     ret = sockaddr_net_match(SA(&ref6), SA(&ref6), -1);
   93     ok(ret == true, "match: ipv6 - identity, auto full prefix");
   94     ret = sockaddr_net_match(SA(&ref6), SA(&ref6), 127);
   95     ok(ret == true, "match: ipv6 - identity, subnet");
   96     ret = sockaddr_net_match(SA(&ref6), SA(&ref6), 128);
   97     ok(ret == true, "match: ipv6 - identity, full prefix");
   98     ret = sockaddr_net_match(SA(&ref6), SA(&ref6), 129);
   99     ok(ret == true, "match: ipv6 - identity, prefix overflow");
  100 
  101     // 124 dec ~ 01111100 bin
  102     check_sockaddr_set(&t, AF_INET, "124.0.0.0", 0);
  103     ret = sockaddr_net_match(SA(&t), SA(&ref4), 5);
  104     ok(ret == true, "match: ipv4 - first byte, shorter prefix");
  105     ret = sockaddr_net_match(SA(&t), SA(&ref4), 6);
  106     ok(ret == true, "match: ipv4 - first byte, precise prefix");
  107     ret = sockaddr_net_match(SA(&t), SA(&ref4), 7);
  108     ok(ret == false, "match: ipv4 - first byte, not match");
  109 
  110     check_sockaddr_set(&t, AF_INET, "127.170.170.124", 0);
  111     ret = sockaddr_net_match(SA(&t), SA(&ref4), 29);
  112     ok(ret == true, "match: ipv4 - last byte, shorter prefix");
  113     ret = sockaddr_net_match(SA(&t), SA(&ref4), 30);
  114     ok(ret == true, "match: ipv4 - last byte, precise prefix");
  115     ret = sockaddr_net_match(SA(&t), SA(&ref4), 31);
  116     ok(ret == false, "match: ipv4 - last byte, not match");
  117 
  118     // 7C hex ~ 01111100 bin
  119     check_sockaddr_set(&t, AF_INET6, "7CAA::", 0);
  120     ret = sockaddr_net_match(SA(&t), SA(&ref6), 5);
  121     ok(ret == true, "match: ipv6 - first byte, shorter prefix");
  122     ret = sockaddr_net_match(SA(&t), SA(&ref6), 6);
  123     ok(ret == true, "match: ipv6 - first byte, precise prefix");
  124     ret = sockaddr_net_match(SA(&t), SA(&ref6), 7);
  125     ok(ret == false, "match: ipv6 - first byte, not match");
  126 
  127     check_sockaddr_set(&t, AF_INET6, "7FAA::AA7C", 0);
  128     ret = sockaddr_net_match(SA(&t), SA(&ref6), 125);
  129     ok(ret == true, "match: ipv6 - last byte, shorter prefix");
  130     ret = sockaddr_net_match(SA(&t), SA(&ref6), 126);
  131     ok(ret == true, "match: ipv6 - last byte, precise prefix");
  132     ret = sockaddr_net_match(SA(&t), SA(&ref6), 127);
  133     ok(ret == false, "match: ipv6 - last byte, not match");
  134 }
  135 
  136 static void test_range_match(void)
  137 {
  138     bool ret;
  139     struct sockaddr_storage t = { 0 };
  140     struct sockaddr_storage min = { 0 };
  141     struct sockaddr_storage max = { 0 };
  142 
  143     // IPv4 tests.
  144 
  145     check_sockaddr_set(&min, AF_INET, "0.0.0.0", 0);
  146     check_sockaddr_set(&max, AF_INET, "255.255.255.255", 0);
  147 
  148     check_sockaddr_set(&t, AF_INET, "0.0.0.0", 0);
  149     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  150     ok(ret == true, "match: ipv4 max range - minimum");
  151     check_sockaddr_set(&t, AF_INET, "255.255.255.255", 0);
  152     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  153     ok(ret == true, "match: ipv4 max range - maximum");
  154 
  155     check_sockaddr_set(&min, AF_INET, "1.13.113.213", 0);
  156     check_sockaddr_set(&max, AF_INET, "2.24.124.224", 0);
  157 
  158     check_sockaddr_set(&t, AF_INET, "1.12.113.213", 0);
  159     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  160     ok(ret == false, "match: ipv4 middle range - negative far min");
  161     check_sockaddr_set(&t, AF_INET, "1.13.113.212", 0);
  162     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  163     ok(ret == false, "match: ipv4 middle range - negative close min");
  164     check_sockaddr_set(&t, AF_INET, "1.13.113.213", 0);
  165     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  166     ok(ret == true, "match: ipv4 middle range - minimum");
  167     check_sockaddr_set(&t, AF_INET, "1.13.213.213", 0);
  168     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  169     ok(ret == true, "match: ipv4 middle range - middle");
  170     check_sockaddr_set(&t, AF_INET, "2.24.124.224", 0);
  171     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  172     ok(ret == true, "match: ipv4 middle range - max");
  173     check_sockaddr_set(&t, AF_INET, "2.24.124.225", 0);
  174     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  175     ok(ret == false, "match: ipv4 middle range - negative close max");
  176     check_sockaddr_set(&t, AF_INET, "2.25.124.225", 0);
  177     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  178     ok(ret == false, "match: ipv4 middle range - negative far max");
  179 
  180     // IPv6 tests.
  181 
  182     check_sockaddr_set(&min, AF_INET6, "::0", 0);
  183     check_sockaddr_set(&max, AF_INET6,
  184                        "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 0);
  185 
  186     check_sockaddr_set(&t, AF_INET6, "::0", 0);
  187     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  188     ok(ret == true, "match: ipv6 max range - minimum");
  189     check_sockaddr_set(&t, AF_INET6,
  190                        "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 0);
  191     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  192     ok(ret == true, "match: ipv6 max range - maximum");
  193 
  194     check_sockaddr_set(&min, AF_INET6, "1:13::ABCD:200B", 0);
  195     check_sockaddr_set(&max, AF_INET6, "2:A24::124:224", 0);
  196 
  197     check_sockaddr_set(&t, AF_INET6, "1:12::BCD:2000", 0);
  198     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  199     ok(ret == false, "match: ipv6 middle range - negative far min");
  200     check_sockaddr_set(&t, AF_INET6, "1:13::ABCD:200A", 0);
  201     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  202     ok(ret == false, "match: ipv6 middle range - negative close min");
  203     check_sockaddr_set(&t, AF_INET6, "1:13::ABCD:200B", 0);
  204     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  205     ok(ret == true, "match: ipv6 middle range - minimum");
  206     check_sockaddr_set(&t, AF_INET6, "1:13:0:12:34:0:ABCD:200B", 0);
  207     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  208     ok(ret == true, "match: ipv6 middle range - middle");
  209     check_sockaddr_set(&t, AF_INET6, "2:A24::124:224", 0);
  210     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  211     ok(ret == true, "match: ipv6 middle range - max");
  212     check_sockaddr_set(&t, AF_INET6, "2:A24::124:225", 0);
  213     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  214     ok(ret == false, "match: ipv6 middle range - negative close max");
  215     check_sockaddr_set(&t, AF_INET6, "2:FA24::4:24", 0);
  216     ret = sockaddr_range_match(SA(&t), SA(&min), SA(&max));
  217     ok(ret == false, "match: ipv6 middle range - negative far max");
  218 }
  219 
  220 int main(int argc, char *argv[])
  221 {
  222     plan_lazy();
  223 
  224     diag("sockaddr_is_any");
  225     test_sockaddr_is_any();
  226 
  227     diag("sockaddr_net_match");
  228     test_net_match();
  229 
  230     diag("sockaddr_range_match");
  231     test_range_match();
  232 
  233     return 0;
  234 }