Skip to content

Commit 87f6142

Browse files
committed
test(cksum): Add sha2 tests
1 parent 7aa46dd commit 87f6142

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

tests/by-util/test_cksum.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use uutests::util_name;
1212
const ALGOS: [&str; 11] = [
1313
"sysv", "bsd", "crc", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "blake2b", "sm3",
1414
];
15+
const SHA_LENGTHS: [u32; 4] = [224, 256, 384, 512];
1516

1617
#[test]
1718
fn test_invalid_arg() {
@@ -292,6 +293,146 @@ fn test_untagged_algorithm_stdin() {
292293
}
293294
}
294295

296+
#[test]
297+
fn test_sha2_wrong_length() {
298+
for l in [0, 13, 819_111_123] {
299+
new_ucmd!()
300+
.arg("--algorithm=sha2")
301+
.arg(format!("--length={l}"))
302+
.arg("lorem_ipsum.txt")
303+
.fails_with_code(1)
304+
.no_stdout()
305+
.stderr_contains(format!("invalid length: '{l}'"));
306+
}
307+
}
308+
309+
#[test]
310+
fn test_sha2_single_file() {
311+
for l in SHA_LENGTHS {
312+
new_ucmd!()
313+
.arg("--algorithm=sha2")
314+
.arg(format!("--length={l}"))
315+
.arg("lorem_ipsum.txt")
316+
.succeeds()
317+
.stdout_is_fixture(format!("sha{l}_single_file.expected"));
318+
}
319+
}
320+
321+
#[test]
322+
fn test_sha2_multiple_files() {
323+
for l in SHA_LENGTHS {
324+
new_ucmd!()
325+
.arg("--algorithm=sha2")
326+
.arg(format!("--length={l}"))
327+
.arg("lorem_ipsum.txt")
328+
.arg("alice_in_wonderland.txt")
329+
.succeeds()
330+
.stdout_is_fixture(format!("sha{l}_multiple_files.expected"));
331+
}
332+
}
333+
334+
#[test]
335+
fn test_sha2_stdin() {
336+
for l in SHA_LENGTHS {
337+
new_ucmd!()
338+
.arg("--algorithm=sha2")
339+
.arg(format!("--length={l}"))
340+
.pipe_in_fixture("lorem_ipsum.txt")
341+
.succeeds()
342+
.stdout_is_fixture(format!("sha{l}_stdin.expected"));
343+
}
344+
}
345+
346+
#[test]
347+
fn test_untagged_sha2_single_file() {
348+
for l in SHA_LENGTHS {
349+
new_ucmd!()
350+
.arg("--untagged")
351+
.arg("--algorithm=sha2")
352+
.arg(format!("--length={l}"))
353+
.arg("lorem_ipsum.txt")
354+
.succeeds()
355+
.stdout_is_fixture(format!("untagged/sha{l}_single_file.expected"));
356+
}
357+
}
358+
359+
#[test]
360+
fn test_untagged_sha2_multiple_files() {
361+
for l in SHA_LENGTHS {
362+
new_ucmd!()
363+
.arg("--untagged")
364+
.arg("--algorithm=sha2")
365+
.arg(format!("--length={l}"))
366+
.arg("lorem_ipsum.txt")
367+
.arg("alice_in_wonderland.txt")
368+
.succeeds()
369+
.stdout_is_fixture(format!("untagged/sha{l}_multiple_files.expected"));
370+
}
371+
}
372+
373+
#[test]
374+
fn test_untagged_sha2_stdin() {
375+
for l in SHA_LENGTHS {
376+
new_ucmd!()
377+
.arg("--untagged")
378+
.arg("--algorithm=sha2")
379+
.arg(format!("--length={l}"))
380+
.pipe_in_fixture("lorem_ipsum.txt")
381+
.succeeds()
382+
.stdout_is_fixture(format!("untagged/sha{l}_stdin.expected"));
383+
}
384+
}
385+
386+
#[test]
387+
fn test_check_tagged_sha2_single_file() {
388+
for l in SHA_LENGTHS {
389+
new_ucmd!()
390+
.arg("--check")
391+
.arg(format!("sha{l}_single_file.expected"))
392+
.succeeds()
393+
.stdout_is("lorem_ipsum.txt: OK\n");
394+
}
395+
}
396+
397+
#[test]
398+
fn test_check_tagged_sha2_multiple_files() {
399+
for l in SHA_LENGTHS {
400+
new_ucmd!()
401+
.arg("--check")
402+
.arg(format!("sha{l}_multiple_files.expected"))
403+
.succeeds()
404+
.stdout_contains("lorem_ipsum.txt: OK\n")
405+
.stdout_contains("alice_in_wonderland.txt: OK\n");
406+
}
407+
}
408+
409+
// When checking sha2 in untagged mode, the length is automatically deduced
410+
// from the length of the digest.
411+
#[test]
412+
fn test_check_untagged_sha2_single_file() {
413+
for l in SHA_LENGTHS {
414+
new_ucmd!()
415+
.arg("--check")
416+
.arg("--algorithm=sha2")
417+
.arg(format!("untagged/sha{l}_single_file.expected"))
418+
.succeeds()
419+
.stdout_is("lorem_ipsum.txt: OK\n");
420+
}
421+
}
422+
423+
#[test]
424+
fn test_check_untagged_sha2_multiple_files() {
425+
for l in SHA_LENGTHS {
426+
new_ucmd!()
427+
.arg("--check")
428+
.arg("--algorithm=sha2")
429+
.arg(format!("untagged/sha{l}_multiple_files.expected"))
430+
.succeeds()
431+
.stdout_contains("lorem_ipsum.txt: OK\n")
432+
.stdout_contains("alice_in_wonderland.txt: OK\n");
433+
}
434+
}
435+
295436
#[test]
296437
fn test_check_algo() {
297438
for algo in ["bsd", "sysv", "crc", "crc32b"] {

0 commit comments

Comments
 (0)