unlink, remove: detect FreeBSD bug
[gnulib.git] / tests / test-unlink.h
1 /* Tests of unlink.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 /* This file is designed to test both unlink(n) and
20    unlinkat(AT_FDCWD,n,0).  FUNC is the function to test.  Assumes
21    that BASE and ASSERT are already defined, and that appropriate
22    headers are already included.  If PRINT, then warn before returning
23    status 77 when symlinks are unsupported.  */
24
25 static int
26 test_unlink_func (int (*func) (char const *name), bool print)
27 {
28   /* Remove any leftovers from a previous partial run.  */
29   ASSERT (system ("rm -rf " BASE "*") == 0);
30
31   /* Setup.  */
32   ASSERT (mkdir (BASE "dir", 0700) == 0);
33   ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
34
35   /* Basic error conditions.  */
36   errno = 0;
37   ASSERT (func ("") == -1);
38   ASSERT (errno == ENOENT);
39   errno = 0;
40   ASSERT (func (BASE "nosuch") == -1);
41   ASSERT (errno == ENOENT);
42   errno = 0;
43   ASSERT (func (BASE "nosuch/") == -1);
44   ASSERT (errno == ENOENT);
45   /* Resulting errno after directories is rather varied across
46      implementations (EPERM, EINVAL, EACCES, EBUSY, EISDIR, ENOTSUP);
47      however, we must be careful to not attempt unlink on a directory
48      unless we know it must fail.  */
49   if (cannot_unlink_dir ())
50     {
51       ASSERT (func (".") == -1);
52       ASSERT (func ("..") == -1);
53       ASSERT (func ("/") == -1);
54       ASSERT (func (BASE "dir") == -1);
55       ASSERT (mkdir (BASE "dir1", 0700) == 0);
56       ASSERT (func (BASE "dir1") == -1);
57       ASSERT (rmdir (BASE "dir1") == 0);
58     }
59   errno = 0;
60   ASSERT (func (BASE "dir/file/") == -1);
61   ASSERT (errno == ENOTDIR);
62
63   /* Test symlink behavior.  Specifying trailing slash will attempt
64      unlink of a directory, so only attempt it if we know it must
65      fail.  */
66   if (symlink (BASE "dir", BASE "link") != 0)
67     {
68       ASSERT (func (BASE "dir/file") == 0);
69       ASSERT (rmdir (BASE "dir") == 0);
70       if (print)
71         fputs ("skipping test: symlinks not supported on this file system\n",
72                stderr);
73       return 77;
74     }
75   if (cannot_unlink_dir ())
76     ASSERT (func (BASE "link/") == -1);
77   ASSERT (func (BASE "link") == 0);
78   ASSERT (symlink (BASE "dir/file", BASE "link") == 0);
79   errno = 0;
80   ASSERT (func (BASE "link/") == -1);
81   ASSERT (errno == ENOTDIR);
82   /* Order here proves unlink of a symlink does not follow through to
83      the file.  */
84   ASSERT (func (BASE "link") == 0);
85   ASSERT (func (BASE "dir/file") == 0);
86   ASSERT (rmdir (BASE "dir") == 0);
87
88   return 0;
89 }