1 /* Test of lstat() function.
2 Copyright (C) 2008, 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 Simon Josefsson, 2008; and Eric Blake, 2009. */
29 #include "same-inode.h"
32 # define symlink(a,b) (-1)
35 #define ASSERT(expr) \
40 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 #define BASE "test-lstat.t"
55 /* Remove any leftovers from a previous partial run. */
56 ASSERT (system ("rm -rf " BASE "*") == 0);
58 /* Test for common directories. */
59 ASSERT (lstat (".", &st1) == 0);
60 ASSERT (lstat ("./", &st2) == 0);
61 ASSERT (SAME_INODE (st1, st2));
62 ASSERT (S_ISDIR (st1.st_mode));
63 ASSERT (S_ISDIR (st2.st_mode));
64 ASSERT (lstat ("/", &st1) == 0);
65 ASSERT (lstat ("///", &st2) == 0);
66 ASSERT (SAME_INODE (st1, st2));
67 ASSERT (S_ISDIR (st1.st_mode));
68 ASSERT (S_ISDIR (st2.st_mode));
69 ASSERT (lstat ("..", &st1) == 0);
70 ASSERT (S_ISDIR (st1.st_mode));
72 /* Test for error conditions. */
74 ASSERT (lstat ("", &st1) == -1);
75 ASSERT (errno == ENOENT);
77 ASSERT (lstat ("nosuch", &st1) == -1);
78 ASSERT (errno == ENOENT);
80 ASSERT (lstat ("nosuch/", &st1) == -1);
81 ASSERT (errno == ENOENT);
83 ASSERT (close (creat (BASE "file", 0600)) == 0);
84 ASSERT (lstat (BASE "file", &st1) == 0);
85 ASSERT (S_ISREG (st1.st_mode));
87 ASSERT (lstat (BASE "file/", &st1) == -1);
88 ASSERT (errno == ENOTDIR);
90 /* Now for some symlink tests, where supported. We set up:
95 then test behavior both with and without trailing slash.
97 if (symlink (".", BASE "link1") != 0)
99 ASSERT (unlink (BASE "file") == 0);
100 fputs ("skipping test: symlinks not supported on this filesystem\n",
104 ASSERT (symlink (BASE "file", BASE "link2") == 0);
105 ASSERT (symlink (BASE "nosuch", BASE "link3") == 0);
106 ASSERT (symlink (BASE "link4", BASE "link4") == 0);
108 ASSERT (lstat (BASE "link1", &st1) == 0);
109 ASSERT (S_ISLNK (st1.st_mode));
110 ASSERT (lstat (BASE "link1/", &st1) == 0);
111 ASSERT (stat (BASE "link1", &st2) == 0);
112 ASSERT (S_ISDIR (st1.st_mode));
113 ASSERT (S_ISDIR (st2.st_mode));
114 ASSERT (SAME_INODE (st1, st2));
116 ASSERT (lstat (BASE "link2", &st1) == 0);
117 ASSERT (S_ISLNK (st1.st_mode));
119 ASSERT (lstat (BASE "link2/", &st1) == -1);
120 ASSERT (errno == ENOTDIR);
122 ASSERT (lstat (BASE "link3", &st1) == 0);
123 ASSERT (S_ISLNK (st1.st_mode));
125 ASSERT (lstat (BASE "link3/", &st1) == -1);
126 ASSERT (errno == ENOENT);
128 ASSERT (lstat (BASE "link4", &st1) == 0);
129 ASSERT (S_ISLNK (st1.st_mode));
131 ASSERT (lstat (BASE "link4/", &st1) == -1);
132 ASSERT (errno == ELOOP);
135 ASSERT (unlink (BASE "file") == 0);
136 ASSERT (unlink (BASE "link1") == 0);
137 ASSERT (unlink (BASE "link2") == 0);
138 ASSERT (unlink (BASE "link3") == 0);
139 ASSERT (unlink (BASE "link4") == 0);