cksum: Match GNU behavior for digest length errors in --check mode#11499
cksum: Match GNU behavior for digest length errors in --check mode#11499cakebaker merged 2 commits intouutils:mainfrom
Conversation
|
GNU testsuite comparison: |
Merging this PR will not alter performance
Comparing Footnotes
|
AldanTanneo
left a comment
There was a problem hiding this comment.
lots of confusing naming regarding bit/byte length, see comments
also a minor correctness issue
| (AlgoKind::Shake128 | AlgoKind::Shake256, Some(bit_len)) => Some(bit_len.div_ceil(8)), | ||
| (AlgoKind::Shake128, None) => Some(sum::Shake128::DEFAULT_BIT_SIZE.div_ceil(8)), | ||
| (AlgoKind::Shake256, None) => Some(sum::Shake256::DEFAULT_BIT_SIZE.div_ceil(8)), | ||
| (AlgoKind::Blake2b | AlgoKind::Blake3, Some(byte_len)) => Some(byte_len * 8), |
There was a problem hiding this comment.
digest_char_length_hint now stores a bit length instead of a byte length hint? the name should probably change then.
| return None; | ||
| } | ||
|
|
||
| let byte_len_hint = bit_len_hint.map(|n| n / 8); |
There was a problem hiding this comment.
the division should probably be a .div_ceil(8), since for instance 4 bits still take a full byte to be represented in a truncated XOF output.
There was a problem hiding this comment.
Indeed, thanks 👍
| // If the algorithm is SHA2 and no length hint was provided, compute it | ||
| // from the size of the expected checksum. | ||
| let algo_byte_len = algo_byte_len | ||
| .or_else(|| (algo_kind == AlgoKind::Sha2).then(|| expected_checksum.len() * 8)); |
There was a problem hiding this comment.
wrong variable name? why do we multiply by 8 if this is a byte length
There was a problem hiding this comment.
Should change the name, but when algo is Blake, this variable holds bytes 😅
| /// When checking untagged format lines, non-XOF non-legacy algorithms | ||
| /// should report "improperly formatted lines" if the digest length isn't | ||
| /// equivalent to this. | ||
| pub fn expected_digest_byte_len(self) -> Option<usize> { |
There was a problem hiding this comment.
wrong function name: returns a bit length, not a byte length.
ac866ea to
59977fb
Compare
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
67e1369 to
4ebde21
Compare
|
GNU testsuite comparison: |
AldanTanneo
left a comment
There was a problem hiding this comment.
Looks good to me now (modulo the upcoming bit/byte uniformization)
tests/by-util/test_cksum.rs
Outdated
| fn test_check_sha_tagged_no_hint() { | ||
| // When checking a tagged line with SHA2 but with missing length, guess | ||
| // from the size of the digest, and raise "improperly formatted" if the | ||
| // size isn't a valid SHA length. | ||
|
|
||
| let (at, mut ucmd) = at_and_ucmd!(); | ||
| at.touch("a"); | ||
| at.touch("b"); | ||
|
|
||
| let checksum = "SHA2 (a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"; | ||
| let invalid = "SHA2 (b) = xxxx"; | ||
|
|
||
| ucmd.arg("-c") | ||
| .pipe_in(format!("{checksum}\n{invalid}")) | ||
| .succeeds() | ||
| .stdout_contains("a: OK") | ||
| .stderr_contains("WARNING: 1 line is improperly formatted"); | ||
| } |
There was a problem hiding this comment.
I think this test is incorrect because I get a failure when running the command manually with GNU cksum:
$ touch a b
$ printf "SHA2 (a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f\nSHA2 (b) = xxxx" | cksum -c
cksum: 'standard input': no properly formatted checksum lines found
$ echo $?
1
If I replace SHA2 with SHA224, however, I get the same result as in the test:
$ printf "SHA224 (a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f\nSHA224 (b) = xxxx" | cksum -c
a: OK
cksum: WARNING: 1 line is improperly formatted
There was a problem hiding this comment.
I made a mistake somewhere when testing. I'm investigating
There was a problem hiding this comment.
Looks like I hallaucinated this feature, I pushed a fix
tests/by-util/test_cksum.rs
Outdated
|
|
||
| let (at, mut ucmd) = at_and_ucmd!(); | ||
| at.touch("null"); | ||
| at.write("one", "A"); |
There was a problem hiding this comment.
I don't understand why you create this file as it isn't used afterwards.
There was a problem hiding this comment.
It was meant to be the filename used in the invalid checksum, thanks for noticing it !
I changed the test a bit to use both files, and I renamed them to a and b.
tests/by-util/test_cksum.rs
Outdated
| .pipe_in(format!("{invalid_checksum}\n{good_checksum}")) | ||
| .succeeds() | ||
| .stdout_contains("null: OK") | ||
| .stdout_does_not_contain("one: FAILED") |
There was a problem hiding this comment.
See my previous comment.
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
cakebaker
left a comment
There was a problem hiding this comment.
It looks good. Can you please do a rebase and reduce the number of commits?
…ssing related issues cksum: Fix --check failing when digest length is wrong on untagged line
c0c831f to
6723393
Compare
Did it, and rebased at the same time. |
|
GNU testsuite comparison: |
|
Thanks! |
This PR fixes #11202, as well as a few other related issues