Skip to content

Commit

Permalink
fix buggy unit test, bug in check for getDigestLength() and make it i…
Browse files Browse the repository at this point in the history
…nto an assert

credit to github user @trollkarlen for finding this.

changed it to an assert since it should be caught in CI if it ever
happens.

while doing that, I detected that the unit test was not sufficient to
cover the assert.

the only consequence was that with SHA512 as checksum, an alarming output
was printed. there was no missed/false duplicate detection.
  • Loading branch information
Paul Dreik authored and pauldreik committed Jan 12, 2025
1 parent 45135f8 commit 64a6725
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
8 changes: 3 additions & 5 deletions Fileinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ Fileinfo::fillwithbytes(enum readtobuffermode filltype,
}

// store the result of the checksum calculation in somebytes
int digestlength = chk.getDigestLength();
if (digestlength <= 0 ||
digestlength >= static_cast<int>(m_somebytes.size())) {
std::cerr << "wrong answer from getDigestLength! FIXME" << std::endl;
}
assert(chk.getDigestLength() > 0);
assert(static_cast<std::size_t>(chk.getDigestLength()) <=
m_somebytes.size());
if (chk.printToBuffer(m_somebytes.data(), m_somebytes.size())) {
std::cerr << "failed writing digest to buffer!!" << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion Fileinfo.hh
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private:
*/
std::int64_t m_identity;

static const int SomeByteSize = 64;
static constexpr int SomeByteSize = 64;

/// a buffer that will be filled with some bytes of the file or a hash
std::array<char, SomeByteSize> m_somebytes;
Expand Down
25 changes: 23 additions & 2 deletions testcases/checksum_options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,37 @@ set -e



allchecksumtypes="md5 sha1 sha256 sha512"


for checksumtype in md5 sha1 sha256 sha512; do
for checksumtype in $allchecksumtypes; do
reset_teststate
dbgecho "trying checksum $checksumtype"
dbgecho "trying checksum $checksumtype with small files"
echo checksumtest >a
echo checksumtest >b
$rdfind -checksum $checksumtype -deleteduplicates true a b
[ -e a ]
[ ! -e b ]
done

for checksumtype in $allchecksumtypes; do
reset_teststate
dbgecho "trying checksum $checksumtype with large files"
head -c 1000000 /dev/zero >a
head -c 1000000 /dev/zero >b
$rdfind -checksum $checksumtype -deleteduplicates true a b
[ -e a ]
[ ! -e b ]
done

for checksumtype in $allchecksumtypes; do
reset_teststate
dbgecho "trying checksum $checksumtype with large files that differ only in the middle"
( head -c 1000000 /dev/zero; echo =====a=====; head -c 1000000 /dev/zero) >a
( head -c 1000000 /dev/zero; echo =====b=====; head -c 1000000 /dev/zero) >b
$rdfind -checksum $checksumtype -deleteduplicates true a b
[ -e a ]
[ -e b ]
done

dbgecho "all is good in this test!"

0 comments on commit 64a6725

Please sign in to comment.