syscalls/readlink.h: Move common macros to readlink.h
PATCH 1/4 syscalls/readlink.h: Move common macros to readlink.h
Signed-off-by Jinhui Huang <[email protected]>
--- testcases/kernel/syscalls/readlink/readlink.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 testcases/kernel/syscalls/readlink/readlink.h
testcases/kernel/syscalls/readlink/readlink.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. +* Author: Jinhui huang <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it would be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* +* You should have received a copy of the GNU General Public License +* alone with this program. +*/ + +#ifndef READLINK_H +#define READLINK_H + +#define TESTFILE "test_file" +#define SYMFILE "slink_file" +#define FILE_MODE (S_IRUSR | S_IRGRP | S_IROTH) +#define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO) +#define MAX_SIZE 256 + +#endif /* READLINK_H */
Signature · 1 line
1.8.3.1
PATCH 2/4 syscalls/readlink01.c: Cleanup && Convert to new API
1) Take use of some safe macros
2) Merge readlink02 into readlink01
Signed-off-by Jinhui Huang <[email protected]>
--- runtest/ltplite | 1 - runtest/quickhit | 2 - runtest/stress.part3 | 1 - runtest/syscalls | 1 - testcases/kernel/syscalls/.gitignore | 1 - testcases/kernel/syscalls/readlink/readlink01.c | 216 +++++------------------- testcases/kernel/syscalls/readlink/readlink02.c | 210 ----------------------- 7 files changed, 44 insertions(+), 388 deletions(-) delete mode 100644 testcases/kernel/syscalls/readlink/readlink02.c
runtest/ltplite +0 -1
@@ -623,7 +623,6 @@ readdir21 readdir21 readlink01A symlink01 -T readlink01 readlink01 readlink01 -readlink02 readlink02 readlink03 readlink03 readlink04 readlink04
runtest/quickhit +0 -2
@@ -195,8 +195,6 @@ readdir01 readdir01 # write multiple files and try to find them with readdir # TEST CASES # 1.) Create n files and check that readdir() finds n files -readlink02 readlink02 -# Basic test for readlink(2) rename02 rename02 # Basic test for rename(2) rmdir04 rmdir04
runtest/stress.part3 +0 -1
@@ -528,7 +528,6 @@ readdir21 readdir21 readlink01A symlink01 -T readlink01 readlink01 readlink01 -readlink02 readlink02 readlink03 readlink03 readlink04 readlink04
runtest/syscalls +0 -1
@@ -882,7 +882,6 @@ readdir21 readdir21 readlink01A symlink01 -T readlink01 readlink01 readlink01 -readlink02 readlink02 readlink03 readlink03 readlink04 readlink04
testcases/kernel/syscalls/.gitignore +0 -1
@@ -738,7 +738,6 @@ /readdir/readdir21 /readlink/creat_slink /readlink/readlink01 -/readlink/readlink02 /readlink/readlink03 /readlink/readlink04 /readlinkat/readlinkat01
testcases/kernel/syscalls/readlink/readlink01.c +44 -172
239 lines not on this page · raw message
testcases/kernel/syscalls/readlink/readlink02.c deleted +0 -210
210 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 3/4 syscalls/readlink03.c: Cleanup && Convert to new API
In reply to Jinhui Huang1) Take use of some safe macros
2) Add a new test for EFAULT
Signed-off-by Jinhui Huang <[email protected]>
--- testcases/kernel/syscalls/readlink/readlink03.c | 217 ++++++++++-------------- 1 file changed, 88 insertions(+), 129 deletions(-)
testcases/kernel/syscalls/readlink/readlink03.c +88 -129
@@ -1,25 +1,20 @@/* -* -* Copyright (c) International Business Machines Corp., 2001 +* Copyright (c) International Business Machines Corp., 2001 * 07/2001 Ported by Wayne Boyer * -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. * -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software Foundation, -* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. */ +/* * Test Description : -* Verify that, * 1) readlink(2) returns -1 and sets errno to EACCES if search/write * permission is denied in the directory where the symbolic link * resides.
@@ -35,155 +30,119 @@* the path prefix is not a directory. * 7) readlink(2) returns -1 and sets errno to ELOOP if too many symbolic * links were encountered in translating the pathname. +* 8) readlink(2) returns -1 and sets errno to EFAULT if buf outside the +* process’s allocated address space. */ -#include <stdio.h> -#include <sys/types.h> -#include <fcntl.h> +#include <pwd.h> #include <errno.h> #include <string.h> -#include <signal.h> -#include <sys/stat.h> -#include <pwd.h> -#include "test.h" -#include "safe_macros.h" - -#define MODE_RWX (S_IRWXU | S_IRWXG | S_IRWXO) -#define FILE_MODE (S_IRUSR | S_IRGRP | S_IROTH) -#define DIR_TEMP "testdir_1" -#define TEST_FILE1 "testdir_1/tfile_1" -#define SYM_FILE1 "testdir_1/sfile_1" -#define TEST_FILE2 "tfile_2" -#define SYM_FILE2 "sfile_2" -#define TEST_FILE3 "tfile_3" -#define SYM_FILE3 "tfile_3/sfile_3" +#include "tst_test.h" +#include "readlink.h" + +#define DIR_TEMP "test_dir_1" +#define TEST_FILE1 "test_dir_1/test_file_1" +#define SYM_FILE1 "test_dir_1/slink_file_1" +#define TEST_FILE2 "test_file_2" +#define SYM_FILE2 "slink_file_2" +#define TEST_FILE3 "test_file_3" +#define SYM_FILE3 "test_file_3/slink_file_3"#define ELOOPFILE "/test_eloop" -#define MAX_SIZE 256static char longpathname[PATH_MAX +2 ];static char elooppathname[sizeof (ELOOPFILE ) *43 ] ="." ; +static char buffer[MAX_SIZE ]; -static struct test_case_t { +static struct tcase {char *link; +char *buf;size_t buf_size;int exp_errno; -} test_cases[] = { - {SYM_FILE1 ,1 ,EACCES }, -/* Don't test with bufsize -1, since this cause a fortify-check-fail when -using glibc and -D_FORITY_SOURCE=2 - -Discussion: http://lkml.org/lkml/2008/10/23/229 -Conclusion: Only test with 0 as non-positive bufsize. - -{ SYM_FILE2, -1, EINVAL, NULL }, -*/ - {SYM_FILE2 ,0 ,EINVAL }, - {TEST_FILE2 ,1 ,EINVAL }, - {longpathname,1 ,ENAMETOOLONG }, - {"" ,1 ,ENOENT }, - {SYM_FILE3 ,1 ,ENOTDIR }, - {elooppathname,1 ,ELOOP }, +} tcases[] = { + {SYM_FILE1 , buffer,sizeof (buffer),EACCES }, +/* Don't test with bufsize -1, since this cause a fortify-check-fail +when using glibc and -D_FORITY_SOURCE=2 + +Discussion: http://lkml.org/lkml/2008/10/23/229 +Conclusion: Only test with 0 as non-positive bufsize. + +{ SYM_FILE2, -1, EINVAL, NULL }, +*/ + {SYM_FILE2 , buffer,0 ,EINVAL }, + {TEST_FILE2 , buffer,sizeof (buffer),EINVAL }, + {longpathname, buffer,sizeof (buffer),ENAMETOOLONG }, + {"" , buffer,sizeof (buffer),ENOENT }, + {SYM_FILE3 , buffer,sizeof (buffer),ENOTDIR }, + {elooppathname, buffer,sizeof (buffer),ELOOP }, + {SYMFILE , (char *)-1 ,sizeof (buffer),EFAULT }, }; -static void setup (void ); -static void readlink_verify (struct test_case_t *); -static void cleanup (void ); - -char *TCID ="readlink03" ; -int TST_TOTAL =ARRAY_SIZE (test_cases); - -int main (int ac,char **av) +static void verify_readlink (unsigned int n) { -int i, lc; - -tst_parse_opts (ac, av,NULL ,NULL ); +struct tcase *tc = &tcases[n]; -setup (); +TEST (readlink (tc->link, tc->buf, tc->buf_size)); +if (TEST_RETURN !=-1 ) { +tst_res (TFAIL ,"readlink() sueeeeded unexpectedly, errno" ); +return ; + } -for (lc =0 ;TEST_LOOPING (lc); lc++) { - tst_count =0 ; +if (TEST_ERRNO != tc->exp_errno) { +tst_res (TFAIL |TTERRNO , +"readlink() failed unexpectedly; expected: %d - %s, got" , + tc->exp_errno,tst_strerrno (tc->exp_errno)); -for (i =0 ; i <TST_TOTAL ; i++) -readlink_verify (&test_cases[i]); +if (tc->exp_errno ==ENOENT &&TEST_ERRNO ==EINVAL ) { +tst_res (TWARN |TTERRNO , +"It may be a Kernel Bug, see the patch:" +"http://git.kernel.org/linus/1fa1e7f6" ); + } + }else { +tst_res (TPASS |TTERRNO ,"readlink() failed as expected" ); } - -cleanup (); -tst_exit (); } -void setup (void ) +static void setup (void ) { -struct passwd *ltpuser;int i; +struct passwd *pwent; -tst_require_root (); - -tst_sig (NOFORK ,DEF_HANDLER , cleanup); - - ltpuser =SAFE_GETPWNAM (cleanup,"nobody" ); -SAFE_SETEUID (cleanup, ltpuser->pw_uid); - -TEST_PAUSE ; + pwent =SAFE_GETPWNAM ("nobody" ); +SAFE_SETEUID (pwent->pw_uid); -tst_tmpdir (); +SAFE_MKDIR (DIR_TEMP ,MODE_RWX ); +SAFE_TOUCH (TEST_FILE1 ,0666 ,NULL ); +SAFE_SYMLINK (TEST_FILE1 ,SYM_FILE1 ); +SAFE_CHMOD (DIR_TEMP ,FILE_MODE ); -SAFE_MKDIR (cleanup,DIR_TEMP ,MODE_RWX ); -SAFE_TOUCH (cleanup,TEST_FILE1 ,0666 ,NULL ); -SAFE_SYMLINK (cleanup,TEST_FILE1 ,SYM_FILE1 ); -SAFE_CHMOD (cleanup,DIR_TEMP ,FILE_MODE ); - -SAFE_TOUCH (cleanup,TEST_FILE2 ,0666 ,NULL ); -SAFE_SYMLINK (cleanup,TEST_FILE2 ,SYM_FILE2 ); +SAFE_TOUCH (TEST_FILE2 ,0666 ,NULL ); +SAFE_SYMLINK (TEST_FILE2 ,SYM_FILE2 );memset (longpathname,'a' ,PATH_MAX +1 ); -SAFE_TOUCH (cleanup,TEST_FILE3 ,0666 ,NULL ); +SAFE_TOUCH (TEST_FILE3 ,0666 ,NULL ); + +SAFE_MKDIR ("test_eloop" ,MODE_RWX ); +SAFE_SYMLINK ("../test_eloop" ,"test_eloop/test_eloop" ); -/* -* NOTE: the ELOOP test is written based on that the consecutive -* symlinks limit in kernel is hardwired to 40. -*/ -SAFE_MKDIR (cleanup,"test_eloop" ,MODE_RWX ); -SAFE_SYMLINK (cleanup,"../test_eloop" ,"test_eloop/test_eloop" );for (i =0 ; i <43 ; i++)strcat (elooppathname,ELOOPFILE ); -} - -void readlink_verify (struct test_case_t *tc) -{ -char buffer[MAX_SIZE ]; - -if (tc->buf_size ==1 ) - tc->buf_size =sizeof (buffer); - -TEST (readlink (tc->link, buffer, tc->buf_size)); - -if (TEST_RETURN !=-1 ) { -tst_resm (TFAIL ,"readlink() returned %ld, " -"expected -1, errno:%d" ,TEST_RETURN , - tc->exp_errno); -return ; - } -if (TEST_ERRNO == tc->exp_errno) { -tst_resm (TPASS |TTERRNO ,"readlink() failed as expected" ); - }else { -tst_resm (TFAIL |TTERRNO , -"readlink() failed unexpectedly; expected: %d - %s" , - tc->exp_errno,strerror (tc->exp_errno)); -if (tc->exp_errno ==ENOENT &&TEST_ERRNO ==EINVAL ) { -tst_resm (TWARN |TTERRNO , -"It may be a Kernel Bug, see the patch:" -"http://git.kernel.org/linus/1fa1e7f6" ); - } - } +SAFE_TOUCH (TESTFILE ,0666 ,NULL ); +SAFE_SYMLINK (TESTFILE ,SYMFILE ); } -void cleanup (void ) +static void cleanup (void ) { -if (seteuid (0 ) ==-1 ) -tst_resm (TWARN |TERRNO ,"seteuid(0) failed" ); - -tst_rmdir (); +SAFE_SETEUID (0 ); } + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (tcases), + .test = verify_readlink, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir =1 , + .needs_root =1 , +};
Signature · 1 line
1.8.3.1
PATCH 4/4 syscalls/readlink04.c: Cleanup && Convert to new API
In reply to Jinhui HuangSigned-off-by Jinhui Huang <[email protected]>
--- testcases/kernel/syscalls/readlink/readlink04.c | 137 +++++++----------------- 1 file changed, 36 insertions(+), 101 deletions(-)
testcases/kernel/syscalls/readlink/readlink04.c +36 -101
@@ -11,131 +11,62 @@* but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /* -* Test Name : readlink04 -* * Test Description : -* Verify that, readlink call will succeed to read the contents of the -* symbolic link if invoked by non-root user who is not the owner of the -* symbolic link. -* -* Expected Result: -* readlink() should return the contents of symbolic link path in the -* specified buffer on success. +* readlink() will succeed to read the contents of the symbolic link if +* invoked by non-root user who is not the owner of the symbolic link. */ -#include <stdlib.h> #include <pwd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <sys/wait.h> #include <errno.h> #include <string.h> -#include "test.h" -#include "safe_macros.h" - -char *TCID ="readlink04" ; -int TST_TOTAL =1 ; -static char *TESTFILE ="./testfile" ; -static char *SYMFILE ="slink_file" ; - -#define MAX_SIZE 256 +#include "tst_test.h" +#include "readlink.h" static int exp_val;static char buffer[MAX_SIZE ]; -static void setup (void ); -static void cleanup (void ); - -int main (int ac,char **av) +static void verify_readlink (void ) { -int lc; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - - tst_count =0 ; - -/* -* Call readlink(2) to read the contents of -* symlink into a buffer. -*/ -TEST (readlink (SYMFILE , buffer,sizeof (buffer))); - -if (TEST_RETURN ==-1 ) { -tst_resm (TFAIL |TTERRNO ,"readlink() on %s failed" , -SYMFILE ); -continue ; - } - -if (TEST_RETURN == exp_val) { -/* Check for the contents of buffer */ -if (memcmp (buffer,TESTFILE , exp_val) !=0 ) { -tst_brkm (TFAIL , cleanup,"TESTFILE %s " -"and buffer contents %s " -"differ" ,TESTFILE , buffer); - }else { -tst_resm (TPASS ,"readlink() " -"functionality on '%s' is " -"correct" ,SYMFILE ); - } +TEST (readlink (SYMFILE , buffer,sizeof (buffer))); +if (TEST_RETURN ==-1 ) +tst_res (TFAIL |TTERRNO ,"readlink() on %s failed" ,SYMFILE ); + +if (TEST_RETURN != exp_val) { +tst_res (TFAIL ,"readlink() return value %ld " +"did match, Expected %d" ,TEST_RETURN , exp_val); + }else { +if (memcmp (buffer,TESTFILE , exp_val) !=0 ) { +tst_brk (TFAIL ,"TESTFILE %s and buffer " +"contents %s differ" ,TESTFILE , buffer); }else { -tst_resm (TFAIL ,"readlink() return value %ld " -"doesn't match, Expected %d" , -TEST_RETURN , exp_val); +tst_res (TPASS ,"readlink() functionality on '%s' was " +"correct" ,SYMFILE ); } } - -cleanup (); -tst_exit (); } -#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH -static void setup (void ) {int fd;char *tmp_dir =NULL ;struct passwd *pwent; -tst_require_root (); - -TEST_PAUSE ; - -tst_tmpdir (); -/* get the name of the temporary directory */ -if ((tmp_dir =getcwd (tmp_dir,0 )) ==NULL ) -tst_brkm (TBROK ,NULL ,"getcwd failed" ); - -if ((pwent =getpwnam ("bin" )) ==NULL ) -tst_brkm (TBROK , cleanup,"getpwname() failed" ); + tmp_dir =SAFE_GETCWD (tmp_dir,0 ); + pwent =SAFE_GETPWNAM ("bin" );/* make the tmp directory belong to bin */ -SAFE_CHOWN (cleanup, tmp_dir, pwent->pw_uid, pwent->pw_gid); - -if (chmod (tmp_dir,0711 ) !=0 ) -tst_brkm (TBROK |TERRNO , cleanup,"chmod(%s) failed" , tmp_dir); +SAFE_CHOWN (tmp_dir, pwent->pw_uid, pwent->pw_gid); +SAFE_CHMOD (tmp_dir,0711 );/* create test file and symlink */ -if ((fd =open (TESTFILE ,O_RDWR |O_CREAT ,FILE_MODE )) ==-1 ) -tst_brkm (TBROK |TERRNO , cleanup,"open(%s) failed" ,TESTFILE ); - -if (close (fd)) -tst_brkm (TBROK |TERRNO , cleanup,"close(%s) failed" ,TESTFILE ); - -SAFE_SYMLINK (cleanup,TESTFILE ,SYMFILE ); + fd =SAFE_OPEN (TESTFILE ,O_RDWR |O_CREAT ,FILE_MODE ); +SAFE_CLOSE (fd); +SAFE_SYMLINK (TESTFILE ,SYMFILE );/* set up the expected return value from the readlink() call */ exp_val =strlen (TESTFILE );
@@ -144,15 +75,19 @@ static void setup(void)memset (buffer,0 ,MAX_SIZE );/* finally, change the id of the parent process to "nobody" */ -if ((pwent =getpwnam ("nobody" )) ==NULL ) -tst_brkm (TBROK , cleanup,"getpwname() failed for nobody" ); - -SAFE_SETEUID (cleanup, pwent->pw_uid); + pwent =SAFE_GETPWNAM ("nobody" ); +SAFE_SETEUID (pwent->pw_uid); }static void cleanup (void ) { -SAFE_SETEUID (NULL ,0 ); - -tst_rmdir (); +SAFE_SETEUID (0 ); } + +static struct tst_test test = { + .test_all = verify_readlink, + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir =1 , + .needs_root =1 , +};
Signature · 1 line
1.8.3.1
PATCH 1/4 syscalls/readlink.h: Move common macros to readlink.h
In reply to Jinhui HuangHi!
I've fixed a few typos and and also merged the readlink04 to readlink01, since these thestcase were nearly the same and pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]