From 2a292c71f40f3592e33534ee6d63eb2ca5221d5e Mon Sep 17 00:00:00 2001 From: Kane York Date: Fri, 31 Jul 2020 16:28:04 -0700 Subject: fix(3p/nix/hash): initialize HashSink.ctx Fixup for CL 1492 (addcba11b05500ba28ade309de6bd53f8153a6c4) Additionally, add a test to verify functionality of HashSink. Change-Id: I2a74b925a1b93ed4d3add29021d759c93e813424 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1507 Tested-by: BuildkiteCI Reviewed-by: glittershark --- third_party/nix/src/libutil/hash.cc | 5 ++++- third_party/nix/src/tests/hash_test.cc | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'third_party/nix/src') diff --git a/third_party/nix/src/libutil/hash.cc b/third_party/nix/src/libutil/hash.cc index e9901d0ccb94..4c6eef0e6d9b 100644 --- a/third_party/nix/src/libutil/hash.cc +++ b/third_party/nix/src/libutil/hash.cc @@ -361,7 +361,10 @@ Hash hashFile(HashType ht, const Path& path) { return hash; } -HashSink::HashSink(HashType ht) : ht(ht), ctx(), bytes(0) { start(ht, *ctx); } +HashSink::HashSink(HashType ht) + : ht(ht), ctx(std::make_unique()), bytes(0) { + start(ht, *ctx); +} HashSink::~HashSink() { bufPos = 0; } diff --git a/third_party/nix/src/tests/hash_test.cc b/third_party/nix/src/tests/hash_test.cc index ea10e7b700b1..caa77f5d6b15 100644 --- a/third_party/nix/src/tests/hash_test.cc +++ b/third_party/nix/src/tests/hash_test.cc @@ -80,4 +80,22 @@ TEST(HashTest, SHA256DecodeFail) { HasSubstr("invalid base-16")); } +TEST(HashSink, SHA256) { + HashSink sink(htSHA256); + + sink.write(reinterpret_cast("fo"), 2); + HashResult partial = sink.currentHash(); + EXPECT_EQ(partial.first.to_string(Base16), + "sha256:" + "9c3aee7110b787f0fb5f81633a36392bd277ea945d44c874a9a23601aefe20cf"); + EXPECT_EQ(partial.second, 2); + + sink.write(reinterpret_cast("o"), 1); + HashResult end = sink.finish(); + EXPECT_EQ(end.first.to_string(Base16), + "sha256:" + "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"); + EXPECT_EQ(end.second, 3); +} + } // namespace nix -- cgit 1.4.1