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