summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrei Pavel <andrei@isc.org>2023-10-02 16:03:09 +0200
committerAndrei Pavel <andrei@isc.org>2023-10-05 16:40:02 +0200
commitc13c670c519a1879bd1bd3d5d11ecfd094b6e0d3 (patch)
tree0076796b0df76c51b45e92c83f4c0e259fb00c0f
parent[#3068] add bigint support to the Element deserializer (diff)
downloadkea-c13c670c519a1879bd1bd3d5d11ecfd094b6e0d3.tar.xz
kea-c13c670c519a1879bd1bd3d5d11ecfd094b6e0d3.zip
[#3068] fix prefixesInRange after uint128_t was moved to checked_uint128_t
-rw-r--r--src/lib/asiolink/addr_utilities.cc9
-rw-r--r--src/lib/asiolink/tests/addr_utilities_unittest.cc45
2 files changed, 35 insertions, 19 deletions
diff --git a/src/lib/asiolink/addr_utilities.cc b/src/lib/asiolink/addr_utilities.cc
index 9dfbf0b244..5b03349aa1 100644
--- a/src/lib/asiolink/addr_utilities.cc
+++ b/src/lib/asiolink/addr_utilities.cc
@@ -9,9 +9,9 @@
#include <asiolink/addr_utilities.h>
#include <exceptions/exceptions.h>
-#include <vector>
+#include <cstring>
#include <limits>
-#include <string.h>
+#include <vector>
using namespace isc;
using namespace isc::asiolink;
@@ -357,8 +357,9 @@ uint128_t prefixesInRange(const uint8_t pool_len, const uint8_t delegated_len) {
uint8_t const count(delegated_len - pool_len);
if (count == 128) {
- // One off is the best we can do, unless we promote to uint256_t.
- return uint128_t(-1);
+ // UINT128_MAX is one off from the real value, but it is the best we
+ // can do, unless we promote to uint256_t.
+ return std::numeric_limits<uint128_t>::max();
}
return (uint128_t(1) << count);
diff --git a/src/lib/asiolink/tests/addr_utilities_unittest.cc b/src/lib/asiolink/tests/addr_utilities_unittest.cc
index 30a98170da..4db67cd5c5 100644
--- a/src/lib/asiolink/tests/addr_utilities_unittest.cc
+++ b/src/lib/asiolink/tests/addr_utilities_unittest.cc
@@ -12,11 +12,11 @@
#include <gtest/gtest.h>
+#include <cstdint>
+#include <cstdlib>
+#include <limits>
#include <vector>
-#include <stdint.h>
-#include <stdlib.h>
-
using namespace std;
using namespace isc::asiolink;
using namespace isc::util;
@@ -346,27 +346,42 @@ TEST(AddrUtilitiesTest, prefixLengthFromRange6) {
// Checks if prefixInRange returns valid number of prefixes in specified range.
TEST(AddrUtilitiesTest, prefixesInRange) {
-
// How many /64 prefixes are in /64 pool?
- EXPECT_EQ(1, prefixesInRange(64, 64));
+ EXPECT_NO_THROW({
+ EXPECT_EQ(1, prefixesInRange(64, 64));
+ });
// How many /63 prefixes are in /64 pool?
- EXPECT_EQ(2, prefixesInRange(63, 64));
+ EXPECT_NO_THROW({
+ EXPECT_EQ(2, prefixesInRange(63, 64));
+ });
// How many /64 prefixes are in /48 pool?
- EXPECT_EQ(65536, prefixesInRange(48, 64));
+ EXPECT_NO_THROW({
+ EXPECT_EQ(65536, prefixesInRange(48, 64));
+ });
// How many /127 prefixes are in /64 pool?
- EXPECT_EQ(uint64_t(9223372036854775808ull), prefixesInRange(64, 127));
+ EXPECT_NO_THROW({
+ EXPECT_EQ(uint64_t(9223372036854775808ull), prefixesInRange(64, 127));
+ });
// How many /128 prefixes are in /64 pool?
- EXPECT_EQ(uint128_t(1) << 64, prefixesInRange(64, 128));
-
- // Let's go overboard again. How many IPv6 addresses are there?
- EXPECT_EQ(uint128_t(1) << 127, prefixesInRange(1, 128));
-
- // Let's go overboard again. How many IPv6 addresses are there?
- EXPECT_EQ(uint128_t(-1), prefixesInRange(0, 128));
+ EXPECT_NO_THROW({
+ EXPECT_EQ(uint128_t(1) << 64, prefixesInRange(64, 128));
+ });
+
+ // Let's go overboard again. How many IPv6 addresses are there in a /1?
+ EXPECT_NO_THROW({
+ EXPECT_EQ(uint128_t(1) << 127, prefixesInRange(1, 128));
+ });
+
+ // Let's go overboard again. How many IPv6 addresses are there? The result is
+ // one off from the real value, but it's preferred rather than having an
+ // overflow_error thrown.
+ EXPECT_NO_THROW({
+ EXPECT_EQ(numeric_limits<uint128_t>::max(), prefixesInRange(0, 128));
+ });
}
// Checks the function which finds an IPv4 address from input address and offset.