block/curl: Use BDRV_SECTOR_SIZE
[qemu/ar7.git] / tests / qemu-iotests / 171
blob257be10a0e7367a824085555ae2da5f68e6fa3f4
1 #!/bin/bash
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.
6 # Copyright (C) 2016 Red Hat, Inc.
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.
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.
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/>.
22 # creator
23 owner=tgolembi@redhat.com
25 seq=`basename $0`
26 echo "QA output created by $seq"
28 here=`pwd`
29 status=1 # failure is the default!
31 _cleanup()
33 _cleanup_test_img
35 trap "_cleanup; exit \$status" 0 1 2 3 15
37 # get standard environment, filters and checks
38 . ./common.rc
39 . ./common.filter
41 _supported_fmt raw
42 _supported_proto file
43 _supported_os Linux
46 # Create JSON with options
47 img_json() {
48 echo -n 'json:{"driver":"raw", '
49 echo -n "\"offset\":\"$img_offset\", "
50 if [ "$img_size" -ne -1 ] ; then
51 echo -n "\"size\":\"$img_size\", "
53 echo -n '"file": {'
54 echo -n '"driver":"file", '
55 echo -n "\"filename\":\"$TEST_IMG\" "
56 echo -n "} }"
59 do_general_test() {
60 if [ "$img_size" -ge 0 ] ; then
61 test_size=$img_size
62 else
63 test_size=$((size-img_offset))
66 echo
67 echo "write to image"
68 $QEMU_IO -c "write -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
70 echo
71 echo "read the image"
72 $QEMU_IO -c "read -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
74 echo
75 echo "check that offset is respected"
76 $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
78 echo
79 echo "write before image boundary"
80 $QEMU_IO -c "write $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
82 echo
83 echo "write across image boundary"
84 $QEMU_IO -c "write $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
86 echo
87 echo "write at image boundary"
88 $QEMU_IO -c "write $test_size 1" "$(img_json)" | _filter_qemu_io
90 echo
91 echo "write after image boundary"
92 $QEMU_IO -c "write $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
94 echo
95 echo "writev before/after image boundary"
96 $QEMU_IO -c "writev $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
98 echo
99 echo "read before image boundary"
100 $QEMU_IO -c "read $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
102 echo
103 echo "read across image boundary"
104 $QEMU_IO -c "read $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
106 echo
107 echo "read at image boundary"
108 $QEMU_IO -c "read $test_size 1" "$(img_json)" | _filter_qemu_io
110 echo
111 echo "read after image boundary"
112 $QEMU_IO -c "read $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
114 echo
115 echo "readv before/after image boundary"
116 $QEMU_IO -c "readv $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
118 echo
119 echo "fill image with pattern"
120 $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
122 echo
123 echo "write zeroes and check"
124 $QEMU_IO -c "write -z 0 512" "$(img_json)" | _filter_qemu_io
125 $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
127 echo
128 echo "write zeroes across image boundary"
129 $QEMU_IO -c "write -z $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
131 echo
132 echo "write zeroes at image boundary and check"
133 $QEMU_IO -c "write -z $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io
134 $QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io
135 $QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io
137 echo
138 echo "fill image with pattern"
139 $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
141 echo
142 echo "discard and check"
143 $QEMU_IO -c "discard 0 512" "$(img_json)" | _filter_qemu_io
144 $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
146 echo
147 echo "discard across image boundary"
148 $QEMU_IO -c "discard $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
150 echo
151 echo "discard at image boundary and check"
152 $QEMU_IO -c "discard $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io
153 $QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io
154 $QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io
157 echo
158 echo "== test 'offset' option =="
159 size=4096
160 img_offset=512
161 img_size=-1
162 _make_test_img $size
163 do_general_test
164 _cleanup_test_img
166 echo
167 echo "== test 'offset' and 'size' options =="
168 size=4096
169 img_offset=512
170 img_size=2048
171 _make_test_img $size
172 do_general_test
173 _cleanup_test_img
175 echo
176 echo "== test misaligned 'offset' =="
177 size=4096
178 img_offset=10
179 img_size=2048
180 _make_test_img $size
181 do_general_test
182 _cleanup_test_img
184 echo
185 echo "== test reopen =="
186 size=4096
187 img_offset=512
188 img_size=512
189 _make_test_img $size
191 $QEMU_IO "$(img_json)" <<EOT
192 write -P 0x0a 0 512
193 write -P 0x0a 511 1
194 write -P 0x0a 512 1
195 reopen -o driver=raw,offset=1536,size=1024
196 write -P 0x0a 0 1024
197 write -P 0x0a 1023 1
198 write -P 0x0a 1024 1
200 ) | _filter_qemu_io
201 echo "checking boundaries"
202 $QEMU_IO -c "read -v 510 4" $TEST_IMG | _filter_qemu_io
203 $QEMU_IO -c "read -v 1022 4" $TEST_IMG | _filter_qemu_io
204 $QEMU_IO -c "read -v 1534 4" $TEST_IMG | _filter_qemu_io
205 $QEMU_IO -c "read -v 2558 4" $TEST_IMG | _filter_qemu_io
206 _cleanup_test_img
208 # success, all done
209 echo
210 echo "*** done"
211 rm -f $seq.full
212 status=0