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