Style, other cleanup, and simplification.
In particular I simplified how cleanup is run and the EACCES functional test.
Signed-off-by: Garrett Cooper <yanegomi@gmail.com>
diff --git a/testcases/kernel/syscalls/chown/chown04.c b/testcases/kernel/syscalls/chown/chown04.c
index 59a45e9..f69d822 100644
--- a/testcases/kernel/syscalls/chown/chown04.c
+++ b/testcases/kernel/syscalls/chown/chown04.c
@@ -92,83 +92,62 @@
#include "test.h"
#include "usctest.h"
-#define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO
-#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
+#define MODE_RWX (S_IRWXU|S_IRWXG|S_IRWXO)
+#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define DIR_TEMP "testdir_1"
#define TEST_FILE1 "tfile_1"
-#define TEST_FILE2 "testdir_1/tfile_2"
+#define TEST_FILE2 (DIR_TEMP "/tfile_2")
#define TEST_FILE3 "t_file/tfile_3"
-int no_setup();
-int setup1(); /* setup function to test chown for EPERM */
-int setup2(); /* setup function to test chown for EACCES */
-int setup3(); /* setup function to test chown for ENOTDIR */
-int longpath_setup(); /* setup function to test chown for ENAMETOOLONG */
-
-char *get_high_address(); /* Function from ltp-lib */
+void setup1();
+void setup2();
+void setup3();
+void longpath_setup();
char Longpathname[PATH_MAX + 2];
-char High_address_node[64];
+char high_address_node[64];
-struct test_case_t { /* test case struct. to hold ref. test cond's */
+struct test_case_t {
char *pathname;
- char *desc;
int exp_errno;
- int (*setupfunc) ();
-} Test_cases[] = {
- {
- TEST_FILE1, "Process is not owner/root", EPERM, setup1}, {
- TEST_FILE2, "No Search permissions to process", EACCES, setup2}, {
- High_address_node, "Address beyond address space", EFAULT, no_setup},
- {
- (char *)-1, "Negative address", EFAULT, no_setup}, {
- Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup}, {
- "", "Pathname is empty", ENOENT, no_setup}, {
- TEST_FILE3, "Path contains regular file", ENOTDIR, setup3}, {
- NULL, NULL, 0, no_setup}
+ void (*setupfunc)(void);
+} test_cases[] = {
+ { TEST_FILE1, EPERM, setup1 },
+ { TEST_FILE2, EACCES, setup2 },
+ { high_address_node, EFAULT, NULL },
+ { (char *)-1, EFAULT, NULL },
+ { Longpathname, ENAMETOOLONG, longpath_setup},
+ { "", ENOENT, NULL},
+ { TEST_FILE3, ENOTDIR, setup3},
};
-char *TCID = "chown04"; /* Test program identifier. */
-int TST_TOTAL = 7; /* Total number of test cases. */
-extern int Tst_count; /* Test Case counter for tst_* routines */
+char *TCID = "chown04";
+int TST_TOTAL = sizeof(test_cases) / sizeof(*test_cases);
int exp_enos[] = { EPERM, EACCES, EFAULT, ENAMETOOLONG, ENOENT, ENOTDIR, 0 };
-char nobody_uid[] = "nobody";
struct passwd *ltpuser;
-char test_home[PATH_MAX]; /* variable to hold TESTHOME env */
char *bad_addr = 0;
-void setup(); /* Main setup function for the tests */
-void cleanup(); /* cleanup function for the test */
+void setup();
+void cleanup();
int main(int ac, char **av)
{
- int lc; /* loop counter */
- char *msg; /* message returned from parse_opts */
- char *file_name; /* ptr. for file name whose mode is modified */
- char *test_desc; /* test specific error message */
- int ind; /* counter to test different test conditions */
- uid_t user_id; /* Effective user id of a test process */
- gid_t group_id; /* Effective group id of a test process */
+ int lc;
+ char *msg;
+ char *file_name;
+ int i;
+ uid_t user_id;
+ gid_t group_id;
- /* Parse standard options given to run the test. */
- msg = parse_opts(ac, av, NULL, NULL);
- if (msg != NULL) {
+ if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
- }
-
- /*
- * Invoke setup function to call individual test setup functions
- * to simulate test conditions.
- */
setup();
- /* set the expected errnos... */
TEST_EXP_ENOS(exp_enos);
- /* Set uid/gid values to that of test process */
user_id = geteuid();
group_id = getegid();
@@ -176,244 +155,140 @@
Tst_count = 0;
- for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
- file_name = Test_cases[ind].pathname;
- test_desc = Test_cases[ind].desc;
+ for (i = 0; i < TST_TOTAL; i++) {
+ file_name = test_cases[i].pathname;
- if (file_name == High_address_node) {
- file_name = (char *)get_high_address();
- }
+ if (file_name == high_address_node)
+ file_name = get_high_address();
- /*
- * Call chown(2) to test different test conditions.
- * verify that it fails with -1 return value and
- * sets appropriate errno.
- */
TEST(chown(file_name, user_id, group_id));
- /* Check return code from chown(2) */
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "chown() returned %ld, expected "
- "-1, errno:%d", TEST_RETURN,
- Test_cases[ind].exp_errno);
+ if (TEST_RETURN == 0) {
+ tst_resm(TFAIL, "chown succeeded unexpectedly");
continue;
- }
-
- TEST_ERROR_LOG(TEST_ERRNO);
-
- if (TEST_ERRNO == Test_cases[ind].exp_errno) {
- tst_resm(TPASS|TTERRNO,
- "chown() fails, %s",
- test_desc);
- } else {
+ } else if (TEST_ERRNO == test_cases[i].exp_errno)
+ tst_resm(TPASS|TTERRNO, "chown failed");
+ else {
tst_resm(TFAIL|TTERRNO,
- "chown() fails, %s, expected errno:%d",
- test_desc, Test_cases[ind].exp_errno);
+ "chown failed; expected: %d - %s",
+ test_cases[i].exp_errno,
+ strerror(test_cases[i].exp_errno));
}
}
}
- /*
- * Invoke cleanup() to delete the test directory/file(s) created
- * in the setup().
- */
cleanup();
+ tst_exit();
}
-/*
- * void
- * setup(void) - performs all ONE TIME setup for this test.
- * Exit the test program on receipt of unexpected signals.
- * Create a temporary directory and change directory to it.
- * Invoke individual test setup functions according to the order
- * set in struct. definition.
- */
void setup()
{
- int ind;
+ int i;
- /* Capture unexpected signals */
+ tst_require_root(NULL);
+
tst_sig(FORK, DEF_HANDLER, cleanup);
- if (getcwd(test_home, sizeof(test_home)) == NULL)
- tst_brkm(TBROK|TERRNO, cleanup,
- "getcwd(3) fails to get working directory of process");
-
- /* Switch to nobody user for correct error code collection */
- if (geteuid() != 0) {
- tst_brkm(TBROK, NULL, "Test must be run as root");
- }
- ltpuser = getpwnam(nobody_uid);
+ ltpuser = getpwnam("nobody");
+ if (ltpuser == NULL)
+ tst_brkm(TBROK|TERRNO, NULL, "getpwnam(\"nobody\") failed");
if (seteuid(ltpuser->pw_uid) == -1)
- tst_resm(TINFO|TERRNO, "seteuid(%d) failed", ltpuser->pw_uid);
+ tst_brkm(TBROK|TERRNO, NULL, "seteuid(%d) failed",
+ ltpuser->pw_uid);
TEST_PAUSE;
- /* Make a temp dir and cd to it */
tst_tmpdir();
bad_addr = mmap(0, 1, PROT_NONE,
- MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
- if (bad_addr == MAP_FAILED) {
+ MAP_PRIVATE_EXCEPT_UCLINUX|MAP_ANONYMOUS, 0, 0);
+ if (bad_addr == MAP_FAILED)
tst_brkm(TBROK|TERRNO, cleanup, "mmap failed");
- }
- Test_cases[3].pathname = bad_addr;
+ test_cases[3].pathname = bad_addr;
- /* call individual setup functions */
- for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
- Test_cases[ind].setupfunc();
- }
+ for (i = 0; i < TST_TOTAL; i++)
+ if (test_cases[i].setupfunc != NULL)
+ test_cases[i].setupfunc();
}
-/*
- * int
- * no_setup() - Some test conditions for mknod(2) do not any setup.
- * Hence, this function just returns 0.
- * This function simply returns 0.
- */
-int no_setup()
+void setup1()
{
- return 0;
-}
-
-/*
- * int
- * setup1() - setup function for a test condition for which chown(2)
- * returns -1 and sets errno to EPERM.
- *
- * Create a testfile under temporary directory and invoke setuid to root
- * program to change the ownership of testfile to that of "ltpuser2" user.
- *
- */
-int setup1()
-{
- int fd; /* file handle for testfile */
+ int fd;
uid_t old_uid;
old_uid = geteuid();
- fd = open(TEST_FILE1, O_RDWR|O_CREAT, 0666);
- if (fd == -1)
- tst_brkm(TBROK|TERRNO, cleanup,
- "open(%s, O_RDWR|O_CREAT, 0666) failed",
- TEST_FILE1);
+ if ((fd = open(TEST_FILE1, O_RDWR|O_CREAT, 0666)) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "opening \"%s\" failed",
+ TEST_FILE1);
- seteuid(0);
- if (fchown(fd, 0, 0) < 0)
- tst_brkm(TBROK|TERRNO, cleanup,
- "Fail to modify %s ownership(s)!", TEST_FILE1);
+ if (seteuid(0) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "seteuid(0) failed");
- seteuid(old_uid);
+ if (fchown(fd, 0, 0) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "fchown failed");
if (close(fd) == -1)
- tst_brkm(TBROK|TERRNO, cleanup, "close(%s) failed",
- TEST_FILE1);
+ tst_brkm(TBROK|TERRNO, cleanup, "closing \"%s\" failed",
+ TEST_FILE1);
- return 0;
+ if (seteuid(old_uid) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "seteuid(%d) failed", old_uid);
+
}
-/*
- * int
- * setup2() - setup function for a test condition for which chown(2)
- * returns -1 and sets errno to EACCES.
- * Create a test directory under temporary directory and create a test file
- * under this directory with mode "0666" permissions.
- * Modify the mode permissions on test directory such that process will not
- * have search permissions on test directory.
- *
- * The function returns 0.
- */
-int setup2()
+void setup2()
{
- int fd; /* file handle for testfile */
+ int fd;
+ uid_t old_uid;
- /* Creat a test directory */
- if (mkdir(DIR_TEMP, MODE_RWX) < 0)
- tst_brkm(TBROK|TERRNO, cleanup, "mkdir(%s) failed", DIR_TEMP);
+ old_uid = geteuid();
- /* Creat a testfile under above test directory */
- fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666);
- if (fd == -1)
- tst_brkm(TBROK|TERRNO, cleanup,
- "open(%s, O_RDWR|O_CREAT, 0666) failed",
- TEST_FILE2);
+ if (seteuid(0) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "seteuid(0) failed");
- /* Close the testfile created */
+ if (mkdir(DIR_TEMP, S_IRWXU) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "mkdir failed");
+
+ if ((fd = open(TEST_FILE2, O_RDWR|O_CREAT, 0666)) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "fchown failed");
+
if (close(fd) == -1)
- tst_brkm(TBROK|TERRNO, cleanup,
- "close(%s) failed", TEST_FILE2);
+ tst_brkm(TBROK|TERRNO, cleanup, "closing \"%s\" failed",
+ TEST_FILE2);
- /* Modify mode permissions on test directory */
- if (chmod(DIR_TEMP, FILE_MODE) < 0)
- tst_brkm(TBROK|TERRNO, cleanup, "chmod(%s) failed", DIR_TEMP);
+ if (seteuid(old_uid) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "seteuid(%d) failed", old_uid);
- return 0;
}
-/*
- * int
- * setup3() - setup function for a test condition for which chown(2)
- * returns -1 and sets errno to ENOTDIR.
- *
- * Create a test file under temporary directory so that test tries to
- * change mode of a testfile "tfile_3" under "t_file" which happens to be
- * another regular file.
- */
-int setup3()
+void setup3()
{
- int fd; /* file handle for test file */
+ int fd;
- /* Creat a testfile under temporary directory */
- fd = open("t_file", O_RDWR | O_CREAT, MODE_RWX);
- if (fd == -1)
- tst_brkm(TBROK|TERRNO, cleanup, "open(t_file) failed");
-
- /* close the test file */
+ if ((fd = open("t_file", O_RDWR|O_CREAT, MODE_RWX)) == -1)
+ tst_brkm(TBROK|TERRNO, cleanup, "opening \"t_file\" failed");
if (close(fd) == -1)
- tst_brkm(TBROK|TERRNO, cleanup, "close(t_file) failed");
-
- return 0;
+ tst_brkm(TBROK|TERRNO, cleanup, "closing \"t_file\" failed");
}
-/*
- * int
- * longpath_setup() - setup to create a node with a name length exceeding
- * the MAX. length of PATH_MAX.
- * This function retruns 0.
- */
-int longpath_setup()
+void longpath_setup()
{
- int ind; /* counter variable */
+ int i;
- for (ind = 0; ind <= (PATH_MAX + 1); ind++) {
- Longpathname[ind] = 'a';
- }
- return 0;
+ for (i = 0; i <= (PATH_MAX + 1); i++)
+ Longpathname[i] = 'a';
}
-/*
- * void
- * cleanup() - Performs all ONE TIME cleanup for this test at
- * completion or premature exit.
- * Print test timing stats and errno log if test executed with options.
- * Remove temporary directory and sub-directories/files under it
- * created during setup().
- * Exit the test program with normal exit code.
- */
void cleanup()
{
- /*
- * print timing stats if that option was specified.
- * print errno log if that option was specified.
- */
TEST_CLEANUP;
- /* Restore mode permissions on test directory created in setup2() */
- if (chmod(DIR_TEMP, MODE_RWX) < 0)
- tst_resm(TBROK|TERRNO, "chmod(%s) failed", DIR_TEMP);
+ if (seteuid(0) == -1)
+ tst_resm(TWARN|TERRNO, "seteuid(0) failed");
tst_rmdir();
-}
\ No newline at end of file
+}