From 8bebf9607cbf07fbf0f98d835f20df1f9736d5ff Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 13 Feb 2024 09:37:45 -0500 Subject: [PATCH] Split `hash.sh` test in two Converting hashes and hashing files are pretty separate tasks, and more test parallelism is better. --- tests/functional/hash-convert.sh | 105 +++++++++++++++++++++ tests/functional/{hash.sh => hash-path.sh} | 104 -------------------- tests/functional/local.mk | 3 +- 3 files changed, 107 insertions(+), 105 deletions(-) create mode 100644 tests/functional/hash-convert.sh rename tests/functional/{hash.sh => hash-path.sh} (51%) diff --git a/tests/functional/hash-convert.sh b/tests/functional/hash-convert.sh new file mode 100644 index 000000000..9b3afc10b --- /dev/null +++ b/tests/functional/hash-convert.sh @@ -0,0 +1,105 @@ +source common.sh + +# Conversion with `nix hash` `nix-hash` and `nix hash convert` +try3() { + # $1 = hash algo + # $2 = expected hash in base16 + # $3 = expected hash in base32 + # $4 = expected hash in base64 + h64=$(nix hash convert --hash-algo "$1" --to base64 "$2") + [ "$h64" = "$4" ] + h64=$(nix-hash --type "$1" --to-base64 "$2") + [ "$h64" = "$4" ] + # Deprecated experiment + h64=$(nix hash to-base64 --type "$1" "$2") + [ "$h64" = "$4" ] + + sri=$(nix hash convert --hash-algo "$1" --to sri "$2") + [ "$sri" = "$1-$4" ] + sri=$(nix-hash --type "$1" --to-sri "$2") + [ "$sri" = "$1-$4" ] + sri=$(nix hash to-sri --type "$1" "$2") + [ "$sri" = "$1-$4" ] + h32=$(nix hash convert --hash-algo "$1" --to base32 "$2") + [ "$h32" = "$3" ] + h32=$(nix-hash --type "$1" --to-base32 "$2") + [ "$h32" = "$3" ] + h32=$(nix hash to-base32 --type "$1" "$2") + [ "$h32" = "$3" ] + h16=$(nix-hash --type "$1" --to-base16 "$h32") + [ "$h16" = "$2" ] + + h16=$(nix hash convert --hash-algo "$1" --to base16 "$h64") + [ "$h16" = "$2" ] + h16=$(nix hash to-base16 --type "$1" "$h64") + [ "$h16" = "$2" ] + h16=$(nix hash convert --to base16 "$sri") + [ "$h16" = "$2" ] + h16=$(nix hash to-base16 "$sri") + [ "$h16" = "$2" ] + + # + # Converting from SRI + # + + # Input hash algo auto-detected from SRI and output defaults to SRI as well. + sri=$(nix hash convert "$1-$4") + [ "$sri" = "$1-$4" ] + + sri=$(nix hash convert --from sri "$1-$4") + [ "$sri" = "$1-$4" ] + + sri=$(nix hash convert --to sri "$1-$4") + [ "$sri" = "$1-$4" ] + + sri=$(nix hash convert --from sri --to sri "$1-$4") + [ "$sri" = "$1-$4" ] + + sri=$(nix hash convert --to base64 "$1-$4") + [ "$sri" = "$4" ] + + # + # Auto-detecting the input from algo and length. + # + + sri=$(nix hash convert --hash-algo "$1" "$2") + [ "$sri" = "$1-$4" ] + sri=$(nix hash convert --hash-algo "$1" "$3") + [ "$sri" = "$1-$4" ] + sri=$(nix hash convert --hash-algo "$1" "$4") + [ "$sri" = "$1-$4" ] + + sri=$(nix hash convert --hash-algo "$1" "$2") + [ "$sri" = "$1-$4" ] + sri=$(nix hash convert --hash-algo "$1" "$3") + [ "$sri" = "$1-$4" ] + sri=$(nix hash convert --hash-algo "$1" "$4") + [ "$sri" = "$1-$4" ] + + # + # Asserting input format succeeds. + # + + sri=$(nix hash convert --hash-algo "$1" --from base16 "$2") + [ "$sri" = "$1-$4" ] + sri=$(nix hash convert --hash-algo "$1" --from nix32 "$3") + [ "$sri" = "$1-$4" ] + sri=$(nix hash convert --hash-algo "$1" --from base64 "$4") + [ "$sri" = "$1-$4" ] + + # + # Asserting input format fails. + # + + fail=$(nix hash convert --hash-algo "$1" --from nix32 "$2" 2>&1 || echo "exit: $?") + [[ "$fail" == *"error: input hash"*"exit: 1" ]] + fail=$(nix hash convert --hash-algo "$1" --from base16 "$3" 2>&1 || echo "exit: $?") + [[ "$fail" == *"error: input hash"*"exit: 1" ]] + fail=$(nix hash convert --hash-algo "$1" --from nix32 "$4" 2>&1 || echo "exit: $?") + [[ "$fail" == *"error: input hash"*"exit: 1" ]] + +} + +try3 sha1 "800d59cfcd3c05e900cb4e214be48f6b886a08df" "vw46m23bizj4n8afrc0fj19wrp7mj3c0" "gA1Zz808BekAy04hS+SPa4hqCN8=" +try3 sha256 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" "1b8m03r63zqhnjf7l5wnldhh7c134ap5vpj0850ymkq1iyzicy5s" "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" +try3 sha512 "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445" "12k9jiq29iyqm03swfsgiw5mlqs173qazm3n7daz43infy12pyrcdf30fkk3qwv4yl2ick8yipc2mqnlh48xsvvxl60lbx8vp38yji0" "IEqPxt2oLwoM7XvrjgikFlfBbvRosiioJ5vjMacDwzWW/RXBOxsH+aodO+pXeJygMa2Fx6cd1wNU7GMSOMo0RQ==" diff --git a/tests/functional/hash.sh b/tests/functional/hash-path.sh similarity index 51% rename from tests/functional/hash.sh rename to tests/functional/hash-path.sh index ff270076e..6d096b29b 100644 --- a/tests/functional/hash.sh +++ b/tests/functional/hash-path.sh @@ -80,107 +80,3 @@ try2 md5 "20f3ffe011d4cfa7d72bfabef7882836" rm $TEST_ROOT/hash-path/hello ln -s x $TEST_ROOT/hash-path/hello try2 md5 "f78b733a68f5edbdf9413899339eaa4a" - -# Conversion with `nix hash` `nix-hash` and `nix hash convert` -try3() { - # $1 = hash algo - # $2 = expected hash in base16 - # $3 = expected hash in base32 - # $4 = expected hash in base64 - h64=$(nix hash convert --hash-algo "$1" --to base64 "$2") - [ "$h64" = "$4" ] - h64=$(nix-hash --type "$1" --to-base64 "$2") - [ "$h64" = "$4" ] - # Deprecated experiment - h64=$(nix hash to-base64 --type "$1" "$2") - [ "$h64" = "$4" ] - - sri=$(nix hash convert --hash-algo "$1" --to sri "$2") - [ "$sri" = "$1-$4" ] - sri=$(nix-hash --type "$1" --to-sri "$2") - [ "$sri" = "$1-$4" ] - sri=$(nix hash to-sri --type "$1" "$2") - [ "$sri" = "$1-$4" ] - h32=$(nix hash convert --hash-algo "$1" --to base32 "$2") - [ "$h32" = "$3" ] - h32=$(nix-hash --type "$1" --to-base32 "$2") - [ "$h32" = "$3" ] - h32=$(nix hash to-base32 --type "$1" "$2") - [ "$h32" = "$3" ] - h16=$(nix-hash --type "$1" --to-base16 "$h32") - [ "$h16" = "$2" ] - - h16=$(nix hash convert --hash-algo "$1" --to base16 "$h64") - [ "$h16" = "$2" ] - h16=$(nix hash to-base16 --type "$1" "$h64") - [ "$h16" = "$2" ] - h16=$(nix hash convert --to base16 "$sri") - [ "$h16" = "$2" ] - h16=$(nix hash to-base16 "$sri") - [ "$h16" = "$2" ] - - # - # Converting from SRI - # - - # Input hash algo auto-detected from SRI and output defaults to SRI as well. - sri=$(nix hash convert "$1-$4") - [ "$sri" = "$1-$4" ] - - sri=$(nix hash convert --from sri "$1-$4") - [ "$sri" = "$1-$4" ] - - sri=$(nix hash convert --to sri "$1-$4") - [ "$sri" = "$1-$4" ] - - sri=$(nix hash convert --from sri --to sri "$1-$4") - [ "$sri" = "$1-$4" ] - - sri=$(nix hash convert --to base64 "$1-$4") - [ "$sri" = "$4" ] - - # - # Auto-detecting the input from algo and length. - # - - sri=$(nix hash convert --hash-algo "$1" "$2") - [ "$sri" = "$1-$4" ] - sri=$(nix hash convert --hash-algo "$1" "$3") - [ "$sri" = "$1-$4" ] - sri=$(nix hash convert --hash-algo "$1" "$4") - [ "$sri" = "$1-$4" ] - - sri=$(nix hash convert --hash-algo "$1" "$2") - [ "$sri" = "$1-$4" ] - sri=$(nix hash convert --hash-algo "$1" "$3") - [ "$sri" = "$1-$4" ] - sri=$(nix hash convert --hash-algo "$1" "$4") - [ "$sri" = "$1-$4" ] - - # - # Asserting input format succeeds. - # - - sri=$(nix hash convert --hash-algo "$1" --from base16 "$2") - [ "$sri" = "$1-$4" ] - sri=$(nix hash convert --hash-algo "$1" --from nix32 "$3") - [ "$sri" = "$1-$4" ] - sri=$(nix hash convert --hash-algo "$1" --from base64 "$4") - [ "$sri" = "$1-$4" ] - - # - # Asserting input format fails. - # - - fail=$(nix hash convert --hash-algo "$1" --from nix32 "$2" 2>&1 || echo "exit: $?") - [[ "$fail" == *"error: input hash"*"exit: 1" ]] - fail=$(nix hash convert --hash-algo "$1" --from base16 "$3" 2>&1 || echo "exit: $?") - [[ "$fail" == *"error: input hash"*"exit: 1" ]] - fail=$(nix hash convert --hash-algo "$1" --from nix32 "$4" 2>&1 || echo "exit: $?") - [[ "$fail" == *"error: input hash"*"exit: 1" ]] - -} - -try3 sha1 "800d59cfcd3c05e900cb4e214be48f6b886a08df" "vw46m23bizj4n8afrc0fj19wrp7mj3c0" "gA1Zz808BekAy04hS+SPa4hqCN8=" -try3 sha256 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" "1b8m03r63zqhnjf7l5wnldhh7c134ap5vpj0850ymkq1iyzicy5s" "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" -try3 sha512 "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445" "12k9jiq29iyqm03swfsgiw5mlqs173qazm3n7daz43infy12pyrcdf30fkk3qwv4yl2ick8yipc2mqnlh48xsvvxl60lbx8vp38yji0" "IEqPxt2oLwoM7XvrjgikFlfBbvRosiioJ5vjMacDwzWW/RXBOxsH+aodO+pXeJygMa2Fx6cd1wNU7GMSOMo0RQ==" diff --git a/tests/functional/local.mk b/tests/functional/local.mk index f369c7c2c..18eb887cd 100644 --- a/tests/functional/local.mk +++ b/tests/functional/local.mk @@ -47,7 +47,8 @@ nix_tests = \ optimise-store.sh \ substitute-with-invalid-ca.sh \ signing.sh \ - hash.sh \ + hash-convert.sh \ + hash-path.sh \ gc-non-blocking.sh \ check.sh \ nix-shell.sh \