From 28efdf322eee13f21f6c5cc393fe69ef3300033a Mon Sep 17 00:00:00 2001
From: "Kyle J. McKay" <mackyle@gmail.com>
Date: Thu, 26 Aug 2021 20:31:33 -0700
Subject: [PATCH] src/tests: add some more hashing tests

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
---
 src/.gitattributes   |   2 ++
 src/gitblob.bin      | Bin 0 -> 7 bytes
 src/gittree.bin      | Bin 0 -> 7 bytes
 src/ltsha1-test.sh   |  20 +++++++++++++++++---
 src/ltsha256-test.sh |  20 +++++++++++++++++---
 5 files changed, 36 insertions(+), 6 deletions(-)
 create mode 100644 src/.gitattributes
 create mode 100644 src/gitblob.bin
 create mode 100644 src/gittree.bin

diff --git a/src/.gitattributes b/src/.gitattributes
new file mode 100644
index 0000000..f1f45c0
--- /dev/null
+++ b/src/.gitattributes
@@ -0,0 +1,2 @@
+/gitblob.bin	binary
+/gittree.bin	binary
diff --git a/src/gitblob.bin b/src/gitblob.bin
new file mode 100644
index 0000000000000000000000000000000000000000..b720c79b73c9e315cd77b89758ea1840b253e379
GIT binary patch
literal 7
OcwS4&$xl)+U;qFKumSJ@

literal 0
HcwPel00001

diff --git a/src/gittree.bin b/src/gittree.bin
new file mode 100644
index 0000000000000000000000000000000000000000..6f6728c121abb1e99329d8536fc34f6b9de57db2
GIT binary patch
literal 7
OcwQ?hN=;QTU;qFLECK-l

literal 0
HcwPel00001

diff --git a/src/ltsha1-test.sh b/src/ltsha1-test.sh
index 1aab5e6..f7585aa 100755
--- a/src/ltsha1-test.sh
+++ b/src/ltsha1-test.sh
@@ -6,14 +6,18 @@ die() { >&2 printf 'Bail out! %s\n' "$*"; exit 1; }
 
 testnum=0
 
-# $1 => value to hash (may contain escapes)
+# $1 => value to hash (may contain escapes) '-' reads stdin
 # $2 => lowercase sha256 hash expected
 # $3... => test name
 test_sha1() {
 	_val="$1"; shift
 	_hash="$1"; shift
 	_test="$*"
-	_check="$(printf '%b' "$_val" | ./ltsha1)" ||
+	if [ "$_val" = "-" ]; then
+		_check="$(./ltsha1)"
+	else
+		_check="$(printf '%b' "$_val" | ./ltsha1)"
+	fi ||
 	die "could not run ltsha1 utility"
 	_result="ok"
 	[ "$_check" = "$_hash" ] || _result="not ok"
@@ -21,7 +25,7 @@ test_sha1() {
 	printf '%s %u - %s\n' "$_result" "$testnum" "$_test"
 }
 
-echo '1..4'
+echo '1..6'
 
 test_sha1 \
 	"abc" \
@@ -43,4 +47,14 @@ test_sha1 \
 	bae5ed658ab3546aee12f23f36392f35dba1ebdd \
 	"quick brown fox"
 
+test_sha1 \
+	- <gitblob.bin \
+	e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 \
+	"git sha1 empty blob"
+
+test_sha1 \
+	- <gittree.bin \
+	4b825dc642cb6eb9a060e54bf8d69288fbee4904 \
+	"git sha1 empty tree"
+
 exit 0
diff --git a/src/ltsha256-test.sh b/src/ltsha256-test.sh
index 359391d..200ef83 100755
--- a/src/ltsha256-test.sh
+++ b/src/ltsha256-test.sh
@@ -6,14 +6,18 @@ die() { >&2 printf 'Bail out! %s\n' "$*"; exit 1; }
 
 testnum=0
 
-# $1 => value to hash (may contain escapes)
+# $1 => value to hash (may contain escapes) '-' reads stdin
 # $2 => lowercase sha256 hash expected
 # $3... => test name
 test_sha256() {
 	_val="$1"; shift
 	_hash="$1"; shift
 	_test="$*"
-	_check="$(printf '%b' "$_val" | ./ltsha256)" ||
+	if [ "$_val" = "-" ]; then
+		_check="$(./ltsha256)"
+	else
+		_check="$(printf '%b' "$_val" | ./ltsha256)"
+	fi ||
 	die "could not run ltsha256 utility"
 	_result="ok"
 	[ "$_check" = "$_hash" ] || _result="not ok"
@@ -21,7 +25,7 @@ test_sha256() {
 	printf '%s %u - %s\n' "$_result" "$testnum" "$_test"
 }
 
-echo '1..4'
+echo '1..6'
 
 test_sha256 \
 	"abc" \
@@ -43,4 +47,14 @@ test_sha256 \
 	40d5c6f7fe5672fb52269f651c2b985867dfcfa4a5c5258e3d4f8736d5095037 \
 	"quick brown fox"
 
+test_sha256 \
+	- <gitblob.bin \
+	473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813 \
+	"git sha256 empty blob"
+
+test_sha256 \
+	- <gittree.bin \
+	6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321 \
+	"git sha256 empty tree"
+
 exit 0
-- 
2.11.4.GIT