From da8f2a565cd9522f2a3a1400e538d1853374aa08 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 6 Oct 2009 15:43:12 -0600 Subject: [PATCH] mkdir, mkdirat: add tests This test exposes failures on cygwin 1.5 and in our mkdirat emulation. * modules/mkdir-tests: New test. * tests/test-mkdir.h: New file. * tests/test-mkdir.c: Likewise. * tests/test-mkdirat.c: Likewise. * modules/openat-tests (Files): Add new files. (Makefile.am): Run new test. Signed-off-by: Eric Blake --- ChangeLog | 10 ++++++ modules/mkdir-tests | 15 ++++++++ modules/openat-tests | 7 ++-- tests/test-mkdir.c | 53 ++++++++++++++++++++++++++++ tests/test-mkdir.h | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-mkdirat.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 modules/mkdir-tests create mode 100644 tests/test-mkdir.c create mode 100644 tests/test-mkdir.h create mode 100644 tests/test-mkdirat.c diff --git a/ChangeLog b/ChangeLog index cc4b51d3f..3ce4f945b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2009-10-07 Eric Blake + + mkdir, mkdirat: add tests + * modules/mkdir-tests: New test. + * tests/test-mkdir.h: New file. + * tests/test-mkdir.c: Likewise. + * tests/test-mkdirat.c: Likewise. + * modules/openat-tests (Files): Add new files. + (Makefile.am): Run new test. + 2009-10-06 Eric Blake doc: tweak *at function documentation diff --git a/modules/mkdir-tests b/modules/mkdir-tests new file mode 100644 index 000000000..cff0a5bb8 --- /dev/null +++ b/modules/mkdir-tests @@ -0,0 +1,15 @@ +Files: +tests/test-mkdir.h +tests/test-mkdir.c + +Depends-on: +progname +stdbool +symlink + +configure.ac: + +Makefile.am: +TESTS += test-mkdir +check_PROGRAMS += test-mkdir +test_mkdir_LDADD = $(LDADD) @LIBINTL@ diff --git a/modules/openat-tests b/modules/openat-tests index 0d0ff8ce1..1440a9bf9 100644 --- a/modules/openat-tests +++ b/modules/openat-tests @@ -1,9 +1,11 @@ Files: tests/test-lstat.h +tests/test-mkdir.h tests/test-rmdir.h tests/test-stat.h tests/test-unlink.h tests/test-fstatat.c +tests/test-mkdirat.c tests/test-openat.c tests/test-unlinkat.c @@ -15,8 +17,9 @@ unlinkdir configure.ac: Makefile.am: -TESTS += test-fstatat test-openat test-unlinkat -check_PROGRAMS += test-fstatat test-openat test-unlinkat +TESTS += test-fstatat test-mkdirat test-openat test-unlinkat +check_PROGRAMS += test-fstatat test-mkdirat test-openat test-unlinkat test_fstatat_LDADD = $(LDADD) @LIBINTL@ +test_mkdirat_LDADD = $(LDADD) @LIBINTL@ test_openat_LDADD = $(LDADD) @LIBINTL@ test_unlinkat_LDADD = $(LDADD) @LIBINTL@ diff --git a/tests/test-mkdir.c b/tests/test-mkdir.c new file mode 100644 index 000000000..cc51fcef1 --- /dev/null +++ b/tests/test-mkdir.c @@ -0,0 +1,53 @@ +/* Tests of mkdir. + Copyright (C) 2009 Free Software Foundation, Inc. + + 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 3 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. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Eric Blake , 2009. */ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#define BASE "test-mkdir.t" + +#include "test-mkdir.h" + +int +main () +{ + /* Clean up any trash from prior testsuite runs. */ + ASSERT (system ("rm -rf " BASE "*") == 0); + + return test_mkdir (mkdir, true); +} diff --git a/tests/test-mkdir.h b/tests/test-mkdir.h new file mode 100644 index 000000000..1fd7fe4a5 --- /dev/null +++ b/tests/test-mkdir.h @@ -0,0 +1,98 @@ +/* Test of mkdir() function. + Copyright (C) 2009 Free Software Foundation, Inc. + + 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. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* This file is designed to test both mkdir(a,b) and + mkdirat(AT_FDCWD,a,b). FUNC is the function to test. Assumes that + BASE and ASSERT are already defined, and that appropriate headers + are already included. If PRINT, warn before skipping tests with + status 77 when symlinks are unsupported. */ + +static int +test_mkdir (int (*func) (char const *, mode_t), bool print) +{ + /* Test basic error handling. */ + ASSERT (close (creat (BASE "file", 0600)) == 0); + errno = 0; + ASSERT (func (BASE "file", 0700) == -1); + ASSERT (errno == EEXIST); + errno = 0; + ASSERT (func (BASE "file/", 0700) == -1); + ASSERT (errno == ENOTDIR || errno == EEXIST); + errno = 0; + ASSERT (func (BASE "file/dir", 0700) == -1); + ASSERT (errno == ENOTDIR || errno == ENOENT); + ASSERT (unlink (BASE "file") == 0); + errno = 0; + ASSERT (func ("", 0700) == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (func (BASE "dir/sub", 0700) == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (func (BASE "dir/.", 0700) == -1); + ASSERT (errno == ENOENT); + errno = 0; + ASSERT (func (BASE "dir/.//", 0700) == -1); + ASSERT (errno == ENOENT); + + /* Test trailing slash handling. */ + ASSERT (func (BASE "dir", 0700) == 0); + errno = 0; + ASSERT (func (BASE "dir", 0700) == -1); + ASSERT (errno == EEXIST); + ASSERT (rmdir (BASE "dir") == 0); + ASSERT (func (BASE "dir/", 0700) == 0); + errno = 0; + ASSERT (func (BASE "dir/", 0700) == -1); + ASSERT (errno == EEXIST); + ASSERT (rmdir (BASE "dir") == 0); + + /* Test symlink behavior. POSIX requires the creation of + directories through a dangling symlink with trailing slash, but + GNU does not yet implement that, so we support either behavior + for now. */ + if (symlink (BASE "dir", BASE "link")) + { + if (print) + fputs ("skipping test: symlinks not supported on this filesystem\n", + stderr); + return 77; + } + errno = 0; + ASSERT (func (BASE "link", 0700) == -1); + ASSERT (errno == EEXIST); + { + int result; + errno = 0; + result = func (BASE "link/", 0700); + if (!result) + ASSERT (rmdir (BASE "dir") == 0); + else + { + ASSERT (result == -1); + ASSERT (errno == EEXIST); + errno = 0; + ASSERT (rmdir (BASE "dir") == -1); + ASSERT (errno == ENOENT); + } + } + errno = 0; + ASSERT (func (BASE "link/.", 0700) == -1); + ASSERT (errno == ENOENT); + ASSERT (unlink (BASE "link") == 0); + + return 0; +} diff --git a/tests/test-mkdirat.c b/tests/test-mkdirat.c new file mode 100644 index 000000000..69d90b81e --- /dev/null +++ b/tests/test-mkdirat.c @@ -0,0 +1,91 @@ +/* Tests of mkdirat. + Copyright (C) 2009 Free Software Foundation, Inc. + + 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 3 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. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Eric Blake , 2009. */ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#define BASE "test-mkdirat.t" + +#include "test-mkdir.h" + +static int dfd = AT_FDCWD; + +/* Wrapper to test mkdirat like mkdir. */ +static int +do_mkdir (char const *name, mode_t mode) +{ + return mkdirat (dfd, name, mode); +} + +int +main () +{ + int result; + + /* Clean up any trash from prior testsuite runs. */ + ASSERT (system ("rm -rf " BASE "*") == 0); + + /* Test basic mkdir functionality. */ + result = test_mkdir (do_mkdir, false); + dfd = open (".", O_RDONLY); + ASSERT (0 <= dfd); + ASSERT (test_mkdir (do_mkdir, false) == result); + + /* Tests specific to mkdirat. */ + ASSERT (mkdirat (dfd, BASE "dir1", 0700) == 0); + ASSERT (chdir (BASE "dir1") == 0); + ASSERT (close (dfd) == 0); + dfd = open ("..", O_RDONLY); + ASSERT (0 <= dfd); + ASSERT (mkdirat (dfd, BASE "dir2", 0700) == 0); + ASSERT (close (dfd) == 0); + errno = 0; + ASSERT (mkdirat (dfd, BASE "dir3", 0700) == -1); + ASSERT (errno == EBADF); + dfd = open ("/dev/null", O_RDONLY); + ASSERT (0 <= dfd); + errno = 0; + ASSERT (mkdirat (dfd, "dir3", 0700) == -1); + ASSERT (errno == ENOTDIR); + ASSERT (close (dfd) == 0); + ASSERT (chdir ("..") == 0); + ASSERT (rmdir (BASE "dir1") == 0); + ASSERT (rmdir (BASE "dir2") == 0); + + return result; +} -- 2.11.0