Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-offset-truncate.sh
blobb09c89e7f50ee568877f75046d46885403291c7f
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright Red Hat
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
33 # Test offset and truncate filter and various corner cases and error
34 # handling between layers.
36 source ./functions.sh
37 set -e
38 set -x
40 requires_run
41 requires_nbdinfo
42 requires_nbdsh_uri
44 function do_test_info ()
46 nbdkit -U - --filter=offset --filter=truncate pattern size=1024 \
47 "$@" --run 'nbdinfo $nbd'
50 function do_test_read512 ()
52 nbdkit -U - --filter=offset --filter=truncate pattern size=1024 \
53 "$@" --run 'nbdsh -u "$uri" -c "
54 try:
55 h.pread(512, 0)
56 except nbd.Error:
57 exit(1)
61 function do_test_zero512 ()
63 nbdkit -U - --filter=offset --filter=truncate memory size=1024 \
64 "$@" --run 'nbdsh -u "$uri" -c "
65 try:
66 h.zero(512, 0)
67 except nbd.Error:
68 exit (1)
72 function expect_fail ()
74 if "$@"; then
75 echo "$0: expected this test to fail"
76 exit 1
80 # Not a test, just check we can combine the two filters and the test
81 # harness works.
82 do_test_info
83 expect_fail do_test_info bleah
85 # Test a non-sector-aligned offset. For nbdkit this should
86 # not make any difference.
87 do_test_info offset=1 range=512
89 # Reading beyond the end of the underlying image, even by 1 byte,
90 # should fail.
91 expect_fail do_test_info offset=513 range=512
93 # Can we extend the image by 1 byte and use the same offset as the
94 # previous test? This should be fine.
95 do_test_info truncate=1025 offset=513 range=512
97 # Truncate the image. Offsets larger than the truncation should fail,
98 # even though the underlying image is big enough.
99 expect_fail do_test_info truncate=513 offset=2 range=512
101 # Both truncation and the offset filter range option should also
102 # prevent operations beyond the end of the truncated size even if the
103 # underlying image is big enough.
104 do_test_read512
105 do_test_zero512
106 expect_fail do_test_read512 truncate=511
107 expect_fail do_test_zero512 truncate=511
108 expect_fail do_test_read512 truncate=511 range=512
109 expect_fail do_test_zero512 truncate=511 range=512
110 expect_fail do_test_read512 range=511
111 expect_fail do_test_zero512 range=511
113 # Reading from an offset much larger than the underlying image should
114 # work if we extended it big enough.
115 do_test_info truncate=4096 offset=2047 range=512
117 # But an offset even larger should fail.
118 expect_fail do_test_info truncate=4096 offset=$((4096-511)) range=512
120 # An offset beyond the end of the disk should fail, whether it's the
121 # underlying disk or a truncated disk.
122 expect_fail do_test_info offset=1025 range=512
123 expect_fail do_test_info truncate=4096 offset=4098 range=512