maint: update copyright
[gnulib.git] / tests / test-symlink.h
1 /* Tests of symlink.
2    Copyright (C) 2009-2014 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 symlink(a,b) and
20    symlinkat(a,AT_FDCWD,b).  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, warn before skipping
23    symlink tests with status 77.  */
24
25 static int
26 test_symlink (int (*func) (char const *, char const *), bool print)
27 {
28   if (func ("nowhere", BASE "link1"))
29     {
30       if (print)
31         fputs ("skipping test: symlinks not supported on this file system\n",
32                stderr);
33       return 77;
34     }
35
36   /* Some systems allow the creation of 0-length symlinks as a synonym
37      for "."; but most reject it.  */
38   {
39     int status;
40     errno = 0;
41     status = func ("", BASE "link2");
42     if (status == -1)
43       ASSERT (errno == ENOENT || errno == EINVAL);
44     else
45       {
46         ASSERT (status == 0);
47         ASSERT (unlink (BASE "link2") == 0);
48       }
49   }
50
51   /* Sanity checks of failures.  */
52   errno = 0;
53   ASSERT (func ("nowhere", "") == -1);
54   ASSERT (errno == ENOENT);
55   errno = 0;
56   ASSERT (func ("nowhere", ".") == -1);
57   ASSERT (errno == EEXIST || errno == EINVAL);
58   errno = 0;
59   ASSERT (func ("somewhere", BASE "link1") == -1);
60   ASSERT (errno == EEXIST);
61   errno = 0;
62   ASSERT (func ("nowhere", BASE "link2/") == -1);
63   ASSERT (errno == ENOTDIR || errno == ENOENT);
64   ASSERT (mkdir (BASE "dir", 0700) == 0);
65   errno = 0;
66   ASSERT (func ("nowhere", BASE "dir") == -1);
67   ASSERT (errno == EEXIST);
68   errno = 0;
69   ASSERT (func ("nowhere", BASE "dir/") == -1);
70   ASSERT (errno == EEXIST || errno == EINVAL);
71   ASSERT (close (creat (BASE "file", 0600)) == 0);
72   errno = 0;
73   ASSERT (func ("nowhere", BASE "file") == -1);
74   ASSERT (errno == EEXIST);
75   errno = 0;
76   ASSERT (func ("nowhere", BASE "file/") == -1);
77   ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
78
79   /* Trailing slash must always be rejected.  */
80   ASSERT (unlink (BASE "link1") == 0);
81   ASSERT (func (BASE "link2", BASE "link1") == 0);
82   errno = 0;
83   ASSERT (func (BASE "nowhere", BASE "link1/") == -1);
84   ASSERT (errno == EEXIST || errno == ENOTDIR || errno == ENOENT);
85   errno = 0;
86   ASSERT (unlink (BASE "link2") == -1);
87   ASSERT (errno == ENOENT);
88
89   /* Cleanup.  */
90   ASSERT (rmdir (BASE "dir") == 0);
91   ASSERT (unlink (BASE "file") == 0);
92   ASSERT (unlink (BASE "link1") == 0);
93
94   return 0;
95 }