aboutsummaryrefslogtreecommitdiff
path: root/src/test/netbase_tests.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-04-28 11:08:57 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2014-05-09 16:45:57 +0200
commite16be73753d870c5ce77094d3a402bbe8e3bf542 (patch)
treed5c308bf0f0a9fa660161043edbabb5541e97a6b /src/test/netbase_tests.cpp
parentd8642752992799b0695cf97ada03c56d0526830c (diff)
net: Add CSubNet class for subnet matching
Diffstat (limited to 'src/test/netbase_tests.cpp')
-rw-r--r--src/test/netbase_tests.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp
index 4321852d11..c26e738384 100644
--- a/src/test/netbase_tests.cpp
+++ b/src/test/netbase_tests.cpp
@@ -102,4 +102,41 @@ BOOST_AUTO_TEST_CASE(onioncat_test)
BOOST_CHECK(addr1.IsRoutable());
}
+BOOST_AUTO_TEST_CASE(subnet_test)
+{
+ BOOST_CHECK(CSubNet("1.2.3.0/24") == CSubNet("1.2.3.0/255.255.255.0"));
+ BOOST_CHECK(CSubNet("1.2.3.0/24") != CSubNet("1.2.4.0/255.255.255.0"));
+ BOOST_CHECK(CSubNet("1.2.3.0/24").Match(CNetAddr("1.2.3.4")));
+ BOOST_CHECK(!CSubNet("1.2.2.0/24").Match(CNetAddr("1.2.3.4")));
+ BOOST_CHECK(CSubNet("1.2.3.4").Match(CNetAddr("1.2.3.4")));
+ BOOST_CHECK(CSubNet("1.2.3.4/32").Match(CNetAddr("1.2.3.4")));
+ BOOST_CHECK(!CSubNet("1.2.3.4").Match(CNetAddr("5.6.7.8")));
+ BOOST_CHECK(!CSubNet("1.2.3.4/32").Match(CNetAddr("5.6.7.8")));
+ BOOST_CHECK(CSubNet("::ffff:127.0.0.1").Match(CNetAddr("127.0.0.1")));
+ BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:8")));
+ BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:9")));
+ BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:0/112").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
+ // All-Matching IPv6 Matches arbitrary IPv4 and IPv6
+ BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
+ BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1.2.3.4")));
+ // All-Matching IPv4 does not Match IPv6
+ BOOST_CHECK(!CSubNet("0.0.0.0/0").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
+ // Invalid subnets Match nothing (not even invalid addresses)
+ BOOST_CHECK(!CSubNet().Match(CNetAddr("1.2.3.4")));
+ BOOST_CHECK(!CSubNet("").Match(CNetAddr("4.5.6.7")));
+ BOOST_CHECK(!CSubNet("bloop").Match(CNetAddr("0.0.0.0")));
+ BOOST_CHECK(!CSubNet("bloop").Match(CNetAddr("hab")));
+ // Check valid/invalid
+ BOOST_CHECK(CSubNet("1.2.3.0/0").IsValid());
+ BOOST_CHECK(!CSubNet("1.2.3.0/-1").IsValid());
+ BOOST_CHECK(CSubNet("1.2.3.0/32").IsValid());
+ BOOST_CHECK(!CSubNet("1.2.3.0/33").IsValid());
+ BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/0").IsValid());
+ BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/33").IsValid());
+ BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
+ BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/128").IsValid());
+ BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8/129").IsValid());
+ BOOST_CHECK(!CSubNet("fuzzy").IsValid());
+}
+
BOOST_AUTO_TEST_SUITE_END()