t: add test functions to translate hash-related values

Add several test functions to make working with various hash-related
values easier.

Add test_oid_init, which loads common hash-related constants and
placeholder object IDs from the newly added files in t/oid-info.
Provide values for these constants for both SHA-1 and SHA-256.

Add test_oid_cache, which accepts data on standard input in the form of
hash-specific key-value pairs that can be looked up later, using the
same format as the files in t/oid-info.  Document this format in a
t/oid-info/README directory so that it's easier to use in the future.

Add test_oid, which is used to specify look up a per-hash value
(produced on standard output) based on the key specified as its
argument.  Usually the data to be looked up will be a hash-related
constant (such as the size of the hash in binary or hexadecimal), a
well-known or placeholder object ID (such as the all-zeros object ID or
one consisting of "deadbeef" repeated), or something similar.  For these
reasons, test_oid will usually be used within a command substitution.
Consequently, redirect the error output to standard error, since
otherwise it will not be displayed.

Add test_detect_hash, which currently only detects SHA-1, and
test_set_hash, which can be used to set a different hash algorithm for
test purposes.  In the future, test_detect_hash will learn to actually
detect the hash depending on how the testsuite is to be run.

Use the local keyword within these functions to avoid overwriting other
shell variables.  We have had a test balloon in place for a couple of
releases to catch shells that don't have this keyword and have not
received any reports of failure.  Note that the varying usages of local
used here are supported by all common open-source shells supporting the
local keyword.

Test these new functions as part of t0000, which also serves to
demonstrate basic usage of them.  In addition, add documentation on how
to format the lookup data and how to use the test functions.

Implement two basic lookup charts, one for common invalid or synthesized
object IDs, and one for various facts about the hash function in use.
Provide versions of the data for both SHA-1 and SHA-256.

Since we use shell variables for storage, names used for lookup can
currently consist only of shell identifier characters.  If this is a
problem in the future, we can hash the names before use.

Improved-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
brian m. carlson
2018-09-13 05:17:31 +00:00
committed by Junio C Hamano
parent 1d4361b0f3
commit 2c02b110da
6 changed files with 184 additions and 0 deletions

View File

@ -1155,3 +1155,72 @@ depacketize () {
}
'
}
# Set the hash algorithm in use to $1. Only useful when testing the testsuite.
test_set_hash () {
test_hash_algo="$1"
}
# Detect the hash algorithm in use.
test_detect_hash () {
# Currently we only support SHA-1, but in the future this function will
# actually detect the algorithm in use.
test_hash_algo='sha1'
}
# Load common hash metadata and common placeholder object IDs for use with
# test_oid.
test_oid_init () {
test -n "$test_hash_algo" || test_detect_hash &&
test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" &&
test_oid_cache <"$TEST_DIRECTORY/oid-info/oid"
}
# Load key-value pairs from stdin suitable for use with test_oid. Blank lines
# and lines starting with "#" are ignored. Keys must be shell identifier
# characters.
#
# Examples:
# rawsz sha1:20
# rawsz sha256:32
test_oid_cache () {
local tag rest k v &&
{ test -n "$test_hash_algo" || test_detect_hash; } &&
while read tag rest
do
case $tag in
\#*)
continue;;
?*)
# non-empty
;;
*)
# blank line
continue;;
esac &&
k="${rest%:*}" &&
v="${rest#*:}" &&
if ! expr "$k" : '[a-z0-9][a-z0-9]*$' >/dev/null
then
error 'bug in the test script: bad hash algorithm'
fi &&
eval "test_oid_${k}_$tag=\"\$v\""
done
}
# Look up a per-hash value based on a key ($1). The value must have been loaded
# by test_oid_init or test_oid_cache.
test_oid () {
local var="test_oid_${test_hash_algo}_$1" &&
# If the variable is unset, we must be missing an entry for this
# key-hash pair, so exit with an error.
if eval "test -z \"\${$var+set}\""
then
error "bug in the test script: undefined key '$1'" >&2
fi &&
eval "printf '%s' \"\${$var}\""
}