This is an automated email from the ASF dual-hosted git repository. bneradt pushed a commit to branch dev-1-2-12 in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git
commit 7143b8d3aa180f00fbfd75919b82f9c1015b2d6c Author: Alan M. Carroll <[email protected]> AuthorDate: Mon Oct 19 18:56:42 2020 -0500 IPSpace: fix bug in blending where the blender returns false. Add tests to verify. --- code/include/swoc/DiscreteRange.h | 2 +- unit_tests/test_ip.cc | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/code/include/swoc/DiscreteRange.h b/code/include/swoc/DiscreteRange.h index 6e2494d..58f2c5b 100644 --- a/code/include/swoc/DiscreteRange.h +++ b/code/include/swoc/DiscreteRange.h @@ -1376,7 +1376,7 @@ DiscreteSpace<METRIC, PAYLOAD>::blend(DiscreteSpace::range_type const& range, U } } else if (pred_plain_colored_p) { // can pull @a pred right to cover. pred->assign_max(remaining.max()); - } else if (!remaining.empty()) { // Must add new range. + } else if (!remaining.empty() && plain_color_p) { // Must add new range if plain color valid. this->insert_before(n, _fa.make(remaining.min(), remaining.max(), plain_color)); } return *this; diff --git a/unit_tests/test_ip.cc b/unit_tests/test_ip.cc index ba81bc6..aed4618 100644 --- a/unit_tests/test_ip.cc +++ b/unit_tests/test_ip.cc @@ -816,10 +816,20 @@ TEST_CASE("IPSpace bitset", "[libswoc][ipspace][bitset]") { TEST_CASE("IPSpace docJJ", "[libswoc][ipspace][docJJ]") { using PAYLOAD = std::bitset<32>; using Space = swoc::IPSpace<PAYLOAD>; + // Add the bits in @rhs to the range. auto blender = [](PAYLOAD&lhs, PAYLOAD const&rhs) -> bool { lhs |= rhs; return true; }; + // Add bit @a idx iff bits are already set. + auto additive = [](PAYLOAD & lhs, unsigned idx) -> bool { + if (! lhs.any()) { + return false; + } + lhs[idx] = true; + return true; + }; + auto make_bits = [](std::initializer_list<unsigned> idx) -> PAYLOAD { PAYLOAD bits; for (auto bit : idx) { @@ -889,6 +899,20 @@ TEST_CASE("IPSpace docJJ", "[libswoc][ipspace][docJJ]") { CHECK(bits == make_bits(results[idx])); ++idx; } + + // This blend should change only existing ranges, not add range. + space.blend(IPRange{"99.128.0.0-100.0.1.255"}, 27, additive); + REQUIRE(space.count() == results.size()); // no more ranges. + // Verify first two ranges modified, but not the next. + REQUIRE(std::get<1>(*(space.find(IP4Addr{"100.0.0.37"}))) == make_bits({0,27,31})); + REQUIRE(std::get<1>(*(space.find(IP4Addr{"100.0.1.37"}))) == make_bits({1,27,30})); + REQUIRE(std::get<1>(*(space.find(IP4Addr{"100.0.2.37"}))) == make_bits({2})); + + space.blend(IPRange{"100.10.1.1-100.10.2.2"}, make_bits({15}), blender); + REQUIRE(space.count() == results.size() + 1); + // Color in empty range - should not add range. + space.blend(IPRange{"100.8.10.25"}, 27, additive); + REQUIRE(space.count() == results.size() + 1); } TEST_CASE("IPSpace Uthira", "[libswoc][ipspace][uthira]") {
