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