]> Savannah Git Hosting - gnulib.git/commitdiff
fchmod-tests, fchmodat tests, lchmod tests: Add more tests.
authorBruno Haible <bruno@clisp.org>
Sat, 9 Jan 2021 05:11:24 +0000 (06:11 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 9 Jan 2021 06:45:33 +0000 (07:45 +0100)
* tests/test-fchmod.c: Include <fcntl.h>.
(BASE): New macro.
(main): Add more tests.
* tests/test-fchmodat.c (main): Add more tests.
* tests/test-lchmod.c (main): Likewise.

ChangeLog
tests/test-fchmod.c
tests/test-fchmodat.c
tests/test-lchmod.c

index 6b5f5b3919a635ccefe0d7a27e0d8452d41e12ed..35b4153e4b84646ddddfb598b0ffa6a16094c8c4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-01-09  Bruno Haible  <bruno@clisp.org>
+
+       fchmod-tests, fchmodat tests, lchmod tests: Add more tests.
+       * tests/test-fchmod.c: Include <fcntl.h>.
+       (BASE): New macro.
+       (main): Add more tests.
+       * tests/test-fchmodat.c (main): Add more tests.
+       * tests/test-lchmod.c (main): Likewise.
+
 2021-01-09  Bruno Haible  <bruno@clisp.org>
 
        utimensat: Fix typo in last commit.
index 43b70ac375e2b2276f907dbc4e28905b7f583ee7..f8db9e51051ddf556c1faaeb98e423ee1042485d 100644 (file)
 SIGNATURE_CHECK (fchmod, int, (int, mode_t));
 
 #include <errno.h>
+#include <fcntl.h>
 #include <unistd.h>
 
 #include "macros.h"
 
+#define BASE "test-fchmod."
+
 int
 main (void)
 {
@@ -42,5 +45,24 @@ main (void)
     ASSERT (errno == EBADF);
   }
 
+  /* Test that fchmod works on regular files.  */
+  {
+    struct stat statbuf;
+    int fd;
+
+    unlink (BASE "file");
+    ASSERT (close (creat (BASE "file", 0600)) == 0);
+    fd = open (BASE "file", O_RDWR);
+    ASSERT (fd >= 0);
+    ASSERT (fchmod (fd, 0400) == 0);
+    ASSERT (stat (BASE "file", &statbuf) >= 0);
+    ASSERT ((statbuf.st_mode & 0700) == 0400);
+    ASSERT (close (fd) == 0);
+
+    /* Clean up.  */
+    ASSERT (chmod (BASE "file", 0600) == 0);
+    ASSERT (unlink (BASE "file") == 0);
+  }
+
   return 0;
 }
index 5e4d2b04f427b58423e6f9f7bb4d32fc77429c60..6d430fa658632e46871d0e3b0a7dcfb71d4e9b8d 100644 (file)
@@ -46,6 +46,39 @@ main (void)
     ASSERT (errno == EBADF);
   }
 
+  /* Test that fchmodat works on regular files.  */
+  {
+    struct stat statbuf;
+
+    unlink (BASE "file");
+    ASSERT (close (creat (BASE "file", 0600)) == 0);
+    ASSERT (fchmodat (AT_FDCWD, BASE "file", 0400, 0) == 0);
+    ASSERT (stat (BASE "file", &statbuf) >= 0);
+    ASSERT ((statbuf.st_mode & 0700) == 0400);
+
+    errno = 0;
+    ASSERT (fchmodat (AT_FDCWD, BASE "file/", 0600, 0) == -1);
+    ASSERT (errno == ENOTDIR);
+
+    /* Clean up.  */
+    ASSERT (chmod (BASE "file", 0600) == 0);
+    ASSERT (unlink (BASE "file") == 0);
+  }
+
+  /* Test that fchmodat works on directories.  */
+  {
+    struct stat statbuf;
+    rmdir (BASE "dir");
+    ASSERT (mkdir (BASE "dir", 0700) == 0);
+    ASSERT (fchmodat (AT_FDCWD, BASE "dir", 0500, 0) == 0);
+    ASSERT (stat (BASE "dir", &statbuf) >= 0);
+    ASSERT ((statbuf.st_mode & 0700) == 0500);
+    ASSERT (fchmodat (AT_FDCWD, BASE "dir/", 0700, 0) == 0);
+
+    /* Clean up.  */
+    ASSERT (rmdir (BASE "dir") == 0);
+  }
+
   /* Test that fchmodat works on non-symlinks, when given
      the AT_SYMLINK_NOFOLLOW flag.  */
   {
index 7b8e8a13de7e32e8422c96f0b4cafa57f4f2a132..d4c811ea7bbacec29aa16e63cf8451aee5c56a67 100644 (file)
@@ -33,7 +33,7 @@ SIGNATURE_CHECK (lchmod, int, (const char *, mode_t));
 int
 main (void)
 {
-  /* Test that lchmod works on non-symlinks.  */
+  /* Test that lchmod works on regular files.  */
   {
     struct stat statbuf;
     unlink (BASE "file");
@@ -41,11 +41,31 @@ main (void)
     ASSERT (lchmod (BASE "file", 0400) == 0);
     ASSERT (stat (BASE "file", &statbuf) >= 0);
     ASSERT ((statbuf.st_mode & 0700) == 0400);
+
+    errno = 0;
+    ASSERT (lchmod (BASE "file/", 0600) == -1);
+    ASSERT (errno == ENOTDIR);
+
     /* Clean up.  */
     ASSERT (chmod (BASE "file", 0600) == 0);
     ASSERT (unlink (BASE "file") == 0);
   }
 
+  /* Test that lchmod works on directories.  */
+  {
+    struct stat statbuf;
+
+    rmdir (BASE "dir");
+    ASSERT (mkdir (BASE "dir", 0700) == 0);
+    ASSERT (lchmod (BASE "dir", 0500) == 0);
+    ASSERT (stat (BASE "dir", &statbuf) >= 0);
+    ASSERT ((statbuf.st_mode & 0700) == 0500);
+    ASSERT (lchmod (BASE "dir/", 0700) == 0);
+
+    /* Clean up.  */
+    ASSERT (rmdir (BASE "dir") == 0);
+  }
+
   /* Test that lchmod on symlinks does not modify the symlink target.  */
   {
     unlink (BASE "file");