#!/bin/sh
set -e


#
# main()
#
TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"
setupenvironment
configarchitecture "i386"
confighashes 'MD5' 'SHA1' 'SHA256' 'SHA512'

db_dump=db_dump
if command -v db_dump-5 >/dev/null 2>&1; then
    db_dump=db_dump-5
fi

# gather the db and the deb, ensure mtime is not modfied as its saved in the DB
cp -p "$TESTDIR/deb-lp1274466-cachedb.deb" foo_1_i386.deb
cp -p "$TESTDIR/cachedb-lp1274466-old-format.db" old-format.db

# verify that the format is different
testsuccess aptftparchive --db new-format.db packages .
$db_dump new-format.db > new-format.dump
$db_dump old-format.db > old-format.dump
testfailure diff -u old-format.dump new-format.dump

# ensure the new format as the sha512
testsuccess grep 7da58ff901a40ecf42a730dc33198b182e9ba9ec98799fc2c2b6fabeeee40cc12a0e7cadb4b66764235c56e1009dbfe8a9a566fb1eedf47a992d1fff2cc3332c new-format.dump
# but the old format does not
testfailure grep 7da58ff901a40ecf42a730dc33198b182e9ba9ec98799fc2c2b6fabeeee40cc12a0e7cadb4b66764235c56e1009dbfe8a9a566fb1eedf47a992d1fff2cc3332c old-format.dump

# regression test for corruption with previous generation of cachedb
testsuccessequal "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: 7da58ff901a40ecf42a730dc33198b182e9ba9ec98799fc2c2b6fabeeee40cc12a0e7cadb4b66764235c56e1009dbfe8a9a566fb1eedf47a992d1fff2cc3332c
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ā€¦
" aptftparchive  --db old-format.db packages .

# ensure that the db is updated and contains the new sha512
$db_dump old-format.db > old-format.dump

testsuccess grep 7da58ff901a40ecf42a730dc33198b182e9ba9ec98799fc2c2b6fabeeee40cc12a0e7cadb4b66764235c56e1009dbfe8a9a566fb1eedf47a992d1fff2cc3332c old-format.dump