cleanup of creat06.c

Signed-off-by: Zeng Linggang <[email protected]>
---
 testcases/kernel/syscalls/creat/creat06.c | 142 ++++++++++--------------------
 1 file changed, 48 insertions(+), 94 deletions(-)

diff --git a/testcases/kernel/syscalls/creat/creat06.c 
b/testcases/kernel/syscalls/creat/creat06.c
index 51e46ab..2b1a9ec 100644
--- a/testcases/kernel/syscalls/creat/creat06.c
+++ b/testcases/kernel/syscalls/creat/creat06.c
@@ -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,61 +70,43 @@
 #include <fcntl.h>
 #include "test.h"
 #include "usctest.h"
+#include "safe_macros.h"
 
-void setup(void);
-void cleanup(void);
+#define        TEST_FILE       "test_dir"
+#define        NO_DIR          "testfile/testdir"
+#define        NOT_DIR         "file1/testdir"
+#define        TEST6_FILE      "dir6/file6"
 
-char user1name[] = "nobody";
+#define        MODE1           0444
+#define        MODE2           0666
 
-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";
+static void setup(void);
+static void cleanup(void);
+#if !defined(UCLINUX)
+static void bad_addr_setup(int);
+#endif
 
-struct passwd *ltpuser;
-struct test_case_t {
+static char long_name[PATH_MAX+2];
+static struct test_case_t {
        char *fname;
        int mode;
        int error;
+       void (*setup)();
 } 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},
+       {TEST_FILE, 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, bad_addr_setup},
 #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)
 {
@@ -137,17 +119,17 @@ int main(int ac, char **av)
 
        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 != NULL)
+                               TC[i].setup(i);
+
                        TEST(creat(TC[i].fname, TC[i].mode));
 
                        if (TEST_RETURN != -1) {
@@ -155,8 +137,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");
@@ -166,26 +146,21 @@ int main(int ac, char **av)
                        }
                }
        }
+
        cleanup();
 
        tst_exit();
 }
 
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup()
+static void setup(void)
 {
-       char *cur_dir = NULL;
+       struct passwd *ltpuser;
 
        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 +168,26 @@ 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");
-       }
+       SAFE_MKDIR(cleanup, TEST_FILE, MODE2);
 
-       strncpy(good_dir, cur_dir, NSIZE);
+       memset(long_name, 'a', PATH_MAX+1);
 
-       /* 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 !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");
-       }
-       TC[4].fname = bad_addr;
-#endif
+       SAFE_TOUCH(cleanup, "file1", MODE1, NULL);
 
-       /* 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()
+#if !defined(UCLINUX)
+static void bad_addr_setup(int i)
 {
-       /*
-        * print timing stats if that option was specified.
-        * print errno log if that option was specified.
-        */
-       close(fileHandle);
+       TC[i].fname = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
+                            MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+}
+#endif
 
+static void cleanup(void)
+{
        TEST_CLEANUP;
 
-       /* delete the test directory created in setup() */
        tst_rmdir();
-
 }
-- 
1.8.2.1




------------------------------------------------------------------------------
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

Reply via email to