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