Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-lstat.h
1 /* Test of lstat() function.
2    Copyright (C) 2008, 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 Simon Josefsson, 2008; and Eric Blake, 2009.  */
18
19 /* This file is designed to test both lstat(n,buf) and
20    fstatat(AT_FDCWD,n,buf,AT_SYMLINK_NOFOLLOW).  FUNC is the function
21    to test.  Assumes that BASE and ASSERT are already defined, and
22    that appropriate headers are already included.  If PRINT, warn
23    before skipping symlink tests with status 77.  */
24
25 static int
26 test_lstat_func (int (*func) (char const *, struct stat *), bool print)
27 {
28   struct stat st1;
29   struct stat st2;
30
31   /* Remove any leftovers from a previous partial run.  */
32   ASSERT (system ("rm -rf " BASE "*") == 0);
33
34   /* Test for common directories.  */
35   ASSERT (func (".", &st1) == 0);
36   ASSERT (func ("./", &st2) == 0);
37   ASSERT (SAME_INODE (st1, st2));
38   ASSERT (S_ISDIR (st1.st_mode));
39   ASSERT (S_ISDIR (st2.st_mode));
40   ASSERT (func ("/", &st1) == 0);
41   ASSERT (func ("///", &st2) == 0);
42   ASSERT (SAME_INODE (st1, st2));
43   ASSERT (S_ISDIR (st1.st_mode));
44   ASSERT (S_ISDIR (st2.st_mode));
45   ASSERT (func ("..", &st1) == 0);
46   ASSERT (S_ISDIR (st1.st_mode));
47
48   /* Test for error conditions.  */
49   errno = 0;
50   ASSERT (func ("", &st1) == -1);
51   ASSERT (errno == ENOENT);
52   errno = 0;
53   ASSERT (func ("nosuch", &st1) == -1);
54   ASSERT (errno == ENOENT);
55   errno = 0;
56   ASSERT (func ("nosuch/", &st1) == -1);
57   ASSERT (errno == ENOENT);
58
59   ASSERT (close (creat (BASE "file", 0600)) == 0);
60   ASSERT (func (BASE "file", &st1) == 0);
61   ASSERT (S_ISREG (st1.st_mode));
62   errno = 0;
63   ASSERT (func (BASE "file/", &st1) == -1);
64   ASSERT (errno == ENOTDIR);
65
66   /* Now for some symlink tests, where supported.  We set up:
67      link1 -> directory
68      link2 -> file
69      link3 -> dangling
70      link4 -> loop
71      then test behavior both with and without trailing slash.
72   */
73   if (symlink (".", BASE "link1") != 0)
74     {
75       ASSERT (unlink (BASE "file") == 0);
76       if (print)
77         fputs ("skipping test: symlinks not supported on this file system\n",
78                stderr);
79       return 77;
80     }
81   ASSERT (symlink (BASE "file", BASE "link2") == 0);
82   ASSERT (symlink (BASE "nosuch", BASE "link3") == 0);
83   ASSERT (symlink (BASE "link4", BASE "link4") == 0);
84
85   ASSERT (func (BASE "link1", &st1) == 0);
86   ASSERT (S_ISLNK (st1.st_mode));
87   ASSERT (func (BASE "link1/", &st1) == 0);
88   ASSERT (stat (BASE "link1", &st2) == 0);
89   ASSERT (S_ISDIR (st1.st_mode));
90   ASSERT (S_ISDIR (st2.st_mode));
91   ASSERT (SAME_INODE (st1, st2));
92
93   ASSERT (func (BASE "link2", &st1) == 0);
94   ASSERT (S_ISLNK (st1.st_mode));
95   errno = 0;
96   ASSERT (func (BASE "link2/", &st1) == -1);
97   ASSERT (errno == ENOTDIR);
98
99   ASSERT (func (BASE "link3", &st1) == 0);
100   ASSERT (S_ISLNK (st1.st_mode));
101   errno = 0;
102   ASSERT (func (BASE "link3/", &st1) == -1);
103   ASSERT (errno == ENOENT);
104
105   ASSERT (func (BASE "link4", &st1) == 0);
106   ASSERT (S_ISLNK (st1.st_mode));
107   errno = 0;
108   ASSERT (func (BASE "link4/", &st1) == -1);
109   ASSERT (errno == ELOOP);
110
111   /* Cleanup.  */
112   ASSERT (unlink (BASE "file") == 0);
113   ASSERT (unlink (BASE "link1") == 0);
114   ASSERT (unlink (BASE "link2") == 0);
115   ASSERT (unlink (BASE "link3") == 0);
116   ASSERT (unlink (BASE "link4") == 0);
117
118   return 0;
119 }