From: Bruno Haible <bruno@clisp.org>
Date: Tue, 20 Sep 2011 20:36:37 +0000 (+0200)
Subject: openat tests: EBADF tests.
X-Git-Tag: v0.1~1826
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=d6095bd826d000e03e6071725a9f0436d3bf3129;p=gnulib.git

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'.
---

diff --git a/ChangeLog b/ChangeLog
index 29048f53c9..04668ed25a 100644
--- 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.
diff --git a/modules/openat-tests b/modules/openat-tests
index b83fb721d2..da00b983f0 100644
--- a/modules/openat-tests
+++ b/modules/openat-tests
@@ -12,6 +12,7 @@ tests/test-fstatat.c
 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 @@ configure.ac:
 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@
diff --git a/tests/test-fchmodat.c b/tests/test-fchmodat.c
new file mode 100644
index 0000000000..d52f5c33fa
--- /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;
+}
diff --git a/tests/test-fchownat.c b/tests/test-fchownat.c
index 060013e1ed..dda6c0fd69 100644
--- a/tests/test-fchownat.c
+++ b/tests/test-fchownat.c
@@ -69,6 +69,18 @@ main (int argc _GL_UNUSED, char *argv[])
   /* 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);
diff --git a/tests/test-fstatat.c b/tests/test-fstatat.c
index aef1136552..297eb798f6 100644
--- a/tests/test-fstatat.c
+++ b/tests/test-fstatat.c
@@ -67,6 +67,22 @@ main (int argc _GL_UNUSED, char *argv[])
   /* 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);
diff --git a/tests/test-mkdirat.c b/tests/test-mkdirat.c
index 0eb5e2d491..cd0c6cdf4c 100644
--- a/tests/test-mkdirat.c
+++ b/tests/test-mkdirat.c
@@ -57,6 +57,18 @@ main (int argc _GL_UNUSED, char *argv[])
   /* 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);
diff --git a/tests/test-openat.c b/tests/test-openat.c
index 6e6ba5b9b0..f36145a3a7 100644
--- a/tests/test-openat.c
+++ b/tests/test-openat.c
@@ -65,6 +65,18 @@ main (int argc _GL_UNUSED, char *argv[])
 
   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);
diff --git a/tests/test-unlinkat.c b/tests/test-unlinkat.c
index 30af671a39..7d25b6edec 100644
--- a/tests/test-unlinkat.c
+++ b/tests/test-unlinkat.c
@@ -68,6 +68,18 @@ main (int argc _GL_UNUSED, char *argv[])
   /* 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);