diff mbox series

[bpf-next,14/16] selftests/bpf: Replace CHECK with ASSERT in test_local_storage

Message ID 20230306084216.3186830-15-martin.lau@linux.dev (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Use bpf_mem_cache_alloc/free in bpf_local_storage | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR pending PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-11 pending Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-19 fail Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on s390x with gcc
netdev/series_format fail Series longer than 15 patches (and no cover letter)
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 12 maintainers not CCed: linux-kselftest@vger.kernel.org jolsa@kernel.org john.fastabend@gmail.com mykolal@fb.com yhs@fb.com brauner@kernel.org kpsingh@kernel.org song@kernel.org haoluo@google.com shuah@kernel.org houtao1@huawei.com sdf@google.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 123 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Martin KaFai Lau March 6, 2023, 8:42 a.m. UTC
From: Martin KaFai Lau <martin.lau@kernel.org>

This patch migrates the CHECK macro to ASSERT macro.

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
---
 .../bpf/prog_tests/test_local_storage.c       | 49 +++++++------------
 1 file changed, 19 insertions(+), 30 deletions(-)

Comments

Andrii Nakryiko March 8, 2023, 1:15 a.m. UTC | #1
On Mon, Mar 6, 2023 at 12:43 AM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>
> From: Martin KaFai Lau <martin.lau@kernel.org>
>
> This patch migrates the CHECK macro to ASSERT macro.
>
> Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
> ---

Thanks for the cleanup!

>  .../bpf/prog_tests/test_local_storage.c       | 49 +++++++------------
>  1 file changed, 19 insertions(+), 30 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/test_local_storage.c b/tools/testing/selftests/bpf/prog_tests/test_local_storage.c
> index 9c77cd6b1eaf..c33f840f4880 100644
> --- a/tools/testing/selftests/bpf/prog_tests/test_local_storage.c
> +++ b/tools/testing/selftests/bpf/prog_tests/test_local_storage.c
> @@ -13,8 +13,6 @@
>  #include "network_helpers.h"
>  #include "task_local_storage_helpers.h"
>
> -static unsigned int duration;
> -
>  #define TEST_STORAGE_VALUE 0xbeefdead
>
>  struct storage {
> @@ -60,36 +58,32 @@ static bool check_syscall_operations(int map_fd, int obj_fd)
>
>         /* Looking up an existing element should fail initially */
>         err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
> -       if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
> -                 "err:%d errno:%d\n", err, errno))
> +       if (!ASSERT_ERR(err, "bpf_map_lookup_elem") ||
> +           !ASSERT_EQ(errno, ENOENT, "errno"))

all libbpf APIs since v1.0 always return actual error number directly,
so no need to check errno anymore, you can simplify this further

>                 return false;
>
>         /* Create a new element */
>         err = bpf_map_update_elem(map_fd, &obj_fd, &val, BPF_NOEXIST);
> -       if (CHECK(err < 0, "bpf_map_update_elem", "err:%d errno:%d\n", err,
> -                 errno))
> +       if (!ASSERT_OK(err, "bpf_map_update_elem"))
>                 return false;
>
>         /* Lookup the newly created element */
>         err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
> -       if (CHECK(err < 0, "bpf_map_lookup_elem", "err:%d errno:%d", err,
> -                 errno))
> +       if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
>                 return false;
>
>         /* Check the value of the newly created element */
> -       if (CHECK(lookup_val.value != val.value, "bpf_map_lookup_elem",
> -                 "value got = %x errno:%d", lookup_val.value, val.value))
> +       if (!ASSERT_EQ(lookup_val.value, val.value, "bpf_map_lookup_elem"))
>                 return false;
>
>         err = bpf_map_delete_elem(map_fd, &obj_fd);
> -       if (CHECK(err, "bpf_map_delete_elem()", "err:%d errno:%d\n", err,
> -                 errno))
> +       if (!ASSERT_OK(err, "bpf_map_delete_elem()"))
>                 return false;
>
>         /* The lookup should fail, now that the element has been deleted */
>         err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
> -       if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
> -                 "err:%d errno:%d\n", err, errno))
> +       if (!ASSERT_ERR(err, "bpf_map_lookup_elem") ||
> +           !ASSERT_EQ(errno, ENOENT, "errno"))

same here and probably in other places (I haven't checked everything)

>                 return false;
>
>         return true;
> @@ -104,35 +98,32 @@ void test_test_local_storage(void)
>         char cmd[256];
>
>         skel = local_storage__open_and_load();
> -       if (CHECK(!skel, "skel_load", "lsm skeleton failed\n"))
> +       if (!ASSERT_OK_PTR(skel, "skel_load"))
>                 goto close_prog;
>
>         err = local_storage__attach(skel);
> -       if (CHECK(err, "attach", "lsm attach failed: %d\n", err))
> +       if (!ASSERT_OK(err, "attach"))
>                 goto close_prog;
>
>         task_fd = sys_pidfd_open(getpid(), 0);
> -       if (CHECK(task_fd < 0, "pidfd_open",
> -                 "failed to get pidfd err:%d, errno:%d", task_fd, errno))
> +       if (!ASSERT_GE(task_fd, 0, "pidfd_open"))
>                 goto close_prog;
>
>         if (!check_syscall_operations(bpf_map__fd(skel->maps.task_storage_map),
>                                       task_fd))
>                 goto close_prog;
>
> -       if (CHECK(!mkdtemp(tmp_dir_path), "mkdtemp",
> -                 "unable to create tmpdir: %d\n", errno))
> +       if (!ASSERT_OK_PTR(mkdtemp(tmp_dir_path), "mkdtemp"))
>                 goto close_prog;
>
>         snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_rm",
>                  tmp_dir_path);
>         snprintf(cmd, sizeof(cmd), "cp /bin/rm %s", tmp_exec_path);
> -       if (CHECK_FAIL(system(cmd)))
> +       if (!ASSERT_OK(system(cmd), "system(cp)"))
>                 goto close_prog_rmdir;
>
>         rm_fd = open(tmp_exec_path, O_RDONLY);
> -       if (CHECK(rm_fd < 0, "open", "failed to open %s err:%d, errno:%d",
> -                 tmp_exec_path, rm_fd, errno))
> +       if (!ASSERT_GE(rm_fd, 0, "open(tmp_exec_path)"))
>                 goto close_prog_rmdir;
>
>         if (!check_syscall_operations(bpf_map__fd(skel->maps.inode_storage_map),
> @@ -145,7 +136,7 @@ void test_test_local_storage(void)
>          * LSM program.
>          */
>         err = run_self_unlink(&skel->bss->monitored_pid, tmp_exec_path);
> -       if (CHECK(err != EPERM, "run_self_unlink", "err %d want EPERM\n", err))
> +       if (!ASSERT_EQ(err, EPERM, "run_self_unlink"))
>                 goto close_prog_rmdir;
>
>         /* Set the process being monitored to be the current process */
> @@ -156,18 +147,16 @@ void test_test_local_storage(void)
>          */
>         snprintf(cmd, sizeof(cmd), "mv %s/copy_of_rm %s/check_null_ptr",
>                  tmp_dir_path, tmp_dir_path);
> -       if (CHECK_FAIL(system(cmd)))
> +       if (!ASSERT_OK(system(cmd), "system(mv)"))
>                 goto close_prog_rmdir;
>
> -       CHECK(skel->data->inode_storage_result != 0, "inode_storage_result",
> -             "inode_local_storage not set\n");
> +       ASSERT_EQ(skel->data->inode_storage_result, 0, "inode_storage_result");
>
>         serv_sk = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
> -       if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
> +       if (!ASSERT_GE(serv_sk, 0, "start_server"))
>                 goto close_prog_rmdir;
>
> -       CHECK(skel->data->sk_storage_result != 0, "sk_storage_result",
> -             "sk_local_storage not set\n");
> +       ASSERT_EQ(skel->data->sk_storage_result, 0, "sk_storage_result");
>
>         if (!check_syscall_operations(bpf_map__fd(skel->maps.sk_storage_map),
>                                       serv_sk))
> --
> 2.30.2
>
Martin KaFai Lau March 8, 2023, 1:24 a.m. UTC | #2
On 3/7/23 5:15 PM, Andrii Nakryiko wrote:
>> @@ -60,36 +58,32 @@ static bool check_syscall_operations(int map_fd, int obj_fd)
>>
>>          /* Looking up an existing element should fail initially */
>>          err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
>> -       if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
>> -                 "err:%d errno:%d\n", err, errno))
>> +       if (!ASSERT_ERR(err, "bpf_map_lookup_elem") ||
>> +           !ASSERT_EQ(errno, ENOENT, "errno"))
> 
> all libbpf APIs since v1.0 always return actual error number directly,
> so no need to check errno anymore, you can simplify this further
> 
>>                  return false;
>>
>>          /* Create a new element */
>>          err = bpf_map_update_elem(map_fd, &obj_fd, &val, BPF_NOEXIST);
>> -       if (CHECK(err < 0, "bpf_map_update_elem", "err:%d errno:%d\n", err,
>> -                 errno))
>> +       if (!ASSERT_OK(err, "bpf_map_update_elem"))
>>                  return false;
>>
>>          /* Lookup the newly created element */
>>          err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
>> -       if (CHECK(err < 0, "bpf_map_lookup_elem", "err:%d errno:%d", err,
>> -                 errno))
>> +       if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
>>                  return false;
>>
>>          /* Check the value of the newly created element */
>> -       if (CHECK(lookup_val.value != val.value, "bpf_map_lookup_elem",
>> -                 "value got = %x errno:%d", lookup_val.value, val.value))
>> +       if (!ASSERT_EQ(lookup_val.value, val.value, "bpf_map_lookup_elem"))
>>                  return false;
>>
>>          err = bpf_map_delete_elem(map_fd, &obj_fd);
>> -       if (CHECK(err, "bpf_map_delete_elem()", "err:%d errno:%d\n", err,
>> -                 errno))
>> +       if (!ASSERT_OK(err, "bpf_map_delete_elem()"))
>>                  return false;
>>
>>          /* The lookup should fail, now that the element has been deleted */
>>          err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
>> -       if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
>> -                 "err:%d errno:%d\n", err, errno))
>> +       if (!ASSERT_ERR(err, "bpf_map_lookup_elem") ||
>> +           !ASSERT_EQ(errno, ENOENT, "errno"))
> 
> same here and probably in other places (I haven't checked everything)

Ack. will simplify.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/test_local_storage.c b/tools/testing/selftests/bpf/prog_tests/test_local_storage.c
index 9c77cd6b1eaf..c33f840f4880 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_local_storage.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_local_storage.c
@@ -13,8 +13,6 @@ 
 #include "network_helpers.h"
 #include "task_local_storage_helpers.h"
 
-static unsigned int duration;
-
 #define TEST_STORAGE_VALUE 0xbeefdead
 
 struct storage {
@@ -60,36 +58,32 @@  static bool check_syscall_operations(int map_fd, int obj_fd)
 
 	/* Looking up an existing element should fail initially */
 	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
-	if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
-		  "err:%d errno:%d\n", err, errno))
+	if (!ASSERT_ERR(err, "bpf_map_lookup_elem") ||
+	    !ASSERT_EQ(errno, ENOENT, "errno"))
 		return false;
 
 	/* Create a new element */
 	err = bpf_map_update_elem(map_fd, &obj_fd, &val, BPF_NOEXIST);
-	if (CHECK(err < 0, "bpf_map_update_elem", "err:%d errno:%d\n", err,
-		  errno))
+	if (!ASSERT_OK(err, "bpf_map_update_elem"))
 		return false;
 
 	/* Lookup the newly created element */
 	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
-	if (CHECK(err < 0, "bpf_map_lookup_elem", "err:%d errno:%d", err,
-		  errno))
+	if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
 		return false;
 
 	/* Check the value of the newly created element */
-	if (CHECK(lookup_val.value != val.value, "bpf_map_lookup_elem",
-		  "value got = %x errno:%d", lookup_val.value, val.value))
+	if (!ASSERT_EQ(lookup_val.value, val.value, "bpf_map_lookup_elem"))
 		return false;
 
 	err = bpf_map_delete_elem(map_fd, &obj_fd);
-	if (CHECK(err, "bpf_map_delete_elem()", "err:%d errno:%d\n", err,
-		  errno))
+	if (!ASSERT_OK(err, "bpf_map_delete_elem()"))
 		return false;
 
 	/* The lookup should fail, now that the element has been deleted */
 	err = bpf_map_lookup_elem_flags(map_fd, &obj_fd, &lookup_val, 0);
-	if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
-		  "err:%d errno:%d\n", err, errno))
+	if (!ASSERT_ERR(err, "bpf_map_lookup_elem") ||
+	    !ASSERT_EQ(errno, ENOENT, "errno"))
 		return false;
 
 	return true;
@@ -104,35 +98,32 @@  void test_test_local_storage(void)
 	char cmd[256];
 
 	skel = local_storage__open_and_load();
-	if (CHECK(!skel, "skel_load", "lsm skeleton failed\n"))
+	if (!ASSERT_OK_PTR(skel, "skel_load"))
 		goto close_prog;
 
 	err = local_storage__attach(skel);
-	if (CHECK(err, "attach", "lsm attach failed: %d\n", err))
+	if (!ASSERT_OK(err, "attach"))
 		goto close_prog;
 
 	task_fd = sys_pidfd_open(getpid(), 0);
-	if (CHECK(task_fd < 0, "pidfd_open",
-		  "failed to get pidfd err:%d, errno:%d", task_fd, errno))
+	if (!ASSERT_GE(task_fd, 0, "pidfd_open"))
 		goto close_prog;
 
 	if (!check_syscall_operations(bpf_map__fd(skel->maps.task_storage_map),
 				      task_fd))
 		goto close_prog;
 
-	if (CHECK(!mkdtemp(tmp_dir_path), "mkdtemp",
-		  "unable to create tmpdir: %d\n", errno))
+	if (!ASSERT_OK_PTR(mkdtemp(tmp_dir_path), "mkdtemp"))
 		goto close_prog;
 
 	snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_rm",
 		 tmp_dir_path);
 	snprintf(cmd, sizeof(cmd), "cp /bin/rm %s", tmp_exec_path);
-	if (CHECK_FAIL(system(cmd)))
+	if (!ASSERT_OK(system(cmd), "system(cp)"))
 		goto close_prog_rmdir;
 
 	rm_fd = open(tmp_exec_path, O_RDONLY);
-	if (CHECK(rm_fd < 0, "open", "failed to open %s err:%d, errno:%d",
-		  tmp_exec_path, rm_fd, errno))
+	if (!ASSERT_GE(rm_fd, 0, "open(tmp_exec_path)"))
 		goto close_prog_rmdir;
 
 	if (!check_syscall_operations(bpf_map__fd(skel->maps.inode_storage_map),
@@ -145,7 +136,7 @@  void test_test_local_storage(void)
 	 * LSM program.
 	 */
 	err = run_self_unlink(&skel->bss->monitored_pid, tmp_exec_path);
-	if (CHECK(err != EPERM, "run_self_unlink", "err %d want EPERM\n", err))
+	if (!ASSERT_EQ(err, EPERM, "run_self_unlink"))
 		goto close_prog_rmdir;
 
 	/* Set the process being monitored to be the current process */
@@ -156,18 +147,16 @@  void test_test_local_storage(void)
 	 */
 	snprintf(cmd, sizeof(cmd), "mv %s/copy_of_rm %s/check_null_ptr",
 		 tmp_dir_path, tmp_dir_path);
-	if (CHECK_FAIL(system(cmd)))
+	if (!ASSERT_OK(system(cmd), "system(mv)"))
 		goto close_prog_rmdir;
 
-	CHECK(skel->data->inode_storage_result != 0, "inode_storage_result",
-	      "inode_local_storage not set\n");
+	ASSERT_EQ(skel->data->inode_storage_result, 0, "inode_storage_result");
 
 	serv_sk = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
-	if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
+	if (!ASSERT_GE(serv_sk, 0, "start_server"))
 		goto close_prog_rmdir;
 
-	CHECK(skel->data->sk_storage_result != 0, "sk_storage_result",
-	      "sk_local_storage not set\n");
+	ASSERT_EQ(skel->data->sk_storage_result, 0, "sk_storage_result");
 
 	if (!check_syscall_operations(bpf_map__fd(skel->maps.sk_storage_map),
 				      serv_sk))