selftests: tmpfs: Add kselftest support to tmpfs
PATCH v3 1/1 selftests: tmpfs: Add kselftest support to tmpfs
Add kselftest support for openat, linkat, unshare, mount tests
- Replace direct error handling with 'ksft_test_result_*' , 'ksft_print_msg' macros for better reporting.
- Add ksft_print_header() and ksft_set_plan()
to structure test outputs more effectively.
- Improve the test flow by adding more detailed pass/fail reporting for unshare, mounting, file opening, and linking operations.
- Skip the test if it's not run as root, providing an appropriate Warning.
Test logs:
Before change:
- Without root
error: unshare, errno 1
- With root
No, output
After change:
- Without root
TAP version 13
1..1
ok 1 # SKIP This test needs root to run
- With root
TAP version 13
1..1
unshare(): Creat new mount namespace: Success. mount(): Root filesystem private mount: Success mount(): Mounting tmpfs on /tmp: Success openat(): Open first temporary file: Success linkat(): Linking the temporary file: Success openat(): Opening the second temporary file: Success ok 1 Test : Success
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0
---
Notes:
Changes in v3:
- Remove extra ksft_set_plan()
- Remove function for unshare()
- Fix the comment style
link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Make the commit message more clear.
link to v1: https://lore.kernel.org/all/[email protected]/T/#u
.../selftests/tmpfs/bug-link-o-tmpfile.c | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-)
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c +57 -12
@@ -23,45 +23,90 @@#include <sys/mount.h> #include <unistd.h> +#include "../kselftest.h" + +int main (void ) {int fd; +/* Setting up kselftest framework */ +ksft_print_header (); +ksft_set_plan (1 ); + +/* Check if test is run as root */ +if (geteuid ()) { +ksft_test_result_skip ("This test needs root to run!\n" ); +return 1 ; + } + +if (unshare (CLONE_NEWNS ) ==-1 ) {if (errno ==ENOSYS || errno ==EPERM ) { -fprintf (stderr,"error: unshare, errno %d\n" , errno); -return 4 ; +ksft_test_result_fail ("unshare() error: unshare, errno %d\n" , errno); +return 1 ; } -fprintf (stderr,"error: unshare, errno %d\n" , errno); -return 1 ; +else { +fprintf (stderr,"unshare() error: unshare, errno %d\n" , errno); +ksft_test_result_fail ("unshare() error: unshare, errno %d\n" , errno); +return 1 ; + + } + } + +else { +ksft_print_msg ("unshare(): Creat new mount namespace: Success.\n" ); + } -if (mount (NULL ,"/" ,NULL ,MS_PRIVATE |MS_REC ,NULL ) ==-1 ) { -fprintf (stderr,"error: mount '/', errno %d\n" , errno); + + + +if (mount (NULL ,"/" ,NULL ,MS_PRIVATE |MS_REC ,NULL ) ==-1 ) { +ksft_test_result_fail ("mount() error: Root filesystem private mount: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("mount(): Root filesystem private mount: Success\n" ); } +/* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */ if (mount (NULL ,"/tmp" ,"tmpfs" ,0 ,"nr_inodes=3" ) ==-1 ) { -fprintf (stderr,"error: mount tmpfs, errno %d\n" , errno); +ksft_test_result_fail ("mount() error: Mounting tmpfs on /tmp: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("mount(): Mounting tmpfs on /tmp: Success\n" ); } - fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 ); + + fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 );if (fd ==-1 ) { -fprintf (stderr,"error: open 1, errno %d\n" , errno); +ksft_test_result_fail ("openat() error: Open first temporary file: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("openat(): Open first temporary file: Success\n" ); } + +if (linkat (fd,"" ,AT_FDCWD ,"/tmp/1" ,AT_EMPTY_PATH ) ==-1 ) { -fprintf (stderr,"error: linkat, errno %d\n" , errno); +ksft_test_result_fail ("linkat() error: Linking the temporary file: Fail %d\n" , errno); +/* Ensure fd is closed on failure */ +close (fd);return 1 ; + }else { +ksft_print_msg ("linkat(): Linking the temporary file: Success\n" ); }close (fd); - fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 ); + + fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 );if (fd ==-1 ) { -fprintf (stderr,"error: open 2, errno %d\n" , errno); +ksft_test_result_fail ("openat() error: Opening the second temporary file: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("openat(): Opening the second temporary file: Success\n" ); } +ksft_test_result_pass ("Test : Success\n" ); +ksft_exit_pass ();return 0 ; }
Signature · 1 line
2.45.2
Shuah Khan
maintainerQuoted 1 line · Shivam Chaudhary
Add kselftest support for openat, linkat, unshare, mount tests
You are combining a few too many changes in this patch. As such this patch doesn't add support for the above tests. Instead it changes the reporting to use ksft_* framework.
Quoted 5 lines
- Replace direct error handling with 'ksft_test_result_*' , '
ksft_print_msg' macros for better reporting.
That
Quoted 9 lines
- Add
ksft_print_header()andksft_set_plan()
to structure test outputs more effectively.- Improve the test flow by adding more detailed pass/fail reporting for unshare, mounting, file opening, and linking operations.
- Skip the test if it's not run as root, providing an appropriate Warning.
Make this a separate patch and the very first patch in the series.
Quoted 62 lines
Test logs:
Before change:
- Without root
error: unshare, errno 1- With root
No, outputAfter change:
- Without root
TAP version 13
1..1
ok 1 # SKIP This test needs root to run- With root
TAP version 13
1..1
unshare(): Creat new mount namespace: Success.mount(): Root filesystem private mount: Successmount(): Mounting tmpfs on /tmp: Successopenat(): Open first temporary file: Successlinkat(): Linking the temporary file: Successopenat(): Opening the second temporary file: Success ok 1 Test : Success
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0Signed-off-by Shivam Chaudhary <[email protected]>---
Notes:
Changes in v3:
- Remove extraksft_set_plan()
- Remove function forunshare()
- Fix the comment stylelink to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Make the commit message more clear.link to v1: https://lore.kernel.org/all/[email protected]/T/#u
.../selftests/tmpfs/bug-link-o-tmpfile.c | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-)tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c +3
-0@@ -23,45 +23,90 @@#include <sys/mount.h> #include <unistd.h> +#include "../kselftest.h" + +int main (void ) {
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c line 40
Quoted 10 lines
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c +8
-0int fd; +/* Setting up kselftest framework */ +ksft_print_header (); +ksft_set_plan (1 ); + +/* Check if test is run as root */ +if (geteuid ()) { +ksft_test_result_skip ("This test needs root to run!\n" ); +return 1 ;
This should be SKIP - simply use KSFT_SKIP
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c line 46
Quoted 8 lines
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c +4
-2+ } + +if (unshare (CLONE_NEWNS ) ==-1 ) {if (errno ==ENOSYS || errno ==EPERM ) { -fprintf (stderr,"error: unshare, errno %d\n" , errno); -return 4 ; +ksft_test_result_fail ("unshare() error: unshare, errno %d\n" , errno);
This isn't a failure - it just means that unsaher() might not be supported. Since root check is already done, EPERM won't be returned here.
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c line 47
Quoted 1 line
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c +1
-0+return 1 ;
Okay this wrong. This changes the return value fro 4 to 1 - SKIP becomes fail. Use KSFT_SKIP
Quoted 65 lines
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c +36
-10} -fprintf (stderr,"error: unshare, errno %d\n" , errno); -return 1 ; +else { +fprintf (stderr,"unshare() error: unshare, errno %d\n" , errno); +ksft_test_result_fail ("unshare() error: unshare, errno %d\n" , errno); +return 1 ; + + } + } + +else { +ksft_print_msg ("unshare(): Creat new mount namespace: Success.\n" ); + } -if (mount (NULL ,"/" ,NULL ,MS_PRIVATE |MS_REC ,NULL ) ==-1 ) { -fprintf (stderr,"error: mount '/', errno %d\n" , errno); + + + +if (mount (NULL ,"/" ,NULL ,MS_PRIVATE |MS_REC ,NULL ) ==-1 ) { +ksft_test_result_fail ("mount() error: Root filesystem private mount: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("mount(): Root filesystem private mount: Success\n" ); } +/* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */ if (mount (NULL ,"/tmp" ,"tmpfs" ,0 ,"nr_inodes=3" ) ==-1 ) { -fprintf (stderr,"error: mount tmpfs, errno %d\n" , errno); +ksft_test_result_fail ("mount() error: Mounting tmpfs on /tmp: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("mount(): Mounting tmpfs on /tmp: Success\n" ); } - fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 ); + + fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 );if (fd ==-1 ) { -fprintf (stderr,"error: open 1, errno %d\n" , errno); +ksft_test_result_fail ("openat() error: Open first temporary file: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("openat(): Open first temporary file: Success\n" ); } + +if (linkat (fd,"" ,AT_FDCWD ,"/tmp/1" ,AT_EMPTY_PATH ) ==-1 ) { -fprintf (stderr,"error: linkat, errno %d\n" , errno); +ksft_test_result_fail ("linkat() error: Linking the temporary file: Fail %d\n" , errno); +/* Ensure fd is closed on failure */ +close (fd);return 1 ; + }else { +ksft_print_msg ("linkat(): Linking the temporary file: Success\n" ); }close (fd); - fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 ); + + fd =openat (AT_FDCWD ,"/tmp" ,O_WRONLY |O_TMPFILE ,0600 );if (fd ==-1 ) { -fprintf (stderr,"error: open 2, errno %d\n" , errno);
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c line 112
Quoted 10 lines
tools/testing/selftests/tmpfs/bug-link-o-tmpfile.c +5
-0+ksft_test_result_fail ("openat() error: Opening the second temporary file: Fail %d\n" , errno);return 1 ; + }else { +ksft_print_msg ("openat(): Opening the second temporary file: Success\n" ); } +ksft_test_result_pass ("Test : Success\n" ); +ksft_exit_pass ();return 0 ; }
Fix these and split the patches. It makes it easier to review.
thanks,
-- Shuah