Merge pull request #887 from jasonkarns/checksum-refactor

Checksum refactor
This commit is contained in:
Mislav Marohnić 2016-01-20 19:08:57 +11:00
commit 8ea265d2fa
4 changed files with 42 additions and 35 deletions

View file

@ -246,24 +246,39 @@ compute_md5() {
fi
}
has_checksum_support() {
local checksum_command="$1"
local has_checksum_var="HAS_CHECKSUM_SUPPORT_${checksum_command}"
if [ -z "${!has_checksum_var+defined}" ]; then
printf -v "$has_checksum_var" "$(echo test | "$checksum_command" >/dev/null; echo $?)"
fi
return "${!has_checksum_var}"
}
verify_checksum() {
# If there's no SHA2 support, return success
[ -n "$HAS_SHA2_SUPPORT" ] || return 0
local checksum_command="compute_sha2"
local checksum_command
local filename="$1"
local expected_checksum="$(echo "$2" | tr [A-Z] [a-z])"
# If the specified filename doesn't exist, return success
local filename="$1"
[ -e "$filename" ] || return 0
# If there's no expected checksum, return success
local expected_checksum=`echo "$2" | tr [A-Z] [a-z]`
[ -n "$expected_checksum" ] || return 0
case "${#expected_checksum}" in
0) return 0 ;; # empty checksum; return success
32) checksum_command="compute_md5" ;;
64) checksum_command="compute_sha2" ;;
*)
{ echo
echo "unexpected checksum length: ${#expected_checksum} (${expected_checksum})"
echo "expected 0 (no checksum), 32 (MD5), or 64 (SHA2-256)"
echo
} >&4
return 1 ;;
esac
# If the checksum length is 32 chars, assume MD5, otherwise SHA2
if [ "${#expected_checksum}" -eq 32 ]; then
[ -n "$HAS_MD5_SUPPORT" ] || return 0
checksum_command="compute_md5"
fi
# If chosen provided checksum algorithm isn't supported, return success
has_checksum_support "$checksum_command" || return 0
# If the computed checksum is empty, return failure
local computed_checksum=`echo "$($checksum_command < "$filename")" | tr [A-Z] [a-z]`
@ -1219,23 +1234,10 @@ else
RUBY_BUILD_DEFAULT_MIRROR=
fi
if [ -n "$RUBY_BUILD_SKIP_MIRROR" ]; then
if [ -n "$RUBY_BUILD_SKIP_MIRROR" ] || ! has_checksum_support compute_sha2; then
unset RUBY_BUILD_MIRROR_URL
fi
if echo test | compute_sha2 >/dev/null; then
HAS_SHA2_SUPPORT=1
else
unset HAS_SHA2_SUPPORT
unset RUBY_BUILD_MIRROR_URL
fi
if echo test | compute_md5 >/dev/null; then
HAS_MD5_SUPPORT=1
else
unset HAS_MD5_SUPPORT
fi
SEED="$(date "+%Y%m%d%H%M%S").$$"
LOG_PATH="${TMP}/ruby-build.${SEED}.log"
RUBY_BIN="${PREFIX_PATH}/bin/ruby"

View file

@ -10,7 +10,6 @@ setup() {
@test "packages are saved to download cache" {
stub shasum true
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
install_fixture definitions/without-checksum
@ -19,12 +18,10 @@ setup() {
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
unstub curl
unstub shasum
}
@test "cached package without checksum" {
stub shasum true
stub curl
cp "${FIXTURE_ROOT}/package-1.0.0.tar.gz" "$RUBY_BUILD_CACHE_PATH"
@ -35,7 +32,6 @@ setup() {
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
unstub curl
unstub shasum
}
@ -79,7 +75,6 @@ setup() {
@test "nonexistent cache directory is ignored" {
stub shasum true
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
export RUBY_BUILD_CACHE_PATH="${TMP}/nonexistent"
@ -91,5 +86,4 @@ setup() {
[ ! -d "$RUBY_BUILD_CACHE_PATH" ]
unstub curl
unstub shasum
}

View file

@ -6,7 +6,6 @@ export RUBY_BUILD_CACHE_PATH=
@test "package URL without checksum" {
stub shasum true
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
install_fixture definitions/without-checksum
@ -14,7 +13,6 @@ export RUBY_BUILD_CACHE_PATH=
[ -x "${INSTALL_ROOT}/bin/package" ]
unstub curl
unstub shasum
}
@ -143,3 +141,16 @@ DEF
unstub shasum
}
@test "package URL with checksum of unexpected length" {
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
run_inline_definition <<DEF
install_package "package-1.0.0" "http://example.com/packages/package-1.0.0.tar.gz#checksum_of_unexpected_length" copy
DEF
assert_failure
[ ! -f "${INSTALL_ROOT}/bin/package" ]
assert_output_contains "unexpected checksum length: 29 (checksum_of_unexpected_length)"
assert_output_contains "expected 0 (no checksum), 32 (MD5), or 64 (SHA2-256)"
}

View file

@ -1 +1 @@
install_package "package-1.0.0" "http://example.com/packages/package-1.0.0.tar.gz#invalid" copy
install_package "package-1.0.0" "http://example.com/packages/package-1.0.0.tar.gz#invalid_64_character_checksum_0000000000000000000000000000000000" copy