diff mbox series

[bpf-next,3/3] bpf: selftests: Test bpf_assert_if() and bpf_assert_with_if()

Message ID b94a4280fb281f13cba3cc314f4c20a34138e6a6.1702594357.git.dxu@dxuuu.xyz (mailing list archive)
State New
Headers show
Series Various BPF exception improvements | expand

Commit Message

Daniel Xu Dec. 14, 2023, 10:56 p.m. UTC
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(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/exceptions.c b/tools/testing/selftests/bpf/prog_tests/exceptions.c
index 516f4a13013c..a41113d72d0d 100644
--- a/tools/testing/selftests/bpf/prog_tests/exceptions.c
+++ b/tools/testing/selftests/bpf/prog_tests/exceptions.c
@@ -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)			  \
 	{									  \
diff --git a/tools/testing/selftests/bpf/progs/exceptions.c b/tools/testing/selftests/bpf/progs/exceptions.c
index 2811ee842b01..e61cb794a305 100644
--- a/tools/testing/selftests/bpf/progs/exceptions.c
+++ b/tools/testing/selftests/bpf/progs/exceptions.c
@@ -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";