xref: /openbmc/qemu/tests/qemu-iotests/171 (revision 7f709ce7)
1#!/bin/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
28here=`pwd`
29status=1	# failure is the default!
30
31_cleanup()
32{
33    _cleanup_test_img
34}
35trap "_cleanup; exit \$status" 0 1 2 3 15
36
37# get standard environment, filters and checks
38. ./common.rc
39. ./common.filter
40
41_supported_fmt raw
42_supported_proto file
43_supported_os Linux
44
45
46# Create JSON with options
47img_json() {
48    printf %s 'json:{"driver":"raw", '
49    printf %s "\"offset\":\"$img_offset\", "
50    if [ "$img_size" -ne -1 ] ; then
51        printf %s "\"size\":\"$img_size\", "
52    fi
53    printf %s '"file": {'
54    printf %s   '"driver":"file", '
55    printf %s   "\"filename\":\"$TEST_IMG\" "
56    printf %s "} }"
57}
58
59do_general_test() {
60    if [ "$img_size" -ge 0 ] ; then
61        test_size=$img_size
62    else
63        test_size=$((size-img_offset))
64    fi
65
66    echo
67    echo "write to image"
68    $QEMU_IO -c "write -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
69
70    echo
71    echo "read the image"
72    $QEMU_IO -c "read -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io
73
74    echo
75    echo "check that offset is respected"
76    $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io
77
78    echo
79    echo "write before image boundary"
80    $QEMU_IO -c "write $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
81
82    echo
83    echo "write across image boundary"
84    $QEMU_IO -c "write $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
85
86    echo
87    echo "write at image boundary"
88    $QEMU_IO -c "write $test_size 1" "$(img_json)" | _filter_qemu_io
89
90    echo
91    echo "write after image boundary"
92    $QEMU_IO -c "write $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
93
94    echo
95    echo "writev before/after image boundary"
96    $QEMU_IO -c "writev $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
97
98    echo
99    echo "read before image boundary"
100    $QEMU_IO -c "read $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io
101
102    echo
103    echo "read across image boundary"
104    $QEMU_IO -c "read $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
105
106    echo
107    echo "read at image boundary"
108    $QEMU_IO -c "read $test_size 1" "$(img_json)" | _filter_qemu_io
109
110    echo
111    echo "read after image boundary"
112    $QEMU_IO -c "read $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io
113
114    echo
115    echo "readv before/after image boundary"
116    $QEMU_IO -c "readv $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io
117
118    echo
119    echo "fill image with pattern"
120    $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
121
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
126
127    echo
128    echo "write zeroes across image boundary"
129    $QEMU_IO -c "write -z $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
130
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
136
137    echo
138    echo "fill image with pattern"
139    $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io
140
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
145
146    echo
147    echo "discard across image boundary"
148    $QEMU_IO -c "discard $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io
149
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
155}
156
157echo
158echo "== test 'offset' option =="
159size=4096
160img_offset=512
161img_size=-1
162_make_test_img $size
163do_general_test
164_cleanup_test_img
165
166echo
167echo "== test 'offset' and 'size' options =="
168size=4096
169img_offset=512
170img_size=2048
171_make_test_img $size
172do_general_test
173_cleanup_test_img
174
175echo
176echo "== test misaligned 'offset' =="
177size=4096
178img_offset=10
179img_size=2048
180_make_test_img $size
181do_general_test
182_cleanup_test_img
183
184echo
185echo "== test reopen =="
186size=4096
187img_offset=512
188img_size=512
189_make_test_img $size
190(
191$QEMU_IO "$(img_json)"  <<EOT
192write -P 0x0a 0 512
193write -P 0x0a 511 1
194write -P 0x0a 512 1
195reopen -o driver=raw,offset=1536,size=1024
196write -P 0x0a 0 1024
197write -P 0x0a 1023 1
198write -P 0x0a 1024 1
199EOT
200) | _filter_qemu_io
201echo "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
207
208# success, all done
209echo
210echo "*** done"
211rm -f $seq.full
212status=0
213