changeset 15700:bbc06c8e298f

openat tests: EBADF tests. * tests/test-fchownat.c (main): Add tests for EBADF. * tests/test-fstatat.c (main): Likewise. * tests/test-mkdirat.c (main): Likewise. * tests/test-openat.c (main): Likewise. * tests/test-unlinkat.c (main): Likewise. * tests/test-fchmodat.c: New file. * modules/openat-tests (Files): Add tests/test-fchmodat.c. (Makefile.am): Also run 'test-fchmodat'.
author Bruno Haible <bruno@clisp.org>
date Tue, 20 Sep 2011 22:36:37 +0200
parents 2b0173137ba0
children 892fd4630714
files ChangeLog modules/openat-tests tests/test-fchmodat.c tests/test-fchownat.c tests/test-fstatat.c tests/test-mkdirat.c tests/test-openat.c tests/test-unlinkat.c
diffstat 8 files changed, 126 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2011-09-20  Bruno Haible  <bruno@clisp.org>
+
+	openat tests: EBADF tests.
+	* tests/test-fchownat.c (main): Add tests for EBADF.
+	* tests/test-fstatat.c (main): Likewise.
+	* tests/test-mkdirat.c (main): Likewise.
+	* tests/test-openat.c (main): Likewise.
+	* tests/test-unlinkat.c (main): Likewise.
+	* tests/test-fchmodat.c: New file.
+	* modules/openat-tests (Files): Add tests/test-fchmodat.c.
+	(Makefile.am): Also run 'test-fchmodat'.
+
 2011-09-20  Bruno Haible  <bruno@clisp.org>
 
 	utimens, futimens, fdutimensat tests: EBADF tests.
--- a/modules/openat-tests
+++ b/modules/openat-tests
@@ -12,6 +12,7 @@
 tests/test-mkdirat.c
 tests/test-openat.c
 tests/test-unlinkat.c
+tests/test-fchmodat.c
 tests/signature.h
 tests/macros.h
 
@@ -29,8 +30,11 @@
 AC_CHECK_FUNCS_ONCE([getegid])
 
 Makefile.am:
-TESTS += test-fchownat test-fstatat test-mkdirat test-openat test-unlinkat
-check_PROGRAMS += test-fchownat test-fstatat test-mkdirat test-openat \
+TESTS += \
+  test-fchmodat test-fchownat test-fstatat test-mkdirat test-openat \
+  test-unlinkat
+check_PROGRAMS += \
+  test-fchmodat test-fchownat test-fstatat test-mkdirat test-openat \
   test-unlinkat
 test_fchownat_LDADD = $(LDADD) @LIBINTL@
 test_fstatat_LDADD = $(LDADD) @LIBINTL@
new file mode 100644
--- /dev/null
+++ b/tests/test-fchmodat.c
@@ -0,0 +1,44 @@
+/* Test changing the protections of a file relative to an open directory.
+   Copyright (C) 2011 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/>.  */
+
+#include <config.h>
+
+#include <sys/stat.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (fchmodat, int, (int, const char *, mode_t, int));
+
+#include <errno.h>
+
+#include "macros.h"
+
+int
+main (void)
+{
+  /* Test behaviour for invalid file descriptors.  */
+  {
+    errno = 0;
+    ASSERT (fchmodat (-1, "foo", 0600, 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+  {
+    errno = 0;
+    ASSERT (fchmodat (99, "foo", 0600, 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+
+  return 0;
+}
--- a/tests/test-fchownat.c
+++ b/tests/test-fchownat.c
@@ -69,6 +69,18 @@
   /* Clean up any trash from prior testsuite runs.  */
   ignore_value (system ("rm -rf " BASE "*"));
 
+  /* Test behaviour for invalid file descriptors.  */
+  {
+    errno = 0;
+    ASSERT (fchownat (-1, "foo", getuid (), getgid (), 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+  {
+    errno = 0;
+    ASSERT (fchownat (99, "foo", getuid (), getgid (), 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+
   /* Basic tests.  */
   result1 = test_chown (do_chown, true);
   result2 = test_lchown (do_lchown, result1 == 0);
--- a/tests/test-fstatat.c
+++ b/tests/test-fstatat.c
@@ -67,6 +67,22 @@
   /* Remove any leftovers from a previous partial run.  */
   ignore_value (system ("rm -rf " BASE "*"));
 
+  /* Test behaviour for invalid file descriptors.  */
+  {
+    struct stat statbuf;
+
+    errno = 0;
+    ASSERT (fstatat (-1, "foo", &statbuf, 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+  {
+    struct stat statbuf;
+
+    errno = 0;
+    ASSERT (fstatat (99, "foo", &statbuf, 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+
   result = test_stat_func (do_stat, false);
   ASSERT (test_lstat_func (do_lstat, false) == result);
   dfd = open (".", O_RDONLY);
--- a/tests/test-mkdirat.c
+++ b/tests/test-mkdirat.c
@@ -57,6 +57,18 @@
   /* Clean up any trash from prior testsuite runs.  */
   ignore_value (system ("rm -rf " BASE "*"));
 
+  /* Test behaviour for invalid file descriptors.  */
+  {
+    errno = 0;
+    ASSERT (mkdirat (-1, "foo", 0700) == -1);
+    ASSERT (errno == EBADF);
+  }
+  {
+    errno = 0;
+    ASSERT (mkdirat (99, "foo", 0700) == -1);
+    ASSERT (errno == EBADF);
+  }
+
   /* Test basic mkdir functionality.  */
   result = test_mkdir (do_mkdir, false);
   dfd = open (".", O_RDONLY);
--- a/tests/test-openat.c
+++ b/tests/test-openat.c
@@ -65,6 +65,18 @@
 
   set_program_name (argv[0]);
 
+  /* Test behaviour for invalid file descriptors.  */
+  {
+    errno = 0;
+    ASSERT (openat (-1, "foo", O_RDONLY) == -1);
+    ASSERT (errno == EBADF);
+  }
+  {
+    errno = 0;
+    ASSERT (openat (99, "foo", O_RDONLY) == -1);
+    ASSERT (errno == EBADF);
+  }
+
   /* Basic checks.  */
   result = test_open (do_open, false);
   dfd = open (".", O_RDONLY);
--- a/tests/test-unlinkat.c
+++ b/tests/test-unlinkat.c
@@ -68,6 +68,18 @@
   /* Remove any leftovers from a previous partial run.  */
   ignore_value (system ("rm -rf " BASE "*"));
 
+  /* Test behaviour for invalid file descriptors.  */
+  {
+    errno = 0;
+    ASSERT (unlinkat (-1, "foo", 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+  {
+    errno = 0;
+    ASSERT (unlinkat (99, "foo", 0) == -1);
+    ASSERT (errno == EBADF);
+  }
+
   result1 = test_rmdir_func (rmdirat, false);
   result2 = test_unlink_func (unlinker, false);
   ASSERT (result1 == result2);