Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 75 additions & 50 deletions inline-checksum
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ usage() {
cat <<-EOF
Calculates a checksum for a target and compares it to a provided checksum.

Usage: ${_program_name} [-h|V] | [-a[=| ]<ALGORITHM>] <CHECKSUM> [TARGET_FILE]
Usage: ${_program_name} [-h|V] | [-x] [-a[=| ]<ALGORITHM>] <CHECKSUM> [TARGET_FILE]

Options:
-h, --help Show this help message and exit.
Expand All @@ -74,8 +74,8 @@ usage() {
[TARGET_FILE] The file to compute a checksum for. If absent or '-', reads input from stdin.

Examples:
curl -fsSL <url> | inline-checksum [--algorithm[=| ]<sha256|sha512|md5>] <checksum> | sh -
inline-checksum [--algorithm[=| ]<sha256|sha512|md5>] <checksum> <file>
curl -fsSL <url> | inline-checksum [-x] [--algorithm[=| ]<sha256|sha512|md5>] <checksum> | sh -
inline-checksum [-x] [--algorithm[=| ]<sha256|sha512|md5>] <checksum> <file> [| sh -]
EOF
}

Expand All @@ -102,6 +102,33 @@ main() {
version
shift 1
return 0
elif printf "%s" "${1}" | grep -q -E -- "^-xa" 2>/dev/null; then
output_file_name="1"
case "${1}" in
*=*)
leader="${1%%=*}="
algorithm="${1#"${leader}"}"
;;
-xa*)
algorithm="${1#-xa}"
;;
*)
printf "[%s]: %s: invalid algorithm format -- '%s\n'" "$(date +'%H:%M:%S')" "${_program_name}" "${1}" >&2
return 2
;;
esac
if [ -z "${algorithm}" ]; then
if [ -n "${2}" ]; then
case "${2}" in
-*) ;;
*)
algorithm="${2}"
shift 1
;;
esac
fi
fi
shift 1
elif printf "%s" "${1}" | grep -q -E -- "(-a)|(--a(l(g(o(r(i(t(h(m)?)?)?)?)?)?)?)?)" 2>/dev/null; then
case "${1}" in
*=*)
Expand Down Expand Up @@ -155,46 +182,6 @@ main() {
fi
fi
shift 1
case "${algorithm}" in
sha256)
if command -v sha256sum >/dev/null 2>&1; then
true
elif command -v shasum >/dev/null 2>&1; then
true
elif command -v openssl >/dev/null 2>&1; then
true
else
printf "[%s]: %s: sha256sum or an equivalent must be installed to check sha256 hashes.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 3
fi
;;
sha512)
if command -v sha512sum >/dev/null 2>&1; then
true
elif command -v shasum >/dev/null 2>&1; then
true
elif command -v openssl >/dev/null 2>&1; then
true
else
printf "[%s]: %s: sha512sum or an equivalent must be installed to check sha512 hashes.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 3
fi
;;
md5)
if command -v md5sum >/dev/null 2>&1; then
true
elif command -v openssl >/dev/null 2>&1; then
true
else
printf "[%s]: %s: md5sum or an equivalent must be installed to check md5 hashes.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 3
fi
;;
*)
printf "[%s]: %s: If '--algorithm' is specified, one of 'sha256', 'sha512', or 'md5' must be provided.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 2
;;
esac
elif printf "%s" "${1}" | grep -q -E -- "(-x)|(--o(u(t(p(u(t(-(f(i(l(e(n(a(m(e)?)?)?)?)?)?)?)?)?)?)?)?)?)?)" 2>/dev/null; then
output_file_name="1"
shift 1
Expand All @@ -218,6 +205,50 @@ main() {
fi
provided_hash="${1}"

# -------------------------- #
# Validate Algorithm Backend #
# -------------------------- #
case "${algorithm}" in
sha256)
if command -v sha256sum >/dev/null 2>&1; then
true
elif command -v shasum >/dev/null 2>&1; then
true
elif command -v openssl >/dev/null 2>&1; then
true
else
printf "[%s]: %s: sha256sum or an equivalent must be installed to check sha256 hashes.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 3
fi
;;
sha512)
if command -v sha512sum >/dev/null 2>&1; then
true
elif command -v shasum >/dev/null 2>&1; then
true
elif command -v openssl >/dev/null 2>&1; then
true
else
printf "[%s]: %s: sha512sum or an equivalent must be installed to check sha512 hashes.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 3
fi
;;
md5)
if command -v md5sum >/dev/null 2>&1; then
true
elif command -v openssl >/dev/null 2>&1; then
true
else
printf "[%s]: %s: md5sum or an equivalent must be installed to check md5 hashes.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 3
fi
;;
*)
printf "[%s]: %s: If '--algorithm' is specified, one of 'sha256', 'sha512', or 'md5' must be provided.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 2
;;
esac

# -------------------------- #
# Parse Target File or Stdin #
# -------------------------- #
Expand All @@ -244,13 +275,7 @@ main() {
# ---------------------------------- #
# Calculate Checksum for Target File #
# ---------------------------------- #
if [ -z "${algorithm}" ]; then
algorithm="sha256"
if ! sha256sum --version >/dev/null 2>&1; then
printf "[%s]: %s: sha256sum must be installed to check sha256 hashes.\n" "$(date +'%H:%M:%S')" "${_program_name}" >&2
return 3
fi
fi
algorithm="${algorithm:-sha256}"
case "${algorithm}" in
sha256)
if command -v sha256sum >/dev/null 2>&1; then
Expand Down
111 changes: 111 additions & 0 deletions tests/combined_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env sh
# ------------------------------------- #
# Return Codes #
# ------------------------------------- #
# 0: All Dependencies Present #
# 1: Missing sha256sum #
# 2: Missing sha512sum #
# 3: Missing md5sum #
# 4: Failed on sha256sum Check #
# 5: Failed on sha512sum Check #
# 6: Failed on md5sum Check #
# ------------------------------------- #

# ---------- #
# Test Setup #
# ---------- #

wkdir="$(CDPATH="" cd -- "$(dirname -- "${0}")" && pwd -P)"
parent_dir="$(dirname "${wkdir}")"

license_file="${parent_dir}/LICENSE"
script="${parent_dir}/inline-checksum"

return_sha256=4
return_sha512=5
return_md5=6

timestamp="$(date +'%Y.%m.%d.%H.%M.%S')"
exec 2>> "${wkdir}/${timestamp}.test.log"

if command -v sha256sum > /dev/null 2>&1; then
correct_sha256="$(sha256sum "${license_file}" | cut -f 1 -d " ")"
elif command -v shasum > /dev/null 2>&1; then
correct_sha256="$(shasum -a 256 "${license_file}" | cut -f 1 -d " ")"
elif command -v openssl > /dev/null 2>&1; then
correct_sha256="$(openssl sha512 "${license_file}" | cut -f 2 -d " ")"
else
printf "[%s]: sha256sum or an equivalent must be installed to check sha256 hashes.\n" "$(date +'%H:%M:%S')" >&2
return 1
fi
if command -v sha512sum > /dev/null 2>&1; then
correct_sha512="$(sha512sum "${license_file}" | cut -f 1 -d " ")"
elif command -v shasum > /dev/null 2>&1; then
correct_sha512="$(shasum -a 512 "${license_file}" | cut -f 1 -d " ")"
elif command -v openssl > /dev/null 2>&1; then
correct_sha512="$(openssl sha512 "${license_file}" | cut -f 2 -d " ")"
else
printf "[%s]: sha512sum or an equivalent must be installed to check sha512 hashes.\n" "$(date +'%H:%M:%S')" >&2
return 2
fi
if command -v md5sum > /dev/null 2>&1; then
correct_md5="$(md5sum "${license_file}" | cut -f 1 -d " ")"
elif command -v openssl > /dev/null 2>&1; then
correct_md5="$(openssl md5 "${license_file}" | cut -f 2 -d " ")"
else
printf "[%s]: md5sum or an equivalent must be installed to check md5 hashes.\n" "$(date +'%H:%M:%S')" >&2
return 3
fi

export correct_md5
export correct_sha256
export correct_sha512
export license_file
export parent_dir
export return_md5
export return_sha256
export return_sha512
export script
export timestamp
export wkdir

# ----------- #
# Begin Tests #
# ----------- #

for algorithm in "sha256" "sha512" "md5"; do
correct_hash_pointer=$(printf "correct_%s" "${algorithm}")
eval "correct_hash=\$${correct_hash_pointer}"
return_code_pointer=$(printf "return_%s" "${algorithm}")
eval "return_code=\$${return_code_pointer}"
# shellcheck disable=SC2154 # correct_hash is dynamically assigned
file_name=$("${script}" -xa"${algorithm}" "${correct_hash}" "${license_file}")
case "${file_name}" in
"${license_file}") ;;
*)
printf "[%s]: Failed combined arguments output test while concatenating to set algorithm with algorithm '%s'.\n" "$(date +'%H:%M:%S')" "${algorithm}" >&2
# shellcheck disable=SC2154 # return_code is dynamically assigned
return "${return_code}"
;;
esac

file_name=$("${script}" -xa="${algorithm}" "${correct_hash}" "${license_file}")
case "${file_name}" in
"${license_file}") ;;
*)
printf "[%s]: Failed combined arguments output test while using '=' to set algorithm with algorithm '%s'.\n" "$(date +'%H:%M:%S')" "${algorithm}" >&2
# shellcheck disable=SC2154 # return_code is dynamically assigned
return "${return_code}"
;;
esac

file_name=$("${script}" -xa "${algorithm}" "${correct_hash}" "${license_file}")
case "${file_name}" in
"${license_file}") ;;
*)
printf "[%s]: Failed combined arguments output test while using ' ' to set algorithm with algorithm '%s'.\n" "$(date +'%H:%M:%S')" "${algorithm}" >&2
# shellcheck disable=SC2154 # return_code is dynamically assigned
return "${return_code}"
;;
esac
done