diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2022-06-15 18:51:47 +0200 |
---|---|---|
committer | Oliver Reiche <oliver.reiche@huawei.com> | 2022-06-20 15:23:02 +0200 |
commit | 855affd9b681d98f248009ddb2c1abe987029f72 (patch) | |
tree | 6d4dbfc2c99020772313f381d2f793950d2b03f4 /test/buildtool/crypto | |
parent | 391d982f2fbd98a2973f14e0b5969f66c2abd756 (diff) | |
download | justbuild-855affd9b681d98f248009ddb2c1abe987029f72.tar.gz |
Crypto: Refactor hash computation
... by renaming HashGenerator to (incremental) Hasher and
dropping support for Git/MD5 hashes. The Hasher does not
expose the actual hash implementation.
Diffstat (limited to 'test/buildtool/crypto')
-rw-r--r-- | test/buildtool/crypto/TARGETS | 10 | ||||
-rw-r--r-- | test/buildtool/crypto/crypto.test.cpp | 57 | ||||
-rw-r--r-- | test/buildtool/crypto/hash_function.test.cpp | 4 | ||||
-rw-r--r-- | test/buildtool/crypto/hasher.test.cpp | 28 |
4 files changed, 35 insertions, 64 deletions
diff --git a/test/buildtool/crypto/TARGETS b/test/buildtool/crypto/TARGETS index 44800068..c12fa855 100644 --- a/test/buildtool/crypto/TARGETS +++ b/test/buildtool/crypto/TARGETS @@ -1,11 +1,11 @@ -{ "crypto": +{ "hasher": { "type": ["@", "rules", "CC/test", "test"] - , "name": ["crypto"] - , "srcs": ["crypto.test.cpp"] + , "name": ["hasher"] + , "srcs": ["hasher.test.cpp"] , "deps": [ ["@", "catch2", "", "catch2"] , ["test", "catch-main"] - , ["src/buildtool/crypto", "hash_generator"] + , ["src/buildtool/crypto", "hasher"] ] , "stage": ["test", "buildtool", "crypto"] } @@ -21,5 +21,5 @@ , "stage": ["test", "buildtool", "crypto"] } , "TESTS": - {"type": "install", "tainted": ["test"], "deps": ["crypto", "hash_function"]} + {"type": "install", "tainted": ["test"], "deps": ["hasher", "hash_function"]} } diff --git a/test/buildtool/crypto/crypto.test.cpp b/test/buildtool/crypto/crypto.test.cpp deleted file mode 100644 index 78bea66b..00000000 --- a/test/buildtool/crypto/crypto.test.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include <algorithm> - -#include "catch2/catch.hpp" -#include "src/buildtool/crypto/hash_generator.hpp" - -template <HashGenerator::HashType type> -void test_single_hash(std::string const& bytes, std::string const& result) { - HashGenerator hash_gen{type}; - auto digest = hash_gen.Run(bytes); - CHECK(digest.HexString() == result); -} - -template <HashGenerator::HashType type> -void test_increment_hash(std::string const& bytes, std::string const& result) { - HashGenerator hash_gen{type}; - auto hasher = hash_gen.IncrementalHasher(); - hasher.Update(bytes); - auto digest = std::move(hasher).Finalize(); - CHECK(digest); - CHECK(digest->HexString() == result); -} - -TEST_CASE("Hash Generator", "[crypto]") { - std::string bytes{"test"}; - - SECTION("MD5") { - // same as: echo -n test | md5sum - test_single_hash<HashGenerator::HashType::MD5>( - bytes, "098f6bcd4621d373cade4e832627b4f6"); - test_increment_hash<HashGenerator::HashType::MD5>( - bytes, "098f6bcd4621d373cade4e832627b4f6"); - } - - SECTION("SHA-1") { - // same as: echo -n test | sha1sum - test_single_hash<HashGenerator::HashType::SHA1>( - bytes, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); - test_increment_hash<HashGenerator::HashType::SHA1>( - bytes, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); - } - - SECTION("SHA-256") { - // same as: echo -n test | sha256sum - test_single_hash<HashGenerator::HashType::SHA256>( - bytes, - "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); - test_increment_hash<HashGenerator::HashType::SHA256>( - bytes, - "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); - } - - SECTION("Git") { - // same as: echo -n test | git hash-object --stdin - test_single_hash<HashGenerator::HashType::GIT>( - bytes, "30d74d258442c7c65512eafab474568dd706c430"); - } -} diff --git a/test/buildtool/crypto/hash_function.test.cpp b/test/buildtool/crypto/hash_function.test.cpp index b56c82a6..2655d7a4 100644 --- a/test/buildtool/crypto/hash_function.test.cpp +++ b/test/buildtool/crypto/hash_function.test.cpp @@ -19,7 +19,7 @@ TEST_CASE("Hash Function", "[crypto]") { auto hasher = HashFunction::Hasher(); hasher.Update(bytes); - CHECK(std::move(hasher).Finalize()->HexString() == // NOLINT + CHECK(std::move(hasher).Finalize().HexString() == // NOLINT "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); } @@ -40,7 +40,7 @@ TEST_CASE("Hash Function", "[crypto]") { auto hasher = HashFunction::Hasher(); hasher.Update(bytes); CHECK( - std::move(hasher).Finalize()->HexString() == // NOLINT + std::move(hasher).Finalize().HexString() == // NOLINT "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); } } diff --git a/test/buildtool/crypto/hasher.test.cpp b/test/buildtool/crypto/hasher.test.cpp new file mode 100644 index 00000000..01a5cb12 --- /dev/null +++ b/test/buildtool/crypto/hasher.test.cpp @@ -0,0 +1,28 @@ +#include "catch2/catch.hpp" +#include "src/buildtool/crypto/hasher.hpp" + +template <Hasher::HashType type> +void test_increment_hash(std::string const& bytes, std::string const& result) { + Hasher hasher{type}; + hasher.Update(bytes.substr(0, bytes.size() / 2)); + hasher.Update(bytes.substr(bytes.size() / 2)); + auto digest = std::move(hasher).Finalize(); + CHECK(digest.HexString() == result); +} + +TEST_CASE("Hasher", "[crypto]") { + std::string bytes{"test"}; + + SECTION("SHA-1") { + // same as: echo -n test | sha1sum + test_increment_hash<Hasher::HashType::SHA1>( + bytes, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); + } + + SECTION("SHA-256") { + // same as: echo -n test | sha256sum + test_increment_hash<Hasher::HashType::SHA256>( + bytes, + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + } +} |