git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 01/11] t: add tool to translate hash-related values

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Aug 29, 2018, 00:56 UTC
Message-ID
<20180829005642.980617-2-sandals@crustytoothpaste.net>
In-Reply-To
<20180829005642.980617-1-sandals@crustytoothpaste.net>

Add a test function helper, test_oid, that produces output that varies depending on the hash in use. Add two additional helpers, test_oid_cache, which can be used to load data for test_oid from standard input, and test_oid_init, which can be used to load certain fixed values from lookup charts. 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. Provide versions for both SHA-1 and SHA-256.

Note that due to the implementation used, 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.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 t/oid-info/hash-info    |  8 ++++++++
 t/oid-info/oid          | 29 +++++++++++++++++++++++++++
 t/t0000-basic.sh        | 35 ++++++++++++++++++++++++++++++++
 t/test-lib-functions.sh | 44 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 116 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..a754970523
--- /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_1		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 850f651e4e..e3cace299e 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -821,6 +821,41 @@ test_expect_success 'tests clean up even on failures' "
 	EOF
 "
 
+test_oid_init
+
+test_expect_success 'test_oid provides sane info by default' '
+	test_oid zero >actual &&
+	grep "^00*$" actual &&
+	rawsz="$(test_oid rawsz)" &&
+	hexsz="$(test_oid hexsz)" &&
+	test "$hexsz" -eq $(wc -c <actual) &&
+	test $(( $rawsz * 2)) -eq "$hexsz"
+'
+
+test_expect_success 'test_oid can look up data for SHA-1' '
+	test_when_finished "test_detect_hash" &&
+	test_set_hash sha1 &&
+	test_oid zero >actual &&
+	grep "^00*$" actual &&
+	rawsz="$(test_oid rawsz)" &&
+	hexsz="$(test_oid hexsz)" &&
+	test $(wc -c <actual) -eq 40 &&
+	test "$rawsz" -eq 20 &&
+	test "$hexsz" -eq 40
+'
+
+test_expect_success 'test_oid can look up data for SHA-256' '
+	test_when_finished "test_detect_hash" &&
+	test_set_hash sha256 &&
+	test_oid zero >actual &&
+	grep "^00*$" actual &&
+	rawsz="$(test_oid rawsz)" &&
+	hexsz="$(test_oid hexsz)" &&
+	test $(wc -c <actual) -eq 64 &&
+	test "$rawsz" -eq 32 &&
+	test "$hexsz" -eq 64
+'
+
 ################################################################
 # Basics of the basics
 
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 4207af4077..2300ec49dd 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1155,3 +1155,47 @@ depacketize () {
 		}
 	'
 }
+
+test_set_hash () {
+	test_hash_algo="$1"
+}
+
+test_detect_hash () {
+	test_hash_algo='sha1'
+}
+
+test_oid_init () {
+	test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" &&
+	test_oid_cache <"$TEST_DIRECTORY/oid-info/oid"
+}
+
+test_oid_cache () {
+	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#*:}" &&
+		{ echo "$_k" | egrep '^[a-z0-9]+$' >/dev/null ||
+			error 'bug in the test script: bad hash algorithm'; } &&
+		eval "test_oid_${_k}_$_tag=\"\$_v\"" || return 1
+	done
+}
+
+test_oid () {
+	eval "
+		test -n \"\${test_oid_${test_hash_algo}_$1+set}\" &&
+		printf '%s' \"\${test_oid_${test_hash_algo}_$1}\"
+	"
+}
Previous: brian m. carlsonNext: Jonathan Nieder
Message 2 of 19 in “Hash-independent tests (part 3)”
  1. 00/11 Hash-independent tests (part 3)brian m. carlson, Aug 29, 2018
  2. 01/11 t: add tool to translate hash-related valuesbrian m. carlson, Aug 29, 2018
  3. Jonathan NiederAug 29, 2018
  4. brian m. carlsonAug 29, 2018
  5. Derrick StoleeAug 29, 2018
  6. brian m. carlsonAug 29, 2018
  7. 02/11 t0000: use hash translation tablebrian m. carlson, Aug 29, 2018
  8. 03/11 t0000: update tests for SHA-256brian m. carlson, Aug 29, 2018
  9. 05/11 t0027: make hash size independentbrian m. carlson, Aug 29, 2018
  10. Torsten BögershausenAug 31, 2018
  11. Eric SunshineAug 31, 2018
  12. brian m. carlsonSep 1, 2018
  13. 04/11 t0002: abstract away SHA-1 specific constantsbrian m. carlson, Aug 29, 2018
  14. 07/11 t1006: make hash size independentbrian m. carlson, Aug 29, 2018
  15. 06/11 t0064: make hash size independentbrian m. carlson, Aug 29, 2018
  16. 08/11 t1400: switch hard-coded object ID to variablebrian m. carlson, Aug 29, 2018
  17. 09/11 t1405: make hash size independentbrian m. carlson, Aug 29, 2018
  18. 10/11 t1406: make hash-size independentbrian m. carlson, Aug 29, 2018
  19. 11/11 t1407: make hash size independentbrian m. carlson, Aug 29, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.