creat/creat06.c: cleanup
PATCH 1/1 creat/creat06.c: cleanup
cleanup of creat06.c
--- testcases/kernel/syscalls/creat/creat06.c | 146 ++++++++++-------------------- 1 file changed, 48 insertions(+), 98 deletions(-)
testcases/kernel/syscalls/creat/creat06.c +48 -98
@@ -13,8 +13,8 @@* 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 +* along with this program; if not, write to the Free Software Foundation, +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /*
@@ -70,84 +70,62 @@#include <fcntl.h> #include "test.h" #include "usctest.h" - -void setup (void ); -void cleanup (void ); - -char user1name[] ="nobody" ; - -char *TCID ="creat06" ; -int fileHandle =0 ; - -int exp_enos[] = {EISDIR ,ENAMETOOLONG ,ENOENT ,ENOTDIR ,EFAULT ,EACCES ,0 }; - -#define MODE1 0444 -#define MODE2 0666 -#define NSIZE 1000 - -char long_name[] = -"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz" ; - -char good_dir[NSIZE ]; -char no_dir[] ="testfile/testdir" ; -char not_dir[] ="file1/testdir" ; -char test6_file[] ="dir6/file6" ; -char nobody_uid[] ="nobody" ; - -struct passwd *ltpuser; -struct test_case_t { +#include "safe_macros.h" + +static void setup (void ); +static void cleanup (void ); + +#define MODE1 0444 +#define MODE2 0666 +#define NSIZE 1000 +#define NO_DIR "testfile/testdir" +#define NOT_DIR "file1/testdir" +#define TEST6_FILE "dir6/file6" + +static char long_name[PATH_MAX +2 ]; +static char good_dir[NSIZE ]; +static int filehandle; +static char *bad_addr; +static struct passwd *ltpuser; +static struct test_case_t {char *fname;int mode;int error; +void (*setup)(void ); }TC [] = { -/* The file name is an existing directory */ - { - good_dir,MODE1 ,EISDIR }, -/* The file name is too long - ENAMETOOLONG */ - { - long_name,MODE1 ,ENAMETOOLONG }, -/* Attempt to create a file in a directory that doesn't exist - ENOENT */ - { - no_dir,MODE1 ,ENOENT }, -/* a compent of the file's path is not a directory - ENOTDIR */ - { - not_dir,MODE1 ,ENOTDIR }, + {good_dir,MODE1 ,EISDIR ,NULL }, + {long_name,MODE1 ,ENAMETOOLONG ,NULL }, + {NO_DIR ,MODE1 ,ENOENT ,NULL }, + {NOT_DIR ,MODE1 ,ENOTDIR ,NULL }, #if !defined(UCLINUX) -/* The file address is bad - EFAULT */ - { - (char *)-1 ,MODE1 ,EFAULT }, + {(char *)-1 ,MODE1 ,EFAULT ,NULL }, #endif -/* The directory lacks execute permission - EACCES */ - { - test6_file,MODE1 ,EACCES } + {TEST6_FILE ,MODE1 ,EACCES ,NULL }, }; -int TST_TOTAL = (sizeof (TC ) /sizeof (*TC )); - -char *bad_addr =0 ; +char *TCID ="creat06" ; +int TST_TOTAL =ARRAY_SIZE (TC ); +static int exp_enos[] = {EISDIR ,ENAMETOOLONG ,ENOENT ,ENOTDIR , +EFAULT ,EACCES ,0 };int main (int ac,char **av) {int lc; -char *msg;int i; -if ((msg =parse_opts (ac, av,NULL ,NULL )) !=NULL ) -tst_brkm (TBROK ,NULL ,"OPTION PARSING ERROR - %s" , msg); -setup (); -/* set up the expected errnos */ TEST_EXP_ENOS (exp_enos);for (lc =0 ;TEST_LOOPING (lc); lc++) { -/* reset tst_count in case we are looping */ tst_count =0 ; -/* loop through the test cases */ for (i =0 ; i <TST_TOTAL ; i++) { +if (TC [i].setup) +TC [i].setup (); +TEST (creat (TC [i].fname,TC [i].mode));if (TEST_RETURN !=-1 ) {
@@ -155,8 +133,6 @@ int main(int ac, char **av)continue ; } -TEST_ERROR_LOG (TEST_ERRNO ); -if (TEST_ERRNO ==TC [i].error) {tst_resm (TPASS |TTERRNO ,"got expected failure" );
@@ -171,21 +147,15 @@ int main(int ac, char **av)tst_exit (); } -/* -* setup() - performs all ONE TIME setup for this test. -*/ -void setup () +static void setup (void ) {char *cur_dir =NULL ;tst_require_root (NULL ); - ltpuser =getpwnam (nobody_uid); -if (ltpuser ==NULL ) -tst_brkm (TBROK |TERRNO , cleanup,"getpwnam failed" ); -if (seteuid (ltpuser->pw_uid) ==-1 ) -tst_brkm (TBROK |TERRNO , cleanup, -"seteuid(%d) failed" , ltpuser->pw_uid); + ltpuser =SAFE_GETPWNAM (cleanup,"nobody" ); + +SAFE_SETEUID (cleanup, ltpuser->pw_uid);tst_sig (NOFORK ,DEF_HANDLER , cleanup);
@@ -193,47 +163,27 @@ void setup()tst_tmpdir (); -/* get the current directory for the first test */ -if ((cur_dir =getcwd (cur_dir,0 )) ==NULL ) { -tst_brkm (TBROK |TERRNO , cleanup,"getcwd failed" ); - } + cur_dir =SAFE_GETCWD (cleanup, cur_dir,0 );strncpy (good_dir, cur_dir,NSIZE ); -/* create a file that will be used in test #3 */ -if ((fileHandle =creat ("file1" ,MODE1 )) ==-1 ) { -tst_brkm (TBROK , cleanup,"couldn't create a test file" ); - } +if (memset (long_name,'a' ,PATH_MAX +1 ) ==NULL ) +tst_brkm (TBROK |TERRNO , cleanup,"getcwd failed" ); + + filehandle =SAFE_CREAT (cleanup,"file1" ,MODE1 ); +#if !defined(UCLINUX ) - bad_addr =mmap (0 ,1 ,PROT_NONE , -MAP_PRIVATE_EXCEPT_UCLINUX |MAP_ANONYMOUS ,0 ,0 ); -if (bad_addr ==MAP_FAILED ) { -tst_brkm (TBROK |TERRNO , cleanup,"mmap failed" ); - } + bad_addr =SAFE_MMAP (cleanup,0 ,1 ,PROT_NONE , +MAP_PRIVATE_EXCEPT_UCLINUX |MAP_ANONYMOUS ,0 ,0 );TC [4 ].fname = bad_addr;#endif -/* create a directory that will be used in test #6 */ -if (mkdir ("dir6" ,MODE2 ) ==-1 ) { -tst_brkm (TBROK , cleanup,"couldn't creat a test directory" ); - } +SAFE_MKDIR (cleanup,"dir6" ,MODE2 ); } -/* -* cleanup() - performs all ONE TIME cleanup for this test at -* completion or premature exit. -*/ -void cleanup () +static void cleanup (void ) { -/* -* print timing stats if that option was specified. -* print errno log if that option was specified. -*/ -close (fileHandle); -TEST_CLEANUP ; -/* delete the test directory created in setup() */ tst_rmdir (); - }
Signature · 15 lines
1.8.2.1 ------------------------------------------------------------------------------ DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access Free app hosting. Or install the open source package on any LAMP server. Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native! http://pubads.g.doubleclick.net/gampad/clk?id=63469471&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
Hi!
Quoted 59 lines
cleanup of
creat06.cSigned-off-by Zeng Linggang <[email protected]>--- testcases/kernel/syscalls/creat/creat06.c | 146 ++++++++++-------------------- 1 file changed, 48 insertions(+), 98 deletions(-)testcases/kernel/syscalls/creat/creat06.c +9
-28@@ -13,8 +13,8 @@* 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 +* along with this program; if not, write to the Free Software Foundation, +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /* @@ -70,84 +70,62 @@#include <fcntl.h> #include "test.h" #include "usctest.h" - -void setup (void ); -void cleanup (void ); - -char user1name[] ="nobody" ; - -char *TCID ="creat06" ; -int fileHandle =0 ; - -int exp_enos[] = {EISDIR ,ENAMETOOLONG ,ENOENT ,ENOTDIR ,EFAULT ,EACCES ,0 }; - -#define MODE1 0444 -#define MODE2 0666 -#define NSIZE 1000 - -char long_name[] = -"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz" ; - -char good_dir[NSIZE ]; -char no_dir[] ="testfile/testdir" ; -char not_dir[] ="file1/testdir" ; -char test6_file[] ="dir6/file6" ; -char nobody_uid[] ="nobody" ; - -struct passwd *ltpuser; -struct test_case_t { +#include "safe_macros.h" + +static void setup (void ); +static void cleanup (void ); + +#define MODE1 0444 +#define MODE2 0666
testcases/kernel/syscalls/creat/creat06.c line 89
Quoted 10 lines
testcases/kernel/syscalls/creat/creat06.c +10
-0+#define NSIZE 1000 +#define NO_DIR "testfile/testdir" +#define NOT_DIR "file1/testdir" +#define TEST6_FILE "dir6/file6" + +static char long_name[PATH_MAX +2 ]; +static char good_dir[NSIZE ]; +static int filehandle; +static char *bad_addr; +static struct passwd *ltpuser;
The ltpuser variable don't need to be a global one, it's used only in the setup, the same goes for bad_addr.
Quoted 106 lines
testcases/kernel/syscalls/creat/creat06.c +19
-40+static struct test_case_t {char *fname;int mode;int error; +void (*setup)(void ); }TC [] = { - /* The file name is an existing directory */ - { - good_dir, MODE1, EISDIR}, - /* The file name is too long - ENAMETOOLONG */ - { - long_name, MODE1, ENAMETOOLONG}, - /* Attempt to create a file in a directory that doesn't exist - ENOENT */ - { - no_dir, MODE1, ENOENT}, - /* a compent of the file's path is not a directory - ENOTDIR */ - { - not_dir, MODE1, ENOTDIR}, + {good_dir,MODE1 ,EISDIR ,NULL }, + {long_name,MODE1 ,ENAMETOOLONG ,NULL }, + {NO_DIR ,MODE1 ,ENOENT ,NULL }, + {NOT_DIR ,MODE1 ,ENOTDIR ,NULL }, #if !defined(UCLINUX) - /* The file address is bad - EFAULT */ - { - (char *)-1, MODE1, EFAULT}, + {(char *)-1 ,MODE1 ,EFAULT ,NULL }, #endif - /* The directory lacks execute permission - EACCES */ - { - test6_file, MODE1, EACCES} + {TEST6_FILE ,MODE1 ,EACCES ,NULL }, }; -int TST_TOTAL = (sizeof(TC) / sizeof(*TC)); - -char *bad_addr = 0; +char *TCID ="creat06" ; +int TST_TOTAL =ARRAY_SIZE (TC ); +static int exp_enos[] = {EISDIR ,ENAMETOOLONG ,ENOENT ,ENOTDIR , +EFAULT ,EACCES ,0 };int main (int ac,char **av) {int lc; - char *msg;int i; - if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) - tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); -setup (); - /* set up the expected errnos */TEST_EXP_ENOS (exp_enos);for (lc =0 ;TEST_LOOPING (lc); lc++) { - /* reset tst_count in case we are looping */ tst_count =0 ; - /* loop through the test cases */for (i =0 ; i <TST_TOTAL ; i++) { +if (TC [i].setup) +TC [i].setup (); +TEST (creat (TC [i].fname,TC [i].mode));if (TEST_RETURN !=-1 ) {@@ -155,8 +133,6 @@ int main(int ac, char **av)continue ; } -TEST_ERROR_LOG (TEST_ERRNO ); -if (TEST_ERRNO ==TC [i].error) {tst_resm (TPASS |TTERRNO ,"got expected failure" );@@ -171,21 +147,15 @@ int main(int ac, char **av)tst_exit (); } -/* -* setup() - performs all ONE TIME setup for this test. -*/ -void setup () +static void setup (void ) {char *cur_dir =NULL ;tst_require_root (NULL ); - ltpuser =getpwnam (nobody_uid); -if (ltpuser ==NULL ) -tst_brkm (TBROK |TERRNO , cleanup,"getpwnam failed" ); -if (seteuid (ltpuser->pw_uid) ==-1 ) -tst_brkm (TBROK |TERRNO , cleanup, -"seteuid(%d) failed" , ltpuser->pw_uid); + ltpuser =SAFE_GETPWNAM (cleanup,"nobody" ); + +SAFE_SETEUID (cleanup, ltpuser->pw_uid);tst_sig (NOFORK ,DEF_HANDLER , cleanup);
testcases/kernel/syscalls/creat/creat06.c void setup() · line 168
Quoted 10 lines
testcases/kernel/syscalls/creat/creat06.c +1
-4@@ -193,47 +163,27 @@ void setup()tst_tmpdir (); -/* get the current directory for the first test */ -if ((cur_dir =getcwd (cur_dir,0 )) ==NULL ) { -tst_brkm (TBROK |TERRNO , cleanup,"getcwd failed" ); - } + cur_dir =SAFE_GETCWD (cleanup, cur_dir,0 );strncpy (good_dir, cur_dir,NSIZE );
What about doing one SAVE_MKDIR(cleanup, TEST_DIR, MODE) and passing the TEST_DIR to the first testcase instead? That would save us time while fidling with the absolute path...
testcases/kernel/syscalls/creat/creat06.c void setup() · line 171
Quoted 6 lines
testcases/kernel/syscalls/creat/creat06.c +2
-4-/* create a file that will be used in test #3 */ -if ((fileHandle =creat ("file1" ,MODE1 )) ==-1 ) { -tst_brkm (TBROK , cleanup,"couldn't create a test file" ); - } +if (memset (long_name,'a' ,PATH_MAX +1 ) ==NULL ) +tst_brkm (TBROK |TERRNO , cleanup,"getcwd failed" );
This part is wrong, memset() can't fail.
testcases/kernel/syscalls/creat/creat06.c void setup() · line 173
Quoted 2 lines
testcases/kernel/syscalls/creat/creat06.c +2
-0+ + filehandle =SAFE_CREAT (cleanup,"file1" ,MODE1 );
This file descriptor is not used at all. You should use SAFE_TOUCH() instead of the SAFE_CREAT() to just create the file.
testcases/kernel/syscalls/creat/creat06.c void setup() · line 177
Quoted 8 lines
testcases/kernel/syscalls/creat/creat06.c +2
-5#if !defined(UCLINUX ) - bad_addr =mmap (0 ,1 ,PROT_NONE , -MAP_PRIVATE_EXCEPT_UCLINUX |MAP_ANONYMOUS ,0 ,0 ); -if (bad_addr ==MAP_FAILED ) { -tst_brkm (TBROK |TERRNO , cleanup,"mmap failed" ); - } + bad_addr =SAFE_MMAP (cleanup,0 ,1 ,PROT_NONE , +MAP_PRIVATE_EXCEPT_UCLINUX |MAP_ANONYMOUS ,0 ,0 );
The MAP_PRIVATE_EXCEPT_UCLINUX is not usefull here because you are inside of the #if !defined(UCLINUX), you can simply use MAP_PRIVATE instead.
testcases/kernel/syscalls/creat/creat06.c void setup() · line 178
Quoted 1 line
testcases/kernel/syscalls/creat/creat06.c +0
-0TC [4 ].fname = bad_addr;
This is hacked up. You should use the per testcase setup function to assign the value instead.
Quoted 8 lines
testcases/kernel/syscalls/creat/creat06.c +1
-4#endif -/* create a directory that will be used in test #6 */ -if (mkdir ("dir6" ,MODE2 ) ==-1 ) { -tst_brkm (TBROK , cleanup,"couldn't creat a test directory" ); - } +SAFE_MKDIR (cleanup,"dir6" ,MODE2 ); }
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Rapidly troubleshoot problems before they affect your business. Most IT organizations don't have a clear picture of how application performance affects their revenue. With AppDynamics, you get 100% visibility into your Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro! http://pubads.g.doubleclick.net/gampad/clk?id=84349351&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
Hi!
testcases/kernel/syscalls/creat/creat06.c line 137
Quoted 9 lines
int
main(int ac, char **av)
{testcases/kernel/syscalls/creat/creat06.c +0
-4int lc; -char *msg;int i; -if ((msg =parse_opts (ac, av,NULL ,NULL )) !=NULL ) -tst_brkm (TBROK ,NULL ,"OPTION PARSING ERROR - %s" , msg); -
Also do not remove the standard option parsing code, it's useful even when the test does not add any additional opions.
Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Rapidly troubleshoot problems before they affect your business. Most IT organizations don't have a clear picture of how application performance affects their revenue. With AppDynamics, you get 100% visibility into your Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro! http://pubads.g.doubleclick.net/gampad/clk?id=84349351&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list