1 /* Tests of symlinkat and readlinkat.
2 Copyright (C) 2009 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. */
31 # define HAVE_SYMLINK 0
34 #define ASSERT(expr) \
39 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
51 /* Create handle for future use. */
52 int dfd = openat (AT_FDCWD, ".", O_RDONLY);
55 /* Sanity checks of failures. Mingw lacks symlinkat, but readlinkat
56 can still distinguish between various errors. */
58 ASSERT (readlinkat (AT_FDCWD, "no_such", buf, sizeof buf) == -1);
59 ASSERT (errno == ENOENT);
61 ASSERT (readlinkat (dfd, "no_such", buf, sizeof buf) == -1);
62 ASSERT (errno == ENOENT);
64 ASSERT (readlinkat (AT_FDCWD, "", buf, sizeof buf) == -1);
65 ASSERT (errno == ENOENT);
67 ASSERT (readlinkat (dfd, "", buf, sizeof buf) == -1);
68 ASSERT (errno == ENOENT);
70 ASSERT (readlinkat (AT_FDCWD, ".", buf, sizeof buf) == -1);
71 ASSERT (errno == EINVAL);
73 ASSERT (readlinkat (dfd, ".", buf, sizeof buf) == -1);
74 ASSERT (errno == EINVAL);
76 ASSERT (symlinkat ("who cares", AT_FDCWD, "") == -1);
77 ASSERT (errno == ENOENT || errno == ENOSYS);
79 ASSERT (symlinkat ("who cares", dfd, "") == -1);
80 ASSERT (errno == ENOENT || errno == ENOSYS);
82 /* Skip everything else on mingw. */
85 const char *linkname = "test-symlinkat.link";
86 const char *contents = "don't matter!";
87 int exp = strlen (contents);
89 /* Create link while cwd is '.', then read it in '..'. */
90 ASSERT (symlinkat (contents, AT_FDCWD, linkname) == 0);
92 ASSERT (symlinkat (contents, dfd, linkname) == -1);
93 ASSERT (errno == EEXIST);
94 ASSERT (chdir ("..") == 0);
96 ASSERT (readlinkat (AT_FDCWD, linkname, buf, sizeof buf) == -1);
97 ASSERT (errno == ENOENT);
98 ASSERT (readlinkat (dfd, linkname, buf, sizeof buf) == exp);
99 ASSERT (strncmp (contents, buf, exp) == 0);
100 ASSERT (unlinkat (dfd, linkname, 0) == 0);
102 /* Create link while cwd is '..', then read it in '.'. */
103 ASSERT (symlinkat (contents, dfd, linkname) == 0);
104 ASSERT (fchdir (dfd) == 0);
106 ASSERT (symlinkat (contents, AT_FDCWD, linkname) == -1);
107 ASSERT (errno == EEXIST);
109 ASSERT (readlinkat (AT_FDCWD, linkname, buf, sizeof buf) == exp);
110 ASSERT (strncmp (contents, buf, exp) == 0);
112 ASSERT (readlinkat (dfd, linkname, buf, sizeof buf) == exp);
113 ASSERT (strncmp (contents, buf, exp) == 0);
114 ASSERT (unlink (linkname) == 0);
117 ASSERT (close (dfd) == 0);