diff mbox series

[bpf-next,2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs

Message ID 20211020082956.8359-3-lmb@cloudflare.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Support RENAME_EXCHANGE on bpffs | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/cover_letter success Series has a cover letter
netdev/fixes_present success Fixes tag not required for -next series
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 5 maintainers not CCed: john.fastabend@gmail.com yhs@fb.com songliubraving@fb.com kafai@fb.com kpsingh@kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success No Fixes tag
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 57 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success No static functions without inline keyword in header files
bpf/vmtest-bpf-next success VM_Test

Commit Message

Lorenz Bauer Oct. 20, 2021, 8:29 a.m. UTC
Add tests to exercise the behaviour of RENAME_EXCHANGE and RENAME_NOREPLACE
on bpffs. The former checks that after an exchange the inode of two
directories has changed. The latter checks that the source still exists
after a failed rename.

Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
---
 .../selftests/bpf/prog_tests/test_bpffs.c     | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)

Comments

Andrii Nakryiko Oct. 20, 2021, 9:43 p.m. UTC | #1
On Wed, Oct 20, 2021 at 1:30 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
>
> Add tests to exercise the behaviour of RENAME_EXCHANGE and RENAME_NOREPLACE
> on bpffs. The former checks that after an exchange the inode of two
> directories has changed. The latter checks that the source still exists
> after a failed rename.
>
> Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
> ---
>  .../selftests/bpf/prog_tests/test_bpffs.c     | 39 +++++++++++++++++++
>  1 file changed, 39 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
> index 172c999e523c..9c28ae9589bf 100644
> --- a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
> +++ b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
> @@ -1,6 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
>  /* Copyright (c) 2020 Facebook */
>  #define _GNU_SOURCE
> +#include <stdio.h>
>  #include <sched.h>
>  #include <sys/mount.h>
>  #include <sys/stat.h>
> @@ -29,6 +30,7 @@ static int read_iter(char *file)
>
>  static int fn(void)
>  {
> +       struct stat a, b;
>         int err, duration = 0;
>
>         err = unshare(CLONE_NEWNS);
> @@ -67,6 +69,43 @@ static int fn(void)
>         err = read_iter(TDIR "/fs2/progs.debug");
>         if (CHECK(err, "reading " TDIR "/fs2/progs.debug", "failed\n"))
>                 goto out;
> +
> +       err = mkdir(TDIR "/fs1/a", 0777);
> +       if (CHECK(err, "creating " TDIR "/fs1/a", "failed\n"))
> +               goto out;
> +       err = mkdir(TDIR "/fs1/a/1", 0777);
> +       if (CHECK(err, "creating " TDIR "/fs1/a/1", "failed\n"))
> +               goto out;
> +       err = mkdir(TDIR "/fs1/b", 0777);
> +       if (CHECK(err, "creating " TDIR "/fs1/b", "failed\n"))
> +               goto out;
> +
> +       /* Check that RENAME_EXCHANGE works. */
> +       err = stat(TDIR "/fs1/a", &a);
> +       if (CHECK(err, "stat(" TDIR "/fs1/a)", "failed\n"))
> +               goto out;
> +       err = renameat2(0, TDIR "/fs1/a", 0, TDIR "/fs1/b", RENAME_EXCHANGE);
> +       if (CHECK(err, "renameat2(RENAME_EXCHANGE)", "failed\n"))
> +               goto out;
> +       err = stat(TDIR "/fs1/b", &b);
> +       if (CHECK(err, "stat(" TDIR "/fs1/b)", "failed\n"))
> +               goto out;
> +       if (CHECK(a.st_ino != b.st_ino, "b should have a's inode", "failed\n"))
> +               goto out;
> +       err = access(TDIR "/fs1/b/1", F_OK);
> +       if (CHECK(err, "access(" TDIR "/fs1/b/1)", "failed\n"))
> +               goto out;
> +
> +       /* Check that RENAME_NOREPLACE works. */
> +       err = renameat2(0, TDIR "/fs1/b", 0, TDIR "/fs1/a", RENAME_NOREPLACE);
> +       if (CHECK(!err, "renameat2(RENAME_NOREPLACE)", "succeeded\n")) {
> +               err = -EINVAL;
> +               goto out;
> +       }
> +       err = access(TDIR "/fs1/b", F_OK);
> +       if (CHECK(err, "access(" TDIR "/fs1/b)", "failed\n"))
> +               goto out;
> +

Please use ASSERT_xxx() for new code.

>  out:
>         umount(TDIR "/fs1");
>         umount(TDIR "/fs2");
> --
> 2.30.2
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
index 172c999e523c..9c28ae9589bf 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
@@ -1,6 +1,7 @@ 
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 #define _GNU_SOURCE
+#include <stdio.h>
 #include <sched.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
@@ -29,6 +30,7 @@  static int read_iter(char *file)
 
 static int fn(void)
 {
+	struct stat a, b;
 	int err, duration = 0;
 
 	err = unshare(CLONE_NEWNS);
@@ -67,6 +69,43 @@  static int fn(void)
 	err = read_iter(TDIR "/fs2/progs.debug");
 	if (CHECK(err, "reading " TDIR "/fs2/progs.debug", "failed\n"))
 		goto out;
+
+	err = mkdir(TDIR "/fs1/a", 0777);
+	if (CHECK(err, "creating " TDIR "/fs1/a", "failed\n"))
+		goto out;
+	err = mkdir(TDIR "/fs1/a/1", 0777);
+	if (CHECK(err, "creating " TDIR "/fs1/a/1", "failed\n"))
+		goto out;
+	err = mkdir(TDIR "/fs1/b", 0777);
+	if (CHECK(err, "creating " TDIR "/fs1/b", "failed\n"))
+		goto out;
+
+	/* Check that RENAME_EXCHANGE works. */
+	err = stat(TDIR "/fs1/a", &a);
+	if (CHECK(err, "stat(" TDIR "/fs1/a)", "failed\n"))
+		goto out;
+	err = renameat2(0, TDIR "/fs1/a", 0, TDIR "/fs1/b", RENAME_EXCHANGE);
+	if (CHECK(err, "renameat2(RENAME_EXCHANGE)", "failed\n"))
+		goto out;
+	err = stat(TDIR "/fs1/b", &b);
+	if (CHECK(err, "stat(" TDIR "/fs1/b)", "failed\n"))
+		goto out;
+	if (CHECK(a.st_ino != b.st_ino, "b should have a's inode", "failed\n"))
+		goto out;
+	err = access(TDIR "/fs1/b/1", F_OK);
+	if (CHECK(err, "access(" TDIR "/fs1/b/1)", "failed\n"))
+		goto out;
+
+	/* Check that RENAME_NOREPLACE works. */
+	err = renameat2(0, TDIR "/fs1/b", 0, TDIR "/fs1/a", RENAME_NOREPLACE);
+	if (CHECK(!err, "renameat2(RENAME_NOREPLACE)", "succeeded\n")) {
+		err = -EINVAL;
+		goto out;
+	}
+	err = access(TDIR "/fs1/b", F_OK);
+	if (CHECK(err, "access(" TDIR "/fs1/b)", "failed\n"))
+		goto out;
+
 out:
 	umount(TDIR "/fs1");
 	umount(TDIR "/fs2");