You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netbase_tests.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2012-2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "netbase.h"
  5. #include "test/test_bitcoin.h"
  6. #include <string>
  7. #include <boost/test/unit_test.hpp>
  8. using namespace std;
  9. BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
  10. BOOST_AUTO_TEST_CASE(netbase_networks)
  11. {
  12. BOOST_CHECK(CNetAddr("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
  13. BOOST_CHECK(CNetAddr("::1").GetNetwork() == NET_UNROUTABLE);
  14. BOOST_CHECK(CNetAddr("8.8.8.8").GetNetwork() == NET_IPV4);
  15. BOOST_CHECK(CNetAddr("2001::8888").GetNetwork() == NET_IPV6);
  16. BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_TOR);
  17. }
  18. BOOST_AUTO_TEST_CASE(netbase_properties)
  19. {
  20. BOOST_CHECK(CNetAddr("127.0.0.1").IsIPv4());
  21. BOOST_CHECK(CNetAddr("::FFFF:192.168.1.1").IsIPv4());
  22. BOOST_CHECK(CNetAddr("::1").IsIPv6());
  23. BOOST_CHECK(CNetAddr("10.0.0.1").IsRFC1918());
  24. BOOST_CHECK(CNetAddr("192.168.1.1").IsRFC1918());
  25. BOOST_CHECK(CNetAddr("172.31.255.255").IsRFC1918());
  26. BOOST_CHECK(CNetAddr("2001:0DB8::").IsRFC3849());
  27. BOOST_CHECK(CNetAddr("169.254.1.1").IsRFC3927());
  28. BOOST_CHECK(CNetAddr("2002::1").IsRFC3964());
  29. BOOST_CHECK(CNetAddr("FC00::").IsRFC4193());
  30. BOOST_CHECK(CNetAddr("2001::2").IsRFC4380());
  31. BOOST_CHECK(CNetAddr("2001:10::").IsRFC4843());
  32. BOOST_CHECK(CNetAddr("FE80::").IsRFC4862());
  33. BOOST_CHECK(CNetAddr("64:FF9B::").IsRFC6052());
  34. BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor());
  35. BOOST_CHECK(CNetAddr("127.0.0.1").IsLocal());
  36. BOOST_CHECK(CNetAddr("::1").IsLocal());
  37. BOOST_CHECK(CNetAddr("8.8.8.8").IsRoutable());
  38. BOOST_CHECK(CNetAddr("2001::1").IsRoutable());
  39. BOOST_CHECK(CNetAddr("127.0.0.1").IsValid());
  40. }
  41. bool static TestSplitHost(string test, string host, int port)
  42. {
  43. string hostOut;
  44. int portOut = -1;
  45. SplitHostPort(test, portOut, hostOut);
  46. return hostOut == host && port == portOut;
  47. }
  48. BOOST_AUTO_TEST_CASE(netbase_splithost)
  49. {
  50. BOOST_CHECK(TestSplitHost("www.bitcoin.org", "www.bitcoin.org", -1));
  51. BOOST_CHECK(TestSplitHost("[www.bitcoin.org]", "www.bitcoin.org", -1));
  52. BOOST_CHECK(TestSplitHost("www.bitcoin.org:80", "www.bitcoin.org", 80));
  53. BOOST_CHECK(TestSplitHost("[www.bitcoin.org]:80", "www.bitcoin.org", 80));
  54. BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", -1));
  55. BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
  56. BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", -1));
  57. BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
  58. BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", -1));
  59. BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
  60. BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
  61. BOOST_CHECK(TestSplitHost("::8333", "::8333", -1));
  62. BOOST_CHECK(TestSplitHost(":8333", "", 8333));
  63. BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
  64. BOOST_CHECK(TestSplitHost("", "", -1));
  65. }
  66. bool static TestParse(string src, string canon)
  67. {
  68. CService addr;
  69. if (!LookupNumeric(src.c_str(), addr, 65535))
  70. return canon == "";
  71. return canon == addr.ToString();
  72. }
  73. BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
  74. {
  75. BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
  76. BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
  77. BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
  78. BOOST_CHECK(TestParse("::", "[::]:65535"));
  79. BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
  80. BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
  81. BOOST_CHECK(TestParse(":::", ""));
  82. }
  83. BOOST_AUTO_TEST_CASE(onioncat_test)
  84. {
  85. // values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat
  86. CNetAddr addr1("5wyqrzbvrdsumnok.onion");
  87. CNetAddr addr2("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca");
  88. BOOST_CHECK(addr1 == addr2);
  89. BOOST_CHECK(addr1.IsTor());
  90. BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion");
  91. BOOST_CHECK(addr1.IsRoutable());
  92. }
  93. BOOST_AUTO_TEST_CASE(subnet_test)
  94. {
  95. BOOST_CHECK(CSubNet("1.2.3.0/24") == CSubNet("1.2.3.0/255.255.255.0"));
  96. BOOST_CHECK(CSubNet("1.2.3.0/24") != CSubNet("1.2.4.0/255.255.255.0"));
  97. BOOST_CHECK(CSubNet("1.2.3.0/24").Match(CNetAddr("1.2.3.4")));
  98. BOOST_CHECK(!CSubNet("1.2.2.0/24").Match(CNetAddr("1.2.3.4")));
  99. BOOST_CHECK(CSubNet("1.2.3.4").Match(CNetAddr("1.2.3.4")));
  100. BOOST_CHECK(CSubNet("1.2.3.4/32").Match(CNetAddr("1.2.3.4")));
  101. BOOST_CHECK(!CSubNet("1.2.3.4").Match(CNetAddr("5.6.7.8")));
  102. BOOST_CHECK(!CSubNet("1.2.3.4/32").Match(CNetAddr("5.6.7.8")));
  103. BOOST_CHECK(CSubNet("::ffff:127.0.0.1").Match(CNetAddr("127.0.0.1")));
  104. BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:8")));
  105. BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:9")));
  106. BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:0/112").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
  107. // All-Matching IPv6 Matches arbitrary IPv4 and IPv6
  108. BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
  109. BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1.2.3.4")));
  110. // All-Matching IPv4 does not Match IPv6
  111. BOOST_CHECK(!CSubNet("0.0.0.0/0").Match(CNetAddr("1:2:3:4:5:6:7:1234")));
  112. // Invalid subnets Match nothing (not even invalid addresses)
  113. BOOST_CHECK(!CSubNet().Match(CNetAddr("1.2.3.4")));
  114. BOOST_CHECK(!CSubNet("").Match(CNetAddr("4.5.6.7")));
  115. BOOST_CHECK(!CSubNet("bloop").Match(CNetAddr("0.0.0.0")));
  116. BOOST_CHECK(!CSubNet("bloop").Match(CNetAddr("hab")));
  117. // Check valid/invalid
  118. BOOST_CHECK(CSubNet("1.2.3.0/0").IsValid());
  119. BOOST_CHECK(!CSubNet("1.2.3.0/-1").IsValid());
  120. BOOST_CHECK(CSubNet("1.2.3.0/32").IsValid());
  121. BOOST_CHECK(!CSubNet("1.2.3.0/33").IsValid());
  122. BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/0").IsValid());
  123. BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/33").IsValid());
  124. BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
  125. BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8/128").IsValid());
  126. BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8/129").IsValid());
  127. BOOST_CHECK(!CSubNet("fuzzy").IsValid());
  128. }
  129. BOOST_AUTO_TEST_SUITE_END()