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. */
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 #define BASE "test-readlink.t"
49 /* Remove any leftovers from a previous partial run. */
50 ASSERT (system ("rm -rf " BASE "*") == 0);
52 /* Sanity checks of failures. Mingw lacks symlink, but readlink can
53 still distinguish between various errors. */
54 memset (buf, 0xff, sizeof buf);
56 ASSERT (readlink ("no_such", buf, sizeof buf) == -1);
57 ASSERT (errno == ENOENT);
59 ASSERT (readlink ("no_such/", buf, sizeof buf) == -1);
60 ASSERT (errno == ENOENT);
62 ASSERT (readlink ("", buf, sizeof buf) == -1);
63 ASSERT (errno == ENOENT);
65 ASSERT (readlink (".", buf, sizeof buf) == -1);
66 ASSERT (errno == EINVAL);
68 ASSERT (readlink ("./", buf, sizeof buf) == -1);
69 ASSERT (errno == EINVAL);
70 ASSERT (close (creat (BASE "file", 0600)) == 0);
72 ASSERT (readlink (BASE "file", buf, sizeof buf) == -1);
73 ASSERT (errno == EINVAL);
75 ASSERT (readlink (BASE "file/", buf, sizeof buf) == -1);
76 ASSERT (errno == ENOTDIR);
77 ASSERT (unlink (BASE "file") == 0);
79 /* Now test actual symlinks. */
80 if (symlink (BASE "dir", BASE "link"))
82 fputs ("skipping test: symlinks not supported on this filesystem\n",
86 ASSERT (mkdir (BASE "dir", 0700) == 0);
88 ASSERT (readlink (BASE "link/", buf, sizeof buf) == -1);
89 ASSERT (errno == EINVAL);
91 /* Up till now, no readlink has been successful, so buf should be
94 for (i = 0; i < sizeof buf; i++)
95 ASSERT (buf[i] == (char) 0xff);
98 size_t len = strlen (BASE "dir");
99 /* When passing too small of a buffer, expect the truncated
100 length. However, a size of 0 is not portable enough to
102 ASSERT (readlink (BASE "link", buf, 1) == 1);
103 ASSERT (buf[0] == BASE[0]);
104 ASSERT (buf[1] == (char) 0xff);
105 ASSERT (readlink (BASE "link", buf, len) == len);
106 ASSERT (strncmp (buf, BASE "dir", len) == 0);
107 ASSERT (buf[len] == (char) 0xff);
108 ASSERT (readlink (BASE "link", buf, sizeof buf) == len);
109 ASSERT (strncmp (buf, BASE "dir", len) == 0);
110 /* POSIX says rest of buf is unspecified; but in practice, it is
111 either left alone, or NUL-terminated. */
112 ASSERT (buf[len] == '\0' || buf[len] == (char) 0xff);
114 ASSERT (rmdir (BASE "dir") == 0);
115 ASSERT (unlink (BASE "link") == 0);