netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: "Carlos Falgueras García" <carlosfg@riseup.net>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH 2/2, libnftnl] Use libnftnl comparators in all tests
Date: Fri, 12 Aug 2016 01:32:22 +0200	[thread overview]
Message-ID: <20160811233222.GB5108@salvia> (raw)
In-Reply-To: <20160811132507.17842-2-carlosfg@riseup.net>

On Thu, Aug 11, 2016 at 03:25:07PM +0200, Carlos Falgueras García wrote:
> Use 'nftnl_expr_cmp' and 'nftnl_rule_cmp' in all tests instead of custom
> comparator for each one. If objects differ both are printed.
> 
> Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
> ---
>  tests/libtest.c                 | 22 +++++++++++++++++++++
>  tests/libtest.h                 |  9 +++++++++
>  tests/nft-expr_bitwise-test.c   | 29 +++------------------------
>  tests/nft-expr_byteorder-test.c | 24 +++-------------------
>  tests/nft-expr_cmp-test.c       | 21 +++-----------------
>  tests/nft-expr_counter-test.c   | 15 +++-----------
>  tests/nft-expr_ct-test.c        | 17 +++-------------
>  tests/nft-expr_dup-test.c       | 15 +++-----------
>  tests/nft-expr_exthdr-test.c    | 21 +++-----------------
>  tests/nft-expr_fwd-test.c       | 12 +++--------
>  tests/nft-expr_immediate-test.c | 44 ++++++-----------------------------------
>  tests/nft-expr_limit-test.c     | 24 +++-------------------
>  tests/nft-expr_log-test.c       | 21 +++-----------------
>  tests/nft-expr_lookup-test.c    | 27 +++----------------------
>  tests/nft-expr_masq-test.c      | 18 +++--------------
>  tests/nft-expr_match-test.c     | 22 +++------------------
>  tests/nft-expr_meta-test.c      | 15 +++-----------
>  tests/nft-expr_nat-test.c       | 30 +++-------------------------
>  tests/nft-expr_payload-test.c   | 21 +++-----------------
>  tests/nft-expr_queue-test.c     | 15 +++-----------
>  tests/nft-expr_redir-test.c     | 18 +++--------------
>  tests/nft-expr_reject-test.c    | 15 +++-----------
>  tests/nft-expr_target-test.c    | 22 +++------------------
>  tests/nft-rule-test.c           | 38 +++--------------------------------
>  24 files changed, 100 insertions(+), 415 deletions(-)
> 
> diff --git a/tests/libtest.c b/tests/libtest.c
> index 91f2d5e..9ce38ff 100644
> --- a/tests/libtest.c
> +++ b/tests/libtest.c
> @@ -11,6 +11,8 @@
>  #define COLOR_GREEN   "\x1b[32m"
>  #define COLOR_RESET   "\x1b[0m"
>  
> +#define SNPRINTF_BUFF_LEN 1024
> +
>  static bool test_ok = true;
>  
>  void __oom(const char *prog, const char *file, int line)
> @@ -47,3 +49,23 @@ int test_exit(const char *prog)
>  		return EXIT_FAILURE;
>  	}
>  }

Better place the definition here, close to where it is used and
meaningful.

> +const char *rule2str(const struct nftnl_rule *r)
> +{
> +	static char buff[SNPRINTF_BUFF_LEN];
> +
> +	nftnl_rule_snprintf(buff, SNPRINTF_BUFF_LEN, r,
> +			    NFTNL_OUTPUT_DEFAULT, 0);

This fits into one line:

	nftnl_rule_snprintf(buf, sizeof(buf), r, NFTNL_OUTPUT_DEFAULT, 0);

> +
> +	return buff;
> +}
> +
> +const char *expr2str(const struct nftnl_expr *e)
> +{
> +	static char buff[SNPRINTF_BUFF_LEN];
> +
> +	nftnl_expr_snprintf(buff, SNPRINTF_BUFF_LEN, e,
> +			    NFTNL_OUTPUT_DEFAULT, 0);
> +
> +	return buff;
> +}
> diff --git a/tests/libtest.h b/tests/libtest.h
> index 810bd82..af469ce 100644
> --- a/tests/libtest.h
> +++ b/tests/libtest.h
> @@ -1,9 +1,18 @@
>  #ifndef _TESTS_UTILS_H
>  #define _TESTS_UTILS_H
>  
> +#include <stdbool.h>
> +#include <libmnl/libmnl.h>
> +#include <libnftnl/common.h>
> +#include <libnftnl/rule.h>
> +#include <libnftnl/expr.h>
> +
>  #define oom(prog) __oom(prog, __FILE__, __LINE__)
>  void __oom(const char *prog, const char *file, int line);
>  void print_err(const char *fmt, ...);
>  int test_exit(const char *prog);
>  
> +const char *rule2str(const struct nftnl_rule *r);
> +const char *expr2str(const struct nftnl_expr *e);
> +
>  #endif
> diff --git a/tests/nft-expr_bitwise-test.c b/tests/nft-expr_bitwise-test.c
> index bf76cb2..a661be6 100644
> --- a/tests/nft-expr_bitwise-test.c
> +++ b/tests/nft-expr_bitwise-test.c
> @@ -21,31 +21,6 @@
>  
>  #include <libtest.h>
>  
> -static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
> -			      struct nftnl_expr *rule_b)
> -{
> -	uint32_t maska, maskb;
> -	uint32_t xora, xorb;
> -
> -	if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_BITWISE_DREG) !=
> -	    nftnl_expr_get_u32(rule_b, NFTNL_EXPR_BITWISE_DREG))
> -		print_err("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("Expr BITWISE_SREG mismatches");
> -	if (nftnl_expr_get_u16(rule_a, NFTNL_EXPR_BITWISE_LEN) !=
> -	    nftnl_expr_get_u16(rule_b, NFTNL_EXPR_BITWISE_LEN))
> -		print_err("Expr BITWISE_DREG mismatches");
> -	nftnl_expr_get(rule_a, NFTNL_EXPR_BITWISE_MASK, &maska);
> -	nftnl_expr_get(rule_b, NFTNL_EXPR_BITWISE_MASK, &maskb);
> -	if (maska != maskb)
> -		print_err("Size of BITWISE_MASK mismatches");
> -	nftnl_expr_get(rule_a, NFTNL_EXPR_BITWISE_XOR, &xora);
> -	nftnl_expr_get(rule_b, NFTNL_EXPR_BITWISE_XOR, &xorb);
> -	if (xora != xorb)
> -		print_err("Size of BITWISE_XOR mismatches");
> -
> -}
>  int main(int argc, char *argv[])
>  {
>  	struct nftnl_rule *a, *b = NULL;
> @@ -96,7 +71,9 @@ int main(int argc, char *argv[])
>  	nftnl_expr_iter_destroy(iter_a);
>  	nftnl_expr_iter_destroy(iter_b);
>  
> -	cmp_nftnl_expr(rule_a,rule_b);
> +	if (!nftnl_expr_cmp(rule_a, rule_b))
> +		print_err("expressions mismatches:\n\texpr 1: %s\n\texpr 2: %s",
> +			  expr2str(rule_a), expr2str(rule_b));

Better, add something like

                print_expr_mismatch(rule_a, rule_b);

This should be "expressions mismatch" instead of "expressions
mismatches".

  reply	other threads:[~2016-08-11 23:32 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-11 13:25 [PATCH 1/2, libnftnl] tests: Consolidate printing error utilities Carlos Falgueras García
2016-08-11 13:25 ` [PATCH 2/2, libnftnl] Use libnftnl comparators in all tests Carlos Falgueras García
2016-08-11 23:32   ` Pablo Neira Ayuso [this message]
2016-08-12 20:16     ` Carlos Falgueras García
2016-08-15 12:23       ` [PATCH 1/2 v2 libnftnl] tests: Consolidate printing error utilities Carlos Falgueras García
2016-08-15 12:23         ` [PATCH 2/2 v2 libnftnl] test: Use libnftnl comparators in all tests Carlos Falgueras García
2016-08-15 12:27           ` Pablo Neira Ayuso
2016-08-16 10:30             ` [PATCH 1/3 v3 nft] tests: Consolidate printing error utilities Carlos Falgueras García
2016-08-16 10:30               ` [PATCH 2/3 v3 nft] tests: Use libnftnl comparators in all tests Carlos Falgueras García
2016-08-16 11:58                 ` Pablo Neira Ayuso
2016-08-16 10:30               ` [PATCH 3/3 v3 nft] tests: Elimine static variable 'test_ok' Carlos Falgueras García
2016-08-12 20:17     ` [PATCH 1/4, V2, libnftnl] tests: Fix segfaults due outbound access Carlos Falgueras García
2016-08-12 20:17       ` [PATCH 2/4, V2, libnftnl] tests: Fix wrong expression creation Carlos Falgueras García
2016-08-13 10:25         ` Pablo Neira Ayuso
2016-08-12 20:17       ` [PATCH 3/4, V2, libnftnl] tests: Consolidate printing error utilities Carlos Falgueras García
2016-08-12 20:17       ` [PATCH 4/4, V2, libnftnl] tests: Use libnftnl comparators in all tests Carlos Falgueras García
2016-08-13 10:12       ` [PATCH 1/4, V2, libnftnl] tests: Fix segfaults due outbound access Pablo Neira Ayuso
2016-08-13 15:25         ` Carlos Falgueras García
2016-08-15  9:12           ` Pablo Neira Ayuso
2016-08-15 10:27             ` [PATCH 1/2 libnftnl] expr: Improve bound checking in stringification functions Carlos Falgueras García
2016-08-15 10:27               ` [PATCH 2/2 libnftnl] expr: cmp: Use cmp2str() instead of directly access to array Carlos Falgueras García
2016-08-15 10:32                 ` Pablo Neira Ayuso
2016-08-15 10:51                   ` [PATCH 1/2 libnftnl] utils: Fix out of bound access in nftnl_family2str Carlos Falgueras García
2016-08-15 10:51                     ` [PATCH 2/2 libnfntl] expr: cmp: Use cmp2str() instead of directly access to array Carlos Falgueras García
2016-08-15 11:49                       ` Pablo Neira Ayuso
2016-08-15 11:03                     ` [PATCH 1/2 libnftnl] utils: Fix out of bound access in nftnl_family2str Pablo Neira Ayuso
2016-08-15 11:45                       ` Carlos Falgueras García
2016-08-15 11:46                         ` Pablo Neira Ayuso
2016-08-15 10:32               ` [PATCH 1/2 libnftnl] expr: Improve bound checking in stringification functions Pablo Neira Ayuso
2016-08-11 23:26 ` [PATCH 1/2, libnftnl] tests: Consolidate printing error utilities Pablo Neira Ayuso
2016-08-12 20:16   ` Carlos Falgueras García

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160811233222.GB5108@salvia \
    --to=pablo@netfilter.org \
    --cc=carlosfg@riseup.net \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).