error: Avoid "function declaration isn't a prototype" warning.
[gnulib.git] / tests / test-readlink.h
blob674aef8141a82e9216ff93d771a85ab67ebbdbc9
1 /* Tests of readlink.
2 Copyright (C) 2009-2017 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
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. */
25 static int
26 test_readlink (ssize_t (*func) (char const *, char *, size_t), bool print)
28 char buf[80];
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 || errno == EINVAL);
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 || errno == EINVAL); /* AIX yields EINVAL */
56 /* Now test actual symlinks. */
57 if (symlink (BASE "dir", BASE "link"))
59 ASSERT (unlink (BASE "file") == 0);
60 if (print)
61 fputs ("skipping test: symlinks not supported on this file system\n",
62 stderr);
63 return 77;
65 ASSERT (mkdir (BASE "dir", 0700) == 0);
66 errno = 0;
67 ASSERT (func (BASE "link/", buf, sizeof buf) == -1);
68 ASSERT (errno == EINVAL);
69 ASSERT (symlink (BASE "link", BASE "link2") == 0);
70 errno = 0;
71 ASSERT (func (BASE "link2/", buf, sizeof buf) == -1);
72 ASSERT (errno == EINVAL);
73 ASSERT (unlink (BASE "link2") == 0);
74 ASSERT (symlink (BASE "file", BASE "link2") == 0);
75 errno = 0;
76 ASSERT (func (BASE "link2/", buf, sizeof buf) == -1);
77 ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */
78 ASSERT (unlink (BASE "file") == 0);
79 ASSERT (unlink (BASE "link2") == 0);
81 /* Up till now, no readlink has been successful, so buf should be
82 unchanged. */
83 int i;
84 for (i = 0; i < sizeof buf; i++)
85 ASSERT (buf[i] == (char) 0xff);
88 size_t len = strlen (BASE "dir");
89 /* When passing too small of a buffer, expect the truncated
90 length, or an ERANGE failure. However, a size of 0 is not
91 portable enough to test. */
92 ssize_t result;
93 errno = 0;
94 result = readlink (BASE "link", buf, 1);
95 if (result == -1)
97 ASSERT (errno == ERANGE);
98 ASSERT (buf[0] == (char) 0xff);
100 else
102 ASSERT (result == 1);
103 ASSERT (buf[0] == BASE[0]);
105 ASSERT (buf[1] == (char) 0xff);
106 ASSERT (func (BASE "link", buf, len) == len);
107 ASSERT (strncmp (buf, BASE "dir", len) == 0);
108 ASSERT (buf[len] == (char) 0xff);
109 ASSERT (func (BASE "link", buf, sizeof buf) == len);
110 ASSERT (strncmp (buf, BASE "dir", len) == 0);
111 /* POSIX says rest of buf is unspecified; but in practice, it is
112 either left alone, or NUL-terminated. */
113 ASSERT (buf[len] == '\0' || buf[len] == (char) 0xff);
115 ASSERT (rmdir (BASE "dir") == 0);
116 ASSERT (unlink (BASE "link") == 0);
118 return 0;