about summary refs log tree commit diff
path: root/third_party/nix/src/tests
diff options
context:
space:
mode:
authorKane York <kanepyork@gmail.com>2020-07-27T23·50-0700
committerkanepyork <rikingcoding@gmail.com>2020-07-28T02·04+0000
commit31f9ee58d0c91d02d96d29ea6e09bf0e4f9c5b92 (patch)
tree82b2b35d194839860240985a99f2a0e32d18c4e6 /third_party/nix/src/tests
parent976a36c2e482f416acd79a624e6d96cce2564b5b (diff)
fix(3p/nix/hash): provide a Status-returning constructor r/1504
Additionally, add IsValidBase16() to restore the behavior of rejecting invalid base16, which absl's HexStringToBytes does not do.

Change-Id: I777a36f5dc787aa54a2aa316d6728f68da129768
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1484
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'third_party/nix/src/tests')
-rw-r--r--third_party/nix/src/tests/hash_test.cc42
1 files changed, 41 insertions, 1 deletions
diff --git a/third_party/nix/src/tests/hash_test.cc b/third_party/nix/src/tests/hash_test.cc
index 2ed4dca3bd44..ea10e7b700b1 100644
--- a/third_party/nix/src/tests/hash_test.cc
+++ b/third_party/nix/src/tests/hash_test.cc
@@ -1,12 +1,16 @@
 #include "libutil/hash.hh"
 
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
 class HashTest : public ::testing::Test {};
 
+using testing::EndsWith;
+using testing::HasSubstr;
+
 namespace nix {
 
-TEST(HASH_TEST, SHA256) {
+TEST(HashTest, SHA256) {
   auto hash = hashString(HashType::htSHA256, "foo");
   ASSERT_EQ(hash.base64Len(), 44);
   ASSERT_EQ(hash.base32Len(), 52);
@@ -40,4 +44,40 @@ TEST(HashTest, SHA256Decode) {
   ASSERT_EQ(hash, *base64);
 }
 
+TEST(HashTest, SHA256DecodeFail) {
+  EXPECT_THAT(
+      Hash::deserialize("sha256:LCa0a2j/xo/5m0U8HTBBNBNCLXBkg7+g+YpeiGJm56==",
+                        HashType::htSHA256)
+          .status()
+          .message(),
+      HasSubstr("wrong length"));
+  EXPECT_THAT(
+      Hash::deserialize("sha256:LCa0a2j/xo/5m0U8HTBBNBNCLXBkg7+g+YpeiGJm56,=",
+                        HashType::htSHA256)
+          .status()
+          .message(),
+      HasSubstr("invalid base-64"));
+
+  EXPECT_THAT(Hash::deserialize(
+                  "sha256:1bp7cri8hplaz6hbz0v4f0nl44rl84q1sg25kgwqzipzd1mv89i",
+                  HashType::htSHA256)
+                  .status()
+                  .message(),
+              HasSubstr("wrong length"));
+  absl::StatusOr<Hash> badB32Char = Hash::deserialize(
+      "sha256:1bp7cri8hplaz6hbz0v4f0nl44rl84q1sg25kgwqzipzd1mv89i,",
+      HashType::htSHA256);
+  EXPECT_THAT(badB32Char.status().message(), HasSubstr("invalid base-32"));
+  EXPECT_THAT(badB32Char.status().message(), EndsWith(","));
+
+  EXPECT_THAT(
+      Hash::deserialize(
+          "sha256:"
+          "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7  ",
+          HashType::htSHA256)
+          .status()
+          .message(),
+      HasSubstr("invalid base-16"));
+}
+
 }  // namespace nix