canonicalize-lgpl: reject non-directory with trailing slash
[pspp] / tests / test-canonicalize-lgpl.c
1 /* Test of execution of program termination handlers.
2    Copyright (C) 2007-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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #if !HAVE_SYMLINK
31 # define symlink(a,b) (-1)
32 #endif /* !HAVE_SYMLINK */
33
34 #define ASSERT(expr) \
35   do                                                                         \
36     {                                                                        \
37       if (!(expr))                                                           \
38         {                                                                    \
39           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40           fflush (stderr);                                                   \
41           abort ();                                                          \
42         }                                                                    \
43     }                                                                        \
44   while (0)
45
46 #define BASE "t-can-lgpl.tmp"
47
48 int
49 main ()
50 {
51 #ifdef GNULIB_CANONICALIZE
52   /* No need to test canonicalize-lgpl module if canonicalize is also
53      in use.  */
54   return 0;
55 #endif
56
57   /* Setup some hierarchy to be used by this test.  Start by removing
58      any leftovers from a previous partial run.  */
59   {
60     int fd;
61     ASSERT (system ("rm -rf " BASE " ise") == 0);
62     ASSERT (mkdir (BASE, 0700) == 0);
63     fd = creat (BASE "/tra", 0600);
64     ASSERT (0 <= fd);
65     ASSERT (close (fd) == 0);
66   }
67
68   /* Check for ., .., intermediate // handling, and for error cases.  */
69   {
70     char *result = canonicalize_file_name (BASE "//./..//" BASE "/tra");
71     ASSERT (result != NULL);
72     ASSERT (strstr (result, "/" BASE "/tra")
73             == result + strlen (result) - strlen ("/" BASE "/tra"));
74     free (result);
75     errno = 0;
76     result = canonicalize_file_name ("");
77     ASSERT (result == NULL);
78     ASSERT (errno == ENOENT);
79     errno = 0;
80     result = canonicalize_file_name (NULL);
81     ASSERT (result == NULL);
82     ASSERT (errno == EINVAL);
83   }
84
85   /* Check that a non-directory with trailing slash yields NULL.  */
86   {
87     char *result;
88     errno = 0;
89     result = canonicalize_file_name (BASE "/tra/");
90     ASSERT (result == NULL);
91     ASSERT (errno == ENOTDIR);
92   }
93
94   /* Check that a missing directory yields NULL.  */
95   {
96     char *result;
97     errno = 0;
98     result = canonicalize_file_name (BASE "/zzz/..");
99     ASSERT (result == NULL);
100     ASSERT (errno == ENOENT);
101   }
102
103   /* From here on out, tests involve symlinks.  */
104   if (symlink (BASE "/ket", "ise") != 0)
105     {
106       ASSERT (remove (BASE "/tra") == 0);
107       ASSERT (rmdir (BASE) == 0);
108       fputs ("skipping test: symlinks not supported on this filesystem\n",
109              stderr);
110       return 77;
111     }
112   ASSERT (symlink ("bef", BASE "/plo") == 0);
113   ASSERT (symlink ("tra", BASE "/huk") == 0);
114   ASSERT (symlink ("lum", BASE "/bef") == 0);
115   ASSERT (symlink ("wum", BASE "/ouk") == 0);
116   ASSERT (symlink ("../ise", BASE "/ket") == 0);
117   ASSERT (mkdir (BASE "/lum", 0700) == 0);
118
119   /* Check that the symbolic link to a file can be resolved.  */
120   {
121     char *result1 = canonicalize_file_name (BASE "/huk");
122     char *result2 = canonicalize_file_name (BASE "/tra");
123     ASSERT (result1 != NULL);
124     ASSERT (result2 != NULL);
125     ASSERT (strcmp (result1, result2) == 0);
126     ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/tra"),
127                     "/" BASE "/tra") == 0);
128     free (result1);
129     free (result2);
130   }
131
132   /* Check that the symbolic link to a directory can be resolved.  */
133   {
134     char *result1 = canonicalize_file_name (BASE "/plo");
135     char *result2 = canonicalize_file_name (BASE "/bef");
136     char *result3 = canonicalize_file_name (BASE "/lum");
137     ASSERT (result1 != NULL);
138     ASSERT (result2 != NULL);
139     ASSERT (result3 != NULL);
140     ASSERT (strcmp (result1, result2) == 0);
141     ASSERT (strcmp (result2, result3) == 0);
142     ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/lum"),
143                     "/" BASE "/lum") == 0);
144     free (result1);
145     free (result2);
146     free (result3);
147   }
148
149   /* Check that a symbolic link to a nonexistent file yields NULL.  */
150   {
151     char *result;
152     errno = 0;
153     result = canonicalize_file_name (BASE "/ouk");
154     ASSERT (result == NULL);
155     ASSERT (errno == ENOENT);
156   }
157
158   /* Check that a non-directory symlink with trailing slash yields NULL.  */
159   {
160     char *result;
161     errno = 0;
162     result = canonicalize_file_name (BASE "/huk/");
163     ASSERT (result == NULL);
164     ASSERT (errno == ENOTDIR);
165   }
166
167   /* Check that a missing directory via symlink yields NULL.  */
168   {
169     char *result;
170     errno = 0;
171     result = canonicalize_file_name (BASE "/ouk/..");
172     ASSERT (result == NULL);
173     ASSERT (errno == ENOENT);
174   }
175
176   /* Check that a loop of symbolic links is detected.  */
177   {
178     char *result;
179     errno = 0;
180     result = canonicalize_file_name ("ise");
181     ASSERT (result == NULL);
182     ASSERT (errno == ELOOP);
183   }
184
185   /* Cleanup.  */
186   ASSERT (remove (BASE "/plo") == 0);
187   ASSERT (remove (BASE "/huk") == 0);
188   ASSERT (remove (BASE "/bef") == 0);
189   ASSERT (remove (BASE "/ouk") == 0);
190   ASSERT (remove (BASE "/ket") == 0);
191   ASSERT (remove (BASE "/lum") == 0);
192   ASSERT (remove (BASE "/tra") == 0);
193   ASSERT (remove (BASE) == 0);
194   ASSERT (remove ("ise") == 0);
195
196   return 0;
197 }