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