[bpf-next,3/3] bpf: selftests: Test bpf_assert_if() and bpf_assert_with_if()
Commit Message
Add some positive and negative test cases that exercise the "callback"
semantics.
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
.../selftests/bpf/prog_tests/exceptions.c | 5 ++
.../testing/selftests/bpf/progs/exceptions.c | 61 +++++++++++++++++++
2 files changed, 66 insertions(+)
@@ -83,6 +83,11 @@ static void test_exceptions_success(void)
RUN_SUCCESS(exception_assert_range_with, 1);
RUN_SUCCESS(exception_bad_assert_range, 0);
RUN_SUCCESS(exception_bad_assert_range_with, 10);
+ RUN_SUCCESS(exception_assert_if_body_not_executed, 2);
+ RUN_SUCCESS(exception_bad_assert_if_body_executed, 1);
+ RUN_SUCCESS(exception_bad_assert_if_throws, 0);
+ RUN_SUCCESS(exception_assert_with_if_body_not_executed, 3);
+ RUN_SUCCESS(exception_bad_assert_with_if_body_executed, 2);
#define RUN_EXT(load_ret, attach_err, expr, msg, after_link) \
{ \
@@ -365,4 +365,65 @@ int exception_bad_assert_range_with(struct __sk_buff *ctx)
return 1;
}
+SEC("tc")
+int exception_assert_if_body_not_executed(struct __sk_buff *ctx)
+{
+ u64 time = bpf_ktime_get_ns();
+
+ bpf_assert_if(time != 0) {
+ return 1;
+ }
+
+ return 2;
+}
+
+SEC("tc")
+int exception_bad_assert_if_body_executed(struct __sk_buff *ctx)
+{
+ u64 time = bpf_ktime_get_ns();
+
+ bpf_assert_if(time == 0) {
+ return 1;
+ }
+
+ return 2;
+}
+
+SEC("tc")
+int exception_bad_assert_if_throws(struct __sk_buff *ctx)
+{
+ u64 time = bpf_ktime_get_ns();
+
+ bpf_assert_if(time == 0) {
+ }
+
+ return 2;
+}
+
+SEC("tc")
+int exception_assert_with_if_body_not_executed(struct __sk_buff *ctx)
+{
+ u64 time = bpf_ktime_get_ns();
+ int ret = 1;
+
+ bpf_assert_with_if(time != 0, ret) {
+ ret = 2;
+ }
+
+ return 3;
+}
+
+SEC("tc")
+int exception_bad_assert_with_if_body_executed(struct __sk_buff *ctx)
+{
+ u64 time = bpf_ktime_get_ns();
+ int ret = 1;
+
+ bpf_assert_with_if(time == 0, ret) {
+ ret = 2;
+ }
+
+ return 3;
+}
+
char _license[] SEC("license") = "GPL";