Skip to content

Commit 70ec975

Browse files
committed
Merge pull request #6556
1123cdb add unit test for CNetAddr::GetGroup. (Alex Morcos) bba3db1 Fix masking of irrelevant bits in address groups. (Alex Morcos)
2 parents 5e6e089 + 1123cdb commit 70ec975

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/netbase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ std::vector<unsigned char> CNetAddr::GetGroup() const
983983
nBits -= 8;
984984
}
985985
if (nBits > 0)
986-
vchRet.push_back(GetByte(15 - nStartByte) | ((1 << nBits) - 1));
986+
vchRet.push_back(GetByte(15 - nStartByte) | ((1 << (8 - nBits)) - 1));
987987

988988
return vchRet;
989989
}

src/test/netbase_tests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <string>
99

10+
#include <boost/assign/list_of.hpp>
1011
#include <boost/test/unit_test.hpp>
1112

1213
using namespace std;
@@ -156,4 +157,20 @@ BOOST_AUTO_TEST_CASE(subnet_test)
156157
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
157158
}
158159

160+
BOOST_AUTO_TEST_CASE(netbase_getgroup)
161+
{
162+
BOOST_CHECK(CNetAddr("127.0.0.1").GetGroup() == boost::assign::list_of(0)); // Local -> !Routable()
163+
BOOST_CHECK(CNetAddr("257.0.0.1").GetGroup() == boost::assign::list_of(0)); // !Valid -> !Routable()
164+
BOOST_CHECK(CNetAddr("10.0.0.1").GetGroup() == boost::assign::list_of(0)); // RFC1918 -> !Routable()
165+
BOOST_CHECK(CNetAddr("169.254.1.1").GetGroup() == boost::assign::list_of(0)); // RFC3927 -> !Routable()
166+
BOOST_CHECK(CNetAddr("1.2.3.4").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // IPv4
167+
BOOST_CHECK(CNetAddr("::FFFF:0:102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6145
168+
BOOST_CHECK(CNetAddr("64:FF9B::102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6052
169+
BOOST_CHECK(CNetAddr("2002:102:304:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC3964
170+
BOOST_CHECK(CNetAddr("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC4380
171+
BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup() == boost::assign::list_of((unsigned char)NET_TOR)(239)); // Tor
172+
BOOST_CHECK(CNetAddr("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(4)(112)(175)); //he.net
173+
BOOST_CHECK(CNetAddr("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(32)(1)); //IPv6
174+
}
175+
159176
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)