]> Savannah Git Hosting - gnulib.git/commitdiff
lchmod: Add tests.
authorBruno Haible <bruno@clisp.org>
Sat, 8 Feb 2020 20:24:35 +0000 (21:24 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 8 Feb 2020 20:24:35 +0000 (21:24 +0100)
* tests/test-lchmod.c: New file.
* modules/lchmod-tests: New file.

ChangeLog
modules/lchmod-tests [new file with mode: 0644]
tests/test-lchmod.c [new file with mode: 0644]

index ffb20961b7a5eae68e4884a052a74042981e51d8..a4ee3dd7eedcc5761b28b4aa6cd99221a5bb775a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-02-08  Bruno Haible  <bruno@clisp.org>
+
+       lchmod: Add tests.
+       * tests/test-lchmod.c: New file.
+       * modules/lchmod-tests: New file.
+
 2020-02-08  Bruno Haible  <bruno@clisp.org>
 
        lchmod: Ensure declaration on HP-UX.
diff --git a/modules/lchmod-tests b/modules/lchmod-tests
new file mode 100644 (file)
index 0000000..cbb2537
--- /dev/null
@@ -0,0 +1,13 @@
+Files:
+tests/test-lchmod.c
+tests/signature.h
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-lchmod
+check_PROGRAMS += test-lchmod
+test_lchmod_LDADD = $(LDADD) @LIBINTL@
diff --git a/tests/test-lchmod.c b/tests/test-lchmod.c
new file mode 100644 (file)
index 0000000..783e608
--- /dev/null
@@ -0,0 +1,67 @@
+/* Test changing the protections of a file.
+   Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <sys/stat.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (lchmod, int, (const char *, mode_t));
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "macros.h"
+
+#define BASE "test-lchmod."
+
+int
+main (void)
+{
+  /* Test that lchmod works on non-symlinks.  */
+  {
+    struct stat statbuf;
+    unlink (BASE "file");
+    ASSERT (close (creat (BASE "file", 0600)) == 0);
+    ASSERT (lchmod (BASE "file", 0400) == 0);
+    ASSERT (stat (BASE "file", &statbuf) >= 0);
+    ASSERT ((statbuf.st_mode & 0700) == 0400);
+    /* Clean up.  */
+    ASSERT (chmod (BASE "file", 0600) == 0);
+    ASSERT (unlink (BASE "file") == 0);
+  }
+
+  /* Test that lchmod on symlinks does not modify the symlink target.  */
+  {
+    unlink (BASE "file");
+    unlink (BASE "link");
+    if (symlink (BASE "file", BASE "link") == 0)
+      {
+        struct stat statbuf;
+        ASSERT (close (creat (BASE "file", 0600)) == 0);
+        lchmod (BASE "link", 0400);
+        ASSERT (stat (BASE "file", &statbuf) >= 0);
+        ASSERT ((statbuf.st_mode & 0700) == 0600);
+      }
+    /* Clean up.  */
+    unlink (BASE "file");
+    unlink (BASE "link");
+  }
+
+  return 0;
+}