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

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

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jun 4, 2018, 23:52 UTC
Message-ID
<20180604235229.279814-2-sandals@crustytoothpaste.net>
In-Reply-To
<20180604235229.279814-1-sandals@crustytoothpaste.net>

Add a test function helper, test_translate, that will produce its first argument if the hash in use is SHA-1 and the second if its argument is NewHash. Implement a mode that can read entries from a file as well for reusability across tests.

For the moment, use the length of the empty blob to determine the hash in use. In the future, we can change this code so that it can use the configuration and learn about the difference in input, output, and on-disk formats.

Implement two basic lookup charts, one for common invalid or synthesized object IDs, and one for various facts about the hash function in use.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 t/test-lib-functions.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 t/translate/hash-info   |  9 +++++++++
 t/translate/oid         | 15 +++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 t/translate/hash-info
 create mode 100644 t/translate/oid
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 2b2181dca0..0e7067460b 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1147,3 +1147,43 @@ depacketize () {
 		}
 	'
 }
+
+test_translate_f_ () {
+	local file="$TEST_DIRECTORY/translate/$2" &&
+	perl -e '
+		$delim = "\t";
+		($hoidlen, $file, $arg) = @ARGV;
+		open($fh, "<", $file) or die "open: $!";
+		while (<$fh>) {
+			# Allow specifying other delimiters.
+			$delim = $1 if /^#!\sdelimiter\s(.)/;
+			next if /^#/;
+			@fields = split /$delim/, $_, 3;
+			if ($fields[0] eq $arg) {
+				print($hoidlen == 40 ? $fields[1] : $fields[2]);
+				last;
+			}
+		}
+	' "$1" "$file" "$3"
+}
+
+# Without -f, print the first argument if we are using SHA-1 and the second if
+# we're using NewHash.
+# With -f FILE ARG, read the (by default) tab-delimited file from
+# t/translate/FILE, finding the first field matching ARG and printing either the
+# second or third field depending on the hash in use.
+test_translate () {
+	local hoidlen=$(printf "%s" "$EMPTY_BLOB" | wc -c) &&
+	if [ "$1" = "-f" ]
+	then
+		shift &&
+		test_translate_f_ "$hoidlen" "$@"
+	else
+		if [ "$hoidlen" -eq 40 ]
+		then
+			printf "%s" "$1"
+		else
+			printf "%s" "$2"
+		fi
+	fi
+}
diff --git a/t/translate/hash-info b/t/translate/hash-info
new file mode 100644
index 0000000000..36cbd9a8eb
--- /dev/null
+++ b/t/translate/hash-info
@@ -0,0 +1,9 @@
+# Various facts about the hash algorithm in use for easy access in tests.
+#
+# Several aliases are provided for easy recall.
+rawsz	20	32
+oidlen	20	32
+hexsz	40	64
+hexoidlen	40	64
+zero	0000000000000000000000000000000000000000	0000000000000000000000000000000000000000000000000000000000000000
+zero-oid	0000000000000000000000000000000000000000	0000000000000000000000000000000000000000000000000000000000000000
diff --git a/t/translate/oid b/t/translate/oid
new file mode 100644
index 0000000000..8de0fd64af
--- /dev/null
+++ b/t/translate/oid
@@ -0,0 +1,15 @@
+# These are some common invalid and partial object IDs used in tests.
+001	0000000000000000000000000000000000000001	0000000000000000000000000000000000000000000000000000000000000001
+002	0000000000000000000000000000000000000002	0000000000000000000000000000000000000000000000000000000000000002
+003	0000000000000000000000000000000000000003	0000000000000000000000000000000000000000000000000000000000000003
+004	0000000000000000000000000000000000000004	0000000000000000000000000000000000000000000000000000000000000004
+005	0000000000000000000000000000000000000005	0000000000000000000000000000000000000000000000000000000000000005
+006	0000000000000000000000000000000000000006	0000000000000000000000000000000000000000000000000000000000000006
+007	0000000000000000000000000000000000000007	0000000000000000000000000000000000000000000000000000000000000007
+# All zeros or Fs missing one or two hex segments.
+zero-1	000000000000000000000000000000000000000	000000000000000000000000000000000000000000000000000000000000000
+zero-2	00000000000000000000000000000000000000	00000000000000000000000000000000000000000000000000000000000000
+ff-1	fffffffffffffffffffffffffffffffffffffff	fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+ff-2	ffffffffffffffffffffffffffffffffffffff	ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+numeric	0123456789012345678901234567890123456789	0123456789012345678901234567890123456789012345678901234567890123
+deadbeef	deadbeefdeadbeefdeadbeefdeadbeefdeadbeef	deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
Previous: brian m. carlsonNext: Torsten Bögershausen
Message 2 of 26 in “Hash-independent tests (part 3)”
  1. 00/10 Hash-independent tests (part 3)brian m. carlson, Jun 4, 2018
  2. 01/10 t: add tool to translate hash-related valuesbrian m. carlson, Jun 4, 2018
  3. Torsten BögershausenJun 6, 2018
  4. Jeff KingJun 6, 2018
  5. brian m. carlsonJun 7, 2018
  6. Jeff KingJun 7, 2018
  7. Eric SunshineJun 11, 2018
  8. brian m. carlsonJun 12, 2018
  9. Eric SunshineJun 12, 2018
  10. brian m. carlsonJun 14, 2018
  11. brian m. carlsonJul 24, 2018
  12. Eric SunshineJul 25, 2018
  13. 05/10 t0064: make hash size independentbrian m. carlson, Jun 4, 2018
  14. Eric SunshineJun 11, 2018
  15. brian m. carlsonJun 12, 2018
  16. 07/10 t1400: switch hard-coded object ID to variablebrian m. carlson, Jun 4, 2018
  17. 04/10 t0027: use $ZERO_OIDbrian m. carlson, Jun 4, 2018
  18. Torsten BögershausenJun 6, 2018
  19. brian m. carlsonJun 7, 2018
  20. 08/10 t1405: make hash size independentbrian m. carlson, Jun 4, 2018
  21. 06/10 t1006: make hash size independentbrian m. carlson, Jun 4, 2018
  22. 03/10 t0002: abstract away SHA-1-specific constantsbrian m. carlson, Jun 4, 2018
  23. Eric SunshineJun 11, 2018
  24. 09/10 t1406: make hash-size independentbrian m. carlson, Jun 4, 2018
  25. 10/10 t1407: make hash size independentbrian m. carlson, Jun 4, 2018
  26. 02/10 t0000: use hash translation tablebrian m. carlson, Jun 4, 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.