| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Test various "flags" properties in JSON syntax: |
| 4 |
# - single item arrays are abbreviated as non-array in output |
| 5 |
# - both non-array and single item array accepted in input |
| 6 |
# - single and multiple item values are correctly printed in output and |
| 7 |
# recognized in input (checked against standard syntax input/output) |
| 8 |
|
| 9 |
# NFT_TEST_REQUIRES(NFT_TEST_HAVE_json) |
| 10 |
# NFT_TEST_REQUIRES(NFT_TEST_HAVE_table_flag_persist) |
| 11 |
|
| 12 |
set -e |
| 13 |
|
| 14 |
json_sanitize() { |
| 15 |
sed -e 's/{"metainfo": {[^}]*}}, //' \ |
| 16 |
-e 's/\("handle":\) [0-9]*/\1 0/g' |
| 17 |
} |
| 18 |
back_n_forth() { # (std, json) |
| 19 |
$NFT flush ruleset |
| 20 |
$NFT -f - <<< "$1" |
| 21 |
diff --label "line ${BASH_LINENO[0]}: JSON output" \ |
| 22 |
--label "line ${BASH_LINENO[0]}: JSON expect" \ |
| 23 |
-u <($NFT -j list ruleset | json_sanitize) <(echo "$2") |
| 24 |
|
| 25 |
$NFT flush ruleset |
| 26 |
$NFT -j -f - <<< "$2" |
| 27 |
diff --label "line ${BASH_LINENO[0]}: std output" \ |
| 28 |
--label "line ${BASH_LINENO[0]}: std expect" \ |
| 29 |
-u <($NFT list ruleset) <(echo "$1") |
| 30 |
} |
| 31 |
json_equiv() { # (json_in, json_out) |
| 32 |
$NFT flush ruleset |
| 33 |
$NFT -j -f - <<< "$1" |
| 34 |
diff --label "line ${BASH_LINENO[0]}: JSON equiv output" \ |
| 35 |
--label "line ${BASH_LINENO[0]}: JSON equiv expect" \ |
| 36 |
-u <($NFT -j list ruleset | json_sanitize) <(echo "$2") |
| 37 |
} |
| 38 |
|
| 39 |
# |
| 40 |
# test table flags |
| 41 |
# |
| 42 |
|
| 43 |
STD_TABLE_1="table ip t { |
| 44 |
flags dormant |
| 45 |
}" |
| 46 |
JSON_TABLE_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0, "flags": "dormant"}}]}' |
| 47 |
JSON_TABLE_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_TABLE_1") |
| 48 |
|
| 49 |
STD_TABLE_2=$(sed 's/\(flags dormant\)/\1,persist/' <<< "$STD_TABLE_1") |
| 50 |
JSON_TABLE_2=$(sed 's/\("dormant"\)/\1, "persist"/' <<< "$JSON_TABLE_1_EQUIV") |
| 51 |
|
| 52 |
back_n_forth "$STD_TABLE_1" "$JSON_TABLE_1_EQUIV" |
| 53 |
json_equiv "$JSON_TABLE_1" "$JSON_TABLE_1_EQUIV" |
| 54 |
back_n_forth "$STD_TABLE_2" "$JSON_TABLE_2" |
| 55 |
|
| 56 |
# |
| 57 |
# test set flags |
| 58 |
# |
| 59 |
|
| 60 |
STD_SET_1="table ip t { |
| 61 |
set s { |
| 62 |
type inet_proto |
| 63 |
flags interval |
| 64 |
} |
| 65 |
}" |
| 66 |
JSON_SET_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0}}, {"set": {"family": "ip", "name": "s", "table": "t", "type": "inet_proto", "handle": 0, "flags": "interval"}}]}' |
| 67 |
JSON_SET_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_SET_1") |
| 68 |
|
| 69 |
STD_SET_2=$(sed 's/\(flags interval\)/\1,timeout/' <<< "$STD_SET_1") |
| 70 |
JSON_SET_2=$(sed 's/\("interval"\)/\1, "timeout"/' <<< "$JSON_SET_1_EQUIV") |
| 71 |
|
| 72 |
back_n_forth "$STD_SET_1" "$JSON_SET_1_EQUIV" |
| 73 |
json_equiv "$JSON_SET_1" "$JSON_SET_1_EQUIV" |
| 74 |
back_n_forth "$STD_SET_2" "$JSON_SET_2" |
| 75 |
|
| 76 |
# |
| 77 |
# test fib expression flags |
| 78 |
# |
| 79 |
|
| 80 |
STD_FIB_1="table ip t { |
| 81 |
chain c { |
| 82 |
fib saddr check exists |
| 83 |
} |
| 84 |
}" |
| 85 |
JSON_FIB_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0}}, {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0}}, {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"match": {"op": "==", "left": {"fib": {"result": "oif", "flags": "saddr"}}, "right": true}}]}}]}' |
| 86 |
JSON_FIB_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_FIB_1") |
| 87 |
|
| 88 |
STD_FIB_2=$(sed 's/\(fib saddr\)/\1 . iif/' <<< "$STD_FIB_1") |
| 89 |
JSON_FIB_2=$(sed 's/\("saddr"\)/\1, "iif"/' <<< "$JSON_FIB_1_EQUIV") |
| 90 |
|
| 91 |
back_n_forth "$STD_FIB_1" "$JSON_FIB_1_EQUIV" |
| 92 |
json_equiv "$JSON_FIB_1" "$JSON_FIB_1_EQUIV" |
| 93 |
back_n_forth "$STD_FIB_2" "$JSON_FIB_2" |
| 94 |
|
| 95 |
# |
| 96 |
# test nat statement flags |
| 97 |
# |
| 98 |
|
| 99 |
STD_NAT_1="table ip t { |
| 100 |
chain c { |
| 101 |
dnat to 192.168.0.0/24 persistent |
| 102 |
} |
| 103 |
}" |
| 104 |
JSON_NAT_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0}}, {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0}}, {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"dnat": {"addr": {"prefix": {"addr": "192.168.0.0", "len": 24}}, "flags": "persistent"}}]}}]}' |
| 105 |
JSON_NAT_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_NAT_1") |
| 106 |
|
| 107 |
STD_NAT_2=$(sed 's/\(persistent\)/random,\1/' <<< "$STD_NAT_1") |
| 108 |
JSON_NAT_2=$(sed 's/\("persistent"\)/"random", \1/' <<< "$JSON_NAT_1_EQUIV") |
| 109 |
|
| 110 |
back_n_forth "$STD_NAT_1" "$JSON_NAT_1_EQUIV" |
| 111 |
json_equiv "$JSON_NAT_1" "$JSON_NAT_1_EQUIV" |
| 112 |
back_n_forth "$STD_NAT_2" "$JSON_NAT_2" |
| 113 |
|
| 114 |
# |
| 115 |
# test log statement flags |
| 116 |
# |
| 117 |
|
| 118 |
STD_LOG_1="table ip t { |
| 119 |
chain c { |
| 120 |
log flags tcp sequence |
| 121 |
} |
| 122 |
}" |
| 123 |
JSON_LOG_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0}}, {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0}}, {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"log": {"flags": "tcp sequence"}}]}}]}' |
| 124 |
JSON_LOG_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_LOG_1") |
| 125 |
|
| 126 |
STD_LOG_2=$(sed 's/\(tcp sequence\)/\1,options/' <<< "$STD_LOG_1") |
| 127 |
JSON_LOG_2=$(sed 's/\("tcp sequence"\)/\1, "tcp options"/' <<< "$JSON_LOG_1_EQUIV") |
| 128 |
|
| 129 |
back_n_forth "$STD_LOG_1" "$JSON_LOG_1_EQUIV" |
| 130 |
json_equiv "$JSON_LOG_1" "$JSON_LOG_1_EQUIV" |
| 131 |
back_n_forth "$STD_LOG_2" "$JSON_LOG_2" |
| 132 |
|
| 133 |
# |
| 134 |
# test synproxy statement flags |
| 135 |
# |
| 136 |
|
| 137 |
STD_SYNPROXY_1="table ip t { |
| 138 |
chain c { |
| 139 |
synproxy sack-perm |
| 140 |
} |
| 141 |
}" |
| 142 |
JSON_SYNPROXY_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0}}, {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0}}, {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"synproxy": {"flags": "sack-perm"}}]}}]}' |
| 143 |
JSON_SYNPROXY_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_SYNPROXY_1") |
| 144 |
|
| 145 |
STD_SYNPROXY_2=$(sed 's/\(sack-perm\)/timestamp \1/' <<< "$STD_SYNPROXY_1") |
| 146 |
JSON_SYNPROXY_2=$(sed 's/\("sack-perm"\)/"timestamp", \1/' <<< "$JSON_SYNPROXY_1_EQUIV") |
| 147 |
|
| 148 |
back_n_forth "$STD_SYNPROXY_1" "$JSON_SYNPROXY_1_EQUIV" |
| 149 |
json_equiv "$JSON_SYNPROXY_1" "$JSON_SYNPROXY_1_EQUIV" |
| 150 |
back_n_forth "$STD_SYNPROXY_2" "$JSON_SYNPROXY_2" |
| 151 |
|
| 152 |
# |
| 153 |
# test synproxy object flags |
| 154 |
# |
| 155 |
|
| 156 |
STD_SYNPROXY_OBJ_1="table ip t { |
| 157 |
synproxy s { |
| 158 |
mss 1280 |
| 159 |
wscale 64 |
| 160 |
sack-perm |
| 161 |
} |
| 162 |
}" |
| 163 |
JSON_SYNPROXY_OBJ_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0}}, {"synproxy": {"family": "ip", "name": "s", "table": "t", "handle": 0, "mss": 1280, "wscale": 64, "flags": "sack-perm"}}]}' |
| 164 |
JSON_SYNPROXY_OBJ_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_SYNPROXY_OBJ_1") |
| 165 |
|
| 166 |
STD_SYNPROXY_OBJ_2=$(sed 's/ \(sack-perm\)/timestamp \1/' <<< "$STD_SYNPROXY_OBJ_1") |
| 167 |
JSON_SYNPROXY_OBJ_2=$(sed 's/\("sack-perm"\)/"timestamp", \1/' <<< "$JSON_SYNPROXY_OBJ_1_EQUIV") |
| 168 |
|
| 169 |
back_n_forth "$STD_SYNPROXY_OBJ_1" "$JSON_SYNPROXY_OBJ_1_EQUIV" |
| 170 |
json_equiv "$JSON_SYNPROXY_OBJ_1" "$JSON_SYNPROXY_OBJ_1_EQUIV" |
| 171 |
back_n_forth "$STD_SYNPROXY_OBJ_2" "$JSON_SYNPROXY_OBJ_2" |
| 172 |
|
| 173 |
# |
| 174 |
# test queue statement flags |
| 175 |
# |
| 176 |
|
| 177 |
STD_QUEUE_1="table ip t { |
| 178 |
chain c { |
| 179 |
queue flags bypass to 1-10 |
| 180 |
} |
| 181 |
}" |
| 182 |
JSON_QUEUE_1='{"nftables": [{"table": {"family": "ip", "name": "t", "handle": 0}}, {"chain": {"family": "ip", "table": "t", "name": "c", "handle": 0}}, {"rule": {"family": "ip", "table": "t", "chain": "c", "handle": 0, "expr": [{"queue": {"num": {"range": [1, 10]}, "flags": "bypass"}}]}}]}' |
| 183 |
JSON_QUEUE_1_EQUIV=$(sed 's/\("flags":\) \([^}]*\)/\1 [\2]/' <<< "$JSON_QUEUE_1") |
| 184 |
|
| 185 |
STD_QUEUE_2=$(sed 's/\(bypass\)/\1,fanout/' <<< "$STD_QUEUE_1") |
| 186 |
JSON_QUEUE_2=$(sed 's/\("bypass"\)/\1, "fanout"/' <<< "$JSON_QUEUE_1_EQUIV") |
| 187 |
|
| 188 |
back_n_forth "$STD_QUEUE_1" "$JSON_QUEUE_1_EQUIV" |
| 189 |
json_equiv "$JSON_QUEUE_1" "$JSON_QUEUE_1_EQUIV" |
| 190 |
back_n_forth "$STD_QUEUE_2" "$JSON_QUEUE_2" |