#!/bin/sh
set -e

TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"
setupenvironment

# This test feeds a cache produced before SHA3 existed through the new code
# to confirm backward compatibility. The cache was produced on amd64. Its
# records are a C struct whose members add up to 148 bytes, the struct holds
# a uint64_t and amd64 aligns that type to 8 bytes so the whole struct is
# padded to 152. i386 aligns it to 4, so there the struct is 148 bytes, and
# the reader accepts only records whose size matches a known layout exactly.
if [ "$(getconf LONG_BIT)" != '64' ]; then
    msgskip 'checked-in cache holds 64-bit record sizes'
    exit 0
fi

confighashes 'MD5' 'SHA1' 'SHA256' 'SHA512' 'SHA3-256' 'SHA3-512'
db_dump=db_dump
if command -v db_dump-5 >/dev/null 2>&1; then
    db_dump=db_dump-5
fi

# the cache stores the mtime of each file and recomputes everything if it
# differs, so pin the mtime the checked-in cache was generated with
cp "$TESTDIR/deb-lp1274466-cachedb.deb" foo_1_i386.deb
touch -d @1000000000 foo_1_i386.deb
cp "$TESTDIR/cachedb-sha2-cache.db" cache.db

SHA512='7da58ff901a40ecf42a730dc33198b182e9ba9ec98799fc2c2b6fabeeee40cc12a0e7cadb4b66764235c56e1009dbfe8a9a566fb1eedf47a992d1fff2cc3332c'
SHA3_256='73b119229027ab1d68a8bc1740df46cccc494628a413a56b8e520818ad87937e'
SHA3_512='a2892c66be02c08742d96e44ae5d5b3fe0002480ddfeb0d232080ec214356f40691e4affd01c95d4edb816428898b032e7c0df8641f40c2de740074f93e55b6d'
STANZA="Package: foo
Architecture: i386
Version: 1
Priority: optional
Section: others
Maintainer: Joe Sixpack <joe@example.org>
Installed-Size: 29
Filename: ./foo_1_i386.deb
Size: 1270
MD5sum: 85d0e908c1a897700e2c5dea72d7e3c0
SHA1: 858b09169032b7925a0e463f46b6634243fc40ce
SHA256: 3750a2c9c6b5beee7f307564be3d51d3ec7cbb78fa4f0b47f84a7c41477bff59
SHA512: $SHA512
SHA3-256: $SHA3_256
SHA3-512: $SHA3_512
Description: an autogenerated dummy foo=1/test
 If you find such a package installed on your system,
 something went horribly wrong! They are autogenerated
 und used only by testcases and surf no other propose…
"

msgmsg 'checked-in cache was written before SHA3 existed'
$db_dump cache.db > before.dump
testsuccess grep "$SHA512" before.dump
testfailure grep "$SHA3_256" before.dump
testfailure grep "$SHA3_512" before.dump

msgmsg 'old cache is accepted; every SHA2 field kept, SHA3 added'
testsuccessequal "$STANZA" aptftparchive --db cache.db packages .

msgmsg 'apt-ftparchive wrote the migrated record back to the cache file'
$db_dump cache.db > after.dump
testsuccess grep "$SHA3_512" after.dump
testsuccess grep "$SHA512" after.dump

msgmsg 'migrated record is used without re-reading the file'
# same size and mtime, different content: only a cache hit gives the old digests
dd if=/dev/zero of=foo_1_i386.deb bs=1270 count=1 2>/dev/null
touch -d @1000000000 foo_1_i386.deb
testsuccessequal "$STANZA" aptftparchive --db cache.db packages .

msgmsg 'a cached SHA3 hash that is no longer requested is not written out'
testsuccess aptftparchive --db cache.db -o APT::FTPArchive::SHA3-512=false packages .
cp rootdir/tmp/testsuccess.output nosha3-512.packages
testfailure grep '^SHA3-512: ' nosha3-512.packages
testsuccess grep "^SHA3-256: $SHA3_256\$" nosha3-512.packages
