]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
tests: bitwise: add tests for new boolean operations
authorJeremy Sowden <jeremy@azazel.net>
Tue, 19 Nov 2024 15:08:47 +0000 (16:08 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 19 Nov 2024 15:08:47 +0000 (16:08 +0100)
We already have tests for mask-and-xor operations with constant RHS
operands.  Add tests for new operations with variable RHS operands.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
tests/nft-expr_bitwise-test.c

index 0f3c26d2495ac2fde853ea04628a38fb7bcd4988..784619e1d9c112a98c889b82f3f0b9c6708e69b1 100644 (file)
@@ -196,6 +196,99 @@ static void test_rshift(void)
        test_shift(NFT_BITWISE_RSHIFT);
 }
 
+static void cmp_nftnl_expr_bool(const char *opname,
+                               const struct nftnl_expr *rule_a,
+                               const struct nftnl_expr *rule_b)
+{
+       if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_BITWISE_DREG) !=
+           nftnl_expr_get_u32(rule_b, NFTNL_EXPR_BITWISE_DREG))
+               print_err(opname, "Expr BITWISE_DREG mismatches");
+       if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_BITWISE_SREG) !=
+           nftnl_expr_get_u32(rule_b, NFTNL_EXPR_BITWISE_SREG))
+               print_err(opname, "Expr BITWISE_SREG mismatches");
+       if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_BITWISE_SREG2) !=
+           nftnl_expr_get_u32(rule_b, NFTNL_EXPR_BITWISE_SREG2))
+               print_err(opname, "Expr BITWISE_SREG2 mismatches");
+       if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_BITWISE_OP) !=
+           nftnl_expr_get_u32(rule_b, NFTNL_EXPR_BITWISE_OP))
+               print_err(opname, "Expr BITWISE_OP mismatches");
+       if (nftnl_expr_get_u16(rule_a, NFTNL_EXPR_BITWISE_LEN) !=
+           nftnl_expr_get_u16(rule_b, NFTNL_EXPR_BITWISE_LEN))
+               print_err(opname, "Expr BITWISE_LEN mismatches");
+}
+
+static void test_bool(enum nft_bitwise_ops op)
+{
+       struct nftnl_rule *a, *b;
+       struct nftnl_expr *ex;
+       struct nlmsghdr *nlh;
+       char buf[4096];
+       struct nftnl_expr_iter *iter_a, *iter_b;
+       struct nftnl_expr *rule_a, *rule_b;
+       const char *opname =
+               op == NFT_BITWISE_AND ? "and" :
+               op == NFT_BITWISE_OR  ? "or"  : "xor";
+
+       a = nftnl_rule_alloc();
+       b = nftnl_rule_alloc();
+       if (a == NULL || b == NULL)
+               print_err(opname, "OOM");
+       ex = nftnl_expr_alloc("bitwise");
+       if (ex == NULL)
+               print_err(opname, "OOM");
+
+       nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_SREG, 0x12345678);
+       nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_SREG2, 0x90abcdef);
+       nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_DREG, 0x78123456);
+       nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_OP, op);
+       nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_LEN, 0x56781234);
+
+       nftnl_rule_add_expr(a, ex);
+
+       nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
+       nftnl_rule_nlmsg_build_payload(nlh, a);
+
+       if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
+               print_err(opname, "parsing problems");
+
+       iter_a = nftnl_expr_iter_create(a);
+       iter_b = nftnl_expr_iter_create(b);
+       if (iter_a == NULL || iter_b == NULL)
+               print_err(opname, "OOM");
+
+       rule_a = nftnl_expr_iter_next(iter_a);
+       rule_b = nftnl_expr_iter_next(iter_b);
+       if (rule_a == NULL || rule_b == NULL)
+               print_err(opname, "OOM");
+
+       if (nftnl_expr_iter_next(iter_a) != NULL ||
+           nftnl_expr_iter_next(iter_b) != NULL)
+               print_err(opname, "More 1 expr.");
+
+       nftnl_expr_iter_destroy(iter_a);
+       nftnl_expr_iter_destroy(iter_b);
+
+       cmp_nftnl_expr_bool(opname, rule_a, rule_b);
+
+       nftnl_rule_free(a);
+       nftnl_rule_free(b);
+}
+
+static void test_and(void)
+{
+       test_bool(NFT_BITWISE_AND);
+}
+
+static void test_or(void)
+{
+       test_bool(NFT_BITWISE_OR);
+}
+
+static void test_xor(void)
+{
+       test_bool(NFT_BITWISE_XOR);
+}
+
 int main(int argc, char *argv[])
 {
        test_mask_xor();
@@ -210,6 +303,18 @@ int main(int argc, char *argv[])
        if (!test_ok)
                exit(EXIT_FAILURE);
 
+       test_and();
+       if (!test_ok)
+               exit(EXIT_FAILURE);
+
+       test_or();
+       if (!test_ok)
+               exit(EXIT_FAILURE);
+
+       test_xor();
+       if (!test_ok)
+               exit(EXIT_FAILURE);
+
        printf("%s: \033[32mOK\e[0m\n", argv[0]);
        return EXIT_SUCCESS;
 }