Fix testing canonicalize on cygwin.
[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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "canonicalize.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
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   return 0;
131 }