Skip to content

Commit

Permalink
lib: Add tests for generating SHA hashes
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <saugustus@vmware.com>
  • Loading branch information
justaugustus committed Aug 20, 2019
1 parent 1c78f0c commit 7c6e425
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 5 deletions.
53 changes: 52 additions & 1 deletion lib/common_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
# common.sh unit tests
#
# shellcheck source=./lib/testing.sh
# shellcheck disable=SC1091
source "$(dirname "$(readlink -ne "${BASH_SOURCE[0]}")")/testing.sh"

# shellcheck source=./lib/common.sh
# shellcheck disable=SC1091
source "$(dirname "$(readlink -ne "${BASH_SOURCE[0]}")")/common.sh"
readonly TESTDATA="$( cd "$(dirname "$0")" && pwd )/testdata"

Expand All @@ -28,7 +30,7 @@ readonly TESTDATA="$( cd "$(dirname "$0")" && pwd )/testdata"
# - add `set -o nounset`
#
# We can do that when all the things we source do not rely on unset
# varaibales and ignoring errors. This will require quite some
# variables and ignoring errors. This will require quite some
# refactoring, so this is the best we can do for now.
set -o errexit
set -o pipefail
Expand All @@ -42,4 +44,53 @@ TEST_run_stateful() {
"passing command and arguments"
}

TEST_generate_sha() {
test_scaffold

local expected_dir
local testfilename1
local testfilename2
local testfile1
local testfile2

expected_dir="$TESTDATA/common/shas"

testfilename1="shafile1.txt"
testfilename2="shafile2.txt"

# shellcheck disable=SC2154
testfile1="${tmp_dir}/${testfilename1}"
testfile2="${tmp_dir}/${testfilename2}"

# shellcheck disable=SC2154
echo -n "This is the first file to test the generation of SHA hashes." > "$testfile1"

# shellcheck disable=SC2154
echo -n "This is the second file to test the generation of SHA hashes." > "$testfile2"

find "$tmp_dir" -type f | while read -r path; do
for bits in "256" "512"; do
sum="$(shasum -a"$bits" "${path}" | sed 's/ .*\// /')" || return 1
echo "$sum" > "${path}.sha${bits}"
echo "$sum" >> "${tmp_dir}/SHA${bits}SUMS"
done
done

for sha_file in "$testfile1" "$testfile2"; do
for bits in "256" "512"; do
assert_equal_content \
<( common::sha "${sha_file}" "${bits}" "full") \
"${expected_dir}/$(basename "${sha_file}").sha${bits}" \
"Validated sha${bits} hash for ${sha_file}"
done
done

for bits in "256" "512"; do
assert_equal_content \
"$tmp_dir/SHA${bits}SUMS" \
"$expected_dir/SHA${bits}SUMS" \
"Validated SHA${bits}SUMS"
done
}

test_main "$@"
1 change: 1 addition & 0 deletions lib/testdata/common/shafile1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the first file to test the generation of SHA hashes.
1 change: 1 addition & 0 deletions lib/testdata/common/shafile2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the second file to test the generation of SHA hashes.
2 changes: 2 additions & 0 deletions lib/testdata/common/shas/SHA256SUMS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
25073faf6553b2ff6c6cfc66c3b52f6bbf7d0f083fd2d171174f00ae70cd7eb7 shafile1.txt
fd3d841e433b61936dc4ed38be28a577cadee21c4181ca2364b43416bb971f04 shafile2.txt
2 changes: 2 additions & 0 deletions lib/testdata/common/shas/SHA512SUMS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
e414bed12a1fb345620d96f4850652ea4e0fc9e9630451ed8bbd7cf8ae78bb0d78a53d36c847ea4e7bf035d2c553717f1456447d0c088b4ba56d9992c8840a52 shafile1.txt
3bd0e80f05efc814afc76c33fbc3e569de780de40e7f74ca7b67ffcbb58bee595e0e1b5047196a488a101ffb6f77cbee5afa029373d59285e5f5ffb1ff4b2477 shafile2.txt
1 change: 1 addition & 0 deletions lib/testdata/common/shas/shafile1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the first file to test the generation of SHA hashes.
1 change: 1 addition & 0 deletions lib/testdata/common/shas/shafile1.txt.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
25073faf6553b2ff6c6cfc66c3b52f6bbf7d0f083fd2d171174f00ae70cd7eb7 shafile1.txt
1 change: 1 addition & 0 deletions lib/testdata/common/shas/shafile1.txt.sha512
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e414bed12a1fb345620d96f4850652ea4e0fc9e9630451ed8bbd7cf8ae78bb0d78a53d36c847ea4e7bf035d2c553717f1456447d0c088b4ba56d9992c8840a52 shafile1.txt
1 change: 1 addition & 0 deletions lib/testdata/common/shas/shafile2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the second file to test the generation of SHA hashes.
1 change: 1 addition & 0 deletions lib/testdata/common/shas/shafile2.txt.sha256
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fd3d841e433b61936dc4ed38be28a577cadee21c4181ca2364b43416bb971f04 shafile2.txt
1 change: 1 addition & 0 deletions lib/testdata/common/shas/shafile2.txt.sha512
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3bd0e80f05efc814afc76c33fbc3e569de780de40e7f74ca7b67ffcbb58bee595e0e1b5047196a488a101ffb6f77cbee5afa029373d59285e5f5ffb1ff4b2477 shafile2.txt
8 changes: 4 additions & 4 deletions lib/testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ test_main() {

test_scaffold() {
func_name="${FUNCNAME[1]}"
tmpDir="$(mktemp -d "${func_name}.XXXXXX")"
trap 'rm -rf -- "$tmpDir"' EXIT
tmp_dir="$(mktemp -d "${func_name}.XXXXXX")"
trap 'rm -rf -- "$tmp_dir"' EXIT

# override some vars and func to not clutter output
common::timestamp() { :; }
# shellcheck disable=SC2034
PROGSTATE="${tmpDir}/${func_name}-state.txt" \
LOGFILE="${tmpDir}/${func_name}.log" \
PROGSTATE="${tmp_dir}/${func_name}-state.txt" \
LOGFILE="${tmp_dir}/${func_name}.log" \
HR='' \
TPUT[BOLD]='' \
TPUT[OFF]=''
Expand Down

0 comments on commit 7c6e425

Please sign in to comment.