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