Skip to content

Commit 7a2fa70

Browse files
anakryikoAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Lorenz Bauer <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 87bd9e6 commit 7a2fa70

File tree

7 files changed

+56
-15
lines changed

7 files changed

+56
-15
lines changed

tools/testing/selftests/bpf/prog_tests/btf_dump.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static int test_btf_dump_case(int n, struct btf_dump_test_case *t)
7777

7878
snprintf(out_file, sizeof(out_file), "/tmp/%s.output.XXXXXX", t->file);
7979
fd = mkstemp(out_file);
80-
if (CHECK(fd < 0, "create_tmp", "failed to create file: %d\n", fd)) {
80+
if (!ASSERT_GE(fd, 0, "create_tmp")) {
8181
err = fd;
8282
goto done;
8383
}

tools/testing/selftests/bpf/prog_tests/btf_endian.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include <test_progs.h>
77
#include <bpf/btf.h>
88

9-
static int duration = 0;
10-
119
void test_btf_endian() {
1210
#if __BYTE_ORDER == __LITTLE_ENDIAN
1311
enum btf_endianness endian = BTF_LITTLE_ENDIAN;
@@ -71,7 +69,7 @@ void test_btf_endian() {
7169

7270
/* now modify original BTF */
7371
var_id = btf__add_var(btf, "some_var", BTF_VAR_GLOBAL_ALLOCATED, 1);
74-
CHECK(var_id <= 0, "var_id", "failed %d\n", var_id);
72+
ASSERT_GT(var_id, 0, "var_id");
7573

7674
btf__free(swap_btf);
7775
swap_btf = NULL;

tools/testing/selftests/bpf/prog_tests/cgroup_link.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void test_cgroup_link(void)
5454

5555
for (i = 0; i < cg_nr; i++) {
5656
cgs[i].fd = create_and_get_cgroup(cgs[i].path);
57-
if (CHECK(cgs[i].fd < 0, "cg_create", "fail: %d\n", cgs[i].fd))
57+
if (!ASSERT_GE(cgs[i].fd, 0, "cg_create"))
5858
goto cleanup;
5959
}
6060

tools/testing/selftests/bpf/prog_tests/kfree_skb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void test_kfree_skb(void)
134134
/* make sure kfree_skb program was triggered
135135
* and it sent expected skb into ring buffer
136136
*/
137-
CHECK_FAIL(!passed);
137+
ASSERT_TRUE(passed, "passed");
138138

139139
err = bpf_map_lookup_elem(bpf_map__fd(global_data), &zero, test_ok);
140140
if (CHECK(err, "get_result",

tools/testing/selftests/bpf/prog_tests/resolve_btfids.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,8 @@ int test_resolve_btfids(void)
160160
break;
161161

162162
if (i > 0) {
163-
ret = CHECK(test_set.ids[i - 1] > test_set.ids[i],
164-
"sort_check",
165-
"test_set is not sorted\n");
166-
if (ret)
167-
break;
163+
if (!ASSERT_LE(test_set.ids[i - 1], test_set.ids[i], "sort_check"))
164+
return -1;
168165
}
169166
}
170167

tools/testing/selftests/bpf/prog_tests/snprintf_btf.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ void test_snprintf_btf(void)
4242
* and it set expected return values from bpf_trace_printk()s
4343
* and all tests ran.
4444
*/
45-
if (CHECK(bss->ret <= 0,
46-
"bpf_snprintf_btf: got return value",
47-
"ret <= 0 %ld test %d\n", bss->ret, bss->ran_subtests))
45+
if (!ASSERT_GT(bss->ret, 0, "bpf_snprintf_ret"))
4846
goto cleanup;
4947

5048
if (CHECK(bss->ran_subtests == 0, "check if subtests ran",

tools/testing/selftests/bpf/test_progs.h

+49-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ extern int test__join_cgroup(const char *path);
130130
#define CHECK_ATTR(condition, tag, format...) \
131131
_CHECK(condition, tag, tattr.duration, format)
132132

133+
#define ASSERT_TRUE(actual, name) ({ \
134+
static int duration = 0; \
135+
bool ___ok = (actual); \
136+
CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \
137+
___ok; \
138+
})
139+
140+
#define ASSERT_FALSE(actual, name) ({ \
141+
static int duration = 0; \
142+
bool ___ok = !(actual); \
143+
CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \
144+
___ok; \
145+
})
146+
133147
#define ASSERT_EQ(actual, expected, name) ({ \
134148
static int duration = 0; \
135149
typeof(actual) ___act = (actual); \
@@ -163,6 +177,39 @@ extern int test__join_cgroup(const char *path);
163177
___ok; \
164178
})
165179

180+
#define ASSERT_LE(actual, expected, name) ({ \
181+
static int duration = 0; \
182+
typeof(actual) ___act = (actual); \
183+
typeof(expected) ___exp = (expected); \
184+
bool ___ok = ___act <= ___exp; \
185+
CHECK(!___ok, (name), \
186+
"unexpected %s: actual %lld > expected %lld\n", \
187+
(name), (long long)(___act), (long long)(___exp)); \
188+
___ok; \
189+
})
190+
191+
#define ASSERT_GT(actual, expected, name) ({ \
192+
static int duration = 0; \
193+
typeof(actual) ___act = (actual); \
194+
typeof(expected) ___exp = (expected); \
195+
bool ___ok = ___act > ___exp; \
196+
CHECK(!___ok, (name), \
197+
"unexpected %s: actual %lld <= expected %lld\n", \
198+
(name), (long long)(___act), (long long)(___exp)); \
199+
___ok; \
200+
})
201+
202+
#define ASSERT_GE(actual, expected, name) ({ \
203+
static int duration = 0; \
204+
typeof(actual) ___act = (actual); \
205+
typeof(expected) ___exp = (expected); \
206+
bool ___ok = ___act >= ___exp; \
207+
CHECK(!___ok, (name), \
208+
"unexpected %s: actual %lld < expected %lld\n", \
209+
(name), (long long)(___act), (long long)(___exp)); \
210+
___ok; \
211+
})
212+
166213
#define ASSERT_STREQ(actual, expected, name) ({ \
167214
static int duration = 0; \
168215
const char *___act = actual; \
@@ -178,7 +225,8 @@ extern int test__join_cgroup(const char *path);
178225
static int duration = 0; \
179226
long long ___res = (res); \
180227
bool ___ok = ___res == 0; \
181-
CHECK(!___ok, (name), "unexpected error: %lld\n", ___res); \
228+
CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \
229+
___res, errno); \
182230
___ok; \
183231
})
184232

0 commit comments

Comments
 (0)