LCOV - code coverage report
Current view: top level - tests - catch_range.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 331 331 100.0 %
Date: 2022-01-30 17:24:05 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2011-2021  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // Project: https://snapwebsites.org/project/libaddr
       4             : //
       5             : // Permission is hereby granted, free of charge, to any
       6             : // person obtaining a copy of this software and
       7             : // associated documentation files (the "Software"), to
       8             : // deal in the Software without restriction, including
       9             : // without limitation the rights to use, copy, modify,
      10             : // merge, publish, distribute, sublicense, and/or sell
      11             : // copies of the Software, and to permit persons to whom
      12             : // the Software is furnished to do so, subject to the
      13             : // following conditions:
      14             : //
      15             : // The above copyright notice and this permission notice
      16             : // shall be included in all copies or substantial
      17             : // portions of the Software.
      18             : //
      19             : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      20             : // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      21             : // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      22             : // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      23             : // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      24             : // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      25             : // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      26             : // ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      27             : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      28             : // SOFTWARE.
      29             : 
      30             : 
      31             : /** \file
      32             :  * \brief Check the addr_range class basics.
      33             :  *
      34             :  * This set of unit tests do basic verification of the range class.
      35             :  *
      36             :  * The point here is to test the range functionality and not the
      37             :  * addr_parser class.
      38             :  */
      39             : 
      40             : 
      41             : // self
      42             : //
      43             : #include    "catch_main.h"
      44             : 
      45             : 
      46             : // last include
      47             : //
      48             : #include    <snapdev/poison.h>
      49             : 
      50             : 
      51             : 
      52             : 
      53             : 
      54           7 : CATCH_TEST_CASE( "ipv4::range", "[ipv4]" )
      55             : {
      56          10 :     CATCH_GIVEN("addr_range()")
      57             :     {
      58           3 :         addr::addr_range range;
      59             : 
      60           6 :         CATCH_SECTION("verify defaults")
      61             :         {
      62           1 :             addr::addr a;
      63             : 
      64           1 :             CATCH_REQUIRE_FALSE(range.has_from());
      65           1 :             CATCH_REQUIRE_FALSE(range.has_to());
      66           1 :             CATCH_REQUIRE_FALSE(range.is_range());
      67           1 :             CATCH_REQUIRE_FALSE(range.is_empty());
      68             : //std::cerr << "++++++++++++++++++ " << a.to_ipv6_string(addr::addr::string_ip_t::STRING_IP_PORT) << "\n";
      69             : //std::cerr << "++++++++++++++++++ " << range.get_from().to_ipv6_string(addr::addr::string_ip_t::STRING_IP_PORT) << "\n";
      70             : //
      71             : //struct sockaddr_in6 in6_a;
      72             : //a.get_ipv6(in6_a);
      73             : //std::cerr << "++++++++++++++++++ in6 --" << std::hex;
      74             : //for(size_t idx(0); idx < sizeof(in6_a); ++idx)
      75             : //    std::cerr << " " << static_cast<int>(reinterpret_cast<char *>(&in6_a)[idx]);
      76             : //std::cerr << "\n";
      77             : //
      78             : //struct sockaddr_in6 in6_b;
      79             : //range.get_from().get_ipv6(in6_b);
      80             : //std::cerr << "++++++++++++++++++ in6 --" << std::hex;
      81             : //for(size_t idx(0); idx < sizeof(in6_b); ++idx)
      82             : //    std::cerr << " " << static_cast<int>(reinterpret_cast<char *>(&in6_b)[idx]);
      83             : //std::cerr << "\n";
      84           1 :             CATCH_REQUIRE(range.get_from() == a);
      85           1 :             CATCH_REQUIRE(range.get_to() == a);
      86             : 
      87             :             // to use the const version of the get_from/to() functions
      88             :             // we have to define a const refence to range
      89             :             //
      90           1 :             auto const & r(range);
      91           1 :             CATCH_REQUIRE(r.get_from() == a);
      92           1 :             CATCH_REQUIRE(r.get_to() == a);
      93             : 
      94           1 :             addr::addr other;
      95           1 :             CATCH_REQUIRE_THROWS_AS(range.is_in(other), addr::addr_invalid_state);
      96           1 :             CATCH_REQUIRE_THROWS_AS(r.is_in(other), addr::addr_invalid_state);
      97             :         }
      98             : 
      99           6 :         CATCH_SECTION("test normal range (from <= to)")
     100             :         {
     101           1 :             addr::addr a;
     102           1 :             addr::addr other;
     103             : 
     104             :             // from is set to 10.0.0.1
     105           1 :             struct sockaddr_in fin = sockaddr_in();
     106           1 :             fin.sin_family = AF_INET;
     107           1 :             fin.sin_port = htons(rand());
     108           1 :             uint32_t faddress((10 << 24)
     109             :                           | (0 << 16)
     110             :                           | (0 << 8)
     111             :                           | 1);
     112           1 :             fin.sin_addr.s_addr = htonl(faddress);
     113           1 :             addr::addr f;
     114           1 :             f.set_ipv4(fin);
     115             : 
     116             :             // from is set to 10.0.0.254
     117           1 :             struct sockaddr_in tin = sockaddr_in();
     118           1 :             tin.sin_family = AF_INET;
     119           1 :             tin.sin_port = htons(rand());
     120           1 :             uint32_t taddress((10 << 24)
     121             :                           | (0 << 16)
     122             :                           | (0 << 8)
     123             :                           | 254);
     124           1 :             tin.sin_addr.s_addr = htonl(taddress);
     125           1 :             addr::addr t;
     126           1 :             t.set_ipv4(tin);
     127             : 
     128             :             // test defaults first
     129             :             //
     130           1 :             CATCH_REQUIRE_FALSE(range.has_from());
     131           1 :             CATCH_REQUIRE_FALSE(range.has_to());
     132           1 :             CATCH_REQUIRE_FALSE(range.is_range());
     133           1 :             CATCH_REQUIRE_FALSE(range.is_empty());
     134           1 :             CATCH_REQUIRE(range.get_from() == a);
     135           1 :             CATCH_REQUIRE(range.get_to() == a);
     136           1 :             auto const & r1(range);
     137           1 :             CATCH_REQUIRE(r1.get_from() == a);
     138           1 :             CATCH_REQUIRE(r1.get_to() == a);
     139           1 :             CATCH_REQUIRE_THROWS_AS(range.is_in(other), addr::addr_invalid_state);
     140           1 :             CATCH_REQUIRE_THROWS_AS(r1.is_in(other), addr::addr_invalid_state);
     141           1 :             CATCH_REQUIRE(range.match(a));
     142           1 :             CATCH_REQUIRE_FALSE(range.match(f));
     143           1 :             CATCH_REQUIRE_FALSE(range.match(t));
     144             : 
     145           1 :             range.set_from(f);
     146             : 
     147             :             // defined "from", test the results
     148             :             //
     149           1 :             CATCH_REQUIRE(range.has_from());
     150           1 :             CATCH_REQUIRE_FALSE(range.has_to());
     151           1 :             CATCH_REQUIRE_FALSE(range.is_range());
     152           1 :             CATCH_REQUIRE_FALSE(range.is_empty());
     153           1 :             CATCH_REQUIRE(range.get_from() == f);
     154           1 :             CATCH_REQUIRE(range.get_to() == a);
     155           1 :             auto const & r2(range);
     156           1 :             CATCH_REQUIRE(r2.get_from() == f);
     157           1 :             CATCH_REQUIRE(r2.get_to() == a);
     158           1 :             CATCH_REQUIRE_THROWS_AS(range.is_in(other), addr::addr_invalid_state);
     159           1 :             CATCH_REQUIRE_THROWS_AS(r2.is_in(other), addr::addr_invalid_state);
     160           1 :             CATCH_REQUIRE_FALSE(range.match(a));
     161           1 :             CATCH_REQUIRE(range.match(f));
     162           1 :             CATCH_REQUIRE_FALSE(range.match(t));
     163             : 
     164           1 :             range.set_to(t);
     165             : 
     166             :             // defined "to", test the results
     167             :             //
     168           1 :             CATCH_REQUIRE(range.has_from());
     169           1 :             CATCH_REQUIRE(range.has_to());
     170           1 :             CATCH_REQUIRE(range.is_range());
     171           1 :             CATCH_REQUIRE_FALSE(range.is_empty());
     172           1 :             CATCH_REQUIRE(range.get_from() == f);
     173           1 :             CATCH_REQUIRE(range.get_to() == t);
     174           1 :             auto const & r3(range);
     175           1 :             CATCH_REQUIRE(r3.get_from() == f);
     176           1 :             CATCH_REQUIRE(r3.get_to() == t);
     177           1 :             CATCH_REQUIRE_FALSE(range.match(a));
     178           1 :             CATCH_REQUIRE(range.match(f));
     179           1 :             CATCH_REQUIRE(range.match(t));
     180             : 
     181             :             // IP before range
     182             :             {
     183           1 :                 struct sockaddr_in bin = sockaddr_in();
     184           1 :                 bin.sin_family = AF_INET;
     185           1 :                 bin.sin_port = htons(rand());
     186           1 :                 uint32_t baddress((10 << 24)
     187             :                               | (0 << 16)
     188             :                               | (0 << 8)
     189             :                               | 0);
     190           1 :                 bin.sin_addr.s_addr = htonl(baddress);
     191           1 :                 addr::addr b;
     192           1 :                 b.set_ipv4(bin);
     193             : 
     194           1 :                 CATCH_REQUIRE_FALSE(range.is_in(b));
     195             :             }
     196             : 
     197          11 :             for(int idx(0); idx < 10; ++idx)
     198             :             {
     199          10 :                 struct sockaddr_in bin = sockaddr_in();
     200          10 :                 bin.sin_family = AF_INET;
     201          10 :                 bin.sin_port = htons(rand());
     202          10 :                 uint32_t baddress(((rand() % 10) << 24)
     203          10 :                               | ((rand() & 255) << 16)
     204          10 :                               | ((rand() & 255) << 8)
     205          10 :                               | (rand() & 255));
     206          10 :                 bin.sin_addr.s_addr = htonl(baddress);
     207          10 :                 addr::addr b;
     208          10 :                 b.set_ipv4(bin);
     209             : 
     210          10 :                 CATCH_REQUIRE_FALSE(range.is_in(b));
     211             :             }
     212             : 
     213             :             // IP after range
     214             :             {
     215           1 :                 struct sockaddr_in ain = sockaddr_in();
     216           1 :                 ain.sin_family = AF_INET;
     217           1 :                 ain.sin_port = htons(rand());
     218           1 :                 uint32_t aaddress((10 << 24)
     219             :                               | (0 << 16)
     220             :                               | (0 << 8)
     221             :                               | 255);
     222           1 :                 ain.sin_addr.s_addr = htonl(aaddress);
     223           1 :                 addr::addr after;
     224           1 :                 after.set_ipv4(ain);
     225             : 
     226           1 :                 CATCH_REQUIRE_FALSE(range.is_in(after));
     227             :             }
     228             : 
     229          11 :             for(int idx(0); idx < 10; ++idx)
     230             :             {
     231          10 :                 struct sockaddr_in ain = sockaddr_in();
     232          10 :                 ain.sin_family = AF_INET;
     233          10 :                 ain.sin_port = htons(rand());
     234          10 :                 uint32_t aaddress(((rand() % (256 - 11) + 11) << 24)
     235          10 :                               | ((rand() & 255) << 16)
     236          10 :                               | ((rand() & 255) << 8)
     237          10 :                               | (rand() & 255));
     238          10 :                 ain.sin_addr.s_addr = htonl(aaddress);
     239          10 :                 addr::addr after;
     240          10 :                 after.set_ipv4(ain);
     241             : 
     242          10 :                 CATCH_REQUIRE_FALSE(range.is_in(after));
     243             :             }
     244             : 
     245             :             // IP in range
     246             :             //
     247         255 :             for(int idx(1); idx < 255; ++idx)
     248             :             {
     249         254 :                 struct sockaddr_in iin = sockaddr_in();
     250         254 :                 iin.sin_family = AF_INET;
     251         254 :                 iin.sin_port = htons(rand());
     252         254 :                 uint32_t iaddress((10 << 24)
     253             :                               | (0 << 16)
     254             :                               | (0 << 8)
     255         254 :                               | idx);
     256         254 :                 iin.sin_addr.s_addr = htonl(iaddress);
     257         254 :                 addr::addr i;
     258         254 :                 i.set_ipv4(iin);
     259             : 
     260         254 :                 CATCH_REQUIRE(range.is_in(i));
     261             :             }
     262             :         }
     263             : 
     264           6 :         CATCH_SECTION("test empty range (from > to)")
     265             :         {
     266           1 :             addr::addr a;
     267           1 :             addr::addr other;
     268             : 
     269             :             // from is set to 10.0.0.254
     270           1 :             struct sockaddr_in fin = sockaddr_in();
     271           1 :             fin.sin_family = AF_INET;
     272           1 :             fin.sin_port = htons(rand());
     273           1 :             uint32_t faddress((10 << 24)
     274             :                           | (0 << 16)
     275             :                           | (0 << 8)
     276             :                           | 254);
     277           1 :             fin.sin_addr.s_addr = htonl(faddress);
     278           1 :             addr::addr f;
     279           1 :             f.set_ipv4(fin);
     280             : 
     281             :             // from is set to 10.0.0.1
     282           1 :             struct sockaddr_in tin = sockaddr_in();
     283           1 :             tin.sin_family = AF_INET;
     284           1 :             tin.sin_port = htons(rand());
     285           1 :             uint32_t taddress((10 << 24)
     286             :                           | (0 << 16)
     287             :                           | (0 << 8)
     288             :                           | 1);
     289           1 :             tin.sin_addr.s_addr = htonl(taddress);
     290           1 :             addr::addr t;
     291           1 :             t.set_ipv4(tin);
     292             : 
     293             :             // test defaults first
     294             :             //
     295           1 :             CATCH_REQUIRE_FALSE(range.has_from());
     296           1 :             CATCH_REQUIRE_FALSE(range.has_to());
     297           1 :             CATCH_REQUIRE_FALSE(range.is_range());
     298           1 :             CATCH_REQUIRE_FALSE(range.is_empty());
     299           1 :             CATCH_REQUIRE(range.get_from() == a);
     300           1 :             CATCH_REQUIRE(range.get_to() == a);
     301           1 :             auto const & r1(range);
     302           1 :             CATCH_REQUIRE(r1.get_from() == a);
     303           1 :             CATCH_REQUIRE(r1.get_to() == a);
     304           1 :             CATCH_REQUIRE_THROWS_AS(range.is_in(other), addr::addr_invalid_state);
     305           1 :             CATCH_REQUIRE_THROWS_AS(r1.is_in(other), addr::addr_invalid_state);
     306           1 :             CATCH_REQUIRE(range.match(a));
     307           1 :             CATCH_REQUIRE_FALSE(range.match(f));
     308           1 :             CATCH_REQUIRE_FALSE(range.match(t));
     309             : 
     310           1 :             range.set_from(f);
     311             : 
     312             :             // defined "from", test the results
     313             :             //
     314           1 :             CATCH_REQUIRE(range.has_from());
     315           1 :             CATCH_REQUIRE_FALSE(range.has_to());
     316           1 :             CATCH_REQUIRE_FALSE(range.is_range());
     317           1 :             CATCH_REQUIRE_FALSE(range.is_empty());
     318           1 :             CATCH_REQUIRE(range.get_from() == f);
     319           1 :             CATCH_REQUIRE(range.get_to() == a);
     320           1 :             auto const & r2(range);
     321           1 :             CATCH_REQUIRE(r2.get_from() == f);
     322           1 :             CATCH_REQUIRE(r2.get_to() == a);
     323           1 :             CATCH_REQUIRE_THROWS_AS(range.is_in(other), addr::addr_invalid_state);
     324           1 :             CATCH_REQUIRE_THROWS_AS(r2.is_in(other), addr::addr_invalid_state);
     325           1 :             CATCH_REQUIRE_FALSE(range.match(a));
     326           1 :             CATCH_REQUIRE(range.match(f));
     327           1 :             CATCH_REQUIRE_FALSE(range.match(t));
     328             : 
     329           1 :             range.set_to(t);
     330             : 
     331             :             // defined "to", test the results
     332             :             //
     333           1 :             CATCH_REQUIRE(range.has_from());
     334           1 :             CATCH_REQUIRE(range.has_to());
     335           1 :             CATCH_REQUIRE(range.is_range());
     336           1 :             CATCH_REQUIRE(range.is_empty());
     337           1 :             CATCH_REQUIRE(range.get_from() == f);
     338           1 :             CATCH_REQUIRE(range.get_to() == t);
     339           1 :             auto const & r3(range);
     340           1 :             CATCH_REQUIRE(r3.get_from() == f);
     341           1 :             CATCH_REQUIRE(r3.get_to() == t);
     342           1 :             CATCH_REQUIRE_FALSE(range.match(a));
     343           1 :             CATCH_REQUIRE_FALSE(range.match(f));
     344           1 :             CATCH_REQUIRE_FALSE(range.match(t));
     345             : 
     346             :             // IP before range
     347             :             {
     348           1 :                 struct sockaddr_in bin = sockaddr_in();
     349           1 :                 bin.sin_family = AF_INET;
     350           1 :                 bin.sin_port = htons(rand());
     351           1 :                 uint32_t baddress((10 << 24)
     352             :                               | (0 << 16)
     353             :                               | (0 << 8)
     354             :                               | 0);
     355           1 :                 bin.sin_addr.s_addr = htonl(baddress);
     356           1 :                 addr::addr b;
     357           1 :                 b.set_ipv4(bin);
     358             : 
     359           1 :                 CATCH_REQUIRE_FALSE(range.is_in(b));
     360             :             }
     361             : 
     362          11 :             for(int idx(0); idx < 10; ++idx)
     363             :             {
     364          10 :                 struct sockaddr_in bin = sockaddr_in();
     365          10 :                 bin.sin_family = AF_INET;
     366          10 :                 bin.sin_port = htons(rand());
     367          10 :                 uint32_t baddress(((rand() % 10) << 24)
     368          10 :                               | ((rand() & 255) << 16)
     369          10 :                               | ((rand() & 255) << 8)
     370          10 :                               | (rand() & 255));
     371          10 :                 bin.sin_addr.s_addr = htonl(baddress);
     372          10 :                 addr::addr b;
     373          10 :                 b.set_ipv4(bin);
     374             : 
     375          10 :                 CATCH_REQUIRE_FALSE(range.is_in(b));
     376             :             }
     377             : 
     378             :             // IP after range
     379             :             {
     380           1 :                 struct sockaddr_in ain = sockaddr_in();
     381           1 :                 ain.sin_family = AF_INET;
     382           1 :                 ain.sin_port = htons(rand());
     383           1 :                 uint32_t aaddress((10 << 24)
     384             :                               | (0 << 16)
     385             :                               | (0 << 8)
     386             :                               | 255);
     387           1 :                 ain.sin_addr.s_addr = htonl(aaddress);
     388           1 :                 addr::addr after;
     389           1 :                 after.set_ipv4(ain);
     390             : 
     391           1 :                 CATCH_REQUIRE_FALSE(range.is_in(after));
     392             :             }
     393             : 
     394          11 :             for(int idx(0); idx < 10; ++idx)
     395             :             {
     396          10 :                 struct sockaddr_in ain = sockaddr_in();
     397          10 :                 ain.sin_family = AF_INET;
     398          10 :                 ain.sin_port = htons(rand());
     399          10 :                 uint32_t aaddress(((rand() % (256 - 11) + 11) << 24)
     400          10 :                               | ((rand() & 255) << 16)
     401          10 :                               | ((rand() & 255) << 8)
     402          10 :                               | (rand() & 255));
     403          10 :                 ain.sin_addr.s_addr = htonl(aaddress);
     404          10 :                 addr::addr after;
     405          10 :                 after.set_ipv4(ain);
     406             : 
     407          10 :                 CATCH_REQUIRE_FALSE(range.is_in(after));
     408             :             }
     409             : 
     410             :             // IP in range
     411             :             //
     412         101 :             for(int idx(0); idx < 100; ++idx)
     413             :             {
     414         100 :                 struct sockaddr_in iin = sockaddr_in();
     415         100 :                 iin.sin_family = AF_INET;
     416         100 :                 iin.sin_port = htons(rand());
     417             :                 uint32_t iaddress((10 << 24)
     418             :                               | (0 << 16)
     419             :                               | (0 << 8)
     420         100 :                               | ((rand() & 253) + 1));
     421         100 :                 iin.sin_addr.s_addr = htonl(iaddress);
     422         100 :                 addr::addr i;
     423         100 :                 i.set_ipv4(iin);
     424             : 
     425         100 :                 CATCH_REQUIRE_FALSE(range.is_in(i));
     426             :             }
     427             :         }
     428             :     }
     429             : 
     430          10 :     CATCH_GIVEN("compute intersection of two ranges")
     431             :     {
     432           4 :         CATCH_SECTION("two ranges that overlap")
     433             :         {
     434             :             // from is set to 10.1.0.0
     435           1 :             struct sockaddr_in f1in = sockaddr_in();
     436           1 :             f1in.sin_family = AF_INET;
     437           1 :             f1in.sin_port = htons(rand());
     438           1 :             uint32_t f1address((10 << 24)
     439             :                           | (1 << 16)
     440             :                           | (0 << 8)
     441             :                           | 0);
     442           1 :             f1in.sin_addr.s_addr = htonl(f1address);
     443           1 :             addr::addr f1;
     444           1 :             f1.set_ipv4(f1in);
     445             : 
     446             :             // from is set to 10.5.255.255
     447           1 :             struct sockaddr_in t1in = sockaddr_in();
     448           1 :             t1in.sin_family = AF_INET;
     449           1 :             t1in.sin_port = htons(rand());
     450           1 :             uint32_t t1address((10 << 24)
     451             :                           | (5 << 16)
     452             :                           | (255 << 8)
     453             :                           | 255);
     454           1 :             t1in.sin_addr.s_addr = htonl(t1address);
     455           1 :             addr::addr t1;
     456           1 :             t1.set_ipv4(t1in);
     457             : 
     458             :             // from is set to 10.2.0.0
     459           1 :             struct sockaddr_in f2in = sockaddr_in();
     460           1 :             f2in.sin_family = AF_INET;
     461           1 :             f2in.sin_port = htons(rand());
     462           1 :             uint32_t f2address((10 << 24)
     463             :                           | (2 << 16)
     464             :                           | (0 << 8)
     465             :                           | 0);
     466           1 :             f2in.sin_addr.s_addr = htonl(f2address);
     467           1 :             addr::addr f2;
     468           1 :             f2.set_ipv4(f2in);
     469             : 
     470             :             // from is set to 10.10.255.255
     471           1 :             struct sockaddr_in t2in = sockaddr_in();
     472           1 :             t2in.sin_family = AF_INET;
     473           1 :             t2in.sin_port = htons(rand());
     474           1 :             uint32_t t2address((10 << 24)
     475             :                           | (10 << 16)
     476             :                           | (255 << 8)
     477             :                           | 255);
     478           1 :             t2in.sin_addr.s_addr = htonl(t2address);
     479           1 :             addr::addr t2;
     480           1 :             t2.set_ipv4(t2in);
     481             : 
     482           1 :             addr::addr_range range1;
     483           1 :             range1.set_from(f1);
     484           1 :             range1.set_to(t1);
     485             : 
     486           1 :             addr::addr_range range2;
     487           1 :             range2.set_from(f2);
     488           1 :             range2.set_to(t2);
     489             : 
     490           1 :             CATCH_REQUIRE(range1.is_range());
     491           1 :             CATCH_REQUIRE_FALSE(range1.is_empty());
     492           1 :             CATCH_REQUIRE(range2.is_range());
     493           1 :             CATCH_REQUIRE_FALSE(range2.is_empty());
     494             : 
     495           1 :             addr::addr_range range3(range1.intersection(range2));
     496             : 
     497           1 :             CATCH_REQUIRE(range3.is_range());
     498           1 :             CATCH_REQUIRE_FALSE(range3.is_empty());
     499             : 
     500           1 :             CATCH_REQUIRE(range3.get_from().to_ipv4_string(addr::addr::string_ip_t::STRING_IP_ONLY) == "10.2.0.0");
     501           1 :             CATCH_REQUIRE(range3.get_to().to_ipv4_string(addr::addr::string_ip_t::STRING_IP_ONLY) == "10.5.255.255");
     502             :         }
     503             : 
     504           4 :         CATCH_SECTION("two ranges that do not overlap")
     505             :         {
     506             :             // from is set to 10.1.0.0
     507           1 :             struct sockaddr_in f1in = sockaddr_in();
     508           1 :             f1in.sin_family = AF_INET;
     509           1 :             f1in.sin_port = htons(rand());
     510           1 :             uint32_t f1address((10 << 24)
     511             :                           | (1 << 16)
     512             :                           | (0 << 8)
     513             :                           | 0);
     514           1 :             f1in.sin_addr.s_addr = htonl(f1address);
     515           1 :             addr::addr f1;
     516           1 :             f1.set_ipv4(f1in);
     517             : 
     518             :             // from is set to 10.5.255.255
     519           1 :             struct sockaddr_in t1in = sockaddr_in();
     520           1 :             t1in.sin_family = AF_INET;
     521           1 :             t1in.sin_port = htons(rand());
     522           1 :             uint32_t t1address((10 << 24)
     523             :                           | (5 << 16)
     524             :                           | (255 << 8)
     525             :                           | 255);
     526           1 :             t1in.sin_addr.s_addr = htonl(t1address);
     527           1 :             addr::addr t1;
     528           1 :             t1.set_ipv4(t1in);
     529             : 
     530             :             // from is set to 10.10.0.0
     531           1 :             struct sockaddr_in f2in = sockaddr_in();
     532           1 :             f2in.sin_family = AF_INET;
     533           1 :             f2in.sin_port = htons(rand());
     534           1 :             uint32_t f2address((10 << 24)
     535             :                           | (10 << 16)
     536             :                           | (0 << 8)
     537             :                           | 0);
     538           1 :             f2in.sin_addr.s_addr = htonl(f2address);
     539           1 :             addr::addr f2;
     540           1 :             f2.set_ipv4(f2in);
     541             : 
     542             :             // from is set to 10.20.255.255
     543           1 :             struct sockaddr_in t2in = sockaddr_in();
     544           1 :             t2in.sin_family = AF_INET;
     545           1 :             t2in.sin_port = htons(rand());
     546           1 :             uint32_t t2address((10 << 24)
     547             :                           | (20 << 16)
     548             :                           | (255 << 8)
     549             :                           | 255);
     550           1 :             t2in.sin_addr.s_addr = htonl(t2address);
     551           1 :             addr::addr t2;
     552           1 :             t2.set_ipv4(t2in);
     553             : 
     554           1 :             addr::addr_range range1;
     555           1 :             range1.set_from(f1);
     556           1 :             range1.set_to(t1);
     557             : 
     558           1 :             addr::addr_range range2;
     559           1 :             range2.set_from(f2);
     560           1 :             range2.set_to(t2);
     561             : 
     562           1 :             CATCH_REQUIRE(range1.is_range());
     563           1 :             CATCH_REQUIRE_FALSE(range1.is_empty());
     564           1 :             CATCH_REQUIRE(range2.is_range());
     565           1 :             CATCH_REQUIRE_FALSE(range2.is_empty());
     566             : 
     567           1 :             addr::addr_range range3(range1.intersection(range2));
     568             : 
     569           1 :             CATCH_REQUIRE(range3.is_range());
     570           1 :             CATCH_REQUIRE(range3.is_empty());
     571             : 
     572             :             // although it is "empty" we know the IPs and can test them
     573             :             //
     574           1 :             CATCH_REQUIRE(range3.get_from().to_ipv4_string(addr::addr::string_ip_t::STRING_IP_ONLY) == "10.10.0.0");
     575           1 :             CATCH_REQUIRE(range3.get_to().to_ipv4_string(addr::addr::string_ip_t::STRING_IP_ONLY) == "10.5.255.255");
     576             : 
     577           2 :             addr::addr_range::vector_t rlist;
     578           1 :             rlist.push_back(range1);
     579           1 :             rlist.push_back(range2);
     580           1 :             CATCH_REQUIRE(addr::address_match_ranges(rlist, f1));
     581           1 :             CATCH_REQUIRE(addr::address_match_ranges(rlist, f2));
     582           1 :             CATCH_REQUIRE(addr::address_match_ranges(rlist, t1));
     583           1 :             CATCH_REQUIRE(addr::address_match_ranges(rlist, t2));
     584             :         }
     585             :     }
     586          11 : }
     587             : 
     588             : 
     589             : 
     590             : 
     591             : 
     592             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13