[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAEf4BzYQZCYZ7aXeSW2xJKLeQTvObiO5eabA5XvX34wF1NTBhw@mail.gmail.com>
Date: Mon, 26 Apr 2021 08:50:08 -0700
From: Andrii Nakryiko <andrii.nakryiko@...il.com>
To: Lorenz Bauer <lmb@...udflare.com>
Cc: Andrii Nakryiko <andrii@...nel.org>, bpf <bpf@...r.kernel.org>,
Networking <netdev@...r.kernel.org>,
Alexei Starovoitov <ast@...com>,
Daniel Borkmann <daniel@...earbox.net>,
Kernel Team <kernel-team@...com>
Subject: Re: [PATCH bpf-next 1/5] selftests/bpf: add remaining ASSERT_xxx() variants
On Mon, Apr 26, 2021 at 1:06 AM Lorenz Bauer <lmb@...udflare.com> wrote:
>
> On Sat, 24 Apr 2021 at 00:36, Andrii Nakryiko <andrii@...nel.org> wrote:
> >
> > Add ASSERT_TRUE/ASSERT_FALSE for conditions calculated with custom logic to
> > true/false. Also add remaining arithmetical assertions:
> > - ASSERT_LE -- less than or equal;
> > - ASSERT_GT -- greater than;
> > - ASSERT_GE -- greater than or equal.
> > This should cover most scenarios where people fall back to error-prone
> > CHECK()s.
> >
> > Also extend ASSERT_ERR() to print out errno, in addition to direct error.
> >
> > Also convert few CHECK() instances to ensure new ASSERT_xxx() variants work as
> > expected. Subsequent patch will also use ASSERT_TRUE/ASSERT_FALSE more
> > extensively.
> >
> > Signed-off-by: Andrii Nakryiko <andrii@...nel.org>
> > ---
> > .../selftests/bpf/prog_tests/btf_dump.c | 2 +-
> > .../selftests/bpf/prog_tests/btf_endian.c | 4 +-
> > .../selftests/bpf/prog_tests/cgroup_link.c | 2 +-
> > .../selftests/bpf/prog_tests/kfree_skb.c | 2 +-
> > .../selftests/bpf/prog_tests/resolve_btfids.c | 7 +--
> > .../selftests/bpf/prog_tests/snprintf_btf.c | 4 +-
> > tools/testing/selftests/bpf/test_progs.h | 50 ++++++++++++++++++-
> > 7 files changed, 56 insertions(+), 15 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
> > index c60091ee8a21..5e129dc2073c 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
> > @@ -77,7 +77,7 @@ static int test_btf_dump_case(int n, struct btf_dump_test_case *t)
> >
> > snprintf(out_file, sizeof(out_file), "/tmp/%s.output.XXXXXX", t->file);
> > fd = mkstemp(out_file);
> > - if (CHECK(fd < 0, "create_tmp", "failed to create file: %d\n", fd)) {
> > + if (!ASSERT_GE(fd, 0, "create_tmp")) {
>
> Nit: I would find ASSERT_LE easier to read here. Inverting boolean
> conditions is easy to get wrong.
You mean if (ASSERT_LE(fd, -1, "create_tmp")) { err = fd; goto done; } ?
That will mark the test failing if fd >= 0, which is exactly opposite
to what we wan't. It's confusing because CHECK() checks invalid
conditions and returns "true" if it holds. But ASSERT_xxx() checks
*valid* condition and returns whether valid condition holds. So the
pattern is always
if (CHECK(expr)) --> if (!ASSERT_xxx(!expr))
And it might feel awkward only when converting original inverted condition.
>
> > err = fd;
> > goto done;
> > }
> > diff --git a/tools/testing/selftests/bpf/prog_tests/btf_endian.c b/tools/testing/selftests/bpf/prog_tests/btf_endian.c
> > index 8c52d72c876e..8ab5d3e358dd 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/btf_endian.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/btf_endian.c
> > @@ -6,8 +6,6 @@
> > #include <test_progs.h>
> > #include <bpf/btf.h>
> >
> > -static int duration = 0;
>
> Good to see this go.
>
> Acked-by: Lorenz Bauer <lmb@...udflare.com>
>
> --
> Lorenz Bauer | Systems Engineer
> 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK
>
> www.cloudflare.com
Powered by blists - more mailing lists