]>
Commit | Line | Data |
---|---|---|
394f4f17 | 1 | /* Test program for returning the canonical absolute name of a given file. |
c84142e8 UD |
2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. | |
4 | Contributed by David Mosberger <davidm@azstarnet.com>. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Library General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Library General Public | |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
394f4f17 RM |
20 | |
21 | /* This file must be run from within a directory called "stdlib". */ | |
22 | ||
23 | #include <errno.h> | |
24 | #include <fcntl.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <sys/param.h> | |
30 | ||
2303f5fd | 31 | static char cwd[PATH_MAX]; |
394f4f17 RM |
32 | static size_t cwd_len; |
33 | ||
34 | struct { | |
35 | const char * name; | |
36 | const char * value; | |
37 | } symlinks[] = { | |
38 | {"SYMLINK_LOOP", "SYMLINK_LOOP"}, | |
39 | {"SYMLINK_1", "."}, | |
40 | {"SYMLINK_2", "//////./../../etc"}, | |
41 | {"SYMLINK_3", "SYMLINK_1"}, | |
42 | {"SYMLINK_4", "SYMLINK_2"}, | |
43 | {"SYMLINK_5", "doesNotExist"}, | |
44 | }; | |
45 | ||
46 | struct { | |
47 | const char * in, * out, * resolved; | |
ec4b0518 | 48 | int error; |
394f4f17 RM |
49 | } tests[] = { |
50 | /* 0 */ | |
51 | {"/", "/"}, | |
52 | {"/////////////////////////////////", "/"}, | |
53 | {"/.././.././.././..///", "/"}, | |
54 | {"/etc", "/etc"}, | |
55 | {"/etc/../etc", "/etc"}, | |
56 | /* 5 */ | |
57 | {"/doesNotExist/../etc", 0, "/doesNotExist", ENOENT}, | |
58 | {"./././././././././.", "."}, | |
59 | {"/etc/.//doesNotExist", 0, "/etc/doesNotExist", ENOENT}, | |
60 | {"./doesExist", "./doesExist"}, | |
61 | {"./doesExist/", "./doesExist"}, | |
62 | /* 10 */ | |
63 | {"./doesExist/../doesExist", "./doesExist"}, | |
64 | {"foobar", 0, "./foobar", ENOENT}, | |
65 | {".", "."}, | |
66 | {"./foobar", 0, "./foobar", ENOENT}, | |
67 | {"SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP}, | |
68 | /* 15 */ | |
69 | {"./SYMLINK_LOOP", 0, "./SYMLINK_LOOP", ELOOP}, | |
70 | {"SYMLINK_1", "."}, | |
71 | {"SYMLINK_1/foobar", 0, "./foobar", ENOENT}, | |
72 | {"SYMLINK_2", "/etc"}, | |
73 | {"SYMLINK_3", "."}, | |
74 | /* 20 */ | |
75 | {"SYMLINK_4", "/etc"}, | |
76 | {"../stdlib/SYMLINK_1", "."}, | |
77 | {"../stdlib/SYMLINK_2", "/etc"}, | |
78 | {"../stdlib/SYMLINK_3", "."}, | |
79 | {"../stdlib/SYMLINK_4", "/etc"}, | |
80 | /* 25 */ | |
81 | {"./SYMLINK_5", 0, "./doesNotExist", ENOENT}, | |
82 | {"SYMLINK_5", 0, "./doesNotExist", ENOENT}, | |
83 | {"SYMLINK_5/foobar", 0, "./doesNotExist", ENOENT}, | |
84 | {"doesExist/../../stdlib/doesExist", "./doesExist"}, | |
85 | {"doesExist/.././../stdlib/.", "."} | |
86 | }; | |
87 | ||
88 | ||
89 | int | |
90 | check_path (const char * result, const char * expected) | |
91 | { | |
92 | int good; | |
93 | ||
94 | if (!result) | |
95 | return (expected == NULL); | |
96 | ||
97 | if (!expected) | |
98 | return 0; | |
99 | ||
100 | if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0')) | |
101 | good = (strncmp (result, cwd, cwd_len) == 0 | |
102 | && strcmp (result + cwd_len, expected + 1) == 0); | |
103 | else | |
104 | good = (strcmp (expected, result) == 0); | |
105 | ||
106 | return good; | |
107 | } | |
108 | ||
109 | ||
d68171ed | 110 | int |
394f4f17 RM |
111 | main (int argc, char ** argv) |
112 | { | |
113 | char * result; | |
114 | int fd, i, errors = 0; | |
115 | char buf[PATH_MAX]; | |
116 | ||
117 | getcwd (cwd, sizeof(buf)); | |
118 | cwd_len = strlen (cwd); | |
119 | ||
d68171ed | 120 | for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i) |
394f4f17 RM |
121 | symlink (symlinks[i].value, symlinks[i].name); |
122 | ||
123 | fd = open("doesExist", O_CREAT | O_EXCL, 0777); | |
124 | ||
d68171ed | 125 | for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i) |
394f4f17 RM |
126 | { |
127 | buf[0] = '\0'; | |
128 | result = realpath (tests[i].in, buf); | |
129 | ||
130 | if (!check_path (result, tests[i].out)) | |
131 | { | |
132 | printf ("%s: flunked test %d (expected `%s', got `%s')\n", | |
133 | argv[0], i, tests[i].out ? tests[i].out : "NULL", | |
134 | result ? result : "NULL"); | |
135 | ++errors; | |
136 | continue; | |
137 | } | |
138 | ||
139 | if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved)) | |
140 | { | |
141 | printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n", | |
142 | argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved, | |
143 | buf); | |
144 | ++errors; | |
145 | continue; | |
146 | } | |
147 | ||
ec4b0518 | 148 | if (!tests[i].out && errno != tests[i].error) |
394f4f17 RM |
149 | { |
150 | printf ("%s: flunked test %d (expected errno %d, got %d)\n", | |
d68171ed | 151 | argv[0], i, tests[i].error, errno); |
394f4f17 RM |
152 | ++errors; |
153 | continue; | |
154 | } | |
155 | } | |
156 | ||
157 | getcwd (buf, sizeof(buf)); | |
158 | if (strcmp (buf, cwd)) | |
159 | { | |
160 | printf ("%s: current working directory changed from %s to %s\n", | |
161 | argv[0], cwd, buf); | |
162 | ++errors; | |
163 | } | |
164 | ||
165 | if (fd >= 0) | |
166 | unlink("doesExist"); | |
167 | ||
d68171ed | 168 | for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i) |
394f4f17 RM |
169 | unlink (symlinks[i].name); |
170 | ||
d68171ed | 171 | if (errors != 0) |
394f4f17 RM |
172 | { |
173 | printf ("%d errors.\n", errors); | |
174 | exit (EXIT_FAILURE); | |
175 | } | |
d68171ed UD |
176 | |
177 | puts ("No errors."); | |
178 | return EXIT_SUCCESS; | |
394f4f17 | 179 | } |