aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/netbase_tests.cpp37
-rw-r--r--src/test/rpc_tests.cpp16
-rw-r--r--src/test/util_tests.cpp33
3 files changed, 75 insertions, 11 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()
diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp
index 5bc38ce2de..107c0f06e7 100644
--- a/src/test/rpc_tests.cpp
+++ b/src/test/rpc_tests.cpp
@@ -6,6 +6,7 @@
#include "rpcclient.h"
#include "base58.h"
+#include "netbase.h"
#include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp>
@@ -138,4 +139,19 @@ BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
BOOST_CHECK(AmountFromValue(ValueFromString("20999999.99999999")) == 2099999999999999LL);
}
+BOOST_AUTO_TEST_CASE(rpc_boostasiotocnetaddr)
+{
+ // Check IPv4 addresses
+ BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("1.2.3.4")).ToString(), "1.2.3.4");
+ BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("127.0.0.1")).ToString(), "127.0.0.1");
+ // Check IPv6 addresses
+ BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::1")).ToString(), "::1");
+ BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("123:4567:89ab:cdef:123:4567:89ab:cdef")).ToString(),
+ "123:4567:89ab:cdef:123:4567:89ab:cdef");
+ // v4 compatible must be interpreted as IPv4
+ BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::0:127.0.0.1")).ToString(), "127.0.0.1");
+ // v4 mapped must be interpreted as IPv4
+ BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::ffff:127.0.0.1")).ToString(), "127.0.0.1");
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index b8f107f644..f4ca8c0539 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -165,17 +165,6 @@ BOOST_AUTO_TEST_CASE(util_GetArg)
BOOST_CHECK_EQUAL(GetBoolArg("booltest4", false), true);
}
-BOOST_AUTO_TEST_CASE(util_WildcardMatch)
-{
- BOOST_CHECK(WildcardMatch("127.0.0.1", "*"));
- BOOST_CHECK(WildcardMatch("127.0.0.1", "127.*"));
- BOOST_CHECK(WildcardMatch("abcdef", "a?cde?"));
- BOOST_CHECK(!WildcardMatch("abcdef", "a?cde??"));
- BOOST_CHECK(WildcardMatch("abcdef", "a*f"));
- BOOST_CHECK(!WildcardMatch("abcdef", "a*x"));
- BOOST_CHECK(WildcardMatch("", "*"));
-}
-
BOOST_AUTO_TEST_CASE(util_FormatMoney)
{
BOOST_CHECK_EQUAL(FormatMoney(0, false), "0.00");
@@ -342,4 +331,26 @@ BOOST_AUTO_TEST_CASE(gettime)
BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
}
+BOOST_AUTO_TEST_CASE(test_ParseInt32)
+{
+ int32_t n;
+ // Valid values
+ BOOST_CHECK(ParseInt32("1234", NULL));
+ BOOST_CHECK(ParseInt32("0", &n) && n == 0);
+ BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
+ BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
+ BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
+ BOOST_CHECK(ParseInt32("-2147483648", &n) && n == -2147483648);
+ BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
+ // Invalid values
+ BOOST_CHECK(!ParseInt32("1a", &n));
+ BOOST_CHECK(!ParseInt32("aap", &n));
+ BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
+ // Overflow and underflow
+ BOOST_CHECK(!ParseInt32("-2147483649", NULL));
+ BOOST_CHECK(!ParseInt32("2147483648", NULL));
+ BOOST_CHECK(!ParseInt32("-32482348723847471234", NULL));
+ BOOST_CHECK(!ParseInt32("32482348723847471234", NULL));
+}
+
BOOST_AUTO_TEST_SUITE_END()