Repository: bitcoin/bitcoin
File: src/test/netbase_tests.cpp (C++)

Code before:
    s << CAddress::V2_NETWORK(fixture_addresses);
    BOOST_CHECK_EQUAL(HexStr(s), stream_addrv2_hex);
}

BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
{
    DataStream s{ParseHex(stream_addrv2_hex)};
    std::vector<CAddress> addresses_unserialized;

    s >> CAddress::V2_NETWORK(addresses_unserialized);
    BOOST_CHECK(fixture_addresses == addresses_unserialized);
}

BOOST_AUTO_TEST_CASE(isbadport)
{
    BOOST_CHECK(IsBadPort(1));
    BOOST_CHECK(IsBadPort(22));
    BOOST_CHECK(IsBadPort(6000));

    BOOST_CHECK(!IsBadPort(80));
    BOOST_CHECK(!IsBadPort(443));
    BOOST_CHECK(!IsBadPort(8333));

    // Check all ports, there must be 80 bad ports in total.
    size_t total_bad_ports{0};
    for (uint16_t port = std::numeric_limits<uint16_t>::max(); port > 0; --port) {
        if (IsBadPort(port)) {
            ++total_bad_ports;
        }
    }
    BOOST_CHECK_EQUAL(total_bad_ports, 80);
}

BOOST_AUTO_TEST_SUITE_END()


Review comment (security):
👍, looks like this contains the end as well, no +1 needed.
Checked with `BOOST_CHECK_EQUAL(std::ranges::count_if(ports, [&](const int i) { return i == std::numeric_limits<uint16_t>::max(); }), 1);` to make sure.

---

Note though that this is a substantial memory allocation, the previous suggestion was lazy.
For the record here's the complete lazy solution I suggested (with fixed imports):
```patch
diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp
index e3bf482873..0404a45c79 100644
--- a/src/test/netbase_tests.cpp
+++ b/src/test/netbase_tests.cpp
@@ -2,6 +2,12 @@
 // Distributed under the MIT software license, see the accompanying
 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
 
+#include <algorithm>
+#include <limits>
+#include <numeric>
+#include <ranges>
+#include <string>
+
 #include <net_permissions.h>
 #include <netaddress.h>
 #include <netbase.h>
@@ -13,12 +19,10 @@
 #include <util/strencodings.h>
 #include <util/translation.h>
 
-#include <string>
-#include <numeric>
-
 #include <boost/test/unit_test.hpp>
 
 using namespace std::literals;
+using namespace std::ranges;
 
 BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
 
@@ -604,10 +608,7 @@ BOOST_AUTO_TEST_CASE(isbadport)
     BOOST_CHECK(!IsBadPort(443));
     BOOST_CHECK(!IsBadPort(8333));
 
-    // Check all ports, there must be 85 bad ports in total.
-    using namespace std::ranges;
-    std::list<int> ports(std::numeric_limits<uint16_t>::max());
-    std::iota(ports.begin(), ports.end(), 1);
+    constexpr auto ports{views::iota(1, std::numeric_limits<uint16_t>::max() + 1)};
     BOOST_CHECK_EQUAL(count_if(ports, IsBadPort), 85);
 }

```

Code after:

    s << CAddress::V2_NETWORK(fixture_addresses);
    BOOST_CHECK_EQUAL(HexStr(s), stream_addrv2_hex);
}

BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
{
    DataStream s{ParseHex(stream_addrv2_hex)};
    std::vector<CAddress> addresses_unserialized;

    s >> CAddress::V2_NETWORK(addresses_unserialized);
    BOOST_CHECK(fixture_addresses == addresses_unserialized);
}

BOOST_AUTO_TEST_CASE(isbadport)
{
    BOOST_CHECK(IsBadPort(1));
    BOOST_CHECK(IsBadPort(22));
    BOOST_CHECK(IsBadPort(6000));

    BOOST_CHECK(!IsBadPort(80));
    BOOST_CHECK(!IsBadPort(443));
    BOOST_CHECK(!IsBadPort(8333));

    // Check all possible ports and ensure we only flag the expected amount as bad
    std::list<int> ports(std::numeric_limits<uint16_t>::max());
    std::iota(ports.begin(), ports.end(), 1);
    BOOST_CHECK_EQUAL(std::ranges::count_if(ports, IsBadPort), 85);
}

BOOST_AUTO_TEST_SUITE_END()