1 /* Test for chmod functions.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
30 #include <support/xunistd.h>
33 #define OUT_OF_MEMORY \
35 puts ("cannot allocate memory"); \
41 do_test (int argc, char *argv[])
48 char *testfile = NULL;
58 error (EXIT_FAILURE, 0, "no parameters");
60 /* This is where we will create the test files. */
62 buflen = strlen (builddir) + 50;
64 startdir = getcwd (NULL, 0);
67 printf ("cannot get current directory: %m\n");
71 /* A buffer large enough for everything we need. */
72 buf = (char *) alloca (buflen);
74 /* Create the directory name. */
75 snprintf (buf, buflen, "%s/chmoddirXXXXXX", builddir);
77 if (mkdtemp (buf) == NULL)
79 printf ("cannot create test directory: %m\n");
83 if (chmod ("", 0600) == 0)
85 puts ("chmod(\"\", 0600 didn't fail");
88 else if (errno != ENOENT)
90 puts ("chmod(\"\",0600) does not set errno to ENOENT");
94 /* Create a duplicate. */
95 testdir = strdup (buf);
99 if (stat64 (testdir, &st1) != 0)
101 printf ("cannot stat test directory: %m\n");
104 if (!S_ISDIR (st1.st_mode))
106 printf ("file not created as directory: %m\n");
110 /* We have to wait for a second to make sure the ctime changes. */
113 /* Remove all access rights from the directory. */
114 if (chmod (testdir, 0) != 0)
116 printf ("cannot change mode of test directory: %m\n");
121 if (stat64 (testdir, &st2) != 0)
123 printf ("cannot stat test directory: %m\n");
128 /* Compare result. */
129 if ((st2.st_mode & ALLPERMS) != 0)
131 printf ("chmod(...,0) on directory left bits nonzero: %o\n",
132 st2.st_mode & ALLPERMS);
135 if (st1.st_ctime >= st2.st_ctime)
137 puts ("chmod(...,0) did not set ctime correctly");
141 /* Name of a file in the directory. */
142 snprintf (buf, buflen, "%s/file", testdir);
143 testfile = strdup (buf);
144 if (testfile == NULL)
147 fd = creat (testfile, 0);
152 puts ("managed to create test file in protected directory");
157 else if (errno != EACCES)
159 puts ("creat didn't generate correct errno value");
163 /* With this mode it still shouldn't be possible to create a file. */
164 if (chmod (testdir, 0600) != 0)
166 printf ("cannot change mode of test directory to 0600: %m\n");
171 fd = creat (testfile, 0);
176 puts ("managed to create test file in no-x protected directory");
181 else if (errno != EACCES)
183 puts ("creat didn't generate correct errno value");
187 /* Change the directory mode back to allow creating a file. This
189 dir = opendir (testdir);
192 if (fchmod (dirfd (dir), 0700) != 0)
194 printf ("cannot change mode of test directory to 0700: %m\n");
204 printf ("cannot open directory: %m\n");
207 if (chmod (testdir, 0700) != 0)
209 printf ("cannot change mode of test directory to 0700: %m\n");
214 fd = creat (testfile, 0);
217 puts ("still didn't manage to create test file in protected directory");
221 if (fstat64 (fd, &st1) != 0)
223 printf ("cannot stat new file: %m\n");
226 else if ((st1.st_mode & ALLPERMS) != 0)
228 puts ("file not created with access mode 0");
233 snprintf (buf, buflen, "%s/..", testdir);
236 /* We are now in the directory above the one we create the test
240 snprintf (buf, buflen, "./%s/../%s/file",
241 basename (testdir), basename (testdir));
242 if (chmod (buf, 0600) != 0)
244 printf ("cannot change mode of file to 0600: %m\n");
248 snprintf (buf, buflen, "./%s//file", basename (testdir));
249 if (stat64 (buf, &st2) != 0)
251 printf ("cannot stat new file: %m\n");
254 else if ((st2.st_mode & ALLPERMS) != 0600)
256 puts ("file mode not changed to 0600");
259 else if (st1.st_ctime >= st2.st_ctime)
261 puts ("chmod(\".../file\",0600) did not set ctime correctly");
265 if (chmod (buf, 0777 | S_ISUID | S_ISGID) != 0)
267 printf ("cannot change mode of file to %o: %m\n",
268 0777 | S_ISUID | S_ISGID);
271 if (stat64 (buf, &st2) != 0)
273 printf ("cannot stat test file: %m\n");
276 else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID))
278 puts ("file mode not changed to 0777 | S_ISUID | S_ISGID");
282 if (chmod (basename (testdir), 0777 | S_ISUID | S_ISGID | S_ISVTX) != 0)
284 printf ("cannot change mode of test directory to %o: %m\n",
285 0777 | S_ISUID | S_ISGID | S_ISVTX);
288 if (stat64 (basename (testdir), &st2) != 0)
290 printf ("cannot stat test directory: %m\n");
293 else if ((st2.st_mode & ALLPERMS) != (0777 | S_ISUID | S_ISGID | S_ISVTX))
295 puts ("directory mode not changed to 0777 | S_ISUID | S_ISGID | S_ISGID");
299 snprintf (buf, buflen, "./%s/no-such-file", basename (testdir));
300 if (chmod (buf, 0600) != -1)
302 puts ("chmod(\".../no-such-file\",0600) did not fail");
305 else if (errno != ENOENT)
307 puts ("chmod(\".../no-such-file\",0600) does not set errno to ENOENT");
311 snprintf (buf, buflen, "%s/", basename (testdir));
312 if (chmod (basename (testdir), 0677) != 0)
314 printf ("cannot change mode of test directory to 0677: %m\n");
319 snprintf (buf, buflen, "./%s/file", basename (testdir));
320 if (chmod (buf, 0600) == 0)
324 puts ("chmod(\".../file\") with no-exec directory succeeded");
328 else if (errno != EACCES)
330 puts ("chmod(\".../file\") with no-exec directory didn't set EACCES");
335 if (chmod (basename (testdir), 0777) != 0)
337 printf ("cannot change mode of test directory to 0777: %m\n");
342 snprintf (buf, buflen, "%s/file/cannot-be", basename (testdir));
343 if (chmod (buf, 0600) == 0)
345 puts ("chmod(\".../file/cannot-be\",0600) did not fail");
348 else if (errno != ENOTDIR)
350 puts ("chmod(\".../file/cannot-be\",0600) does not set errno to ENOTDIR");
357 /* Remove all the files. */
358 chmod (testdir, 0700);
359 if (testfile != NULL)
361 chmod (testfile, 0700);
366 /* Free the resources. */
374 #include "../test-skeleton.c"