Blame SOURCES/0029-segtree-Fix-get-element-command-with-prefixes.patch

8fd843
From 3a2016f539e46183965bada40946e259c33158d9 Mon Sep 17 00:00:00 2001
8fd843
From: Phil Sutter <psutter@redhat.com>
8fd843
Date: Tue, 30 Jun 2020 16:20:23 +0200
8fd843
Subject: [PATCH] segtree: Fix get element command with prefixes
8fd843
8fd843
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1832235
8fd843
Upstream Status: nftables commit 506fb113f7ca4
8fd843
8fd843
commit 506fb113f7ca4fbb3d6da09ef6f9dc2b31f54a1f
8fd843
Author: Phil Sutter <phil@nwl.cc>
8fd843
Date:   Thu Apr 30 14:02:44 2020 +0200
8fd843
8fd843
    segtree: Fix get element command with prefixes
8fd843
8fd843
    Code wasn't aware of prefix elements in interval sets. With previous
8fd843
    changes in place, they merely need to be accepted in
8fd843
    get_set_interval_find() - value comparison and expression duplication is
8fd843
    identical to ranges.
8fd843
8fd843
    Extend sets/0034get_element_0 test to cover prefixes as well. While
8fd843
    being at it, also cover concatenated ranges.
8fd843
8fd843
    Signed-off-by: Phil Sutter <phil@nwl.cc>
8fd843
---
8fd843
 src/segtree.c                                |  1 +
8fd843
 tests/shell/testcases/sets/0034get_element_0 | 62 ++++++++++++++++++++--------
8fd843
 2 files changed, 45 insertions(+), 18 deletions(-)
8fd843
8fd843
diff --git a/src/segtree.c b/src/segtree.c
8fd843
index 6e1f696..073c6ec 100644
8fd843
--- a/src/segtree.c
8fd843
+++ b/src/segtree.c
8fd843
@@ -689,6 +689,7 @@ static struct expr *get_set_interval_find(const struct table *table,
8fd843
 
8fd843
 	list_for_each_entry(i, &set->init->expressions, list) {
8fd843
 		switch (i->key->etype) {
8fd843
+		case EXPR_PREFIX:
8fd843
 		case EXPR_RANGE:
8fd843
 			range_expr_value_low(val, i);
8fd843
 			if (left && mpz_cmp(left->key->value, val))
8fd843
diff --git a/tests/shell/testcases/sets/0034get_element_0 b/tests/shell/testcases/sets/0034get_element_0
8fd843
index e23dbda..3343529 100755
8fd843
--- a/tests/shell/testcases/sets/0034get_element_0
8fd843
+++ b/tests/shell/testcases/sets/0034get_element_0
8fd843
@@ -2,43 +2,69 @@
8fd843
 
8fd843
 RC=0
8fd843
 
8fd843
-check() { # (elems, expected)
8fd843
-	out=$($NFT get element ip t s "{ $1 }")
8fd843
+check() { # (set, elems, expected)
8fd843
+	out=$($NFT get element ip t $1 "{ $2 }")
8fd843
 	out=$(grep "elements =" <<< "$out")
8fd843
 	out="${out#* \{ }"
8fd843
 	out="${out% \}}"
8fd843
-	[[ "$out" == "$2" ]] && return
8fd843
-	echo "ERROR: asked for '$1', expecting '$2' but got '$out'"
8fd843
+	[[ "$out" == "$3" ]] && return
8fd843
+	echo "ERROR: asked for '$2' in set $1, expecting '$3' but got '$out'"
8fd843
 	((RC++))
8fd843
 }
8fd843
 
8fd843
 RULESET="add table ip t
8fd843
 add set ip t s { type inet_service; flags interval; }
8fd843
 add element ip t s { 10, 20-30, 40, 50-60 }
8fd843
+add set ip t ips { type ipv4_addr; flags interval; }
8fd843
+add element ip t ips { 10.0.0.1, 10.0.0.5-10.0.0.8 }
8fd843
+add element ip t ips { 10.0.0.128/25, 10.0.1.0/24, 10.0.2.3-10.0.2.12 }
8fd843
+add set ip t cs { type ipv4_addr . inet_service; flags interval; }
8fd843
+add element ip t cs { 10.0.0.1 . 22, 10.1.0.0/16 . 1-1024 }
8fd843
+add element ip t cs { 10.2.0.1-10.2.0.8 . 1024-65535 }
8fd843
 "
8fd843
 
8fd843
 $NFT -f - <<< "$RULESET"
8fd843
 
8fd843
 # simple cases, (non-)existing values and ranges
8fd843
-check 10 10
8fd843
-check 11 ""
8fd843
-check 20-30 20-30
8fd843
-check 15-18 ""
8fd843
+check s 10 10
8fd843
+check s 11 ""
8fd843
+check s 20-30 20-30
8fd843
+check s 15-18 ""
8fd843
 
8fd843
 # multiple single elements, ranges smaller than present
8fd843
-check "10, 40" "10, 40"
8fd843
-check "22-24, 26-28" "20-30, 20-30"
8fd843
-check 21-29 20-30
8fd843
+check s "10, 40" "10, 40"
8fd843
+check s "22-24, 26-28" "20-30, 20-30"
8fd843
+check s 21-29 20-30
8fd843
 
8fd843
 # mixed single elements and ranges
8fd843
-check "10, 20" "10, 20-30"
8fd843
-check "10, 22" "10, 20-30"
8fd843
-check "10, 22-24" "10, 20-30"
8fd843
+check s "10, 20" "10, 20-30"
8fd843
+check s "10, 22" "10, 20-30"
8fd843
+check s "10, 22-24" "10, 20-30"
8fd843
 
8fd843
 # non-existing ranges matching elements
8fd843
-check 10-40 ""
8fd843
-check 10-20 ""
8fd843
-check 10-25 ""
8fd843
-check 25-55 ""
8fd843
+check s 10-40 ""
8fd843
+check s 10-20 ""
8fd843
+check s 10-25 ""
8fd843
+check s 25-55 ""
8fd843
+
8fd843
+# playing with IPs, ranges and prefixes
8fd843
+check ips 10.0.0.1 10.0.0.1
8fd843
+check ips 10.0.0.2 ""
8fd843
+check ips 10.0.1.0/24 10.0.1.0/24
8fd843
+check ips 10.0.1.2/31 10.0.1.0/24
8fd843
+check ips 10.0.1.0 10.0.1.0/24
8fd843
+check ips 10.0.1.3 10.0.1.0/24
8fd843
+check ips 10.0.1.255 10.0.1.0/24
8fd843
+check ips 10.0.2.3-10.0.2.12 10.0.2.3-10.0.2.12
8fd843
+check ips 10.0.2.10 10.0.2.3-10.0.2.12
8fd843
+check ips 10.0.2.12 10.0.2.3-10.0.2.12
8fd843
+
8fd843
+# test concatenated ranges, i.e. Pi, Pa and Po
8fd843
+check cs "10.0.0.1 . 22" "10.0.0.1 . 22"
8fd843
+check cs "10.0.0.1 . 23" ""
8fd843
+check cs "10.0.0.2 . 22" ""
8fd843
+check cs "10.1.0.1 . 42" "10.1.0.0/16 . 1-1024"
8fd843
+check cs "10.1.1.0/24 . 10-20" "10.1.0.0/16 . 1-1024"
8fd843
+check cs "10.2.0.3 . 20000" "10.2.0.1-10.2.0.8 . 1024-65535"
8fd843
 
8fd843
 exit $RC
8fd843
-- 
8fd843
1.8.3.1
8fd843