Makefile: Add security compiling option (RELRO, SC, and FORTIFY)
[platform/upstream/cryptsetup.git] / tests / discards-test
1 #!/bin/bash
2
3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
5 DEV_NAME="discard-t3st"
6 DEV=""
7 PWD1="93R4P4pIqAH8"
8
9 CRYPTSETUP_VALGRIND=../.libs/cryptsetup
10 CRYPTSETUP_LIB_VALGRIND=../.libs
11
12 cleanup() {
13         [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove --retry $DEV_NAME
14         udevadm settle >/dev/null 2>&1
15         rmmod scsi_debug >/dev/null 2>&1
16         sleep 2
17 }
18
19 fail()
20 {
21         echo "FAILED backtrace:"
22         while caller $frame; do ((frame++)); done
23         cleanup
24         exit 100
25 }
26
27 skip()
28 {
29         [ -n "$1" ] && echo "$1"
30         exit 77
31 }
32
33 function valgrind_setup()
34 {
35         command -v valgrind >/dev/null || fail "Cannot find valgrind."
36         [ ! -f $CRYPTSETUP_VALGRIND ] && fail "Unable to get location of cryptsetup executable."
37         export LD_LIBRARY_PATH="$CRYPTSETUP_LIB_VALGRIND:$LD_LIBRARY_PATH"
38 }
39
40 function valgrind_run()
41 {
42         INFOSTRING="$(basename ${BASH_SOURCE[1]})-line-${BASH_LINENO[0]}" ./valg.sh ${CRYPTSETUP_VALGRIND} "$@"
43 }
44
45 add_device() {
46         rmmod scsi_debug >/dev/null 2>&1
47         if [ -d /sys/module/scsi_debug ] ; then
48                 echo "Cannot use scsi_debug module (in use or compiled-in), test skipped."
49                 exit 77
50         fi
51         modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
52         if [ $? -ne 0 ] ; then
53                 echo "This kernel seems to not support proper scsi_debug module, test skipped."
54                 exit 77
55         fi
56
57         sleep 2
58         DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
59
60         DEV="/dev/$DEV"
61         [ -b $DEV ] || fail "Cannot find $DEV."
62 }
63
64 function check_version()
65 {
66         VER_STR=$(dmsetup targets | grep crypt | cut -f 2 -dv)
67         [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
68
69         VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
70         VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
71
72         # option supported in 1.11
73         test $VER_MAJ -gt 1 && return 0
74         test $VER_MIN -ge 11 && return 0
75         return 1
76 }
77
78 [ ! -x "$CRYPTSETUP" ] && skip "Cannot find $CRYPTSETUP, test skipped."
79 [ -n "$VALG" ] && valgrind_setup && CRYPTSETUP=valgrind_run
80 if [ $(id -u) != 0 ]; then
81         echo "WARNING: You must be root to run this test, test skipped."
82         exit 77
83 fi
84
85 modprobe dm-crypt >/dev/null 2>&1
86 if ! check_version ; then
87         echo "Probably old kernel, test skipped."
88         exit 77
89 fi
90
91 add_device dev_size_mb=16 sector_size=512 num_tgts=1 lbpu=1
92
93 # FIXME test hash of device (unmap -> zero)
94 # for now just check that flag is enabled
95
96 echo "[1] Allowing discards for LUKS device"
97 echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q -i1 || fail
98 echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME --allow-discards || fail
99 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
100 $CRYPTSETUP resize $DEV_NAME --size 100 || fail
101 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
102 dmsetup table $DEV_NAME | grep allow_discards >/dev/null || fail
103 $CRYPTSETUP luksClose $DEV_NAME || fail
104
105 echo "[2] Allowing discards for plain device"
106 echo $PWD1 | $CRYPTSETUP create -q $DEV_NAME $DEV --hash sha256 --allow-discards || fail
107 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
108 $CRYPTSETUP resize $DEV_NAME --size 100 || fail
109 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
110 dmsetup table $DEV_NAME | grep allow_discards >/dev/null || fail
111 $CRYPTSETUP remove $DEV_NAME || fail
112
113 cleanup