1 /* Test of execution of file name canonicalization.
2 Copyright (C) 2007 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__); \
38 const char *program_name = "test-canonicalize";
43 /* Check that the symbolic link to a file can be resolved. */
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);
59 /* Check that the symbolic link to a directory can be resolved. */
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);
79 /* Check that a symbolic link to a nonexistent file yields NULL. */
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);
87 /* Check that a loop of symbolic links is detected. */
89 char *result1 = canonicalize_file_name ("ise");
90 char *result2 = canonicalize_filename_mode ("ise", CAN_EXISTING);
91 ASSERT (result1 == NULL);
92 ASSERT (result2 == NULL);
95 /* Check that alternate modes can resolve missing basenames. */
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);
107 /* Check that alternate modes can resolve broken symlink basenames. */
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);
119 /* Check that alternate modes can handle missing dirnames. */
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);
129 /* Ensure that the following is resolved properly.
130 Before 2007-09-27, it would mistakenly report a loop. */
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);