test-canonicalize-lgpl: consolidate into single C program
[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 "canonicalize.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #if !HAVE_SYMLINK
32 # define symlink(a,b) (-1)
33 #endif /* !HAVE_SYMLINK */
34
35 #define ASSERT(expr) \
36   do                                                                         \
37     {                                                                        \
38       if (!(expr))                                                           \
39         {                                                                    \
40           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41           fflush (stderr);                                                   \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 #define BASE "t-can-lgpl.tmp"
48
49 int
50 main ()
51 {
52 #ifdef GNULIB_CANONICALIZE
53   /* No need to test canonicalize-lgpl module if canonicalize is also
54      in use.  */
55   return 0;
56 #endif
57
58   /* Setup some hierarchy to be used by this test.  Start by removing
59      any leftovers from a previous partial run.  */
60   {
61     int fd;
62     ASSERT (system ("rm -rf " BASE " ise") == 0);
63     ASSERT (mkdir (BASE, 0700) == 0);
64     fd = creat (BASE "/tra", 0600);
65     ASSERT (0 <= fd);
66     ASSERT (close (fd) == 0);
67   }
68
69   /* Check for ., .., intermediate // handling, and for error cases.  */
70   {
71     char *result = canonicalize_file_name (BASE "//./..//" BASE "/tra");
72     ASSERT (result != NULL);
73     ASSERT (strstr (result, "/" BASE "/tra")
74             == result + strlen (result) - strlen ("/" BASE "/tra"));
75     free (result);
76     errno = 0;
77     result = canonicalize_file_name ("");
78     ASSERT (result == NULL);
79     ASSERT (errno == ENOENT);
80     errno = 0;
81     result = canonicalize_file_name (NULL);
82     ASSERT (result == NULL);
83     ASSERT (errno == EINVAL);
84   }
85
86   /* From here on out, tests involve symlinks.  */
87   if (symlink (BASE "/ket", "ise") != 0)
88     {
89       ASSERT (remove (BASE "/tra") == 0);
90       ASSERT (rmdir (BASE) == 0);
91       fputs ("skipping test: symlinks not supported on this filesystem\n",
92              stderr);
93       return 77;
94     }
95   ASSERT (symlink ("bef", BASE "/plo") == 0);
96   ASSERT (symlink ("tra", BASE "/huk") == 0);
97   ASSERT (symlink ("lum", BASE "/bef") == 0);
98   ASSERT (symlink ("wum", BASE "/ouk") == 0);
99   ASSERT (symlink ("../ise", BASE "/ket") == 0);
100   ASSERT (mkdir (BASE "/lum", 0700) == 0);
101
102   /* Check that the symbolic link to a file can be resolved.  */
103   {
104     char *result1 = canonicalize_file_name (BASE "/huk");
105     char *result2 = canonicalize_file_name (BASE "/tra");
106     ASSERT (result1 != NULL);
107     ASSERT (result2 != NULL);
108     ASSERT (strcmp (result1, result2) == 0);
109     ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/tra"),
110                     "/" BASE "/tra") == 0);
111     free (result1);
112     free (result2);
113   }
114
115   /* Check that the symbolic link to a directory can be resolved.  */
116   {
117     char *result1 = canonicalize_file_name (BASE "/plo");
118     char *result2 = canonicalize_file_name (BASE "/bef");
119     char *result3 = canonicalize_file_name (BASE "/lum");
120     ASSERT (result1 != NULL);
121     ASSERT (result2 != NULL);
122     ASSERT (result3 != NULL);
123     ASSERT (strcmp (result1, result2) == 0);
124     ASSERT (strcmp (result2, result3) == 0);
125     ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/lum"),
126                     "/" BASE "/lum") == 0);
127     free (result1);
128     free (result2);
129     free (result3);
130   }
131
132   /* Check that a symbolic link to a nonexistent file yields NULL.  */
133   {
134     char *result;
135     errno = 0;
136     result = canonicalize_file_name (BASE "/ouk");
137     ASSERT (result == NULL);
138     ASSERT (errno == ENOENT);
139   }
140
141   /* Check that a loop of symbolic links is detected.  */
142   {
143     char *result;
144     errno = 0;
145     result = canonicalize_file_name ("ise");
146     ASSERT (result == NULL);
147     ASSERT (errno == ELOOP);
148   }
149
150   /* Cleanup.  */
151   ASSERT (remove (BASE "/plo") == 0);
152   ASSERT (remove (BASE "/huk") == 0);
153   ASSERT (remove (BASE "/bef") == 0);
154   ASSERT (remove (BASE "/ouk") == 0);
155   ASSERT (remove (BASE "/ket") == 0);
156   ASSERT (remove (BASE "/lum") == 0);
157   ASSERT (remove (BASE "/tra") == 0);
158   ASSERT (remove (BASE) == 0);
159   ASSERT (remove ("ise") == 0);
160
161   return 0;
162 }