lstat: fix Solaris 9 bug
[gnulib.git] / tests / test-lstat.c
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 #include <config.h>
20
21 #include <sys/stat.h>
22
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include "same-inode.h"
30
31 #if !HAVE_SYMLINK
32 # define symlink(a,b) (-1)
33 #endif
34
35 #define ASSERT(expr) \
36   do                                                                         \
37     {                                                                        \
38       if (!(expr))                                                           \
39         {                                                                    \
40           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
41           fflush (stderr);                                                   \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 #define BASE "test-lstat.t"
48
49 int
50 main ()
51 {
52   struct stat st1;
53   struct stat st2;
54
55   /* Remove any leftovers from a previous partial run.  */
56   ASSERT (system ("rm -rf " BASE "*") == 0);
57
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));
71
72   /* Test for error conditions.  */
73   errno = 0;
74   ASSERT (lstat ("", &st1) == -1);
75   ASSERT (errno == ENOENT);
76   errno = 0;
77   ASSERT (lstat ("nosuch", &st1) == -1);
78   ASSERT (errno == ENOENT);
79   errno = 0;
80   ASSERT (lstat ("nosuch/", &st1) == -1);
81   ASSERT (errno == ENOENT);
82
83   ASSERT (close (creat (BASE "file", 0600)) == 0);
84   ASSERT (lstat (BASE "file", &st1) == 0);
85   ASSERT (S_ISREG (st1.st_mode));
86   errno = 0;
87   ASSERT (lstat (BASE "file/", &st1) == -1);
88   ASSERT (errno == ENOTDIR);
89
90   /* Now for some symlink tests, where supported.  We set up:
91      link1 -> directory
92      link2 -> file
93      link3 -> dangling
94      link4 -> loop
95      then test behavior both with and without trailing slash.
96   */
97   if (symlink (".", BASE "link1") != 0)
98     {
99       ASSERT (unlink (BASE "file") == 0);
100       fputs ("skipping test: symlinks not supported on this filesystem\n",
101              stderr);
102       return 77;
103     }
104   ASSERT (symlink (BASE "file", BASE "link2") == 0);
105   ASSERT (symlink (BASE "nosuch", BASE "link3") == 0);
106   ASSERT (symlink (BASE "link4", BASE "link4") == 0);
107
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));
115
116   ASSERT (lstat (BASE "link2", &st1) == 0);
117   ASSERT (S_ISLNK (st1.st_mode));
118   errno = 0;
119   ASSERT (lstat (BASE "link2/", &st1) == -1);
120   ASSERT (errno == ENOTDIR);
121
122   ASSERT (lstat (BASE "link3", &st1) == 0);
123   ASSERT (S_ISLNK (st1.st_mode));
124   errno = 0;
125   ASSERT (lstat (BASE "link3/", &st1) == -1);
126   ASSERT (errno == ENOENT);
127
128   ASSERT (lstat (BASE "link4", &st1) == 0);
129   ASSERT (S_ISLNK (st1.st_mode));
130   errno = 0;
131   ASSERT (lstat (BASE "link4/", &st1) == -1);
132   ASSERT (errno == ELOOP);
133
134   /* Cleanup.  */
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);
140
141   return 0;
142 }