>From 8cc34c349afc79e2728093e109a09f9f0aaa4b50 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 8 Feb 2020 21:24:35 +0100 Subject: [PATCH 2/2] lchmod: Add tests. * tests/test-lchmod.c: New file. * modules/lchmod-tests: New file. --- ChangeLog | 6 +++++ modules/lchmod-tests | 13 ++++++++++ tests/test-lchmod.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 modules/lchmod-tests create mode 100644 tests/test-lchmod.c diff --git a/ChangeLog b/ChangeLog index ffb2096..a4ee3dd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2020-02-08 Bruno Haible + lchmod: Add tests. + * tests/test-lchmod.c: New file. + * modules/lchmod-tests: New file. + +2020-02-08 Bruno Haible + lchmod: Ensure declaration on HP-UX. * lib/sys_stat.in.h (lchown): Declare also on HP-UX. * doc/glibc-functions/lchmod.texi: Mention the HP-UX problem. diff --git a/modules/lchmod-tests b/modules/lchmod-tests new file mode 100644 index 0000000..cbb2537 --- /dev/null +++ b/modules/lchmod-tests @@ -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 index 0000000..783e608 --- /dev/null +++ b/tests/test-lchmod.c @@ -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 . */ + +#include + +#include + +#include "signature.h" +SIGNATURE_CHECK (lchmod, int, (const char *, mode_t)); + +#include +#include +#include +#include + +#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; +} -- 2.7.4