Skip to content

Commit

Permalink
syscalls/mknodat01: Fixes.
Browse files Browse the repository at this point in the history
* Cleanup

* Use tst_exit() instead of return 0
  (the test failure wasn't propagated out at all!)

* Make use of tst_tmpdir()

* Fix test iterations with -i

* Fix dir permissions (fixes test failure)

  the positive testcase was failing because the test had not
  search permission on the directory pointed to by dirfd

* Fix typo in mkdirat01.c (misspelled name)

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
  • Loading branch information
metan-ucw committed Mar 20, 2014
1 parent 7525e62 commit bc63a8f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 159 deletions.
2 changes: 1 addition & 1 deletion testcases/kernel/syscalls/mkdirat/mkdirat01.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/******************************************************************************
*
* Copyright (c) International Business Machines Corp., 2006
* Author i Yang <yyangcdl@cn.ibm.com>
* Author: Yi Yang <yyangcdl@cn.ibm.com>
* Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
*
* This program is free software; you can redistribute it and/or modify
Expand Down
264 changes: 106 additions & 158 deletions testcases/kernel/syscalls/mknodat/mknodat01.c
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
/******************************************************************************
*
* Copyright (c) International Business Machines Corp., 2006
* Copyright (c) International Business Machines Corp., 2006
* Author: Yi Yang <yyangcdl@cn.ibm.com>
* Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
*
* 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 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 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.
*
* 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
*
* NAME
* mknodat01.c
* 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
*
* DESCRIPTION
* This test case will verify basic function of mknodat
* added by kernel 2.6.16 or up.
*
* USAGE: <for command-line>
* mknodat01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
* where:
* -c n : Run n copies simultaneously.
* -e : Turn on errno logging.
* -i n : Execute test n times.
* -I x : Execute test for x seconds.
* -p : Pause for SIGUSR1 before starting
* -P x : Pause for x seconds between iterations.
* -t : Turn on syscall timing.
*
* Author
* Yi Yang <yyangcdl@cn.ibm.com>
*
* History
* 08/23/2006 Created first by Yi Yang <yyangcdl@cn.ibm.com>
* This test case will verify basic function of mknodat
* added by kernel 2.6.16 or up.
*
*****************************************************************************/

Expand All @@ -54,166 +36,132 @@
#include <signal.h>
#include "test.h"
#include "usctest.h"
#include "linux_syscall_numbers.h"
#include "safe_macros.h"
#include "lapi/fcntl.h"
#include "mknodat.h"

#define TEST_CASES 5

void setup();
void cleanup();
void setup_every_copy();
#define PATHNAME "mknodattestdir"

static void setup(void);
static void cleanup(void);
static void clean(void);

static char testfilepath[256];
static char testfile[256];
static char testfile2[256];
static char testfile3[256];

static int dirfd, fd;
static int fd_invalid = 100;
static int fd_atcwd = AT_FDCWD;

static struct test_case {
int *dirfd;
const char *name;
int exp_ret;
int exp_errno;
} test_cases[] = {
{&dirfd, testfile, 0, 0},
{&dirfd, testfile3, 0, 0},
{&fd, testfile2, -1, ENOTDIR},
{&fd_invalid, testfile, -1, EBADF},
{&fd_atcwd, testfile, 0, 0}
};

char *TCID = "mknodat01";
int TST_TOTAL = TEST_CASES;
char pathname[256];
char testfile[256];
char testfile2[256];
char testfile3[256];
int dirfd, fd, ret;
int fds[TEST_CASES];
char *filenames[TEST_CASES];
int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0 };

dev_t dev;
int TST_TOTAL = ARRAY_SIZE(test_cases);

static dev_t dev;

static void verify_mknodat(struct test_case *test)
{
TEST(mknodat(*test->dirfd, test->name, S_IFREG, dev));

if (TEST_RETURN != test->exp_ret) {
tst_resm(TFAIL | TTERRNO,
"mknodat() returned %ld, expected %d",
TEST_RETURN, test->exp_ret);
return;
}

if (TEST_ERRNO != test->exp_errno) {
tst_resm(TFAIL | TTERRNO,
"mknodat() returned wrong errno, expected %d",
test->exp_errno);
return;
}

tst_resm(TPASS | TTERRNO, "mknodat() returned %ld", TEST_RETURN);
}

int main(int ac, char **av)
{
int lc;
char *msg;
int i;

/* Disable test if the version of the kernel is less than 2.6.16 */
if (tst_kvercmp(2, 6, 16) < 0) {
tst_resm(TWARN, "This test can only run on kernels that are ");
tst_resm(TWARN, "2.6.16 and higher");
exit(0);
}

/***************************************************************
* parse standard options
***************************************************************/
if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

/***************************************************************
* perform global setup for test
***************************************************************/
setup();

/***************************************************************
* check looping state if -c option given
***************************************************************/
for (lc = 0; TEST_LOOPING(lc); lc++) {
setup_every_copy();

tst_count = 0;

/*
* Call mknodat
*/
for (i = 0; i < TST_TOTAL; i++) {
for (i = 0; i < TST_TOTAL; i++)
verify_mknodat(test_cases + i);

TEST(mknodat(fds[i], filenames[i], S_IFREG, dev));
/* clean created nodes before next run */
clean();
}

/* check return code */
if (TEST_ERRNO == expected_errno[i]) {
cleanup();
tst_exit();
}

/***************************************************************
* only perform functional verification if flag set (-f not given)
***************************************************************/
if (STD_FUNCTIONAL_TEST) {
/* No Verification test, yet... */
tst_resm(TPASS | TTERRNO,
"mknodat() returned the "
"expected errno");
}
} else {
TEST_ERROR_LOG(TEST_ERRNO);
tst_resm(TFAIL | TTERRNO, "mknodat() failed");
}
static void setup(void)
{
char *tmpdir;

}
if (tst_kvercmp(2, 6, 16) < 0)
tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");

}
tst_sig(NOFORK, DEF_HANDLER, cleanup);

/***************************************************************
* cleanup and exit
***************************************************************/
cleanup();
TEST_PAUSE;

return (0);
}
tst_tmpdir();

void setup_every_copy()
{
/* Initialize test dir and file names */
sprintf(pathname, "mknodattestdir%d", getpid());
sprintf(testfile, "mknodattestfile%d.txt", getpid());
sprintf(testfile2, "mknodattestfile%d_2.txt", getpid());
sprintf(testfile3, "/tmp/mknodattestfile%d.txt", getpid());

ret = mkdir(pathname, 0600);
if (ret < 0) {
perror("mkdir: ");
exit(-1);
}
tmpdir = get_tst_tmpdir();
sprintf(testfilepath, PATHNAME"/mknodattestfile%d", getpid());
sprintf(testfile, "mknodattestfile%d", getpid());
sprintf(testfile2, "mknodattestfile2%d", getpid());
sprintf(testfile3, "%s/mknodattestfile3%d", tmpdir, getpid());
free(tmpdir);

dirfd = open(pathname, O_DIRECTORY);
if (dirfd < 0) {
perror("open: ");
exit(-1);
}
SAFE_MKDIR(cleanup, PATHNAME, 0700);

fd = open(testfile2, O_CREAT | O_RDWR, 0600);
if (fd < 0) {
perror("open: ");
exit(-1);
}
fds[0] = fds[1] = dirfd;
fds[2] = fd;
fds[3] = 100;
fds[4] = AT_FDCWD;

filenames[0] = filenames[3] = filenames[4] = testfile;
filenames[1] = testfile3;
filenames[2] = testfile2;
dirfd = SAFE_OPEN(cleanup, PATHNAME, O_DIRECTORY);
fd = SAFE_OPEN(cleanup, testfile2, O_CREAT | O_RDWR, 0600);
}

/***************************************************************
* setup() - performs all ONE TIME setup for this test.
***************************************************************/
void setup()
static void clean(void)
{
/* Set dev to 0 */
memset(&dev, 0, sizeof(dev_t));

tst_sig(NOFORK, DEF_HANDLER, cleanup);

TEST_PAUSE;
SAFE_UNLINK(cleanup, testfilepath);
SAFE_UNLINK(cleanup, testfile3);
SAFE_UNLINK(cleanup, testfile);
}

/***************************************************************
* cleanup() - performs all ONE TIME cleanup for this test at
* completion or premature exit.
***************************************************************/
void cleanup()
static void cleanup(void)
{
/* Remove them */
char tmppathname[256];
strcpy(tmppathname, pathname);
unlink(strcat(strcat(tmppathname, "/"), testfile));
unlink(testfile);
strcpy(tmppathname, pathname);
unlink(strcat(strcat(tmppathname, "/"), testfile2));
unlink(testfile2);
unlink(testfile3);
rmdir(pathname);

/*
* print timing stats if that option was specified.
* print errno log if that option was specified.
*/
TEST_CLEANUP;
if (dirfd > 0 && close(dirfd))
tst_resm(TWARN | TERRNO, "Failed to close(dirfd)");

if (fd > 0 && close(fd))
tst_resm(TWARN | TERRNO, "Failed to close(fd)");

tst_rmdir();

TEST_CLEANUP;
}

0 comments on commit bc63a8f

Please sign in to comment.