summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nix/libutil/hash.cc7
-rw-r--r--tests/derivations.scm12
2 files changed, 17 insertions, 2 deletions
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc
index 9ba604eb85d..9b83ffcdd9b 100644
--- a/nix/libutil/hash.cc
+++ b/nix/libutil/hash.cc
@@ -76,8 +76,11 @@ string printHash(const Hash & hash)
76Hash parseHash(HashType ht, const string & s) 76Hash parseHash(HashType ht, const string & s)
77{ 77{
78 Hash hash(ht); 78 Hash hash(ht);
79 if (s.length() != hash.hashSize * 2) 79 if (s.length() != hash.hashSize * 2) {
80 throw Error(format("invalid hash `%1%'") % s); 80 string algo = gcry_md_algo_name(ht);
81 throw Error(format("invalid %1% hash '%2%' (%3% bytes but expected %4%)")
82 % algo % s % (s.length() / 2) % hash.hashSize);
83 }
81 for (unsigned int i = 0; i < hash.hashSize; i++) { 84 for (unsigned int i = 0; i < hash.hashSize; i++) {
82 string s2(s, i * 2, 2); 85 string s2(s, i * 2, 2);
83 if (!isxdigit(s2[0]) || !isxdigit(s2[1])) 86 if (!isxdigit(s2[0]) || !isxdigit(s2[1]))
diff --git a/tests/derivations.scm b/tests/derivations.scm
index 3d25365b14c..66c777cfe7e 100644
--- a/tests/derivations.scm
+++ b/tests/derivations.scm
@@ -396,6 +396,18 @@
396 (call-with-input-file p get-bytevector-all)) 396 (call-with-input-file p get-bytevector-all))
397 (bytevector? (query-path-hash %store p))))))) 397 (bytevector? (query-path-hash %store p)))))))
398 398
399(test-assert "fixed-output derivation, invalid hash size"
400 (guard (c ((store-protocol-error? c)
401 (string-contains-ci (store-protocol-error-message c)
402 "invalid SHA512 hash")))
403 (derivation %store "download-with-invalid-hash"
404 "builtin:download" '()
405 #:env-vars `(("url"
406 . ,(object->string "http://example.org")))
407 #:hash-algo 'sha512
408 #:hash #vu8(1 2 3))
409 #f))
410
399(test-assert "derivation with a fixed-output input" 411(test-assert "derivation with a fixed-output input"
400 ;; A derivation D using a fixed-output derivation F doesn't has the same 412 ;; A derivation D using a fixed-output derivation F doesn't has the same
401 ;; output path when passed F or F', as long as F and F' have the same output 413 ;; output path when passed F or F', as long as F and F' have the same output