1 /* Test of execution of file name canonicalization.
2 Copyright (C) 2007-2008 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
21 #include "canonicalize.h"
27 #define ASSERT(expr) \
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
39 const char *program_name = "test-canonicalize";
44 /* Check that the symbolic link to a file can be resolved. */
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);
60 /* Check that the symbolic link to a directory can be resolved. */
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);
80 /* Check that a symbolic link to a nonexistent file yields NULL. */
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);
88 /* Check that a loop of symbolic links is detected. */
90 char *result1 = canonicalize_file_name ("ise");
91 char *result2 = canonicalize_filename_mode ("ise", CAN_EXISTING);
92 ASSERT (result1 == NULL);
93 ASSERT (result2 == NULL);
96 /* Check that alternate modes can resolve missing basenames. */
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);
108 /* Check that alternate modes can resolve broken symlink basenames. */
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);
120 /* Check that alternate modes can handle missing dirnames. */
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);
130 /* Ensure that the following is resolved properly.
131 Before 2007-09-27, it would mistakenly report a loop. */
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);