mkdir, mkdirat: add tests
authorEric Blake <ebb9@byu.net>
Tue, 6 Oct 2009 21:43:12 +0000 (15:43 -0600)
committerEric Blake <ebb9@byu.net>
Wed, 7 Oct 2009 13:12:26 +0000 (07:12 -0600)
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 <ebb9@byu.net>
ChangeLog
modules/mkdir-tests [new file with mode: 0644]
modules/openat-tests
tests/test-mkdir.c [new file with mode: 0644]
tests/test-mkdir.h [new file with mode: 0644]
tests/test-mkdirat.c [new file with mode: 0644]

index cc4b51d..3ce4f94 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2009-10-07  Eric Blake  <ebb9@byu.net>
+
+       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  <ebb9@byu.net>
 
        doc: tweak *at function documentation
diff --git a/modules/mkdir-tests b/modules/mkdir-tests
new file mode 100644 (file)
index 0000000..cff0a5b
--- /dev/null
@@ -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@
index 0d0ff8c..1440a9b 100644 (file)
@@ -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 (file)
index 0000000..cc51fce
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
+
+#include <config.h>
+
+#include <sys/stat.h>
+
+#include <fcntl.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#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 (file)
index 0000000..1fd7fe4
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.  */
+
+/* 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 (file)
index 0000000..69d90b8
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Eric Blake <ebb9@byu.net>, 2009.  */
+
+#include <config.h>
+
+#include <sys/stat.h>
+
+#include <fcntl.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#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;
+}