xref: /qemu/tests/qemu-iotests/171 (revision f917eed3)
1#!/usr/bin/env bash
2#
3# Test 'offset' and 'size' options of the raw driver. Make sure we can't
4# (or can) read and write outside of the image size.
5#
6# Copyright (C) 2016 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22# creator
23owner=tgolembi@redhat.com
24
25seq=`basename $0`
26echo "QA output created by $seq"
27
28status=1	# failure is the default!
29
30_cleanup()
31{
32    _cleanup_test_img
33}
34trap "_cleanup; exit \$status" 0 1 2 3 15
35
36# get standard environment, filters and checks
37. ./common.rc
38. ./common.filter
39
40_supported_fmt raw
41_supported_proto file fuse
42_supported_os Linux
43
44
45# Create JSON with options
46img_json() {
47    printf %s 'json:{"driver":"raw", '
48    printf %s "\"offset\":\"$img_offset\", "
49    if [ "$img_size" -ne -1 ] ; then
50        printf %s "\"size\":\"$img_size\", "
51    fi
52    printf %s '"file": {'
53    printf %s   '"driver":"file", '
54    printf %s   "\"filename\":\"$TEST_IMG\" "
55    printf %s "} }"
56}
57
58do_general_test() {
59    if [ "$img_size" -ge 0 ] ; then
60        test_size=$img_size
61    else
62        test_size=$((size-img_offset))
63    fi
64
65    echo
66    echo "write to image"
67    $QEMU_IO -c "write -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
68
69    echo
70    echo "read the image"
71    $QEMU_IO -c "read -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
72
73    echo
74    echo "check that offset is respected"
75    $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
76
77    echo
78    echo "write before image boundary"
79    $QEMU_IO -c "write $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
80
81    echo
82    echo "write across image boundary"
83    $QEMU_IO -c "write $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
84
85    echo
86    echo "write at image boundary"
87    $QEMU_IO -c "write $test_size 1" "$(img_json)" | _filter_qemu_io
88
89    echo
90    echo "write after image boundary"
91    $QEMU_IO -c "write $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
92
93    echo
94    echo "writev before/after image boundary"
95    $QEMU_IO -c "writev $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
96
97    echo
98    echo "read before image boundary"
99    $QEMU_IO -c "read $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
100
101    echo
102    echo "read across image boundary"
103    $QEMU_IO -c "read $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
104
105    echo
106    echo "read at image boundary"
107    $QEMU_IO -c "read $test_size 1" "$(img_json)" | _filter_qemu_io
108
109    echo
110    echo "read after image boundary"
111    $QEMU_IO -c "read $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
112
113    echo
114    echo "readv before/after image boundary"
115    $QEMU_IO -c "readv $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
116
117    echo
118    echo "fill image with pattern"
119    $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
120
121    echo
122    echo "write zeroes and check"
123    $QEMU_IO -c "write -z 0 512" "$(img_json)" | _filter_qemu_io
124    $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
125
126    echo
127    echo "write zeroes across image boundary"
128    $QEMU_IO -c "write -z $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
129
130    echo
131    echo "write zeroes at image boundary and check"
132    $QEMU_IO -c "write -z $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io
133    $QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io
134    $QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io
135
136    echo
137    echo "fill image with pattern"
138    $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
139
140    echo
141    echo "discard and check"
142    $QEMU_IO -c "discard 0 512" "$(img_json)" | _filter_qemu_io
143    $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
144
145    echo
146    echo "discard across image boundary"
147    $QEMU_IO -c "discard $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
148
149    echo
150    echo "discard at image boundary and check"
151    $QEMU_IO -c "discard $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io
152    $QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io
153    $QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io
154}
155
156echo
157echo "== test 'offset' option =="
158size=4096
159img_offset=512
160img_size=-1
161_make_test_img $size
162do_general_test
163_cleanup_test_img
164
165echo
166echo "== test 'offset' and 'size' options =="
167size=4096
168img_offset=512
169img_size=2048
170_make_test_img $size
171do_general_test
172_cleanup_test_img
173
174echo
175echo "== test misaligned 'offset' =="
176size=4096
177img_offset=10
178img_size=2048
179_make_test_img $size
180do_general_test
181_cleanup_test_img
182
183echo
184echo "== test reopen =="
185size=4096
186img_offset=512
187img_size=512
188_make_test_img $size
189(
190$QEMU_IO "$(img_json)"  <<EOT
191write -P 0x0a 0 512
192write -P 0x0a 511 1
193write -P 0x0a 512 1
194reopen -o driver=raw,offset=1536,size=1024
195write -P 0x0a 0 1024
196write -P 0x0a 1023 1
197write -P 0x0a 1024 1
198EOT
199) | _filter_qemu_io
200echo "checking boundaries"
201$QEMU_IO -c "read -v 510 4" $TEST_IMG | _filter_qemu_io
202$QEMU_IO -c "read -v 1022 4" $TEST_IMG | _filter_qemu_io
203$QEMU_IO -c "read -v 1534 4" $TEST_IMG | _filter_qemu_io
204$QEMU_IO -c "read -v 2558 4" $TEST_IMG | _filter_qemu_io
205_cleanup_test_img
206
207# success, all done
208echo
209echo "*** done"
210rm -f $seq.full
211status=0
212