Fix typos in changelog.
[platform/upstream/cryptsetup.git] / tests / align-test
1 #!/bin/bash
2
3 CRYPTSETUP="../src/cryptsetup"
4 DEV=""
5 DEV_STACKED="luks0xbabe"
6
7 cleanup() {
8         udevadm settle >/dev/null 2>&1
9         [ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove $DEV_STACKED >/dev/null 2>&1
10         rmmod scsi_debug 2>/dev/null
11         sleep 2
12 }
13
14 fail()
15 {
16         echo "FAIL"
17         [ -n "$1" ] && echo "$1"
18         cleanup
19         exit 100
20 }
21
22 add_device() {
23         modprobe scsi_debug $@
24         if [ $? -ne 0 ] ; then
25                 echo "This kernel seems to not support proper scsi_debug module, test skipped."
26                 exit 0
27         fi
28
29         sleep 2
30         DEV=$(grep scsi_debug /sys/block/*/device/model | cut -f4 -d /)
31
32         if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
33                 echo "This kernel seems to not support topology info, test skipped."
34                 cleanup
35                 exit 0
36         fi
37
38         DEV="/dev/$DEV"
39         [ -b $DEV ] || fail "Cannot find $DEV."
40 }
41
42 format() # key_bits expected [forced]
43 {
44         if [ -z "$3" ] ; then
45                 echo -n "Formatting using topology info ($1 bits key)..."
46                 echo xxx| $CRYPTSETUP luksFormat $DEV -q -i1 -c aes-cbc-essiv:sha256 -s $1
47         else
48                 echo -n "Formatting using forced sector alignment $3 ($1 bits key)..."
49                 echo xxx| $CRYPTSETUP luksFormat $DEV -q -i1 -s $1 -c aes-cbc-essiv:sha256 --align-payload=$2
50         fi
51
52         ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
53         #echo "ALIGN = $ALIGN"
54
55         [ -z "$ALIGN" ] && fail
56         [ $ALIGN -ne $2 ] && fail "Expected alignment differs: expected $2 != detected $ALIGN"
57
58         # test some operation, just in case
59         echo -e "xxx\naaa" | $CRYPTSETUP luksAddKey $DEV -i1 --key-slot 1
60         [ $? -ne 0 ] && fail "Keyslot add failed."
61
62         $CRYPTSETUP -q luksKillSlot $DEV 1
63         [ $? -ne 0 ] && fail "Keyslot removal failed."
64
65         echo "PASSED"
66 }
67
68 get_offsets()
69 {
70         $CRYPTSETUP luksDump $DEV | grep "$1" | cut -s -d ':' -f 2 | sed  -e 's/\s//g' -e :a -e N -e 's/\n/:/g' -e 's/\s//g' -e ta
71 }
72
73 format_null()
74 {
75         if [ $3 -eq 0 ] ; then
76                 echo -n "Formatting using topology info ($1 bits key) [slot 0"
77                 echo xxx| $CRYPTSETUP luksFormat $DEV -q -i1 -c null -s $1
78         else
79                 echo -n "Formatting using forced sector alignment $3 ($1 bits key) [slot 0"
80                 echo xxx| $CRYPTSETUP luksFormat $DEV -q -i1 -c null -s $1 --align-payload=$3
81         fi
82
83         POFF=$(get_offsets "Payload offset")
84         [ -z "$POFF" ] && fail
85         [ $POFF != $2 ] && fail "Expected data offset differs: expected $2 != detected $POFF"
86         if [ -n "$4" ] ; then
87                 for j in 1 2 3 4 5 6 7 ; do
88                         echo -e "xxx\nblah$j" | $CRYPTSETUP luksAddKey $DEV -q -i1 --key-slot $j -c null $PARAMS
89                         echo -n $j
90                         [ $? -ne 0 ] && fail
91                 done
92
93                 KOFF=$(get_offsets "Key material offset")
94                 [ -z "$KOFF" ] && fail
95                 [ $KOFF != $4 ] && fail "Expected keyslots offsets differ: expected $4 != detected $KOFF"
96         fi
97
98         echo "]...PASSED"
99 }
100
101 if [ $(id -u) != 0 ]; then
102         echo "WARNING: You must be root to run this test, test skipped."
103         exit 0
104 fi
105
106 modprobe --dry-run scsi_debug || exit 0
107 cleanup
108
109 echo "# Create desktop-class 4K drive"
110 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
111 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1
112 format 256 4096
113 format 256 2112 8
114 format 128 2048
115 format 128 1088 8
116 format 256 8192 8192
117 format 128 8192 8192
118 cleanup
119
120 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
121 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
122 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
123 format 256 4103
124 format 256 2119 8
125 format 128 2055
126 format 128 1095 8
127 cleanup
128
129 echo "# Create enterprise-class 4K drive"
130 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
131 add_device dev_size_mb=16 sector_size=4096 num_tgts=1
132 format 256 4096
133 format 256 2560 8
134 format 128 2048
135 format 128 1536 8
136 cleanup
137
138 echo "# Create classic 512B drive and stack dm-linear"
139 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
140 add_device dev_size_mb=16 sector_size=512 num_tgts=1
141 DEV2=$DEV
142 DEV=/dev/mapper/$DEV_STACKED
143 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
144 format 256 4096
145 format 256 2112 8
146 format 128 2048
147 format 128 1088 8
148 format 128 8192 8192
149 cleanup
150
151 echo "# Offset check: 512B sector drive"
152 add_device dev_size_mb=16 sector_size=512 num_tgts=1
153 #           |k| expO reqO expected slot offsets
154 format_null  64 2048    0 8:72:136:200:264:328:392:456
155 format_null  64  520    1
156 format_null  64  520    8
157 format_null  64  640  128
158 format_null  64 2048 2048
159 format_null 128 2048    0 8:136:264:392:520:648:776:904
160 format_null 128 1032    1
161 format_null 128 1032    8
162 format_null 128 1152  128
163 format_null 128 2048 2048
164 format_null 256 4096    0 8:264:520:776:1032:1288:1544:1800
165 format_null 256 2056    1
166 format_null 256 2056    8
167 format_null 256 2176  128
168 format_null 256 4096 2048
169 format_null 512 4096    0 8:512:1016:1520:2024:2528:3032:3536
170 format_null 512 4040    1
171 format_null 512 4040    8
172 format_null 512 4096  128
173 format_null 512 4096 2048
174 cleanup
175
176 echo "# Offset check: 4096B sector drive"
177 add_device dev_size_mb=16 sector_size=4096 num_tgts=1
178 format_null  64 2048    0 8:72:136:200:264:328:392:456
179 format_null  64  520    1
180 format_null  64  520    8
181 format_null  64  640  128
182 format_null  64 2048 2048
183 format_null 128 2048    0 8:136:264:392:520:648:776:904
184 format_null 128 1032    1
185 format_null 128 1032    8
186 format_null 128 1152  128
187 format_null 128 2048 2048
188 format_null 256 4096    0 8:264:520:776:1032:1288:1544:1800
189 format_null 256 2056    1
190 format_null 256 2056    8
191 format_null 256 2176  128
192 format_null 256 4096 2048
193 format_null 512 4096    0 8:512:1016:1520:2024:2528:3032:3536
194 format_null 512 4040    1
195 format_null 512 4040    8
196 format_null 512 4096  128
197 format_null 512 4096 2048
198 cleanup