tests: add unit test vectors for asmap interpreter
What changed, and why it matters
This commit only adds new test cases to Bitcoin Core. It does not change any production code, so it cannot introduce a security vulnerability or fix one. The tests verify that a piece of networking code (the ASMap interpreter, used to map IP addresses to autonomous system numbers) produces expected results for a set of known inputs.
No security action needed. This is a test-only change and can be reviewed as normal quality assurance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds a single BOOST_AUTO_TEST_CASE named asmap_test_vectors to src/test/netbase_tests.cpp. It creates a hard-coded ASMap bit vector, feeds it to NetGroupManager, and asserts that GetMappedAS returns specific AS numbers for a list of IPv6 addresses. No functional code in src/net* or elsewhere is modified.
Changed components
src/test/netbase_tests.cppInspect captured patch +53 / −0
diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp
index a088bf9d..996f2586 100644
--- a/src/test/netbase_tests.cpp
+++ b/src/test/netbase_tests.cpp
@@ -19,6 +19,7 @@
#include <boost/test/unit_test.hpp>
using namespace std::literals;
+using namespace util::hex_literals;
BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
@@ -610,4 +611,56 @@ BOOST_AUTO_TEST_CASE(isbadport)
BOOST_CHECK_EQUAL(std::ranges::count_if(ports, IsBadPort), 85);
}
+
+BOOST_AUTO_TEST_CASE(asmap_test_vectors)
+{
+ // Randomly generated encoded ASMap with 128 ranges, up to 20-bit AS numbers.
+ constexpr auto ASMAP_DATA{
+ "fd38d50f7d5d665357f64bba6bfc190d6078a7e68e5d3ac032edf47f8b5755f87881bfd3633d9aa7c1fa279b3"
+ "6fe26c63bbc9de44e0f04e5a382d8e1cddbe1c26653bc939d4327f287e8b4d1f8aff33176787cb0ff7cb28e3f"
+ "daef0f8f47357f801c9f7ff7a99f7f9c9f99de7f3156ae00f23eb27a303bc486aa3ccc31ec19394c2f8a53ddd"
+ "ea3cc56257f3b7e9b1f488be9c1137db823759aa4e071eef2e984aaf97b52d5f88d0f373dd190fe45e06efef1"
+ "df7278be680a73a74c76db4dd910f1d30752c57fe2bc9f079f1a1e1b036c2a69219f11c5e11980a3fa51f4f82"
+ "d36373de73b1863a8c27e36ae0e4f705be3d76ecff038a75bc0f92ba7e7f6f4080f1c47c34d095367ecf4406c"
+ "1e3bbc17ba4d6f79ea3f031b876799ac268b1e0ea9babf0f9a8e5f6c55e363c6363df46afc696d7afceaf49b6"
+ "e62df9e9dc27e70664cafe5c53df66dd0b8237678ada90e73f05ec60e6f6e96c3cbb1ea2f9dece115d5bdba10"
+ "33e53662a7d72a29477b5beb35710591d3e23e5f0379baea62ffdee535bcdf879cbf69b88d7ea37c8015381cf"
+ "63dc33d28f757a4a5e15d6a08"_hex};
+
+ // Convert to std::vector<bool> format that the ASMap interpreter uses.
+ std::vector<bool> asmap_bits;
+ asmap_bits.reserve(ASMAP_DATA.size() * 8);
+ for (auto byte : ASMAP_DATA) {
+ for (int bit = 0; bit < 8; ++bit) {
+ asmap_bits.push_back((std::to_integer<uint8_t>(byte) >> bit) & 1);
+ }
+ }
+
+ // Construct NetGroupManager with this data.
+ NetGroupManager netgroup{std::move(asmap_bits)};
+ BOOST_CHECK(netgroup.UsingASMap());
+
+ // Check some randomly-generated IPv6 addresses in it (biased towards the very beginning and
+ // very end of the 128-bit range).
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("0:1559:183:3728:224c:65a5:62e6:e991", false)), 961340);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("d0:d493:faa0:8609:e927:8b75:293c:f5a4", false)), 961340);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("2a0:26f:8b2c:2ee7:c7d1:3b24:4705:3f7f", false)), 693761);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("a77:7cd4:4be5:a449:89f2:3212:78c6:ee38", false)), 0);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("1336:1ad6:2f26:4fe3:d809:7321:6e0d:4615", false)), 672176);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("1d56:abd0:a52f:a8d5:d5a7:a610:581d:d792", false)), 499880);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("378e:7290:54e5:bd36:4760:971c:e9b9:570d", false)), 0);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("406c:820b:272a:c045:b74e:fc0a:9ef2:cecc", false)), 248495);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("46c2:ae07:9d08:2d56:d473:2bc7:57e3:20ac", false)), 248495);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("50d2:3db6:52fa:2e7:12ec:5bc4:1bd1:49f9", false)), 124471);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("53e1:1812:ffa:dccf:f9f2:64be:75fa:795", false)), 539993);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("544d:eeba:3990:35d1:ad66:f9a3:576d:8617", false)), 374443);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("6a53:40dc:8f1d:3ffa:efeb:3aa3:df88:b94b", false)), 435070);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("87aa:d1c9:9edb:91e7:aab1:9eb9:baa0:de18", false)), 244121);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("9f00:48fa:88e3:4b67:a6f3:e6d2:5cc1:5be2", false)), 862116);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("c49f:9cc6:86ad:ba08:4580:315e:dbd1:8a62", false)), 969411);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("dff5:8021:61d:b17d:406d:7888:fdac:4a20", false)), 969411);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("e888:6791:2960:d723:bcfd:47e1:2d8c:599f", false)), 824019);
+ BOOST_CHECK_EQUAL(netgroup.GetMappedAS(*LookupHost("ffff:d499:8c4b:4941:bc81:d5b9:b51e:85a8", false)), 824019);
+}
+
BOOST_AUTO_TEST_SUITE_END()
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.