From: Pablo Neira Ayuso <pablo@netfilter.org>
To: "Carlos Falgueras García" <carlosfg@riseup.net>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH 1/2, libnftnl] tests: Consolidate printing error utilities
Date: Fri, 12 Aug 2016 01:26:23 +0200 [thread overview]
Message-ID: <20160811232623.GA5108@salvia> (raw)
In-Reply-To: <20160811132507.17842-1-carlosfg@riseup.net>
On Thu, Aug 11, 2016 at 03:25:06PM +0200, Carlos Falgueras García wrote:
> Created libtest.[hc] in order to consolidate code that is repeated in all
> tests.
Please, use present tense, eg.
This patch adds libtest.c and libtest.h to reduce test code and
consolidate it.
> diff --git a/tests/libtest.c b/tests/libtest.c
> new file mode 100644
> index 0000000..91f2d5e
> --- /dev/null
> +++ b/tests/libtest.c
> @@ -0,0 +1,49 @@
> +#include <libtest.h>
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <stdarg.h>
> +#include <string.h>
> +#include <errno.h>
> +#include <stdbool.h>
> +
> +#define COLOR_RED "\x1b[31m"
> +#define COLOR_GREEN "\x1b[32m"
> +#define COLOR_RESET "\x1b[0m"
> +
> +static bool test_ok = true;
I don't like this internal test_ok.
> +void __oom(const char *prog, const char *file, int line)
> +{
> + fprintf(stderr,
> + COLOR_RED "OOM" COLOR_RESET " at %s:%d\n\t%s\n", file, line,
> + strerror(errno));
> +
> + test_ok = false;
> + test_exit(prog);
> + exit(EXIT_FAILURE);
> +}
> +
> +void print_err(const char *fmt, ...)
> +{
> + va_list args;
> +
> + fprintf(stderr, COLOR_RED "ERROR: " COLOR_RESET);
> + va_start(args, fmt);
> + vfprintf(stderr, fmt, args);
> + va_end(args);
> + fprintf(stderr, "\n");
> +
> + test_ok = false;
> +}
> +
> +int test_exit(const char *prog)
> +{
> + if (test_ok) {
> + printf("%s: " COLOR_GREEN "OK\n" COLOR_RESET, prog);
> + return EXIT_SUCCESS;
> + } else {
> + printf("%s: " COLOR_RED "FAIL\n" COLOR_RESET, prog);
> + return EXIT_FAILURE;
> + }
Looks better like this?
switch (test_ok) {
case true:
printf("%s: " COLOR_GREEN "OK\n" COLOR_RESET, prog);
return EXIT_SUCCESS;
case false:
printf("%s: " COLOR_RED "FAIL\n" COLOR_RESET, prog);
return EXIT_FAILURE;
}
> +}
> diff --git a/tests/libtest.h b/tests/libtest.h
> new file mode 100644
> index 0000000..810bd82
> --- /dev/null
> +++ b/tests/libtest.h
> @@ -0,0 +1,9 @@
> +#ifndef _TESTS_UTILS_H
> +#define _TESTS_UTILS_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);
> +
> +#endif
> diff --git a/tests/nft-chain-test.c b/tests/nft-chain-test.c
> index d678d46..82431c2 100644
> --- a/tests/nft-chain-test.c
> +++ b/tests/nft-chain-test.c
> @@ -15,13 +15,7 @@
> #include <linux/netfilter/nf_tables.h>
> #include <libnftnl/chain.h>
>
> -static int test_ok = 1;
> -
> -static void print_err(const char *msg)
> -{
> - test_ok = 0;
> - printf("\033[31mERROR:\e[0m %s\n", msg);
> -}
> +#include <libtest.h>
Please, use:
#include "libtest.h" instead, I prefer it so we know this is locally
declare it.
> static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b)
> {
> @@ -73,7 +67,7 @@ int main(int argc, char *argv[])
> a = nftnl_chain_alloc();
> b = nftnl_chain_alloc();
> if (a == NULL || b == NULL)
> - print_err("OOM");
> + oom(argv[0]);
You can replace this above by:
test_assert(a != NULL);
test_assert(b != NULL);
> nftnl_chain_set_str(a, NFTNL_CHAIN_NAME, "test");
> nftnl_chain_set_u32(a, NFTNL_CHAIN_FAMILY, AF_INET);
> @@ -101,10 +95,6 @@ int main(int argc, char *argv[])
> nftnl_chain_free(a);
> nftnl_chain_free(b);
>
> - if (!test_ok)
> - exit(EXIT_FAILURE);
> -
> - printf("%s: \033[32mOK\e[0m\n", argv[0]);
> - return EXIT_SUCCESS;
> + return test_exit(argv[0]);
Better name, instead of test_exit:
return test_report(argv[0]);
next prev parent reply other threads:[~2016-08-11 23:26 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
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 ` Pablo Neira Ayuso [this message]
2016-08-12 20:16 ` [PATCH 1/2, libnftnl] tests: Consolidate printing error utilities 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=20160811232623.GA5108@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).