Debian Codegraph @

tests/by-util/test_readlink.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 //
7 // spell-checker:ignore regfile parentdir
8
9 use uutests::util::{TestScenario, get_root_path};
10 use uutests::{at_and_ucmd, new_ucmd, path_concat, util_name};
11
12 static GIBBERISH: &str = "supercalifragilisticexpialidocious";
13
14 #[cfg(not(windows))]
15 static NOT_A_DIRECTORY: &str = "Not a directory";
16 #[cfg(windows)]
17 static NOT_A_DIRECTORY: &str = "The directory name is invalid.";
18
19 #[test]
20 fn test_no_args() {
21 new_ucmd!()
22 .fails_with_code(1)
23 .no_stdout()
24 .stderr_contains("the following required arguments were not provided"); // clap provided message
25 }
26
27 #[test]
28 fn test_invalid_arg() {
29 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
30 }
31
32 #[test]
33 fn test_resolve() {
34 let (at, mut ucmd) = at_and_ucmd!();
35
36 at.touch("foo");
37 at.symlink_file("foo", "bar");
38
39 ucmd.arg("bar").succeeds().stdout_contains("foo\n");
40 }
41
42 #[test]
43 fn test_canonicalize() {
44 let (at, mut ucmd) = at_and_ucmd!();
45 let actual = ucmd.arg("-f").arg(".").succeeds().stdout_move_str();
46 let expect = at.root_dir_resolved() + "\n";
47 println!("actual: {actual:?}");
48 println!("expect: {expect:?}");
49 assert_eq!(actual, expect);
50 }
51
52 #[test]
53 fn test_canonicalize_existing() {
54 let (at, mut ucmd) = at_and_ucmd!();
55 let actual = ucmd.arg("-e").arg(".").succeeds().stdout_move_str();
56 let expect = at.root_dir_resolved() + "\n";
57 println!("actual: {actual:?}");
58 println!("expect: {expect:?}");
59 assert_eq!(actual, expect);
60 }
61
62 #[test]
63 fn test_canonicalize_missing() {
64 let (at, mut ucmd) = at_and_ucmd!();
65 let actual = ucmd.arg("-m").arg(GIBBERISH).succeeds().stdout_move_str();
66 let expect = path_concat!(at.root_dir_resolved(), GIBBERISH) + "\n";
67 println!("actual: {actual:?}");
68 println!("expect: {expect:?}");
69 assert_eq!(actual, expect);
70 }
71
72 #[test]
73 #[cfg(unix)]
74 fn test_canonicalize_symlink_before_parentdir() {
75 // GNU readlink follows the symlink first and only then evaluates `..`.
76 // Logical resolution would collapse `link/..` up front and return the current directory instead.
77 let (at, mut ucmd) = at_and_ucmd!();
78 at.mkdir("real");
79 at.mkdir("real/sub");
80 at.relative_symlink_dir("real/sub", "link");
81
82 let actual = ucmd.args(&["-f", "link/.."]).succeeds().stdout_move_str();
83 let expect = format!("{}/real\n", at.root_dir_resolved());
84 assert_eq!(actual, expect);
85 }
86
87 #[test]
88 fn test_long_redirection_to_current_dir() {
89 let (at, mut ucmd) = at_and_ucmd!();
90 // Create a 256-character path to current directory
91 let dir = path_concat!(".", ..128);
92 let actual = ucmd
93 .arg("-n")
94 .arg("-m")
95 .arg(dir)
96 .succeeds()
97 .stdout_move_str();
98 let expect = at.root_dir_resolved();
99 println!("actual: {actual:?}");
100 println!("expect: {expect:?}");
101 assert_eq!(actual, expect);
102 }
103
104 #[test]
105 fn test_long_redirection_to_root() {
106 // Create a 255-character path to root
107 let dir = path_concat!("..", ..85);
108 let actual = new_ucmd!()
109 .arg("-n")
110 .arg("-m")
111 .arg(dir)
112 .succeeds()
113 .stdout_move_str();
114 let expect = get_root_path();
115 println!("actual: {actual:?}");
116 println!("expect: {expect:?}");
117 assert_eq!(actual, expect);
118 }
119
120 #[test]
121 fn test_symlink_to_itself_verbose() {
122 let (at, mut ucmd) = at_and_ucmd!();
123 at.relative_symlink_file("a", "a");
124 ucmd.args(&["-ev", "a"])
125 .fails_with_code(1)
126 .stderr_contains("Too many levels of symbolic links");
127 }
128
129 #[test]
130 #[cfg(not(windows))]
131 fn test_posixly_correct_regular_file() {
132 let scene = TestScenario::new(util_name!());
133 let at = &scene.fixtures;
134 at.touch("regfile");
135 scene
136 .ucmd()
137 .env("POSIXLY_CORRECT", "1")
138 .arg("regfile")
139 .fails_with_code(1)
140 .stderr_contains("Invalid argument")
141 .no_stdout();
142
143 // GNU behavior is to ignore -s and -q when POSIXLY_CORRECT is set.
144 scene
145 .ucmd()
146 .env("POSIXLY_CORRECT", "1")
147 .args(&["-s", "regfile"])
148 .fails_with_code(1)
149 .stderr_contains("Invalid argument")
150 .no_stdout();
151 scene
152 .ucmd()
153 .env("POSIXLY_CORRECT", "1")
154 .args(&["-q", "regfile"])
155 .fails_with_code(1)
156 .stderr_contains("Invalid argument")
157 .no_stdout();
158 }
159
160 #[test]
161 fn test_trailing_slash_regular_file() {
162 let scene = TestScenario::new(util_name!());
163 let at = &scene.fixtures;
164 at.touch("regfile");
165 scene
166 .ucmd()
167 .args(&["-ev", "./regfile/"])
168 .fails_with_code(1)
169 .stderr_contains(NOT_A_DIRECTORY)
170 .no_stdout();
171 scene
172 .ucmd()
173 .args(&["-e", "./regfile"])
174 .succeeds()
175 .stdout_contains("regfile");
176 }
177
178 #[test]
179 fn test_trailing_slash_symlink_to_regular_file() {
180 let scene = TestScenario::new(util_name!());
181 let at = &scene.fixtures;
182 at.touch("regfile");
183 at.relative_symlink_file("regfile", "link");
184 scene
185 .ucmd()
186 .args(&["-ev", "./link/"])
187 .fails_with_code(1)
188 .stderr_contains(NOT_A_DIRECTORY)
189 .no_stdout();
190 scene
191 .ucmd()
192 .args(&["-e", "./link"])
193 .succeeds()
194 .stdout_contains("regfile");
195 scene
196 .ucmd()
197 .args(&["-e", "./link/more"])
198 .fails_with_code(1)
199 .no_stdout();
200 }
201
202 #[test]
203 fn test_trailing_slash_directory() {
204 let scene = TestScenario::new(util_name!());
205 let at = &scene.fixtures;
206 at.mkdir("directory");
207 for query in ["./directory", "./directory/"] {
208 scene
209 .ucmd()
210 .args(&["-e", query])
211 .succeeds()
212 .stdout_contains("directory");
213 }
214 }
215
216 #[test]
217 fn test_trailing_slash_symlink_to_directory() {
218 let scene = TestScenario::new(util_name!());
219 let at = &scene.fixtures;
220 at.mkdir("directory");
221 at.relative_symlink_dir("directory", "link");
222 for query in ["./link", "./link/"] {
223 scene
224 .ucmd()
225 .args(&["-e", query])
226 .succeeds()
227 .stdout_contains("directory");
228 }
229 scene
230 .ucmd()
231 .args(&["-ev", "./link/more"])
232 .fails_with_code(1)
233 .stderr_contains("No such file or directory")
234 .no_stdout();
235 }
236
237 #[test]
238 fn test_trailing_slash_symlink_to_missing() {
239 let scene = TestScenario::new(util_name!());
240 let at = &scene.fixtures;
241 at.mkdir("subdir");
242 at.relative_symlink_file("missing", "link");
243 at.relative_symlink_file("subdir/missing", "link2");
244 for query in [
245 "missing",
246 "./missing/",
247 "link",
248 "./link/",
249 "link/more",
250 "link2",
251 "./link2/",
252 "link2/more",
253 ] {
254 scene
255 .ucmd()
256 .args(&["-ev", query])
257 .fails_with_code(1)
258 .stderr_contains("No such file or directory")
259 .no_stdout();
260 }
261 }
262
263 #[test]
264 fn test_canonicalize_trailing_slash_regfile() {
265 let scene = TestScenario::new(util_name!());
266 let at = &scene.fixtures;
267 at.touch("regfile");
268 at.relative_symlink_file("regfile", "link1");
269 for name in ["regfile", "link1"] {
270 scene
271 .ucmd()
272 .args(&["-f", name])
273 .succeeds()
274 .stdout_contains("regfile");
275 scene
276 .ucmd()
277 .args(&["-fv", &format!("./{name}/")])
278 .fails_with_code(1)
279 .stderr_contains(NOT_A_DIRECTORY)
280 .no_stdout();
281 scene
282 .ucmd()
283 .args(&["-fv", &format!("{name}/more")])
284 .fails_with_code(1)
285 .stderr_contains(NOT_A_DIRECTORY)
286 .no_stdout();
287 scene
288 .ucmd()
289 .args(&["-fv", &format!("./{name}/more/")])
290 .fails_with_code(1)
291 .stderr_contains(NOT_A_DIRECTORY)
292 .no_stdout();
293 }
294 }
295
296 #[test]
297 fn test_canonicalize_trailing_slash_subdir() {
298 let scene = TestScenario::new(util_name!());
299 let at = &scene.fixtures;
300 at.mkdir("subdir");
301 at.relative_symlink_dir("subdir", "link2");
302 for name in ["subdir", "link2"] {
303 scene
304 .ucmd()
305 .args(&["-f", name])
306 .succeeds()
307 .stdout_contains("subdir");
308 scene
309 .ucmd()
310 .args(&["-f", &format!("./{name}/")])
311 .succeeds()
312 .stdout_contains("subdir");
313 scene
314 .ucmd()
315 .args(&["-f", &format!("{name}/more")])
316 .succeeds()
317 .stdout_contains(path_concat!("subdir", "more"));
318 scene
319 .ucmd()
320 .args(&["-f", &format!("./{name}/more/")])
321 .succeeds()
322 .stdout_contains(path_concat!("subdir", "more"));
323 scene
324 .ucmd()
325 .args(&["-f", &format!("{name}/more/more2")])
326 .fails_with_code(1)
327 .no_stdout();
328 scene
329 .ucmd()
330 .args(&["-f", &format!("./{name}/more/more2/")])
331 .fails_with_code(1)
332 .no_stdout();
333 }
334 }
335
336 #[test]
337 fn test_canonicalize_trailing_slash_missing() {
338 let scene = TestScenario::new(util_name!());
339 let at = &scene.fixtures;
340 at.relative_symlink_file("missing", "link3");
341 for name in ["missing", "link3"] {
342 scene
343 .ucmd()
344 .args(&["-f", name])
345 .succeeds()
346 .stdout_contains("missing");
347 scene
348 .ucmd()
349 .args(&["-f", &format!("./{name}/")])
350 .succeeds()
351 .stdout_contains("missing");
352 scene
353 .ucmd()
354 .args(&["-f", &format!("{name}/more")])
355 .fails_with_code(1)
356 .no_stdout();
357 scene
358 .ucmd()
359 .args(&["-f", &format!("./{name}/more/")])
360 .fails_with_code(1)
361 .no_stdout();
362 }
363 }
364
365 #[test]
366 fn test_canonicalize_trailing_slash_subdir_missing() {
367 let scene = TestScenario::new(util_name!());
368 let at = &scene.fixtures;
369 at.mkdir("subdir");
370 at.relative_symlink_file("subdir/missing", "link4");
371 for query in ["link4", "./link4/"] {
372 scene
373 .ucmd()
374 .args(&["-f", query])
375 .succeeds()
376 .stdout_contains(path_concat!("subdir", "missing"));
377 }
378 for query in ["link4/more", "./link4/more/"] {
379 scene
380 .ucmd()
381 .args(&["-f", query])
382 .fails_with_code(1)
383 .no_stdout();
384 }
385 }
386
387 #[test]
388 fn test_canonicalize_trailing_slash_symlink_loop() {
389 let scene = TestScenario::new(util_name!());
390 let at = &scene.fixtures;
391 at.relative_symlink_file("link5", "link5");
392 for query in ["link5", "./link5/", "link5/more", "./link5/more/"] {
393 scene
394 .ucmd()
395 .args(&["-f", query])
396 .fails_with_code(1)
397 .no_stdout();
398 }
399 }
400
401 #[test]
402 #[cfg(not(windows))]
403 fn test_delimiters() {
404 new_ucmd!()
405 .args(&["--zero", "-n", "-m", "/a"])
406 .succeeds()
407 .stdout_only("/a");
408 new_ucmd!()
409 .args(&["-n", "-m", "/a"])
410 .succeeds()
411 .stdout_only("/a");
412 new_ucmd!()
413 .args(&["--zero", "-m", "/a"])
414 .succeeds()
415 .stdout_only("/a\0");
416 new_ucmd!()
417 .args(&["-m", "/a"])
418 .succeeds()
419 .stdout_only("/a\n");
420 new_ucmd!()
421 .args(&["--zero", "-n", "-m", "/a", "/a"])
422 .succeeds()
423 .stderr_contains("ignoring --no-newline with multiple arguments")
424 .stdout_is("/a\0/a\0");
425 new_ucmd!()
426 .args(&["-n", "-m", "/a", "/a"])
427 .succeeds()
428 .stderr_contains("ignoring --no-newline with multiple arguments")
429 .stdout_is("/a\n/a\n");
430 }
431
432 #[test]
433 #[cfg(target_os = "linux")]
434 fn test_readlink_non_utf8_paths() {
435 use std::ffi::OsStr;
436 use std::os::unix::ffi::OsStrExt;
437
438 let scene = TestScenario::new(util_name!());
439 let at = &scene.fixtures;
440 let file_name = "target_file";
441 at.touch(file_name);
442 let non_utf8_bytes = b"symlink_\xFF\xFE";
443 let non_utf8_name = OsStr::from_bytes(non_utf8_bytes);
444
445 std::os::unix::fs::symlink(at.plus_as_string(file_name), at.plus(non_utf8_name)).unwrap();
446
447 // Test that readlink handles non-UTF-8 symlink names without crashing
448 let result = scene.ucmd().arg(non_utf8_name).succeeds();
449
450 let output = result.stdout_str_lossy();
451 assert!(output.contains(file_name));
452 }
453
454 #[test]
455 fn test_verbose_or_silent() {
456 let scene = TestScenario::new(util_name!());
457 let at = &scene.fixtures;
458 at.touch("regfile");
459 scene.ucmd().arg("regfile").fails_with_code(1).no_output();
460 scene
461 .ucmd()
462 .args(&["-v", "regfile"])
463 .fails_with_code(1)
464 .stderr_contains(
465 #[cfg(not(windows))]
466 "Invalid argument",
467 #[cfg(windows)]
468 "regfile: The file or directory is not a reparse point.",
469 )
470 .no_stdout();
471 scene
472 .ucmd()
473 .args(&["-vs", "regfile"])
474 .fails_with_code(1)
475 .no_output();
476 scene
477 .ucmd()
478 .args(&["-sv", "regfile"])
479 .fails_with_code(1)
480 .stderr_contains(
481 #[cfg(not(windows))]
482 "Invalid argument",
483 #[cfg(windows)]
484 "regfile: The file or directory is not a reparse point.",
485 )
486 .no_stdout();
487 }