#!/usr/bin/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

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