Commit 7a2fa70a authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov
Browse files

selftests/bpf: Add remaining ASSERT_xxx() variants



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: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarLorenz Bauer <lmb@cloudflare.com>
Link: https://lore.kernel.org/bpf/20210426192949.416837-2-andrii@kernel.org
parent 87bd9e60
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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;
	}
+1 −3
Original line number Diff line number Diff line
@@ -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;
+1 −1
Original line number Diff line number Diff line
@@ -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;
	}

+1 −1
Original line number Diff line number Diff line
@@ -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",
+2 −5
Original line number Diff line number Diff line
@@ -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;
		}
	}

Loading