diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2022-06-15 18:37:59 +0200 |
---|---|---|
committer | Oliver Reiche <oliver.reiche@huawei.com> | 2022-06-20 15:23:02 +0200 |
commit | 391d982f2fbd98a2973f14e0b5969f66c2abd756 (patch) | |
tree | 52530f2b8b67bd01a4a01a9704c06e9f7624763b | |
parent | 1ea0b1d9ff98d8c44658046019c94f5952ad5c66 (diff) | |
download | justbuild-391d982f2fbd98a2973f14e0b5969f66c2abd756.tar.gz |
Crypto: Add tests for globally used hash functions
-rw-r--r-- | test/buildtool/crypto/TARGETS | 14 | ||||
-rw-r--r-- | test/buildtool/crypto/hash_function.test.cpp | 46 |
2 files changed, 59 insertions, 1 deletions
diff --git a/test/buildtool/crypto/TARGETS b/test/buildtool/crypto/TARGETS index 179d70ad..44800068 100644 --- a/test/buildtool/crypto/TARGETS +++ b/test/buildtool/crypto/TARGETS @@ -9,5 +9,17 @@ ] , "stage": ["test", "buildtool", "crypto"] } -, "TESTS": {"type": "install", "tainted": ["test"], "deps": ["crypto"]} +, "hash_function": + { "type": ["@", "rules", "CC/test", "test"] + , "name": ["hash_function"] + , "srcs": ["hash_function.test.cpp"] + , "deps": + [ ["@", "catch2", "", "catch2"] + , ["test", "catch-main"] + , ["src/buildtool/crypto", "hash_function"] + ] + , "stage": ["test", "buildtool", "crypto"] + } +, "TESTS": + {"type": "install", "tainted": ["test"], "deps": ["crypto", "hash_function"]} } diff --git a/test/buildtool/crypto/hash_function.test.cpp b/test/buildtool/crypto/hash_function.test.cpp new file mode 100644 index 00000000..b56c82a6 --- /dev/null +++ b/test/buildtool/crypto/hash_function.test.cpp @@ -0,0 +1,46 @@ +#include "catch2/catch.hpp" +#include "src/buildtool/crypto/hash_function.hpp" + +TEST_CASE("Hash Function", "[crypto]") { + std::string bytes{"test"}; + + SECTION("Native") { + HashFunction::SetHashType(HashFunction::JustHash::Native); + + // same as: echo -n test | sha1sum + CHECK(HashFunction::ComputeHash(bytes).HexString() == + "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); + // same as: echo -n test | git hash-object --stdin + CHECK(HashFunction::ComputeBlobHash(bytes).HexString() == + "30d74d258442c7c65512eafab474568dd706c430"); + // same as: echo -n test | git hash-object -t "tree" --stdin --literally + CHECK(HashFunction::ComputeTreeHash(bytes).HexString() == + "5f0ecc1a989593005e80f457446133250fcc43cc"); + + auto hasher = HashFunction::Hasher(); + hasher.Update(bytes); + CHECK(std::move(hasher).Finalize()->HexString() == // NOLINT + "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); + } + + SECTION("Compatible") { + HashFunction::SetHashType(HashFunction::JustHash::Compatible); + + // all same as: echo -n test | sha256sum + CHECK( + HashFunction::ComputeHash(bytes).HexString() == + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + CHECK( + HashFunction::ComputeBlobHash(bytes).HexString() == + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + CHECK( + HashFunction::ComputeTreeHash(bytes).HexString() == + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + + auto hasher = HashFunction::Hasher(); + hasher.Update(bytes); + CHECK( + std::move(hasher).Finalize()->HexString() == // NOLINT + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + } +} |