Imported upstream version 1.6.7
[platform/upstream/cryptsetup.git] / tests / device-test
1 #!/bin/bash
2
3 CRYPTSETUP="../src/cryptsetup"
4 MNT_DIR="./mnt_luks"
5 DEV_NAME="dummy"
6 PWD1="93R4P4pIqAH8"
7 PWD2="mymJeD8ivEhE"
8
9 cleanup() {
10         [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
11         udevadm settle >/dev/null 2>&1
12         if [ -d "$MNT_DIR" ] ; then
13             umount -f $MNT_DIR 2>/dev/null
14             rmdir $MNT_DIR 2>/dev/null
15         fi
16         sleep 2
17 }
18
19 fail()
20 {
21         if [ -n "$1" ] ; then echo "FAIL $1" ; else echo "FAIL" ; fi
22         cleanup
23         exit 100
24 }
25
26 skip()
27 {
28         echo "TEST SKIPPED: $1"
29         cleanup
30         exit 0
31 }
32
33 format() # key_bits expected [forced]
34 {
35         dd if=/dev/zero of=$DEV bs=1M count=5 >/dev/null 2>&1
36
37         echo $PWD1 | $CRYPTSETUP luksFormat $DEV -q -i1 -c aes-cbc-essiv:sha256
38         [ $? -ne 0 ] && fail "Format failed."
39
40         # test some operation, just in case
41         echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV -i1 --key-slot 1
42         [ $? -ne 0 ] && fail "Keyslot add failed."
43
44         $CRYPTSETUP -q luksKillSlot $DEV 1
45         [ $? -ne 0 ] && fail "Keyslot removal failed."
46 }
47
48 if [ $(id -u) != 0 ]; then
49         echo "WARNING: You must be root to run this test, test skipped."
50         exit 0
51 fi
52
53 [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
54
55 echo "[1] Using tmpfs for image"
56 DEV="$MNT_DIR/test.img"
57 mount -t tmpfs none $MNT_DIR || skip "Mounting tmpfs not available."
58 format
59
60 echo "[2] Kernel dmcrypt performace options"
61 echo -e "$PWD1" | $CRYPTSETUP open --type plain $DEV $DEV_NAME --perf-same_cpu_crypt >/dev/null 2>&1
62 if [ $? -ne 0 ] ; then
63         echo "TEST SKIPPED: dmcrypt options not available"
64 else
65         $CRYPTSETUP close $DEV_NAME || fail
66         # plain
67         echo -e "$PWD1" | $CRYPTSETUP open --type plain $DEV $DEV_NAME --perf-same_cpu_crypt --perf-submit_from_crypt_cpus || fail
68         $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
69         $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
70         $CRYPTSETUP close $DEV_NAME || fail
71         echo -e "$PWD1" | $CRYPTSETUP open --type plain $DEV $DEV_NAME --perf-same_cpu_crypt --allow-discards || fail
72         $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
73         $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
74         $CRYPTSETUP close $DEV_NAME || fail
75         # LUKS
76         echo -e "$PWD1" | $CRYPTSETUP open --type luks1 $DEV $DEV_NAME --perf-same_cpu_crypt --perf-submit_from_crypt_cpus || fail
77         $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
78         $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
79         $CRYPTSETUP close $DEV_NAME || fail
80         echo -e "$PWD1" | $CRYPTSETUP open --type luks1 $DEV $DEV_NAME --perf-same_cpu_crypt --allow-discards || fail
81         $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
82         $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
83         $CRYPTSETUP close $DEV_NAME || fail
84 fi
85
86 cleanup