Debian Codegraph @

tests/by-util/test_pathchk.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 #[cfg(target_os = "linux")]
7 use std::os::unix::ffi::OsStringExt;
8 #[cfg(unix)]
9 use uutests::new_ucmd;
10
11 #[test]
12 #[cfg(unix)]
13 fn test_no_args() {
14 new_ucmd!()
15 .fails()
16 .no_stdout()
17 .stderr_contains("the following required arguments were not provided"); // clap provided message
18 }
19
20 #[test]
21 #[cfg(unix)]
22 fn test_invalid_arg() {
23 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
24 }
25
26 #[test]
27 #[cfg(unix)]
28 fn test_default_mode() {
29 // accept some reasonable default
30 new_ucmd!().args(&["dir/file"]).succeeds().no_stdout();
31
32 // accept non-portable chars
33 new_ucmd!().args(&["dir#/$file"]).succeeds().no_stdout();
34
35 // fail on empty path
36 new_ucmd!()
37 .args(&[""])
38 .fails()
39 .stderr_only("pathchk: '': No such file or directory\n");
40
41 new_ucmd!().args(&["", ""]).fails().stderr_only(
42 "pathchk: '': No such file or directory\n\
43 pathchk: '': No such file or directory\n",
44 );
45
46 // fail on long path
47 new_ucmd!()
48 .args(&["dir".repeat(libc::PATH_MAX as usize + 1)])
49 .fails()
50 .no_stdout();
51
52 // fail on long filename
53 new_ucmd!()
54 .args(&[format!(
55 "dir/{}",
56 "file".repeat(libc::FILENAME_MAX as usize + 1)
57 )])
58 .fails()
59 .no_stdout();
60 }
61
62 #[test]
63 #[cfg(unix)]
64 fn test_posix_mode() {
65 // accept some reasonable default
66 new_ucmd!().args(&["-p", "dir/file"]).succeeds().no_stdout();
67
68 // fail on long path
69 new_ucmd!()
70 .args(&["-p", "dir".repeat(libc::PATH_MAX as usize + 1).as_str()])
71 .fails()
72 .no_stdout();
73
74 // fail on long filename
75 new_ucmd!()
76 .args(&[
77 "-p",
78 format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(),
79 ])
80 .fails()
81 .no_stdout();
82
83 // fail on non-portable chars
84 new_ucmd!().args(&["-p", "dir#/$file"]).fails().no_stdout();
85 }
86
87 #[test]
88 #[cfg(unix)]
89 fn test_posix_special() {
90 // accept some reasonable default
91 new_ucmd!().args(&["-P", "dir/file"]).succeeds().no_stdout();
92
93 // accept non-portable chars
94 new_ucmd!()
95 .args(&["-P", "dir#/$file"])
96 .succeeds()
97 .no_stdout();
98
99 // accept non-leading hyphen
100 new_ucmd!()
101 .args(&["-P", "dir/file-name"])
102 .succeeds()
103 .no_stdout();
104
105 // fail on long path
106 new_ucmd!()
107 .args(&["-P", "dir".repeat(libc::PATH_MAX as usize + 1).as_str()])
108 .fails()
109 .no_stdout();
110
111 // fail on long filename
112 new_ucmd!()
113 .args(&[
114 "-P",
115 format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(),
116 ])
117 .fails()
118 .no_stdout();
119
120 // fail on leading hyphen char
121 new_ucmd!().args(&["-P", "dir/-file"]).fails().no_stdout();
122
123 // fail on empty path
124 new_ucmd!().args(&["-P", ""]).fails().no_stdout();
125 }
126
127 #[test]
128 #[cfg(unix)]
129 fn test_posix_all() {
130 // accept some reasonable default
131 new_ucmd!()
132 .args(&["-p", "-P", "dir/file"])
133 .succeeds()
134 .no_stdout();
135
136 // accept non-leading hyphen
137 new_ucmd!()
138 .args(&["-p", "-P", "dir/file-name"])
139 .succeeds()
140 .no_stdout();
141
142 // fail on long path
143 new_ucmd!()
144 .args(&[
145 "-p",
146 "-P",
147 "dir".repeat(libc::PATH_MAX as usize + 1).as_str(),
148 ])
149 .fails()
150 .no_stdout();
151
152 // fail on long filename
153 new_ucmd!()
154 .args(&[
155 "-p",
156 "-P",
157 format!("dir/{}", "file".repeat(libc::FILENAME_MAX as usize + 1)).as_str(),
158 ])
159 .fails()
160 .no_stdout();
161
162 // fail on non-portable chars
163 new_ucmd!()
164 .args(&["-p", "-P", "dir#/$file"])
165 .fails()
166 .no_stdout();
167
168 // fail on leading hyphen char
169 new_ucmd!()
170 .args(&["-p", "-P", "dir/-file"])
171 .fails()
172 .no_stdout();
173
174 // fail on empty path
175 new_ucmd!()
176 .args(&["-p", "-P", ""])
177 .fails()
178 .stderr_only("pathchk: empty file name\n");
179 }
180
181 #[test]
182 #[cfg(unix)]
183 fn test_empty_path_portability_message() {
184 // In every portability mode (-p, -P, --portability) GNU reports an empty
185 // file name with the program-name prefix and before any filesystem check.
186 for flag in ["-p", "-P", "--portability"] {
187 new_ucmd!()
188 .args(&[flag, ""])
189 .fails()
190 .stderr_only("pathchk: empty file name\n");
191 }
192 }
193
194 #[test]
195 #[cfg(target_os = "linux")]
196 fn test_pathchk_non_utf8_paths() {
197 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
198 new_ucmd!().arg(&filename).succeeds();
199 }
200
201 #[test]
202 #[cfg(target_os = "linux")]
203 fn test_not_a_directory_clean() {
204 // https://github.com/uutils/coreutils/issues/13888
205 new_ucmd!()
206 .arg("/dev/full/")
207 .fails()
208 .stderr_is("pathchk: /dev/full/: Not a directory\n");
209 }