{"thread":{"id":"16017","subject":"[PATCH] improve index-pack tests","startedAt":"2008-10-23T00:59:22Z","lastAt":"2008-10-23T00:59:22Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"93750","messageId":"alpine.LFD.2.00.0810222045490.26244@xanadu.home","threadId":"16017","inReplyTo":null,"subject":"[PATCH] improve index-pack tests","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2008-10-23T00:59:22Z","receivedAt":"2008-10-23T00:59:22Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"Commit 9441b61dc5 introduced serious bugs in index-pack which are\ndescribed and fixed by commit ce3f6dc655.  However, despite the\nboldness of those bugs, the test suite still passed.\n\nThis improves t5302-pack-index.sh so to ensure a much better code\npath coverage.  With commit ce3f6dc655 reverted, 17 of the 26 tests\ndo fail now.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n---\n\ndiff --git a/t/t5302-pack-index.sh b/t/t5302-pack-index.sh\nindex 344ab25..b0b0fda 100755\n--- a/t/t5302-pack-index.sh\n+++ b/t/t5302-pack-index.sh\n@@ -11,13 +11,18 @@ test_expect_success \\\n     'rm -rf .git\n      git init &&\n      i=1 &&\n-\t while test $i -le 100\n+     while test $i -le 100\n      do\n-\t\t i=`printf '%03i' $i`\n-         echo $i >file_$i &&\n-         test-genrandom \"$i\" 8192 >>file_$i &&\n-         git update-index --add file_$i &&\n-\t\t i=`expr $i + 1` || return 1\n+         iii=`printf '%03i' $i`\n+         test-genrandom \"bar\" 200 > wide_delta_$iii &&\n+         test-genrandom \"baz $iii\" 50 >> wide_delta_$iii &&\n+         test-genrandom \"foo\"$i 100 > deep_delta_$iii &&\n+         test-genrandom \"foo\"`expr $i + 1` 100 >> deep_delta_$iii &&\n+         test-genrandom \"foo\"`expr $i + 2` 100 >> deep_delta_$iii &&\n+         echo $iii >file_$iii &&\n+         test-genrandom \"$iii\" 8192 >>file_$iii &&\n+         git update-index --add file_$iii deep_delta_$iii wide_delta_$iii &&\n+         i=`expr $i + 1` || return 1\n      done &&\n      { echo 101 && test-genrandom 100 8192; } >file_101 &&\n      git update-index --add file_101 &&\n@@ -92,6 +97,31 @@ test_expect_success \\\n     '64-bit offsets: index-pack result should match pack-objects one' \\\n     'cmp \"test-3-${pack3}.idx\" \"3.idx\"'\n \n+# returns the object number for given object in given pack index\n+index_obj_nr()\n+{\n+    idx_file=$1\n+    object_sha1=$2\n+    nr=0\n+    git show-index < $idx_file |\n+    while read offs sha1 extra\n+    do\n+      nr=$(($nr + 1))\n+      test \"$sha1\" = \"$object_sha1\" || continue\n+      echo \"$(($nr - 1))\"\n+      break\n+    done\n+}\n+\n+# returns the pack offset for given object as found in given pack index\n+index_obj_offset()\n+{\n+    idx_file=$1\n+    object_sha1=$2\n+    git show-index < $idx_file | grep $object_sha1 |\n+    ( read offs extra && echo \"$offs\" )\n+}\n+\n test_expect_success \\\n     '[index v1] 1) stream pack to repository' \\\n     'git index-pack --index-version=1 --stdin < \"test-1-${pack1}.pack\" &&\n@@ -102,19 +132,22 @@ test_expect_success \\\n \n test_expect_success \\\n     '[index v1] 2) create a stealth corruption in a delta base reference' \\\n-    '# this test assumes a delta smaller than 16 bytes at the end of the pack\n-     git show-index <1.idx | sort -n | sed -ne \\$p | (\n-       read delta_offs delta_sha1 &&\n-       git cat-file blob \"$delta_sha1\" > blob_1 &&\n-       chmod +w \".git/objects/pack/pack-${pack1}.pack\" &&\n-       dd of=\".git/objects/pack/pack-${pack1}.pack\" seek=$(($delta_offs + 1)) \\\n-\t  if=\".git/objects/pack/pack-${pack1}.idx\" skip=$((256 * 4 + 4)) \\\n-\t  bs=1 count=20 conv=notrunc &&\n-       git cat-file blob \"$delta_sha1\" > blob_2 )'\n+    '# This test assumes file_101 is a delta smaller than 16 bytes.\n+     # It should be against file_100 but we substitute its base for file_099\n+     sha1_101=`git hash-object file_101` &&\n+     sha1_099=`git hash-object file_099` &&\n+     offs_101=`index_obj_offset 1.idx $sha1_101` &&\n+     nr_099=`index_obj_nr 1.idx $sha1_099` &&\n+     chmod +w \".git/objects/pack/pack-${pack1}.pack\" &&\n+     dd of=\".git/objects/pack/pack-${pack1}.pack\" seek=$(($offs_101 + 1)) \\\n+        if=\".git/objects/pack/pack-${pack1}.idx\" \\\n+        skip=$((4 + 256 * 4 + $nr_099 * 24)) \\\n+        bs=1 count=20 conv=notrunc &&\n+     git cat-file blob $sha1_101 > file_101_foo1'\n \n test_expect_success \\\n     '[index v1] 3) corrupted delta happily returned wrong data' \\\n-    '! cmp blob_1 blob_2'\n+    'test -f file_101_foo1 && ! cmp file_101 file_101_foo1'\n \n test_expect_success \\\n     '[index v1] 4) confirm that the pack is actually corrupted' \\\n@@ -140,19 +173,22 @@ test_expect_success \\\n \n test_expect_success \\\n     '[index v2] 2) create a stealth corruption in a delta base reference' \\\n-    '# this test assumes a delta smaller than 16 bytes at the end of the pack\n-     git show-index <1.idx | sort -n | sed -ne \\$p | (\n-       read delta_offs delta_sha1 delta_crc &&\n-       git cat-file blob \"$delta_sha1\" > blob_3 &&\n-       chmod +w \".git/objects/pack/pack-${pack1}.pack\" &&\n-       dd of=\".git/objects/pack/pack-${pack1}.pack\" seek=$(($delta_offs + 1)) \\\n-\t  if=\".git/objects/pack/pack-${pack1}.idx\" skip=$((8 + 256 * 4)) \\\n-\t  bs=1 count=20 conv=notrunc &&\n-       git cat-file blob \"$delta_sha1\" > blob_4 )'\n+    '# This test assumes file_101 is a delta smaller than 16 bytes.\n+     # It should be against file_100 but we substitute its base for file_099\n+     sha1_101=`git hash-object file_101` &&\n+     sha1_099=`git hash-object file_099` &&\n+     offs_101=`index_obj_offset 1.idx $sha1_101` &&\n+     nr_099=`index_obj_nr 1.idx $sha1_099` &&\n+     chmod +w \".git/objects/pack/pack-${pack1}.pack\" &&\n+     dd of=\".git/objects/pack/pack-${pack1}.pack\" seek=$(($offs_101 + 1)) \\\n+        if=\".git/objects/pack/pack-${pack1}.idx\" \\\n+        skip=$((8 + 256 * 4 + $nr_099 * 20)) \\\n+        bs=1 count=20 conv=notrunc &&\n+     git cat-file blob $sha1_101 > file_101_foo2'\n \n test_expect_success \\\n     '[index v2] 3) corrupted delta happily returned wrong data' \\\n-    '! cmp blob_3 blob_4'\n+    'test -f file_101_foo2 && ! cmp file_101 file_101_foo2'\n \n test_expect_success \\\n     '[index v2] 4) confirm that the pack is actually corrupted' \\\n@@ -167,9 +203,11 @@ test_expect_success \\\n     'rm -f .git/objects/pack/* &&\n      git index-pack --index-version=2 --stdin < \"test-1-${pack1}.pack\" &&\n      git verify-pack \".git/objects/pack/pack-${pack1}.pack\" &&\n+     obj=`git hash-object file_001` &&\n+     nr=`index_obj_nr \".git/objects/pack/pack-${pack1}.idx\" $obj` &&\n      chmod +w \".git/objects/pack/pack-${pack1}.idx\" &&\n      dd if=/dev/zero of=\".git/objects/pack/pack-${pack1}.idx\" conv=notrunc \\\n-        bs=1 count=4 seek=$((8 + 256 * 4 + `wc -l <obj-list` * 20 + 0)) &&\n+        bs=1 count=4 seek=$((8 + 256 * 4 + `wc -l <obj-list` * 20 + $nr * 4)) &&\n      ( while read obj\n        do git cat-file -p $obj >/dev/null || exit 1\n        done <obj-list ) &&\n"}]}