Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-disk2data.sh
blob2644b9d1e20aa29372643e104bb104f5fcf743f8
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 the plugins/data/disk2data.pl script.
35 # This is a stochastic test to try to find corner cases. However if
36 # we simply used a random disk then we wouldn't be testing the
37 # particular features of this script, namely that it can skip long
38 # runs of zeroes and compress short-period repeated data. So instead
39 # we have a small script which generates this kind of "disk-like"
40 # data.
42 source ./functions.sh
43 set -e
44 set -x
46 requires_run
47 requires perl --version
48 requires $PYTHON -c 'import random'
49 requires_nbdsh_uri
50 requires_nbdcopy
51 requires hexdump --version
53 disk2data=$srcdir/../plugins/data/disk2data.pl
54 requires test -x $disk2data
56 disk=test-disk2data.disk
57 cmd=test-disk2data.cmd
58 out=test-disk2data.out
59 cleanup_fn rm -f $disk $cmd $out
60 rm -f $disk $cmd $out
62 export disk=$disk
63 $PYTHON -c '
64 import os
65 import random
67 pop = []
68 # Zeroes.
69 pop.append([0]*3)
70 pop.append([0]*10)
71 pop.append([0]*10)
72 pop.append([0]*10)
73 pop.append([0]*50)
74 pop.append([0]*100)
75 pop.append([0]*200)
76 pop.append([0]*300)
77 pop.append([0]*500)
78 pop.append([0]*1000)
79 pop.append([0]*10000)
80 # Non-zero periodic sequences.
81 pop.append([1]*10)
82 pop.append([1,2]*10)
83 pop.append([1,2,3]*10)
84 pop.append([1,2,3,4]*10)
85 pop.append([1,2,3,4,5]*10)
86 pop.append([1,2,3,4,5,6]*10)
87 pop.append([1,2,3,4,5,6,7]*10)
88 pop.append([1,2,3,4,5,6,7,8]*10)
89 pop.append([1,2,3,4,5,6,7,8,9,10]*10) # too long for the script to detect
90 # Random non-repeating data sequences.
91 pop.append([1,4,5,2,9,8,3,5,3,1,3,4,5])
92 pop.append([9,7,5,3,1,2,4,6,8])
94 len = random.randint(0, 20)
95 choices = random.choices(pop, k=len)
96 with open(os.environ["disk"], "w") as f:
97 for i in choices:
98 f.write(bytearray(i).decode("ascii"))
100 hexdump -C $disk
102 # Run the script to convert the disk to an nbdkit data command.
103 $disk2data $disk > $cmd
105 # Modify the generated nbdkit command.
106 $SED -i -e $'s/^nbdkit /nbdkit -U - --run \'nbdcopy "$uri" -\' /' $cmd
107 chmod +x $cmd
108 cat $cmd
110 # Run the command. It should re-generate the original disk image.
111 ./$cmd > $out
112 hexdump -C $out
114 # Compare the original disk with the output.
115 cmp $disk $out