Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / tests / test-canonicalize.c
1 /* Test of execution of file name canonicalization.
2    Copyright (C) 2007 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
37
38 const char *program_name = "test-canonicalize";
39
40 int
41 main ()
42 {
43   /* Check that the symbolic link to a file can be resolved.  */
44   {
45     char *result1 = canonicalize_file_name ("t-can.tmp/huk");
46     char *result2 = canonicalize_file_name ("t-can.tmp/tra");
47     char *result3 = canonicalize_filename_mode ("t-can.tmp/huk", CAN_EXISTING);
48     ASSERT (result1 != NULL);
49     ASSERT (result2 != NULL);
50     ASSERT (result3 != NULL);
51     ASSERT (strcmp (result1, result2) == 0);
52     ASSERT (strcmp (result2, result3) == 0);
53     ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/tra") == 0);
54     free (result1);
55     free (result2);
56     free (result3);
57   }
58
59   /* Check that the symbolic link to a directory can be resolved.  */
60   {
61     char *result1 = canonicalize_file_name ("t-can.tmp/plo");
62     char *result2 = canonicalize_file_name ("t-can.tmp/bef");
63     char *result3 = canonicalize_file_name ("t-can.tmp/lum");
64     char *result4 = canonicalize_filename_mode ("t-can.tmp/plo", CAN_EXISTING);
65     ASSERT (result1 != NULL);
66     ASSERT (result2 != NULL);
67     ASSERT (result3 != NULL);
68     ASSERT (result4 != NULL);
69     ASSERT (strcmp (result1, result2) == 0);
70     ASSERT (strcmp (result2, result3) == 0);
71     ASSERT (strcmp (result3, result4) == 0);
72     ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/lum") == 0);
73     free (result1);
74     free (result2);
75     free (result3);
76     free (result4);
77   }
78
79   /* Check that a symbolic link to a nonexistent file yields NULL.  */
80   {
81     char *result1 = canonicalize_file_name ("t-can.tmp/ouk");
82     char *result2 = canonicalize_filename_mode ("t-can.tmp/ouk", CAN_EXISTING);
83     ASSERT (result1 == NULL);
84     ASSERT (result2 == NULL);
85   }
86
87   /* Check that a loop of symbolic links is detected.  */
88   {
89     char *result1 = canonicalize_file_name ("ise");
90     char *result2 = canonicalize_filename_mode ("ise", CAN_EXISTING);
91     ASSERT (result1 == NULL);
92     ASSERT (result2 == NULL);
93   }
94
95   /* Check that alternate modes can resolve missing basenames.  */
96   {
97     char *result1 = canonicalize_filename_mode ("t-can.tmp/zzz", CAN_ALL_BUT_LAST);
98     char *result2 = canonicalize_filename_mode ("t-can.tmp/zzz", CAN_MISSING);
99     ASSERT (result1 != NULL);
100     ASSERT (result2 != NULL);
101     ASSERT (strcmp (result1, result2) == 0);
102     ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/zzz") == 0);
103     free (result1);
104     free (result2);
105   }
106
107   /* Check that alternate modes can resolve broken symlink basenames.  */
108   {
109     char *result1 = canonicalize_filename_mode ("t-can.tmp/ouk", CAN_ALL_BUT_LAST);
110     char *result2 = canonicalize_filename_mode ("t-can.tmp/ouk", CAN_MISSING);
111     ASSERT (result1 != NULL);
112     ASSERT (result2 != NULL);
113     ASSERT (strcmp (result1, result2) == 0);
114     ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/wum") == 0);
115     free (result1);
116     free (result2);
117   }
118
119   /* Check that alternate modes can handle missing dirnames.  */
120   {
121     char *result1 = canonicalize_filename_mode ("t-can.zzz/zzz", CAN_ALL_BUT_LAST);
122     char *result2 = canonicalize_filename_mode ("t-can.zzz/zzz", CAN_MISSING);
123     ASSERT (result1 == NULL);
124     ASSERT (result2 != NULL);
125     ASSERT (strcmp (result2 + strlen (result2) - 14, "/t-can.zzz/zzz") == 0);
126     free (result2);
127   }
128
129   /* Ensure that the following is resolved properly.
130      Before 2007-09-27, it would mistakenly report a loop.  */
131   {
132     char *result1 = canonicalize_filename_mode ("t-can.tmp", CAN_EXISTING);
133     char *result2 = canonicalize_filename_mode ("t-can.tmp/p/1", CAN_EXISTING);
134     ASSERT (result1 != NULL);
135     ASSERT (result2 != NULL);
136     ASSERT (strcmp (result2 + strlen (result1), "/d/2") == 0);
137     free (result1);
138     free (result2);
139   }
140
141   return 0;
142 }