Add a test function helper, test_oid, that produces output that varies
depending on the hash in use.  Add an additional helper, test_oid_cache,
that can be used to load data for test_oid, either through a list of
filenames or on standard input.  Check that these functions work in
t0000, as the rest of the testsuite will soon come to depend on them.

Implement two basic lookup charts, one for common invalid or synthesized
object IDs, and one for various facts about the hash function in use.
Individual tests can use their own translation files to map object IDs
or other data that needs to vary across hash functions.  Provide
versions for both SHA-1 and SHA-256.

Note that due to the implementation used, keys cannot be anything but
shell identifier characters.

Signed-off-by: Eric Sunshine <[email protected]>
Signed-off-by: brian m. carlson <[email protected]>
---
 t/oid-info/hash-info    |  8 ++++++++
 t/oid-info/oid          | 29 +++++++++++++++++++++++++++++
 t/t0000-basic.sh        | 29 +++++++++++++++++++++++++++++
 t/test-lib-functions.sh | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 102 insertions(+)
 create mode 100644 t/oid-info/hash-info
 create mode 100644 t/oid-info/oid

diff --git a/t/oid-info/hash-info b/t/oid-info/hash-info
new file mode 100644
index 0000000000..ccdbfdf974
--- /dev/null
+++ b/t/oid-info/hash-info
@@ -0,0 +1,8 @@
+rawsz sha1:20
+rawsz sha256:32
+
+hexsz sha1:40
+hexsz sha256:64
+
+zero sha1:0000000000000000000000000000000000000000
+zero sha256:0000000000000000000000000000000000000000000000000000000000000000
diff --git a/t/oid-info/oid b/t/oid-info/oid
new file mode 100644
index 0000000000..6f1c49dbc1
--- /dev/null
+++ b/t/oid-info/oid
@@ -0,0 +1,29 @@
+# These are some common invalid and partial object IDs used in tests.
+001    sha1:0000000000000000000000000000000000000001
+001    sha256:0000000000000000000000000000000000000000000000000000000000000001
+002    sha1:0000000000000000000000000000000000000002
+002    sha256:0000000000000000000000000000000000000000000000000000000000000002
+003    sha1:0000000000000000000000000000000000000003
+003    sha256:0000000000000000000000000000000000000000000000000000000000000003
+004    sha1:0000000000000000000000000000000000000004
+004    sha256:0000000000000000000000000000000000000000000000000000000000000004
+005    sha1:0000000000000000000000000000000000000005
+005    sha256:0000000000000000000000000000000000000000000000000000000000000005
+006    sha1:0000000000000000000000000000000000000006
+006    sha256:0000000000000000000000000000000000000000000000000000000000000006
+007    sha1:0000000000000000000000000000000000000007
+007    sha256:0000000000000000000000000000000000000000000000000000000000000007
+# All zeros or Fs missing one or two hex segments.
+zero_1         sha1:000000000000000000000000000000000000000
+zero_2         
sha256:000000000000000000000000000000000000000000000000000000000000000
+zero_2         sha1:00000000000000000000000000000000000000
+zero_2         
sha256:00000000000000000000000000000000000000000000000000000000000000
+ff_1           sha1:fffffffffffffffffffffffffffffffffffffff
+ff_1           
sha256:fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+ff_2           sha1:ffffffffffffffffffffffffffffffffffffff
+ff_2           
sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+# More various invalid OIDs.
+numeric                sha1:0123456789012345678901234567890123456789
+numeric                
sha256:0123456789012345678901234567890123456789012345678901234567890123
+deadbeef       sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
+deadbeef       
sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index 34859fe4a5..ace779bf7d 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -821,6 +821,35 @@ test_expect_success 'tests clean up even on failures' "
        EOF
 "
 
+test_oid_cache hash-info oid
+
+test_expect_success 'test_oid_info provides sane info by default' '
+       test_oid zero &&
+       test_oid zero >actual &&
+       grep "00*" actual &&
+       test "$(test_oid hexsz)" -eq $(wc -c <actual) &&
+       test $(( $(test_oid rawsz) * 2)) -eq "$(test_oid hexsz)"
+'
+
+test_expect_success 'test_oid_info can look up data for SHA-1' '
+       test_detect_hash sha1 &&
+       test_oid zero >actual &&
+       grep "00*" actual &&
+       test $(wc -c <actual) -eq 40 &&
+       test "$(test_oid rawsz)" -eq 20 &&
+       test "$(test_oid hexsz)" -eq 40
+'
+
+test_expect_success 'test_oid_info can look up data for SHA-256' '
+       test_when_finished "test_detect_hash" &&
+       test_detect_hash sha256 &&
+       test_oid zero >actual &&
+       grep "00*" actual &&
+       test $(wc -c <actual) -eq 64 &&
+       test "$(test_oid rawsz)" -eq 32 &&
+       test "$(test_oid hexsz)" -eq 64
+'
+
 ################################################################
 # Basics of the basics
 
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 2b2181dca0..ac18789a70 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1147,3 +1147,39 @@ depacketize () {
                }
        '
 }
+
+test_detect_hash () {
+       if test -n "$1"
+       then
+               test_hash_algo="$1"
+       else
+               test_hash_algo='sha1'
+       fi
+}
+
+test_oid_cache () {
+       test -n "$test_hash_algo" || test_detect_hash
+       if test -n "$1"
+       then
+               while test -n "$1"
+               do
+                       test_oid_cache <"$TEST_DIRECTORY/oid-info/$1"
+                       shift
+               done
+               return $?
+       fi
+       while read _tag _rest
+       do
+               case $_tag in \#*)
+                       continue;;
+               esac
+
+               _k="${_rest%:*}"
+               _v="${_rest#*:}"
+               eval "test_oid_${_k}_$_tag=\"$_v\""
+       done
+}
+
+test_oid () {
+       eval "printf '%s' \"\${test_oid_${test_hash_algo}_$1}\""
+}

Reply via email to