Skip to content

Commit 2c02b11

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1d4361b commit 2c02b11

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

t/README

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,28 @@ library for your script to use.
806806
the symbolic link in the file system and a part that does; then only
807807
the latter part need be protected by a SYMLINKS prerequisite (see below).
808808

809+
- test_oid_init
810+
811+
This function loads facts and useful object IDs related to the hash
812+
algorithm(s) in use from the files in t/oid-info.
813+
814+
- test_oid_cache
815+
816+
This function reads per-hash algorithm information from standard
817+
input (usually a heredoc) in the format described in
818+
t/oid-info/README. This is useful for test-specific values, such as
819+
object IDs, which must vary based on the hash algorithm.
820+
821+
Certain fixed values, such as hash sizes and common placeholder
822+
object IDs, can be loaded with test_oid_init (described above).
823+
824+
- test_oid <key>
825+
826+
This function looks up a value for the hash algorithm in use, based
827+
on the key given. The value must have been loaded using
828+
test_oid_init or test_oid_cache. Providing an unknown key is an
829+
error.
830+
809831
Prerequisites
810832
-------------
811833

t/oid-info/README

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
This directory contains various per-hash values that are used in the testsuite.
2+
3+
Each file contains lines containing a key-value pair; blank lines and lines
4+
starting with `#` are ignored. The key and value are separated by whitespace
5+
(specifically, those whitespace in the default `$IFS`). The key consists only
6+
of shell identifier characters, and the value consists of a hash algorithm,
7+
colon, and value. The hash algorithm also consists only of shell identifier
8+
characters; it should match the value in sha1-file.c.
9+
10+
For example, the following lines map the key "rawsz" to "20" if SHA-1 is in use
11+
and to "32" if SHA-256 is in use:
12+
13+
----
14+
rawsz sha1:20
15+
rawsz sha256:32
16+
----
17+
18+
The keys and values used here are loaded by `test_oid_init` (see the README file
19+
in the "t" directory) and are used by calling `test_oid`.

t/oid-info/hash-info

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
rawsz sha1:20
2+
rawsz sha256:32
3+
4+
hexsz sha1:40
5+
hexsz sha256:64
6+
7+
zero sha1:0000000000000000000000000000000000000000
8+
zero sha256:0000000000000000000000000000000000000000000000000000000000000000

t/oid-info/oid

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# These are some common invalid and partial object IDs used in tests.
2+
001 sha1:0000000000000000000000000000000000000001
3+
001 sha256:0000000000000000000000000000000000000000000000000000000000000001
4+
002 sha1:0000000000000000000000000000000000000002
5+
002 sha256:0000000000000000000000000000000000000000000000000000000000000002
6+
003 sha1:0000000000000000000000000000000000000003
7+
003 sha256:0000000000000000000000000000000000000000000000000000000000000003
8+
004 sha1:0000000000000000000000000000000000000004
9+
004 sha256:0000000000000000000000000000000000000000000000000000000000000004
10+
005 sha1:0000000000000000000000000000000000000005
11+
005 sha256:0000000000000000000000000000000000000000000000000000000000000005
12+
006 sha1:0000000000000000000000000000000000000006
13+
006 sha256:0000000000000000000000000000000000000000000000000000000000000006
14+
007 sha1:0000000000000000000000000000000000000007
15+
007 sha256:0000000000000000000000000000000000000000000000000000000000000007
16+
# All zeros or Fs missing one or two hex segments.
17+
zero_1 sha1:000000000000000000000000000000000000000
18+
zero_1 sha256:000000000000000000000000000000000000000000000000000000000000000
19+
zero_2 sha1:00000000000000000000000000000000000000
20+
zero_2 sha256:00000000000000000000000000000000000000000000000000000000000000
21+
ff_1 sha1:fffffffffffffffffffffffffffffffffffffff
22+
ff_1 sha256:fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
23+
ff_2 sha1:ffffffffffffffffffffffffffffffffffffff
24+
ff_2 sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
25+
# More various invalid OIDs.
26+
numeric sha1:0123456789012345678901234567890123456789
27+
numeric sha256:0123456789012345678901234567890123456789012345678901234567890123
28+
deadbeef sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
29+
deadbeef sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef

t/t0000-basic.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,43 @@ test_expect_success 'tests clean up even on failures' "
821821
EOF
822822
"
823823

824+
test_expect_success 'test_oid setup' '
825+
test_oid_init
826+
'
827+
828+
test_expect_success 'test_oid provides sane info by default' '
829+
test_oid zero >actual &&
830+
grep "^00*\$" actual &&
831+
rawsz="$(test_oid rawsz)" &&
832+
hexsz="$(test_oid hexsz)" &&
833+
test "$hexsz" -eq $(wc -c <actual) &&
834+
test $(( $rawsz * 2)) -eq "$hexsz"
835+
'
836+
837+
test_expect_success 'test_oid can look up data for SHA-1' '
838+
test_when_finished "test_detect_hash" &&
839+
test_set_hash sha1 &&
840+
test_oid zero >actual &&
841+
grep "^00*\$" actual &&
842+
rawsz="$(test_oid rawsz)" &&
843+
hexsz="$(test_oid hexsz)" &&
844+
test $(wc -c <actual) -eq 40 &&
845+
test "$rawsz" -eq 20 &&
846+
test "$hexsz" -eq 40
847+
'
848+
849+
test_expect_success 'test_oid can look up data for SHA-256' '
850+
test_when_finished "test_detect_hash" &&
851+
test_set_hash sha256 &&
852+
test_oid zero >actual &&
853+
grep "^00*\$" actual &&
854+
rawsz="$(test_oid rawsz)" &&
855+
hexsz="$(test_oid hexsz)" &&
856+
test $(wc -c <actual) -eq 64 &&
857+
test "$rawsz" -eq 32 &&
858+
test "$hexsz" -eq 64
859+
'
860+
824861
################################################################
825862
# Basics of the basics
826863

t/test-lib-functions.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,3 +1155,72 @@ depacketize () {
11551155
}
11561156
'
11571157
}
1158+
1159+
# Set the hash algorithm in use to $1. Only useful when testing the testsuite.
1160+
test_set_hash () {
1161+
test_hash_algo="$1"
1162+
}
1163+
1164+
# Detect the hash algorithm in use.
1165+
test_detect_hash () {
1166+
# Currently we only support SHA-1, but in the future this function will
1167+
# actually detect the algorithm in use.
1168+
test_hash_algo='sha1'
1169+
}
1170+
1171+
# Load common hash metadata and common placeholder object IDs for use with
1172+
# test_oid.
1173+
test_oid_init () {
1174+
test -n "$test_hash_algo" || test_detect_hash &&
1175+
test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" &&
1176+
test_oid_cache <"$TEST_DIRECTORY/oid-info/oid"
1177+
}
1178+
1179+
# Load key-value pairs from stdin suitable for use with test_oid. Blank lines
1180+
# and lines starting with "#" are ignored. Keys must be shell identifier
1181+
# characters.
1182+
#
1183+
# Examples:
1184+
# rawsz sha1:20
1185+
# rawsz sha256:32
1186+
test_oid_cache () {
1187+
local tag rest k v &&
1188+
1189+
{ test -n "$test_hash_algo" || test_detect_hash; } &&
1190+
while read tag rest
1191+
do
1192+
case $tag in
1193+
\#*)
1194+
continue;;
1195+
?*)
1196+
# non-empty
1197+
;;
1198+
*)
1199+
# blank line
1200+
continue;;
1201+
esac &&
1202+
1203+
k="${rest%:*}" &&
1204+
v="${rest#*:}" &&
1205+
1206+
if ! expr "$k" : '[a-z0-9][a-z0-9]*$' >/dev/null
1207+
then
1208+
error 'bug in the test script: bad hash algorithm'
1209+
fi &&
1210+
eval "test_oid_${k}_$tag=\"\$v\""
1211+
done
1212+
}
1213+
1214+
# Look up a per-hash value based on a key ($1). The value must have been loaded
1215+
# by test_oid_init or test_oid_cache.
1216+
test_oid () {
1217+
local var="test_oid_${test_hash_algo}_$1" &&
1218+
1219+
# If the variable is unset, we must be missing an entry for this
1220+
# key-hash pair, so exit with an error.
1221+
if eval "test -z \"\${$var+set}\""
1222+
then
1223+
error "bug in the test script: undefined key '$1'" >&2
1224+
fi &&
1225+
eval "printf '%s' \"\${$var}\""
1226+
}

0 commit comments

Comments
 (0)