maint: prefer 'file system' over 'filesystem'
[gnulib.git] / tests / test-readlink.h
1 /* Tests of readlink.
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 readlink(a,b,c) and
20    readlinkat(AT_FDCWD,a,b,c).  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_readlink (ssize_t (*func) (char const *, char *, size_t), bool print)
27 {
28   char buf[80];
29
30   /* Sanity checks of failures.  Mingw lacks symlink, but readlink can
31      still distinguish between various errors.  */
32   memset (buf, 0xff, sizeof buf);
33   errno = 0;
34   ASSERT (func ("no_such", buf, sizeof buf) == -1);
35   ASSERT (errno == ENOENT);
36   errno = 0;
37   ASSERT (func ("no_such/", buf, sizeof buf) == -1);
38   ASSERT (errno == ENOENT);
39   errno = 0;
40   ASSERT (func ("", buf, sizeof buf) == -1);
41   ASSERT (errno == ENOENT);
42   errno = 0;
43   ASSERT (func (".", buf, sizeof buf) == -1);
44   ASSERT (errno == EINVAL);
45   errno = 0;
46   ASSERT (func ("./", buf, sizeof buf) == -1);
47   ASSERT (errno == EINVAL);
48   ASSERT (close (creat (BASE "file", 0600)) == 0);
49   errno = 0;
50   ASSERT (func (BASE "file", buf, sizeof buf) == -1);
51   ASSERT (errno == EINVAL);
52   errno = 0;
53   ASSERT (func (BASE "file/", buf, sizeof buf) == -1);
54   ASSERT (errno == ENOTDIR);
55   ASSERT (unlink (BASE "file") == 0);
56
57   /* Now test actual symlinks.  */
58   if (symlink (BASE "dir", BASE "link"))
59     {
60       if (print)
61         fputs ("skipping test: symlinks not supported on this file system\n",
62                stderr);
63       return 77;
64     }
65   ASSERT (mkdir (BASE "dir", 0700) == 0);
66   errno = 0;
67   ASSERT (func (BASE "link/", buf, sizeof buf) == -1);
68   ASSERT (errno == EINVAL);
69   {
70     /* Up till now, no readlink has been successful, so buf should be
71        unchanged.  */
72     int i;
73     for (i = 0; i < sizeof buf; i++)
74       ASSERT (buf[i] == (char) 0xff);
75   }
76   {
77     size_t len = strlen (BASE "dir");
78     /* When passing too small of a buffer, expect the truncated
79        length, or an ERANGE failure.  However, a size of 0 is not
80        portable enough to test.  */
81     ssize_t result;
82     errno = 0;
83     result = readlink (BASE "link", buf, 1);
84     if (result == -1)
85       {
86         ASSERT (errno == ERANGE);
87         ASSERT (buf[0] == (char) 0xff);
88       }
89     else
90       {
91         ASSERT (result == 1);
92         ASSERT (buf[0] == BASE[0]);
93       }
94     ASSERT (buf[1] == (char) 0xff);
95     ASSERT (func (BASE "link", buf, len) == len);
96     ASSERT (strncmp (buf, BASE "dir", len) == 0);
97     ASSERT (buf[len] == (char) 0xff);
98     ASSERT (func (BASE "link", buf, sizeof buf) == len);
99     ASSERT (strncmp (buf, BASE "dir", len) == 0);
100     /* POSIX says rest of buf is unspecified; but in practice, it is
101        either left alone, or NUL-terminated.  */
102     ASSERT (buf[len] == '\0' || buf[len] == (char) 0xff);
103   }
104   ASSERT (rmdir (BASE "dir") == 0);
105   ASSERT (unlink (BASE "link") == 0);
106
107   return 0;
108 }