Debian Codegraph @

tests/by-util/test_basenc.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 // spell-checker: ignore (encodings) lsbf msbf
7 // spell-checker: ignore autopad MFRGG MFRGGZDF abcdeabc baddecode CPNMUO
8
9 use uutests::{at_and_ucmd, new_ucmd};
10
11 #[test]
12 fn test_version() {
13 new_ucmd!()
14 .arg("--version")
15 .succeeds()
16 .no_stderr()
17 .stdout_is(format!("basenc {}\n", uucore::crate_version!()));
18 }
19
20 #[test]
21 fn test_z85_not_padded_decode() {
22 // The z85 crate deviates from the standard in some cases; we have to catch those
23 new_ucmd!()
24 .args(&["--z85", "-d"])
25 .pipe_in("##########")
26 .fails()
27 .stderr_only("basenc: error: invalid input\n");
28 }
29
30 #[test]
31 fn test_z85_not_padded_encode() {
32 // The z85 crate deviates from the standard in some cases; we have to catch those
33 new_ucmd!()
34 .args(&["--z85"])
35 .pipe_in("123")
36 .fails()
37 .stderr_only("basenc: error: invalid input (length must be multiple of 4 characters)\n");
38 }
39
40 #[test]
41 fn test_invalid_input() {
42 let cmd = new_ucmd!().args(&["--base32", "."]).fails();
43 if cfg!(windows) {
44 cmd.stderr_only("basenc: .: Permission denied\n");
45 } else if std::env::var("UUTESTS_WASM_RUNNER").is_ok() {
46 // wasi-libc may report a different error string than the host libc
47 cmd.stderr_contains("basenc: read error:");
48 } else {
49 cmd.stderr_only("basenc: read error: Is a directory\n");
50 }
51 }
52
53 #[test]
54 fn test_base64() {
55 new_ucmd!()
56 .arg("--base64")
57 .pipe_in("to>be?")
58 .succeeds()
59 .stdout_only("dG8+YmU/\n");
60 }
61
62 #[test]
63 fn test_base64_decode() {
64 new_ucmd!()
65 .args(&["--base64", "-d"])
66 .pipe_in("dG8+YmU/")
67 .succeeds()
68 .stdout_only("to>be?");
69 }
70
71 #[test]
72 fn test_base64url() {
73 new_ucmd!()
74 .arg("--base64url")
75 .pipe_in("to>be?")
76 .succeeds()
77 .stdout_only("dG8-YmU_\n");
78 }
79
80 #[test]
81 fn test_base64url_decode() {
82 new_ucmd!()
83 .args(&["--base64url", "-d"])
84 .pipe_in("dG8-YmU_")
85 .succeeds()
86 .stdout_only("to>be?");
87 }
88
89 #[test]
90 fn test_base32() {
91 new_ucmd!()
92 .arg("--base32")
93 .pipe_in("nice>base?")
94 .succeeds()
95 .stdout_only("NZUWGZJ6MJQXGZJ7\n"); // spell-checker:disable-line
96 }
97
98 #[test]
99 fn test_base32_decode() {
100 new_ucmd!()
101 .args(&["--base32", "-d"])
102 .pipe_in("NZUWGZJ6MJQXGZJ7") // spell-checker:disable-line
103 .succeeds()
104 .stdout_only("nice>base?");
105 }
106
107 #[test]
108 fn test_base32hex() {
109 new_ucmd!()
110 .arg("--base32hex")
111 .pipe_in("nice>base?")
112 .succeeds()
113 .stdout_only("DPKM6P9UC9GN6P9V\n"); // spell-checker:disable-line
114 }
115
116 #[test]
117 fn test_base32hex_decode() {
118 new_ucmd!()
119 .args(&["--base32hex", "-d"])
120 .pipe_in("DPKM6P9UC9GN6P9V") // spell-checker:disable-line
121 .succeeds()
122 .stdout_only("nice>base?");
123 }
124
125 #[test]
126 fn test_base32_autopad_short_quantum() {
127 new_ucmd!()
128 .args(&["--base32", "--decode"])
129 .pipe_in("MFRGG")
130 .succeeds()
131 .stdout_only("abc");
132 }
133
134 #[test]
135 fn test_base32_autopad_multiline_stream() {
136 new_ucmd!()
137 .args(&["--base32", "--decode"])
138 .pipe_in("MFRGGZDF\nMFRGG")
139 .succeeds()
140 .stdout_only("abcdeabc");
141 }
142
143 #[test]
144 fn test_base32_baddecode_keeps_prefix() {
145 new_ucmd!()
146 .args(&["--base32", "--decode"])
147 .pipe_in("MFRGGZDF=")
148 .fails()
149 .stdout_is("abcde")
150 .stderr_is("basenc: error: invalid input\n");
151 }
152
153 #[test]
154 fn test_base32hex_autopad_short_quantum() {
155 new_ucmd!()
156 .args(&["--base32hex", "--decode"])
157 .pipe_in("C5H66")
158 .succeeds()
159 .stdout_only("abc");
160 }
161
162 #[test]
163 fn test_base32hex_rejects_trailing_garbage() {
164 new_ucmd!()
165 .args(&["--base32hex", "-d"])
166 .pipe_in("VNC0FKD5W")
167 .fails()
168 .stdout_is_bytes(b"\xFD\xD8\x07\xD1\xA5")
169 .stderr_is("basenc: error: invalid input\n");
170 }
171
172 #[test]
173 fn test_base32hex_truncated_block_keeps_prefix() {
174 new_ucmd!()
175 .args(&["--base32hex", "-d"])
176 .pipe_in("CPNMUO")
177 .fails()
178 .stdout_is_bytes(b"foo")
179 .stderr_is("basenc: error: invalid input\n");
180 }
181
182 #[test]
183 fn test_base16() {
184 new_ucmd!()
185 .arg("--base16")
186 .pipe_in("Hello, World!")
187 .succeeds()
188 .stdout_only("48656C6C6F2C20576F726C6421\n");
189 }
190
191 #[test]
192 fn test_base16_decode() {
193 new_ucmd!()
194 .args(&["--base16", "-d"])
195 .pipe_in("48656C6C6F2C20576F726C6421")
196 .succeeds()
197 .stdout_only("Hello, World!");
198 }
199
200 #[test]
201 fn test_base16_decode_lowercase() {
202 new_ucmd!()
203 .args(&["--base16", "-d"])
204 .pipe_in("48656c6c6f2c20576f726c6421")
205 .succeeds()
206 .stdout_only("Hello, World!");
207 }
208
209 #[test]
210 fn test_base16_decode_and_ignore_garbage_lowercase() {
211 new_ucmd!()
212 .args(&["--base16", "-d", "-i"])
213 .pipe_in("48656c6c6f2c20576f726c6421")
214 .succeeds()
215 .stdout_only("Hello, World!");
216 }
217
218 #[test]
219 fn test_base2msbf() {
220 new_ucmd!()
221 .arg("--base2msbf")
222 .pipe_in("msbf")
223 .succeeds()
224 .stdout_only("01101101011100110110001001100110\n");
225 }
226
227 #[test]
228 fn test_base2msbf_decode() {
229 new_ucmd!()
230 .args(&["--base2msbf", "-d"])
231 .pipe_in("01101101011100110110001001100110")
232 .succeeds()
233 .stdout_only("msbf");
234 }
235
236 #[test]
237 fn test_base2lsbf() {
238 new_ucmd!()
239 .arg("--base2lsbf")
240 .pipe_in("lsbf")
241 .succeeds()
242 .stdout_only("00110110110011100100011001100110\n");
243 }
244
245 #[test]
246 fn test_base2lsbf_decode() {
247 new_ucmd!()
248 .args(&["--base2lsbf", "-d"])
249 .pipe_in("00110110110011100100011001100110")
250 .succeeds()
251 .stdout_only("lsbf");
252 }
253
254 #[test]
255 fn test_z85_decode() {
256 new_ucmd!()
257 .args(&["--z85", "-d"])
258 .pipe_in("nm=QNz.92jz/PV8")
259 .succeeds()
260 .stdout_only("Hello, World");
261 }
262
263 #[test]
264 fn test_base58() {
265 new_ucmd!()
266 .arg("--base58")
267 .pipe_in("Hello, World!")
268 .succeeds()
269 .stdout_only("72k1xXWG59fYdzSNoA\n");
270 }
271
272 #[test]
273 fn test_base58_decode() {
274 new_ucmd!()
275 .args(&["--base58", "-d"])
276 .pipe_in("72k1xXWG59fYdzSNoA")
277 .succeeds()
278 .stdout_only("Hello, World!");
279 }
280
281 #[test]
282 fn test_base58_large_file_no_chunking() {
283 // Regression test: base58 must process entire input as one big integer,
284 // not in 1024-byte chunks. This test ensures files >1024 bytes work correctly.
285 let (at, mut ucmd) = at_and_ucmd!();
286 let filename = "large_file.txt";
287
288 // spell-checker:disable
289 let input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(50);
290 // spell-checker:enable
291 at.write(filename, &input);
292
293 let result = ucmd.arg("--base58").arg(filename).succeeds();
294 let encoded = result.stdout_str();
295
296 // Verify the output ends with the expected suffix (matches GNU basenc output)
297 // spell-checker:disable
298 assert!(
299 encoded
300 .trim_end()
301 .ends_with("ZNRRacEnhrY83ZEYkpwWVZNFK5DFRasr\nw693NsNGtiQ9fYAj")
302 );
303 // spell-checker:enable
304 }
305
306 #[test]
307 fn test_choose_last_encoding_base64() {
308 new_ucmd!()
309 .args(&[
310 "--base2msbf",
311 "--base2lsbf",
312 "--base64url",
313 "--base32hex",
314 "--base32",
315 "--base16",
316 "--z85",
317 "--base64",
318 ])
319 .pipe_in("Hello, World!")
320 .succeeds()
321 .stdout_only("SGVsbG8sIFdvcmxkIQ==\n"); // spell-checker:disable-line
322 }
323
324 #[test]
325 fn test_choose_last_encoding_base2lsbf() {
326 new_ucmd!()
327 .args(&[
328 "--base64url",
329 "--base16",
330 "--base2msbf",
331 "--base32",
332 "--base64",
333 "--z85",
334 "--base32hex",
335 "--base2lsbf",
336 ])
337 .pipe_in("lsbf")
338 .succeeds()
339 .stdout_only("00110110110011100100011001100110\n");
340 }
341
342 #[test]
343 fn test_choose_last_encoding_base58() {
344 new_ucmd!()
345 .args(&["--base64", "--base32", "--base16", "--z85", "--base58"])
346 .pipe_in("Hello!")
347 .succeeds()
348 .stdout_only("d3yC1LKr\n");
349 }
350
351 #[test]
352 fn test_base32_decode_repeated() {
353 new_ucmd!()
354 .args(&[
355 "--ignore",
356 "--wrap=80",
357 "--base32hex",
358 "--z85",
359 "--ignore",
360 "--decode",
361 "--z85",
362 "--base32",
363 "-w",
364 "10",
365 ])
366 .pipe_in("NZUWGZJ6MJQXGZJ7") // spell-checker:disable-line
367 .succeeds()
368 .stdout_only("nice>base?");
369 }
370
371 // The restriction that input length has to be divisible by 4 only applies to data being encoded with Z85, not to the
372 // decoding of Z85-encoded data
373 #[test]
374 fn test_z85_length_check() {
375 new_ucmd!()
376 .args(&["--decode", "--z85"])
377 // Input has length 10, not divisible by 4
378 // spell-checker:disable-next-line
379 .pipe_in("f!$Kwh8WxM")
380 .succeeds()
381 .stdout_only("12345678");
382 }
383
384 #[test]
385 fn test_file() {
386 let (at, mut ucmd) = at_and_ucmd!();
387 let filename = "file";
388
389 at.write(filename, "foo");
390
391 ucmd.arg(filename)
392 .arg("--base64")
393 .succeeds()
394 .stdout_is("Zm9v\n");
395 }
396
397 #[test]
398 #[cfg(target_os = "linux")]
399 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
400 fn test_file_with_non_utf8_name() {
401 use std::os::unix::ffi::OsStringExt;
402 let (at, mut ucmd) = at_and_ucmd!();
403
404 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
405 std::fs::write(at.plus(&filename), b"foo").unwrap();
406
407 ucmd.arg(filename)
408 .arg("--base64")
409 .succeeds()
410 .stdout_is("Zm9v\n");
411 }