#!/usr/bin/env bash
# group: rw quick
#
# Test qemu-img check for parallels format
#
# Copyright (C) 2022 Virtuozzo International GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# creator
owner=alexander.ivanov@virtuozzo.com

seq=`basename $0`
echo "QA output created by $seq"

status=1	# failure is the default!

_cleanup()
{
    _cleanup_test_img
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# get standard environment, filters and checks
. ../common.rc
. ../common.filter

_supported_fmt parallels
_supported_proto file
_supported_os Linux

SIZE=$((4 * 1024 * 1024))
IMGFMT=parallels
CLUSTER_SIZE_OFFSET=28
BAT_ENTRIES_OFFSET=32
DATA_OFF_OFFSET=48
BAT_OFFSET=64

_make_test_img $SIZE

CLUSTER_SIZE=$(peek_file_le $TEST_IMG $CLUSTER_SIZE_OFFSET 4)
CLUSTER_SIZE=$((CLUSTER_SIZE * 512))
LAST_CLUSTER_OFF=$((SIZE - CLUSTER_SIZE))
LAST_CLUSTER=$((LAST_CLUSTER_OFF/CLUSTER_SIZE))

echo "== TEST OUT OF IMAGE CHECK =="

echo "== write pattern =="
{ $QEMU_IO -c "write -P 0x11 0 $SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== corrupt image =="
cluster=$(($LAST_CLUSTER + 2))
poke_file "$TEST_IMG" "$BAT_OFFSET" "\x$cluster\x00\x00\x00"

echo "== read corrupted image with repairing =="
{ $QEMU_IO -c "read -P 0x00 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST LEAK CHECK =="

echo "== write pattern to last cluster =="
echo "write -P 0x11 $LAST_CLUSTER_OFF $CLUSTER_SIZE"
{ $QEMU_IO -c "write -P 0x11 $LAST_CLUSTER_OFF $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

file_size=`stat --printf="%s" "$TEST_IMG"`
echo "file size: $file_size"

echo "== extend image by 1 cluster =="
fallocate -xl $((file_size + CLUSTER_SIZE)) "$TEST_IMG"

file_size=`stat --printf="%s" "$TEST_IMG"`
echo "file size: $file_size"

echo "== repair image =="
_check_test_img -r all

file_size=`stat --printf="%s" "$TEST_IMG"`
echo "file size: $file_size"

echo "== check last cluster =="
{ $QEMU_IO -r -c "read -P 0x11 $LAST_CLUSTER_OFF $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST DUPLICATION CHECK =="

echo "== write pattern to whole image =="
{ $QEMU_IO -c "write -P 0x11 0 $SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== write another pattern to second cluster =="
{ $QEMU_IO -c "write -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check second cluster =="
{ $QEMU_IO -r -c "read -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir


echo "== corrupt image =="
poke_file "$TEST_IMG" "$(($BAT_OFFSET + 4))" "\x01\x00\x00\x00"

echo "== check second cluster =="
{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== repair image =="
_check_test_img -r all

echo "== check the first cluster =="
{ $QEMU_IO -r -c "read -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check second cluster =="
{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== write another pattern to the first clusters =="
{ $QEMU_IO -c "write -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check the first cluster =="
{ $QEMU_IO -r -c "read -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check the second cluster (deduplicated) =="
{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST DUPLICATION SELF-CURE =="

echo "== write pattern to whole image =="
{ $QEMU_IO -c "write -P 0x11 0 $SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== write another pattern to second cluster =="
{ $QEMU_IO -c "write -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check second cluster =="
{ $QEMU_IO -r -c "read -P 0x55 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir


echo "== corrupt image =="
poke_file "$TEST_IMG" "$(($BAT_OFFSET + 4))" "\x01\x00\x00\x00"

echo "== check second cluster =="
{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check the first cluster with self-repair =="
{ $QEMU_IO -c "read -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check second cluster =="
{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== write another pattern to the first clusters =="
{ $QEMU_IO -c "write -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check the first cluster =="
{ $QEMU_IO -r -c "read -P 0x66 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== check the second cluster (deduplicated) =="
{ $QEMU_IO -r -c "read -P 0x11 $CLUSTER_SIZE $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST DATA_OFF CHECK =="

echo "== write pattern to first cluster =="
{ $QEMU_IO -c "write -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== spoil data_off field =="
poke_file "$TEST_IMG" "$DATA_OFF_OFFSET" "\xff\xff\xff\xff"

echo "== check first cluster =="
{ $QEMU_IO -c "read -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST DATA_OFF THROUGH REPAIR =="

echo "== write pattern to first cluster =="
{ $QEMU_IO -c "write -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== spoil data_off field =="
poke_file "$TEST_IMG" "$DATA_OFF_OFFSET" "\xff\xff\xff\xff"

echo "== repair image =="
_check_test_img -r all

echo "== check first cluster =="
{ $QEMU_IO -r -c "read -P 0x55 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST HUGE BAT TABLE OPEN =="

# Overflows a single read request, but stays under parallels_open()'s
# own catalog-size cap.
BAT_ENTRIES=536870896
HEADER_SIZE=$((64 + 4 * BAT_ENTRIES))

echo "== advertise a BAT table larger than BDRV_REQUEST_MAX_BYTES =="
poke_file "$TEST_IMG" "$BAT_ENTRIES_OFFSET" "\xf0\xff\xff\x1f"

echo "== grow the file to match, without writing real data =="
truncate -s $HEADER_SIZE "$TEST_IMG"

echo "== open must succeed: the header/BAT read is chunked =="
_img_info

echo "== an unallocated cluster still reads as zeroes =="
{ $QEMU_IO -r -c "read -P 0x00 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST OVERSIZED VIRTUAL DISK CHECK =="

BAT_ENTRIES_OFFSET=32
NB_SECTORS_OFFSET=36

TRACKS=$(peek_file_le $TEST_IMG $CLUSTER_SIZE_OFFSET 4)
BAT_ENTRIES=$(peek_file_le $TEST_IMG $BAT_ENTRIES_OFFSET 4)
COVERED_SECTORS=$((BAT_ENTRIES * TRACKS))

echo "== advertise one more cluster than the BAT covers =="
poke_file_le "$TEST_IMG" $NB_SECTORS_OFFSET 8 $((COVERED_SECTORS + TRACKS))

echo "== open must fail cleanly instead of aborting =="
_img_info

echo "== write into the uncovered range must fail cleanly too =="
{ $QEMU_IO -c "write -P 0x41 $((COVERED_SECTORS * 512)) $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST BAT ENTRY POINTING OUTSIDE IMAGE =="

echo "== corrupt image: point first cluster far outside the file =="
poke_file_le "$TEST_IMG" $BAT_OFFSET 4 1000000

echo "== read-only read must return zeroes, not an I/O error =="
{ $QEMU_IO -r -c "read -P 0x00 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== write must allocate a fresh cluster instead of trusting the entry =="
{ $QEMU_IO -c "write -P 0x77 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== file did not grow anywhere near the bogus offset =="
file_size=`stat --printf="%s" "$TEST_IMG"`
if [ "$file_size" -lt $((16 * 1024 * 1024)) ]; then
    echo "file size sane: yes"
else
    echo "file size sane: no ($file_size bytes)"
fi

echo "== data reads back correctly =="
{ $QEMU_IO -r -c "read -P 0x77 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image, with a small cluster size so the BAT table itself spans
# more than one cluster and there is room to point before data_off.
_make_test_img -o cluster_size=512 65536

SMALL_CLUSTER_SIZE=$(peek_file_le $TEST_IMG $CLUSTER_SIZE_OFFSET 4)
SMALL_CLUSTER_SIZE=$((SMALL_CLUSTER_SIZE * 512))
DATA_OFF=$(peek_file_le $TEST_IMG $DATA_OFF_OFFSET 4)
echo "cluster size: $SMALL_CLUSTER_SIZE, data offset (sectors): $DATA_OFF"

# Cluster index 1 starts at this byte offset, which must be < data_off
# in sectors * 512 for this test to actually exercise the bug.
VICTIM_OFFSET=$SMALL_CLUSTER_SIZE

echo "== TEST BAT ENTRY POINTING BEFORE DATA AREA =="

echo "== corrupt image: point first cluster into the BAT table itself =="
poke_file_le "$TEST_IMG" $BAT_OFFSET 4 1

echo "== qemu-img check detects it without repairing =="
_check_test_img

echo "== bytes at the victim offset before write =="
echo "$(peek_file_le "$TEST_IMG" $VICTIM_OFFSET 4)"

echo "== write must allocate a fresh cluster instead of clobbering the BAT =="
{ $QEMU_IO -c "write -P 0x88 0 $SMALL_CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== bytes at the victim offset are unchanged =="
echo "$(peek_file_le "$TEST_IMG" $VICTIM_OFFSET 4)"

echo "== data reads back correctly =="
{ $QEMU_IO -r -c "read -P 0x88 0 $SMALL_CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $((64 * 1024 * 1024))

echo "== TEST REUSE OF PREALLOCATED SPACE =="

echo "== write 16 clusters, preallocating 16 of them at a time =="
opts=(--image-opts "driver=$IMGFMT,file.filename=$TEST_IMG,prealloc-size=16M")
for i in $(seq 0 15); do
    opts+=(-c "write -P 0x11 $(($i * $CLUSTER_SIZE)) $CLUSTER_SIZE")
done
# Die before close(), which would truncate the preallocated tail away
opts+=(-c "sigraise $(kill -l KILL)")
orig_io_options=$QEMU_IO_OPTIONS
QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT
echo "clusters written: `$QEMU_IO "${opts[@]}" 2>&1 | grep -c '^wrote'`"
QEMU_IO_OPTIONS=$orig_io_options

echo "== the space preallocated first must have been handed out since =="
file_size=`stat --printf="%s" "$TEST_IMG"`
echo "clusters behind the header: $(($file_size / $CLUSTER_SIZE - 1))"

# Clear image
_make_test_img $SIZE

echo "== the second cluster comes from the space preallocated for the first =="
{ $QEMU_IO -c "write -P 0x11 0 $CLUSTER_SIZE" \
           -c "write -P 0x22 $CLUSTER_SIZE $CLUSTER_SIZE" \
           "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== both of them survive the close =="
{ $QEMU_IO -r -c "read -P 0x11 0 $CLUSTER_SIZE" \
              -c "read -P 0x22 $CLUSTER_SIZE $CLUSTER_SIZE" \
              "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST A CLUSTER WHICH RUNS PAST THE END OF THE FILE =="

echo "== write two clusters =="
{ $QEMU_IO -c "write -P 0x11 0 $CLUSTER_SIZE" \
           -c "write -P 0x22 $CLUSTER_SIZE $CLUSTER_SIZE" \
           "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== cut the second one in half =="
file_size=`stat --printf="%s" "$TEST_IMG"`
truncate -s $((file_size - CLUSTER_SIZE / 2)) "$TEST_IMG"

echo "== the check completes and reports the cluster =="
_check_test_img

echo "== nothing can be reclaimed behind it =="
_check_test_img -r leaks
echo "file size: `stat --printf="%s" "$TEST_IMG"`"

echo "== a full repair drops the entry and truncates the image =="
_check_test_img -r all
echo "file size: `stat --printf="%s" "$TEST_IMG"`"

echo "== the first cluster survived =="
{ $QEMU_IO -c "read -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
_make_test_img $SIZE

echo "== TEST FORMAT EXTENSION IS NOT LEAKED SPACE =="

BITMAP=b2c9e1a4-5d3f-4e8b-9a7c-6f0d1e2b3a45

echo "== a name which is not a UUID can not be stored, so refuse it =="
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" bitmap0 2>&1 | _filter_testdir

echo "== add a persistent dirty bitmap =="
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir

# A dirty run shorter than a cluster is stored as a bitmap data cluster
# instead of the all-ones L1 entry, so the extension spans two clusters.
echo "== dirty a single granule of the bitmap =="
{ $QEMU_IO -c "write -P 0x11 0 65536" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

file_size=`stat --printf="%s" "$TEST_IMG"`
echo "file size: $file_size"

echo "== the bitmap is reported by qemu-img info =="
_img_info --format-specific

echo "== the extension and its bitmap data are not a leak =="
_check_test_img

echo "== extend image by 1 cluster =="
fallocate -xl $((file_size + CLUSTER_SIZE)) "$TEST_IMG"

echo "== a real leak is still reported =="
_check_test_img

echo "== repair reclaims the leak only =="
_check_test_img -r leaks

file_size=`stat --printf="%s" "$TEST_IMG"`
echo "file size: $file_size"

echo "== the bitmap survived the repair =="
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir

echo "== data survived the repair =="
{ $QEMU_IO -r -c "read -P 0x11 0 65536" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== dropping the bitmap makes the space reclaimable again =="
$QEMU_IMG bitmap --remove -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir

file_size=`stat --printf="%s" "$TEST_IMG"`
echo "file size: $file_size"

_check_test_img

# Clear image
_make_test_img $SIZE

echo "== TEST BITMAP OF AN IMAGE WHICH WAS NOT CLOSED =="

INUSE_OFFSET=44

echo "== add a persistent dirty bitmap and dirty it =="
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir
{ $QEMU_IO -c "write -P 0x11 0 65536" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== pretend the image was not closed correctly =="
poke_file "$TEST_IMG" "$INUSE_OFFSET" "\x59\x6e\x6f\x74"

echo "== a read-only open still reports it, marked inconsistent =="
_img_info --format-specific

echo "== the bitmap is stale, so it can not be used and is dropped =="
$QEMU_IMG bitmap --clear -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir

echo "== the name is free again =="
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir

echo "== guest data was never in doubt =="
{ $QEMU_IO -r -c "read -P 0x11 0 65536" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST BROKEN EXTENSION OF AN IMAGE WHICH WAS NOT CLOSED =="

EXT_OFF_OFFSET=56

echo "== add a persistent dirty bitmap =="
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir

ext_off=$(peek_file_le "$TEST_IMG" $EXT_OFF_OFFSET 8)

echo "== a guest write may reuse the extension cluster, so clobber it =="
poke_file "$TEST_IMG" $((ext_off * 512)) "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"

echo "== pretend the image was not closed correctly =="
poke_file "$TEST_IMG" "$INUSE_OFFSET" "\x59\x6e\x6f\x74"

echo "== the image opens, the extension is dropped =="
{ $QEMU_IO -c "write -P 0x11 0 64k" "$TEST_IMG"; } 2>&1 | _filter_qemu_io |
    _filter_testdir | _filter_generated_node_ids

echo "== and it stopped pointing at the broken extension =="
echo "ext_off: $(peek_file_le "$TEST_IMG" $EXT_OFF_OFFSET 8)"

echo "== an older qemu leaves the same damage behind a clean close =="
_make_test_img $SIZE
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir
ext_off=$(peek_file_le "$TEST_IMG" $EXT_OFF_OFFSET 8)
poke_file "$TEST_IMG" $((ext_off * 512)) "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
_img_info | _filter_generated_node_ids

echo "== truncating the extension away is dropped as well =="
_make_test_img $SIZE
$QEMU_IMG bitmap --add -f $IMGFMT "$TEST_IMG" $BITMAP 2>&1 | _filter_testdir
ext_off=$(peek_file_le "$TEST_IMG" $EXT_OFF_OFFSET 8)
truncate -s $((ext_off * 512)) "$TEST_IMG"
_img_info | _filter_generated_node_ids

# Clear image
_make_test_img $SIZE

echo "== TEST REPAIR OF A DUPLICATE ENTRY NEXT TO A LEAK =="

echo "== write two clusters =="
{ $QEMU_IO -c "write -P 0x11 0 $CLUSTER_SIZE" \
           -c "write -P 0x22 $CLUSTER_SIZE $CLUSTER_SIZE" \
           "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== point the second BAT entry at the first cluster =="
first=$(peek_file_le "$TEST_IMG" $BAT_OFFSET 4)
poke_file_le "$TEST_IMG" $(($BAT_OFFSET + 4)) 4 $first

echo "== leak a cluster at the end of the image =="
file_size=`stat --printf="%s" "$TEST_IMG"`
fallocate -xl $((file_size + CLUSTER_SIZE)) "$TEST_IMG"

echo "== both are repaired in one go =="
_check_test_img -r all

echo "== and the image opens read-write afterwards =="
{ $QEMU_IO -c "read -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

# Clear image
_make_test_img $SIZE

echo "== TEST A LEAK WHICH DOES NOT FIT AN INT =="

echo "== write one cluster =="
{ $QEMU_IO -c "write -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== leave 3 GiB of unused space behind it =="
truncate -s $((3 * 1024 * 1024 * 1024)) "$TEST_IMG"

echo "== closing the image truncates it without complaining =="
{ $QEMU_IO -c "read -P 0x11 0 $CLUSTER_SIZE" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

file_size=`stat --printf="%s" "$TEST_IMG"`
echo "file size: $file_size"

# Clear image
_make_test_img $SIZE

echo "== TEST A DUPLICATE IN THE LAST ALLOCATED BAT ENTRY =="

echo "== write two clusters =="
{ $QEMU_IO -c "write -P 0x11 0 $CLUSTER_SIZE" \
           -c "write -P 0x22 $CLUSTER_SIZE $CLUSTER_SIZE" \
           "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir

echo "== point the second entry at the first cluster =="
poke_file "$TEST_IMG" "$(($BAT_OFFSET + 4))" "\x01\x00\x00\x00"

echo "== the check completes and reports the duplicate =="
_check_test_img

# success, all done
echo "*** done"
rm -f $seq.full
status=0
