1 /* Test for chmod functions.
2 Copyright (C) 2000, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
32 #define OUT_OF_MEMORY \
34 puts ("cannot allocate memory"); \
40 do_test (int argc
, char *argv
[])
47 char *testfile
= NULL
;
57 error (EXIT_FAILURE
, 0, "no parameters");
59 /* This is where we will create the test files. */
61 buflen
= strlen (builddir
) + 50;
63 startdir
= getcwd (NULL
, 0);
66 printf ("cannot get current directory: %m\n");
70 /* A buffer large enough for everything we need. */
71 buf
= (char *) alloca (buflen
);
73 /* Create the directory name. */
74 snprintf (buf
, buflen
, "%s/chmoddirXXXXXX", builddir
);
76 if (mkdtemp (buf
) == NULL
)
78 printf ("cannot create test directory: %m\n");
82 if (chmod ("", 0600) == 0)
84 puts ("chmod(\"\", 0600 didn't fail");
87 else if (errno
!= ENOENT
)
89 puts ("chmod(\"\",0600) does not set errno to ENOENT");
93 /* Create a duplicate. */
94 testdir
= strdup (buf
);
98 if (stat64 (testdir
, &st1
) != 0)
100 printf ("cannot stat test directory: %m\n");
103 if (!S_ISDIR (st1
.st_mode
))
105 printf ("file not created as directory: %m\n");
109 /* We have to wait for a second to make sure the ctime changes. */
112 /* Remove all access rights from the directory. */
113 if (chmod (testdir
, 0) != 0)
115 printf ("cannot change mode of test directory: %m\n");
120 if (stat64 (testdir
, &st2
) != 0)
122 printf ("cannot stat test directory: %m\n");
127 /* Compare result. */
128 if ((st2
.st_mode
& ALLPERMS
) != 0)
130 printf ("chmod(...,0) on directory left bits nonzero: %o\n",
131 st2
.st_mode
& ALLPERMS
);
134 if (st1
.st_ctime
>= st2
.st_ctime
)
136 puts ("chmod(...,0) did not set ctime correctly");
140 /* Name of a file in the directory. */
141 snprintf (buf
, buflen
, "%s/file", testdir
);
142 testfile
= strdup (buf
);
143 if (testfile
== NULL
)
146 fd
= creat (testfile
, 0);
151 puts ("managed to create test file in protected directory");
156 else if (errno
!= EACCES
)
158 puts ("creat didn't generate correct errno value");
162 /* With this mode it still shouldn't be possible to create a file. */
163 if (chmod (testdir
, 0600) != 0)
165 printf ("cannot change mode of test directory to 0600: %m\n");
170 fd
= creat (testfile
, 0);
175 puts ("managed to create test file in no-x protected directory");
180 else if (errno
!= EACCES
)
182 puts ("creat didn't generate correct errno value");
186 /* Change the directory mode back to allow creating a file. This
188 dir
= opendir (testdir
);
191 if (fchmod (dirfd (dir
), 0700) != 0)
193 printf ("cannot change mode of test directory to 0700: %m\n");
203 printf ("cannot open directory: %m\n");
206 if (chmod (testdir
, 0700) != 0)
208 printf ("cannot change mode of test directory to 0700: %m\n");
213 fd
= creat (testfile
, 0);
216 puts ("still didn't manage to create test file in protected directory");
220 if (fstat64 (fd
, &st1
) != 0)
222 printf ("cannot stat new file: %m\n");
225 else if ((st1
.st_mode
& ALLPERMS
) != 0)
227 puts ("file not created with access mode 0");
232 snprintf (buf
, buflen
, "%s/..", testdir
);
234 /* We are now in the directory above the one we create the test
238 snprintf (buf
, buflen
, "./%s/../%s/file",
239 basename (testdir
), basename (testdir
));
240 if (chmod (buf
, 0600) != 0)
242 printf ("cannot change mode of file to 0600: %m\n");
246 snprintf (buf
, buflen
, "./%s//file", basename (testdir
));
247 if (stat64 (buf
, &st2
) != 0)
249 printf ("cannot stat new file: %m\n");
252 else if ((st2
.st_mode
& ALLPERMS
) != 0600)
254 puts ("file mode not changed to 0600");
257 else if (st1
.st_ctime
>= st2
.st_ctime
)
259 puts ("chmod(\".../file\",0600) did not set ctime correctly");
263 if (chmod (buf
, 0777 | S_ISUID
| S_ISGID
) != 0)
265 printf ("cannot change mode of file to %o: %m\n",
266 0777 | S_ISUID
| S_ISGID
);
269 if (stat64 (buf
, &st2
) != 0)
271 printf ("cannot stat test file: %m\n");
274 else if ((st2
.st_mode
& ALLPERMS
) != (0777 | S_ISUID
| S_ISGID
))
276 puts ("file mode not changed to 0777 | S_ISUID | S_ISGID");
280 if (chmod (basename (testdir
), 0777 | S_ISUID
| S_ISGID
| S_ISVTX
) != 0)
282 printf ("cannot change mode of test directory to %o: %m\n",
283 0777 | S_ISUID
| S_ISGID
| S_ISVTX
);
286 if (stat64 (basename (testdir
), &st2
) != 0)
288 printf ("cannot stat test directory: %m\n");
291 else if ((st2
.st_mode
& ALLPERMS
) != (0777 | S_ISUID
| S_ISGID
| S_ISVTX
))
293 puts ("directory mode not changed to 0777 | S_ISUID | S_ISGID | S_ISGID");
297 snprintf (buf
, buflen
, "./%s/no-such-file", basename (testdir
));
298 if (chmod (buf
, 0600) != -1)
300 puts ("chmod(\".../no-such-file\",0600) did not fail");
303 else if (errno
!= ENOENT
)
305 puts ("chmod(\".../no-such-file\",0600) does not set errno to ENOENT");
309 snprintf (buf
, buflen
, "%s/", basename (testdir
));
310 if (chmod (basename (testdir
), 0677) != 0)
312 printf ("cannot change mode of test directory to 0677: %m\n");
317 snprintf (buf
, buflen
, "./%s/file", basename (testdir
));
318 if (chmod (buf
, 0600) == 0)
322 puts ("chmod(\".../file\") with no-exec directory succeeded");
326 else if (errno
!= EACCES
)
328 puts ("chmod(\".../file\") with no-exec directory didn't set EACCES");
333 if (chmod (basename (testdir
), 0777) != 0)
335 printf ("cannot change mode of test directory to 0777: %m\n");
340 snprintf (buf
, buflen
, "%s/file/cannot-be", basename (testdir
));
341 if (chmod (buf
, 0600) == 0)
343 puts ("chmod(\".../file/cannot-be\",0600) did not fail");
346 else if (errno
!= ENOTDIR
)
348 puts ("chmod(\".../file/cannot-be\",0600) does not set errno to ENOTDIR");
355 /* Remove all the files. */
356 chmod (testdir
, 0700);
357 if (testfile
!= NULL
)
359 chmod (testfile
, 0700);
364 /* Free the resources. */
373 /* We need a few seconds since we have a few sleeps in the code. */
377 #include "../test-skeleton.c"