Skip to content

Commit d555663

Browse files
committed
Merge branch 'bc/hash-independent-tests'
Various tests have been updated to make it easier to swap the hash function used for object identification. * bc/hash-independent-tests: t5318: use test_oid for HASH_LEN t1407: make hash size independent t1406: make hash-size independent t1405: make hash size independent t1400: switch hard-coded object ID to variable t1006: make hash size independent t0064: make hash size independent t0002: abstract away SHA-1 specific constants t0000: update tests for SHA-256 t0000: use hash translation table t: add test functions to translate hash-related values
2 parents 77b5046 + ae0c89d commit d555663

14 files changed

+350
-113
lines changed

t/README

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

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

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: 146 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,87 @@ 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

864+
test_oid_cache <<\EOF
865+
path0f sha1:f87290f8eb2cbbea7857214459a0739927eab154
866+
path0f sha256:638106af7c38be056f3212cbd7ac65bc1bac74f420ca5a436ff006a9d025d17d
867+
868+
path0s sha1:15a98433ae33114b085f3eb3bb03b832b3180a01
869+
path0s sha256:3a24cc53cf68edddac490bbf94a418a52932130541361f685df685e41dd6c363
870+
871+
path2f sha1:3feff949ed00a62d9f7af97c15cd8a30595e7ac7
872+
path2f sha256:2a7f36571c6fdbaf0e3f62751a0b25a3f4c54d2d1137b3f4af9cb794bb498e5f
873+
874+
path2s sha1:d8ce161addc5173867a3c3c730924388daedbc38
875+
path2s sha256:18fd611b787c2e938ddcc248fabe4d66a150f9364763e9ec133dd01d5bb7c65a
876+
877+
path2d sha1:58a09c23e2ca152193f2786e06986b7b6712bdbe
878+
path2d sha256:00e4b32b96e7e3d65d79112dcbea53238a22715f896933a62b811377e2650c17
879+
880+
path3f sha1:0aa34cae68d0878578ad119c86ca2b5ed5b28376
881+
path3f sha256:09f58616b951bd571b8cb9dc76d372fbb09ab99db2393f5ab3189d26c45099ad
882+
883+
path3s sha1:8599103969b43aff7e430efea79ca4636466794f
884+
path3s sha256:fce1aed087c053306f3f74c32c1a838c662bbc4551a7ac2420f5d6eb061374d0
885+
886+
path3d sha1:21ae8269cacbe57ae09138dcc3a2887f904d02b3
887+
path3d sha256:9b60497be959cb830bf3f0dc82bcc9ad9e925a24e480837ade46b2295e47efe1
888+
889+
subp3f sha1:00fb5908cb97c2564a9783c0c64087333b3b464f
890+
subp3f sha256:a1a9e16998c988453f18313d10375ee1d0ddefe757e710dcae0d66aa1e0c58b3
891+
892+
subp3s sha1:6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c
893+
subp3s sha256:81759d9f5e93c6546ecfcadb560c1ff057314b09f93fe8ec06e2d8610d34ef10
894+
895+
subp3d sha1:3c5e5399f3a333eddecce7a9b9465b63f65f51e2
896+
subp3d sha256:76b4ef482d4fa1c754390344cf3851c7f883b27cf9bc999c6547928c46aeafb7
897+
898+
root sha1:087704a96baf1c2d1c869a8b084481e121c88b5b
899+
root sha256:9481b52abab1b2ffeedbf9de63ce422b929f179c1b98ff7bee5f8f1bc0710751
900+
901+
simpletree sha1:7bb943559a305bdd6bdee2cef6e5df2413c3d30a
902+
simpletree sha256:1710c07a6c86f9a3c7376364df04c47ee39e5a5e221fcdd84b743bc9bb7e2bc5
903+
EOF
904+
827905
# updating a new file without --add should fail.
828906
test_expect_success 'git update-index without --add should fail adding' '
829907
test_must_fail git update-index should-be-empty
@@ -839,8 +917,8 @@ test_expect_success 'writing tree out with git write-tree' '
839917
'
840918

841919
# we know the shape and contents of the tree and know the object ID for it.
842-
test_expect_success SHA1 'validate object ID of a known tree' '
843-
test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
920+
test_expect_success 'validate object ID of a known tree' '
921+
test "$tree" = "$(test_oid simpletree)"
844922
'
845923

846924
# Removing paths.
@@ -882,16 +960,16 @@ test_expect_success 'showing stage with git ls-files --stage' '
882960
git ls-files --stage >current
883961
'
884962

885-
test_expect_success SHA1 'validate git ls-files output for a known tree' '
886-
cat >expected <<-\EOF &&
887-
100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
888-
120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
889-
100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
890-
120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
891-
100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
892-
120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
893-
100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
894-
120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
963+
test_expect_success 'validate git ls-files output for a known tree' '
964+
cat >expected <<-EOF &&
965+
100644 $(test_oid path0f) 0 path0
966+
120000 $(test_oid path0s) 0 path0sym
967+
100644 $(test_oid path2f) 0 path2/file2
968+
120000 $(test_oid path2s) 0 path2/file2sym
969+
100644 $(test_oid path3f) 0 path3/file3
970+
120000 $(test_oid path3s) 0 path3/file3sym
971+
100644 $(test_oid subp3f) 0 path3/subp3/file3
972+
120000 $(test_oid subp3s) 0 path3/subp3/file3sym
895973
EOF
896974
test_cmp expected current
897975
'
@@ -900,20 +978,20 @@ test_expect_success 'writing tree out with git write-tree' '
900978
tree=$(git write-tree)
901979
'
902980

903-
test_expect_success SHA1 'validate object ID for a known tree' '
904-
test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
981+
test_expect_success 'validate object ID for a known tree' '
982+
test "$tree" = "$(test_oid root)"
905983
'
906984

907985
test_expect_success 'showing tree with git ls-tree' '
908986
git ls-tree $tree >current
909987
'
910988

911-
test_expect_success SHA1 'git ls-tree output for a known tree' '
912-
cat >expected <<-\EOF &&
913-
100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
914-
120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
915-
040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
916-
040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
989+
test_expect_success 'git ls-tree output for a known tree' '
990+
cat >expected <<-EOF &&
991+
100644 blob $(test_oid path0f) path0
992+
120000 blob $(test_oid path0s) path0sym
993+
040000 tree $(test_oid path2d) path2
994+
040000 tree $(test_oid path3d) path3
917995
EOF
918996
test_cmp expected current
919997
'
@@ -924,16 +1002,16 @@ test_expect_success 'showing tree with git ls-tree -r' '
9241002
git ls-tree -r $tree >current
9251003
'
9261004

927-
test_expect_success SHA1 'git ls-tree -r output for a known tree' '
928-
cat >expected <<-\EOF &&
929-
100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
930-
120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
931-
100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
932-
120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
933-
100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
934-
120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
935-
100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
936-
120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
1005+
test_expect_success 'git ls-tree -r output for a known tree' '
1006+
cat >expected <<-EOF &&
1007+
100644 blob $(test_oid path0f) path0
1008+
120000 blob $(test_oid path0s) path0sym
1009+
100644 blob $(test_oid path2f) path2/file2
1010+
120000 blob $(test_oid path2s) path2/file2sym
1011+
100644 blob $(test_oid path3f) path3/file3
1012+
120000 blob $(test_oid path3s) path3/file3sym
1013+
100644 blob $(test_oid subp3f) path3/subp3/file3
1014+
120000 blob $(test_oid subp3s) path3/subp3/file3sym
9371015
EOF
9381016
test_cmp expected current
9391017
'
@@ -943,19 +1021,19 @@ test_expect_success 'showing tree with git ls-tree -r -t' '
9431021
git ls-tree -r -t $tree >current
9441022
'
9451023

946-
test_expect_success SHA1 'git ls-tree -r output for a known tree' '
947-
cat >expected <<-\EOF &&
948-
100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
949-
120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
950-
040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
951-
100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
952-
120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
953-
040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
954-
100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
955-
120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
956-
040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
957-
100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
958-
120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
1024+
test_expect_success 'git ls-tree -r output for a known tree' '
1025+
cat >expected <<-EOF &&
1026+
100644 blob $(test_oid path0f) path0
1027+
120000 blob $(test_oid path0s) path0sym
1028+
040000 tree $(test_oid path2d) path2
1029+
100644 blob $(test_oid path2f) path2/file2
1030+
120000 blob $(test_oid path2s) path2/file2sym
1031+
040000 tree $(test_oid path3d) path3
1032+
100644 blob $(test_oid path3f) path3/file3
1033+
120000 blob $(test_oid path3s) path3/file3sym
1034+
040000 tree $(test_oid subp3d) path3/subp3
1035+
100644 blob $(test_oid subp3f) path3/subp3/file3
1036+
120000 blob $(test_oid subp3s) path3/subp3/file3sym
9591037
EOF
9601038
test_cmp expected current
9611039
'
@@ -964,26 +1042,27 @@ test_expect_success 'writing partial tree out with git write-tree --prefix' '
9641042
ptree=$(git write-tree --prefix=path3)
9651043
'
9661044

967-
test_expect_success SHA1 'validate object ID for a known tree' '
968-
test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
1045+
test_expect_success 'validate object ID for a known tree' '
1046+
test "$ptree" = $(test_oid path3d)
9691047
'
9701048

9711049
test_expect_success 'writing partial tree out with git write-tree --prefix' '
9721050
ptree=$(git write-tree --prefix=path3/subp3)
9731051
'
9741052

975-
test_expect_success SHA1 'validate object ID for a known tree' '
976-
test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
1053+
test_expect_success 'validate object ID for a known tree' '
1054+
test "$ptree" = $(test_oid subp3d)
9771055
'
9781056

9791057
test_expect_success 'put invalid objects into the index' '
9801058
rm -f .git/index &&
981-
cat >badobjects <<-\EOF &&
982-
100644 blob 1000000000000000000000000000000000000000 dir/file1
983-
100644 blob 2000000000000000000000000000000000000000 dir/file2
984-
100644 blob 3000000000000000000000000000000000000000 dir/file3
985-
100644 blob 4000000000000000000000000000000000000000 dir/file4
986-
100644 blob 5000000000000000000000000000000000000000 dir/file5
1059+
suffix=$(echo $ZERO_OID | sed -e "s/^.//") &&
1060+
cat >badobjects <<-EOF &&
1061+
100644 blob $(test_oid 001) dir/file1
1062+
100644 blob $(test_oid 002) dir/file2
1063+
100644 blob $(test_oid 003) dir/file3
1064+
100644 blob $(test_oid 004) dir/file4
1065+
100644 blob $(test_oid 005) dir/file5
9871066
EOF
9881067
git update-index --index-info <badobjects
9891068
'
@@ -1006,16 +1085,16 @@ test_expect_success 'git read-tree followed by write-tree should be idempotent'
10061085
test "$newtree" = "$tree"
10071086
'
10081087

1009-
test_expect_success SHA1 'validate git diff-files output for a know cache/work tree state' '
1010-
cat >expected <<\EOF &&
1011-
:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
1012-
:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
1013-
:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
1014-
:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
1015-
:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
1016-
:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
1017-
:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
1018-
:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
1088+
test_expect_success 'validate git diff-files output for a know cache/work tree state' '
1089+
cat >expected <<EOF &&
1090+
:100644 100644 $(test_oid path0f) $ZERO_OID M path0
1091+
:120000 120000 $(test_oid path0s) $ZERO_OID M path0sym
1092+
:100644 100644 $(test_oid path2f) $ZERO_OID M path2/file2
1093+
:120000 120000 $(test_oid path2s) $ZERO_OID M path2/file2sym
1094+
:100644 100644 $(test_oid path3f) $ZERO_OID M path3/file3
1095+
:120000 120000 $(test_oid path3s) $ZERO_OID M path3/file3sym
1096+
:100644 100644 $(test_oid subp3f) $ZERO_OID M path3/subp3/file3
1097+
:120000 120000 $(test_oid subp3s) $ZERO_OID M path3/subp3/file3sym
10191098
EOF
10201099
git diff-files >current &&
10211100
test_cmp current expected
@@ -1031,23 +1110,23 @@ test_expect_success 'no diff after checkout and git update-index --refresh' '
10311110
'
10321111

10331112
################################################################
1034-
P=087704a96baf1c2d1c869a8b084481e121c88b5b
1113+
P=$(test_oid root)
10351114

1036-
test_expect_success SHA1 'git commit-tree records the correct tree in a commit' '
1115+
test_expect_success 'git commit-tree records the correct tree in a commit' '
10371116
commit0=$(echo NO | git commit-tree $P) &&
10381117
tree=$(git show --pretty=raw $commit0 |
10391118
sed -n -e "s/^tree //p" -e "/^author /q") &&
10401119
test "z$tree" = "z$P"
10411120
'
10421121

1043-
test_expect_success SHA1 'git commit-tree records the correct parent in a commit' '
1122+
test_expect_success 'git commit-tree records the correct parent in a commit' '
10441123
commit1=$(echo NO | git commit-tree $P -p $commit0) &&
10451124
parent=$(git show --pretty=raw $commit1 |
10461125
sed -n -e "s/^parent //p" -e "/^author /q") &&
10471126
test "z$commit0" = "z$parent"
10481127
'
10491128

1050-
test_expect_success SHA1 'git commit-tree omits duplicated parent in a commit' '
1129+
test_expect_success 'git commit-tree omits duplicated parent in a commit' '
10511130
commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
10521131
parent=$(git show --pretty=raw $commit2 |
10531132
sed -n -e "s/^parent //p" -e "/^author /q" |

0 commit comments

Comments
 (0)