diff options
| author | Phil Sutter <phil@nwl.cc> | 2021-08-30 14:38:27 +0200 |
|---|---|---|
| committer | Phil Sutter <phil@nwl.cc> | 2024-10-29 23:26:43 +0100 |
| commit | 0576274ad54a3aae2750e7a49bed1e0f406b61cc (patch) | |
| tree | 103b004cdb5c58529e253c873c0c47d08afc7aff /tests | |
| parent | faab4a300784cc04f35a0cf40b7fdb858a7ece3e (diff) | |
Introduce struct nftnl_str_array
This data structure holds an array of allocated strings for use in
nftnl_chain and nftnl_flowtable structs. For convenience, implement
functions to clear, populate and iterate over contents.
While at it, extend chain and flowtable tests to cover these attributes,
too.
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/nft-chain-test.c | 37 | ||||
| -rw-r--r-- | tests/nft-flowtable-test.c | 21 |
2 files changed, 56 insertions, 2 deletions
diff --git a/tests/nft-chain-test.c b/tests/nft-chain-test.c index 35a65be..64c506e 100644 --- a/tests/nft-chain-test.c +++ b/tests/nft-chain-test.c @@ -23,9 +23,25 @@ static void print_err(const char *msg) printf("\033[31mERROR:\e[0m %s\n", msg); } -static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b) +static void cmp_devices(const char * const *adevs, + const char * const *bdevs) { + int i; + + if (!adevs && !bdevs) + return; + if (!!adevs ^ !!bdevs) + print_err("Chain devices mismatches"); + for (i = 0; adevs[i] && bdevs[i]; i++) { + if (strcmp(adevs[i], bdevs[i])) + break; + } + if (adevs[i] || bdevs[i]) + print_err("Chain devices mismatches"); +} +static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b) +{ if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_NAME), nftnl_chain_get_str(b, NFTNL_CHAIN_NAME)) != 0) print_err("Chain name mismatches"); @@ -59,13 +75,17 @@ static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b) if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_TYPE), nftnl_chain_get_str(b, NFTNL_CHAIN_TYPE)) != 0) print_err("Chain type mismatches"); - if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_DEV), + if (nftnl_chain_is_set(a, NFTNL_CHAIN_DEV) && + strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_DEV), nftnl_chain_get_str(b, NFTNL_CHAIN_DEV)) != 0) print_err("Chain device mismatches"); + cmp_devices(nftnl_chain_get_array(a, NFTNL_CHAIN_DEVICES), + nftnl_chain_get_array(b, NFTNL_CHAIN_DEVICES)); } int main(int argc, char *argv[]) { + const char *devs[] = { "eth0", "eth1", "eth2", NULL }; struct nftnl_chain *a, *b; char buf[4096]; struct nlmsghdr *nlh; @@ -97,6 +117,19 @@ int main(int argc, char *argv[]) cmp_nftnl_chain(a, b); + /* repeat test with multiple devices */ + + nftnl_chain_unset(a, NFTNL_CHAIN_DEV); + nftnl_chain_set_array(a, NFTNL_CHAIN_DEVICES, devs); + + nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWCHAIN, AF_INET, 0, 1234); + nftnl_chain_nlmsg_build_payload(nlh, a); + + if (nftnl_chain_nlmsg_parse(nlh, b) < 0) + print_err("parsing problems"); + + cmp_nftnl_chain(a, b); + nftnl_chain_free(a); nftnl_chain_free(b); diff --git a/tests/nft-flowtable-test.c b/tests/nft-flowtable-test.c index 8ab8d4c..49bc0a1 100644 --- a/tests/nft-flowtable-test.c +++ b/tests/nft-flowtable-test.c @@ -13,6 +13,23 @@ static void print_err(const char *msg) printf("\033[31mERROR:\e[0m %s\n", msg); } +static void cmp_devices(const char * const *adevs, + const char * const *bdevs) +{ + int i; + + if (!adevs && !bdevs) + return; + if (!!adevs ^ !!bdevs) + print_err("Flowtable devices mismatches"); + for (i = 0; adevs[i] && bdevs[i]; i++) { + if (strcmp(adevs[i], bdevs[i])) + break; + } + if (adevs[i] || bdevs[i]) + print_err("Flowtable devices mismatches"); +} + static void cmp_nftnl_flowtable(struct nftnl_flowtable *a, struct nftnl_flowtable *b) { if (strcmp(nftnl_flowtable_get_str(a, NFTNL_FLOWTABLE_NAME), @@ -44,10 +61,13 @@ static void cmp_nftnl_flowtable(struct nftnl_flowtable *a, struct nftnl_flowtabl if (nftnl_flowtable_get_u64(a, NFTNL_FLOWTABLE_HANDLE) != nftnl_flowtable_get_u64(b, NFTNL_FLOWTABLE_HANDLE)) print_err("Flowtable handle mismatches"); + cmp_devices(nftnl_flowtable_get_array(a, NFTNL_FLOWTABLE_DEVICES), + nftnl_flowtable_get_array(b, NFTNL_FLOWTABLE_DEVICES)); } int main(int argc, char *argv[]) { + const char *devs[] = { "eth0", "eth1", "eth2", NULL }; struct nftnl_flowtable *a, *b; char buf[4096]; struct nlmsghdr *nlh; @@ -66,6 +86,7 @@ int main(int argc, char *argv[]) nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_SIZE, 0x89016745); nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FLAGS, 0x45016723); nftnl_flowtable_set_u64(a, NFTNL_FLOWTABLE_HANDLE, 0x2345016789); + nftnl_flowtable_set_array(a, NFTNL_FLOWTABLE_DEVICES, devs); nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWFLOWTABLE, AF_INET, 0, 1234); |
