Message ID | 20210423233058.3386115-2-andrii@kernel.org (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | BPF |
Headers | show |
Series | CO-RE relocation selftests fixes | expand |
On Sat, 24 Apr 2021 at 00:36, Andrii Nakryiko <andrii@kernel.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@kernel.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. > 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@cloudflare.com>
On Mon, Apr 26, 2021 at 1:06 AM Lorenz Bauer <lmb@cloudflare.com> wrote: > > On Sat, 24 Apr 2021 at 00:36, Andrii Nakryiko <andrii@kernel.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@kernel.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@cloudflare.com> > > -- > Lorenz Bauer | Systems Engineer > 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK > > www.cloudflare.com
Andrii Nakryiko <andrii.nakryiko@gmail.com> writes: > On Mon, Apr 26, 2021 at 1:06 AM Lorenz Bauer <lmb@cloudflare.com> wrote: >> >> On Sat, 24 Apr 2021 at 00:36, Andrii Nakryiko <andrii@kernel.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@kernel.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 There's already an ASSERT_OK_PTR(), so maybe a corresponding ASSERT_OK_FD() would be handy? -Toke
On Mon, Apr 26, 2021 at 8:59 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote: > > Andrii Nakryiko <andrii.nakryiko@gmail.com> writes: > > > On Mon, Apr 26, 2021 at 1:06 AM Lorenz Bauer <lmb@cloudflare.com> wrote: > >> > >> On Sat, 24 Apr 2021 at 00:36, Andrii Nakryiko <andrii@kernel.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@kernel.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 > > There's already an ASSERT_OK_PTR(), so maybe a corresponding > ASSERT_OK_FD() would be handy? I honestly don't see the point. OK_PTR is special, it checks NULL and the special ERR_PTR() variants, which is a lot of hassle to check manually. While for FD doing ASSERT_GE(fd, 0) seems to be fine and just mostly natural. Also for some APIs valid FD is > 0 and for other cases valid FD is plain >= 0, so that just adds to the confusion. > > -Toke >
Andrii Nakryiko <andrii.nakryiko@gmail.com> writes: > On Mon, Apr 26, 2021 at 8:59 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote: >> >> Andrii Nakryiko <andrii.nakryiko@gmail.com> writes: >> >> > On Mon, Apr 26, 2021 at 1:06 AM Lorenz Bauer <lmb@cloudflare.com> wrote: >> >> >> >> On Sat, 24 Apr 2021 at 00:36, Andrii Nakryiko <andrii@kernel.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@kernel.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 >> >> There's already an ASSERT_OK_PTR(), so maybe a corresponding >> ASSERT_OK_FD() would be handy? > > I honestly don't see the point. OK_PTR is special, it checks NULL and > the special ERR_PTR() variants, which is a lot of hassle to check > manually. While for FD doing ASSERT_GE(fd, 0) seems to be fine and > just mostly natural. Also for some APIs valid FD is > 0 and for other > cases valid FD is plain >= 0, so that just adds to the confusion. Alright, fair enough. I just wondered because I had the same feeling of slight awkwardness when I was writing an fd check the other day, so thought I'd air the thought; but as you say not *really* a big deal, so I'm also OK with just using LE or GE for this... -Toke
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")) { 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; - void test_btf_endian() { #if __BYTE_ORDER == __LITTLE_ENDIAN enum btf_endianness endian = BTF_LITTLE_ENDIAN; @@ -71,7 +69,7 @@ void test_btf_endian() { /* now modify original BTF */ var_id = btf__add_var(btf, "some_var", BTF_VAR_GLOBAL_ALLOCATED, 1); - CHECK(var_id <= 0, "var_id", "failed %d\n", var_id); + ASSERT_GT(var_id, 0, "var_id"); btf__free(swap_btf); swap_btf = NULL; diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_link.c b/tools/testing/selftests/bpf/prog_tests/cgroup_link.c index 4d9b514b3fd9..736796e56ed1 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_link.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_link.c @@ -54,7 +54,7 @@ void test_cgroup_link(void) for (i = 0; i < cg_nr; i++) { cgs[i].fd = create_and_get_cgroup(cgs[i].path); - if (CHECK(cgs[i].fd < 0, "cg_create", "fail: %d\n", cgs[i].fd)) + if (!ASSERT_GE(cgs[i].fd, 0, "cg_create")) goto cleanup; } diff --git a/tools/testing/selftests/bpf/prog_tests/kfree_skb.c b/tools/testing/selftests/bpf/prog_tests/kfree_skb.c index 42c3a3103c26..d65107919998 100644 --- a/tools/testing/selftests/bpf/prog_tests/kfree_skb.c +++ b/tools/testing/selftests/bpf/prog_tests/kfree_skb.c @@ -134,7 +134,7 @@ void test_kfree_skb(void) /* make sure kfree_skb program was triggered * and it sent expected skb into ring buffer */ - CHECK_FAIL(!passed); + ASSERT_TRUE(passed, "passed"); err = bpf_map_lookup_elem(bpf_map__fd(global_data), &zero, test_ok); if (CHECK(err, "get_result", diff --git a/tools/testing/selftests/bpf/prog_tests/resolve_btfids.c b/tools/testing/selftests/bpf/prog_tests/resolve_btfids.c index 6ace5e9efec1..d3c2de2c24d1 100644 --- a/tools/testing/selftests/bpf/prog_tests/resolve_btfids.c +++ b/tools/testing/selftests/bpf/prog_tests/resolve_btfids.c @@ -160,11 +160,8 @@ int test_resolve_btfids(void) break; if (i > 0) { - ret = CHECK(test_set.ids[i - 1] > test_set.ids[i], - "sort_check", - "test_set is not sorted\n"); - if (ret) - break; + if (!ASSERT_LE(test_set.ids[i - 1], test_set.ids[i], "sort_check")) + return -1; } } diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf_btf.c b/tools/testing/selftests/bpf/prog_tests/snprintf_btf.c index 686b40f11a45..76e1f5fe18fa 100644 --- a/tools/testing/selftests/bpf/prog_tests/snprintf_btf.c +++ b/tools/testing/selftests/bpf/prog_tests/snprintf_btf.c @@ -42,9 +42,7 @@ void test_snprintf_btf(void) * and it set expected return values from bpf_trace_printk()s * and all tests ran. */ - if (CHECK(bss->ret <= 0, - "bpf_snprintf_btf: got return value", - "ret <= 0 %ld test %d\n", bss->ret, bss->ran_subtests)) + if (!ASSERT_GT(bss->ret, 0, "bpf_snprintf_ret")) goto cleanup; if (CHECK(bss->ran_subtests == 0, "check if subtests ran", diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index ee7e3b45182a..dda52cb649dc 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -130,6 +130,20 @@ extern int test__join_cgroup(const char *path); #define CHECK_ATTR(condition, tag, format...) \ _CHECK(condition, tag, tattr.duration, format) +#define ASSERT_TRUE(actual, name) ({ \ + static int duration = 0; \ + bool ___ok = (actual); \ + CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \ + ___ok; \ +}) + +#define ASSERT_FALSE(actual, name) ({ \ + static int duration = 0; \ + bool ___ok = !(actual); \ + CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \ + ___ok; \ +}) + #define ASSERT_EQ(actual, expected, name) ({ \ static int duration = 0; \ typeof(actual) ___act = (actual); \ @@ -163,6 +177,39 @@ extern int test__join_cgroup(const char *path); ___ok; \ }) +#define ASSERT_LE(actual, expected, name) ({ \ + static int duration = 0; \ + typeof(actual) ___act = (actual); \ + typeof(expected) ___exp = (expected); \ + bool ___ok = ___act <= ___exp; \ + CHECK(!___ok, (name), \ + "unexpected %s: actual %lld > expected %lld\n", \ + (name), (long long)(___act), (long long)(___exp)); \ + ___ok; \ +}) + +#define ASSERT_GT(actual, expected, name) ({ \ + static int duration = 0; \ + typeof(actual) ___act = (actual); \ + typeof(expected) ___exp = (expected); \ + bool ___ok = ___act > ___exp; \ + CHECK(!___ok, (name), \ + "unexpected %s: actual %lld <= expected %lld\n", \ + (name), (long long)(___act), (long long)(___exp)); \ + ___ok; \ +}) + +#define ASSERT_GE(actual, expected, name) ({ \ + static int duration = 0; \ + typeof(actual) ___act = (actual); \ + typeof(expected) ___exp = (expected); \ + bool ___ok = ___act >= ___exp; \ + CHECK(!___ok, (name), \ + "unexpected %s: actual %lld < expected %lld\n", \ + (name), (long long)(___act), (long long)(___exp)); \ + ___ok; \ +}) + #define ASSERT_STREQ(actual, expected, name) ({ \ static int duration = 0; \ const char *___act = actual; \ @@ -178,7 +225,8 @@ extern int test__join_cgroup(const char *path); static int duration = 0; \ long long ___res = (res); \ bool ___ok = ___res == 0; \ - CHECK(!___ok, (name), "unexpected error: %lld\n", ___res); \ + CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \ + ___res, errno); \ ___ok; \ })
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@kernel.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(-)