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