qemu/tests/qemu-iotests/171
Eric Blake b43671f80c tests: Avoid non-portable 'echo -ARG'
POSIX says that backslashes in the arguments to 'echo', as well as
any use of 'echo -n' and 'echo -e', are non-portable; it recommends
people should favor 'printf' instead.  This is definitely true where
we do not control which shell is running (such as in makefile snippets
or in documentation examples).  But even for scripts where we
require bash (and therefore, where echo does what we want by default),
it is still possible to use 'shopt -s xpg_echo' to change bash's
behavior of echo.  And setting a good example never hurts when we are
not sure if a snippet will be copied from a bash-only script to a
general shell script (although I don't change the use of non-portable
\e for ESC when we know the running shell is bash).

Replace 'echo -n "..."' with 'printf %s "..."', and 'echo -e "..."'
with 'printf %b "...\n"', with the optimization that the %s/%b
argument can be omitted if the string being printed is a strict
literal with no '%', '$', or '`' (we could technically also make
this optimization when there are $ or `` substitutions but where
we can prove their results will not be problematic, but proving
that such substitutions are safe makes the patch less trivial
compared to just being consistent).

In the qemu-iotests check script, fix unusual shell quoting
that would result in word-splitting if 'date' outputs a space.

In test 051, take an opportunity to shorten the line.

In test 068, get rid of a pointless second invocation of bash.

CC: qemu-trivial@nongnu.org
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-id: 20170703180950.9895-1-eblake@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
2017-07-11 17:45:00 +02:00

213 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
#
# Test 'offset' and 'size' options of the raw driver. Make sure we can't
# (or can) read and write outside of the image size.
#
# Copyright (C) 2016 Red Hat, Inc.
#
# 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=tgolembi@redhat.com
seq=`basename $0`
echo "QA output created by $seq"
here=`pwd`
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 raw
_supported_proto file
_supported_os Linux
# Create JSON with options
img_json() {
printf %s 'json:{"driver":"raw", '
printf %s "\"offset\":\"$img_offset\", "
if [ "$img_size" -ne -1 ] ; then
printf %s "\"size\":\"$img_size\", "
fi
printf %s '"file": {'
printf %s '"driver":"file", '
printf %s "\"filename\":\"$TEST_IMG\" "
printf %s "} }"
}
do_general_test() {
if [ "$img_size" -ge 0 ] ; then
test_size=$img_size
else
test_size=$((size-img_offset))
fi
echo
echo "write to image"
$QEMU_IO -c "write -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
echo
echo "read the image"
$QEMU_IO -c "read -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
echo
echo "check that offset is respected"
$QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
echo
echo "write before image boundary"
$QEMU_IO -c "write $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
echo
echo "write across image boundary"
$QEMU_IO -c "write $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
echo
echo "write at image boundary"
$QEMU_IO -c "write $test_size 1" "$(img_json)" | _filter_qemu_io
echo
echo "write after image boundary"
$QEMU_IO -c "write $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
echo
echo "writev before/after image boundary"
$QEMU_IO -c "writev $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
echo
echo "read before image boundary"
$QEMU_IO -c "read $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
echo
echo "read across image boundary"
$QEMU_IO -c "read $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
echo
echo "read at image boundary"
$QEMU_IO -c "read $test_size 1" "$(img_json)" | _filter_qemu_io
echo
echo "read after image boundary"
$QEMU_IO -c "read $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
echo
echo "readv before/after image boundary"
$QEMU_IO -c "readv $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
echo
echo "fill image with pattern"
$QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
echo
echo "write zeroes and check"
$QEMU_IO -c "write -z 0 512" "$(img_json)" | _filter_qemu_io
$QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
echo
echo "write zeroes across image boundary"
$QEMU_IO -c "write -z $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
echo
echo "write zeroes at image boundary and check"
$QEMU_IO -c "write -z $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io
$QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io
$QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io
echo
echo "fill image with pattern"
$QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
echo
echo "discard and check"
$QEMU_IO -c "discard 0 512" "$(img_json)" | _filter_qemu_io
$QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
echo
echo "discard across image boundary"
$QEMU_IO -c "discard $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
echo
echo "discard at image boundary and check"
$QEMU_IO -c "discard $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io
$QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io
$QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io
}
echo
echo "== test 'offset' option =="
size=4096
img_offset=512
img_size=-1
_make_test_img $size
do_general_test
_cleanup_test_img
echo
echo "== test 'offset' and 'size' options =="
size=4096
img_offset=512
img_size=2048
_make_test_img $size
do_general_test
_cleanup_test_img
echo
echo "== test misaligned 'offset' =="
size=4096
img_offset=10
img_size=2048
_make_test_img $size
do_general_test
_cleanup_test_img
echo
echo "== test reopen =="
size=4096
img_offset=512
img_size=512
_make_test_img $size
(
$QEMU_IO "$(img_json)" <<EOT
write -P 0x0a 0 512
write -P 0x0a 511 1
write -P 0x0a 512 1
reopen -o driver=raw,offset=1536,size=1024
write -P 0x0a 0 1024
write -P 0x0a 1023 1
write -P 0x0a 1024 1
EOT
) | _filter_qemu_io
echo "checking boundaries"
$QEMU_IO -c "read -v 510 4" $TEST_IMG | _filter_qemu_io
$QEMU_IO -c "read -v 1022 4" $TEST_IMG | _filter_qemu_io
$QEMU_IO -c "read -v 1534 4" $TEST_IMG | _filter_qemu_io
$QEMU_IO -c "read -v 2558 4" $TEST_IMG | _filter_qemu_io
_cleanup_test_img
# success, all done
echo
echo "*** done"
rm -f $seq.full
status=0