Skip to content

Commit

Permalink
add test for Hasher::clone
Browse files Browse the repository at this point in the history
Reviewed By: mingtaoy

Differential Revision: D64566688

fbshipit-source-id: 84a6d98a1f67cbf2704af8fc391e166982d98d20
  • Loading branch information
Zale Young authored and facebook-github-bot committed Oct 28, 2024
1 parent 5a9099d commit 9ac2204
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
50 changes: 50 additions & 0 deletions fizz/crypto/test/HashTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ const std::vector<HashTestVector> kHashTestVectors = {

};

void doHashFinal(
const std::unique_ptr<Hasher>& hasher,
size_t len,
const std::string& expectedDigest) {
std::vector<unsigned char> out(len, 0);
folly::MutableByteRange outRange(out.data(), out.size());

hasher->hash_final(outRange);

ASSERT_EQ(folly::hexlify(out), expectedDigest);
}

void runHashTestWithFizzHasher(
const fizz::HasherFactoryWithMetadata* makeHasher) {
size_t hashLen = makeHasher->hashLength();
Expand All @@ -68,4 +80,42 @@ void runHashTestWithFizzHasher(
}
}

void runHashTestWithCloning(const fizz::HasherFactoryWithMetadata* makeHasher) {
size_t hashLen = makeHasher->hashLength();

for (auto& testVector : kHashTestVectors) {
std::unique_ptr<Hasher> outlivedHasher;
{
auto ogHasher = makeHasher->make();

folly::IOBuf messageBuf(
folly::IOBuf::COPY_BUFFER,
reinterpret_cast<const uint8_t*>(testVector.message.c_str()),
testVector.message.size());

ogHasher->hash_update(messageBuf);

{
auto hasherCopy = ogHasher->clone();
doHashFinal(
hasherCopy, hashLen, testVector.digest.at(makeHasher->id()));

// here the hasherCopy is destroyed
// we want to test that the destruction of hasherCopy does not affect
// ogHasher
}

outlivedHasher = ogHasher->clone();

doHashFinal(ogHasher, hashLen, testVector.digest.at(makeHasher->id()));

// ogHasher is destroyed here
// this tests that the destruction of ogHasher also does not affect any
// copy of it
}
doHashFinal(
outlivedHasher, hashLen, testVector.digest.at(makeHasher->id()));
}
}

} // namespace fizz::test
1 change: 1 addition & 0 deletions fizz/crypto/test/HashTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ extern const std::vector<HashTestVector> kHashTestVectors;

void runHashTestWithFizzHasher(
const fizz::HasherFactoryWithMetadata* makeHasher);
void runHashTestWithCloning(const fizz::HasherFactoryWithMetadata* makeHasher);
} // namespace fizz::test

0 comments on commit 9ac2204

Please sign in to comment.