Bump to ecryptfs-utils 111
[platform/upstream/ecryptfs-utils.git] / tests / run_tests.sh
1 #!/bin/bash
2 #
3 # eCryptfs test suite harness
4 # Author: Tyler Hicks <tyhicks@canonical.com>
5 #
6 # Copyright (C) 2012 Canonical, Ltd.
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 #
22 # Example usage:
23 #
24 # # ./tests/run_tests.sh -K -c destructive -d /dev/vdb -l /lower -u /upper
25 #
26 # This would run kernel tests in the destructive category, as defined in
27 # kernel/tests.rc. /dev/vdb would be the block device containing the lower
28 # filesystem, which would be mounted at /lower. The eCryptfs mount point would
29 # be /upper.
30 #
31
32 run_tests_dir=$(dirname $0)
33 rc=1
34
35 . ${run_tests_dir}/lib/etl_funcs.sh
36
37 blocks=0
38 categories=""
39 cleanup_lower_mnt=0
40 cleanup_upper_mnt=0
41 default_lower_fses="ext4"
42 device=""
43 disk_dir=""
44 failed=0
45 kernel=false
46 ktests=""
47 lower_fses=""
48 lower_mnt=""
49 passed=0
50 tests=""
51 upper_mnt=""
52 userspace=false
53 utests=""
54
55 run_tests_cleanup()
56 {
57         if [ $cleanup_upper_mnt -ne 0 ] && [ -n "$upper_mnt" ]; then
58                 rm -rf "$upper_mnt"
59         fi
60         if [ $cleanup_lower_mnt -ne 0 ] && [ -n "$lower_mnt" ]; then
61                 rm -rf "$lower_mnt"
62         fi
63         etl_remove_disk
64         exit $rc
65 }
66 trap run_tests_cleanup 0 1 2 3 15
67
68 run_tests()
69 {
70         test_dir=$1
71         tests=$2
72
73         for etest in $tests; do
74                 printf "%-16s\t" $(basename "$etest" .sh)
75
76                 ${test_dir}/${etest}
77                 if [ $? -ne 0 ]; then
78                         ((failed++))
79                         printf "FAIL\n"
80                 else
81                         ((passed++))
82                         printf "pass\n"
83                 fi
84         done
85 }
86
87 run_kernel_tests_on_existing_device()
88 {
89         echo "Running eCryptfs filesystem tests"
90
91         run_tests "${run_tests_dir}/kernel" "$ktests"
92         if [ $? -ne 0 ]; then
93                 echo "Failed to run eCryptfs filesystem tests" 1>&2
94                 rc=1
95                 exit
96         fi
97
98         if [ -n "$ETL_DISK" ]; then
99                 etl_remove_disk
100         fi
101 }
102
103 run_kernel_tests_on_created_disk_image()
104 {
105         lower_fses=$(echo $lower_fses | tr ',' ' ')
106         for lower_fs in $lower_fses; do
107                 echo "Running eCryptfs filesystem tests on $lower_fs"
108
109                 if [ "$blocks" -gt 0 ]; then
110                         export ETL_LFS=$lower_fs
111                         etl_create_disk $blocks $disk_dir
112                         if [ $? -ne 0 ]; then
113                                 echo "Failed to create disk for $lower_fs"\
114                                      "(skipping all tests on $lower_fs)" 1>&2
115                                 continue
116                         fi
117                         export ETL_LMOUNT_SRC=$ETL_DISK
118                 fi
119
120                 run_tests "${run_tests_dir}/kernel" "$ktests"
121                 if [ $? -ne 0 ]; then
122                         echo "Failed to run eCryptfs filesystem tests on"\
123                              "$lower_fs" 1>&2
124                         rc=1
125                         exit
126                 fi
127
128                 if [ -n "$ETL_DISK" ]; then
129                         etl_remove_disk
130                 fi
131         done
132 }
133
134 usage()
135 {
136         echo "Usage: $(basename $0) [options] -K -c categories -b blocks"
137         echo "  or:  $(basename $0) [options] -K -c categories -d device"
138         echo "  or:  $(basename $0) [options] -U -c categories"
139         echo "  or:  $(basename $0) [options] -K -U -c categories -b blocks"
140         echo
141         echo "eCryptfs test harness"
142         echo
143         echo "  -b blocks       number of 1K blocks used when creating backing "
144         echo "          disk for lower filesystem (not compatible "
145         echo "          with -d)"
146         echo "  -c categories   comma-separated test categories" \
147                                 "(e.g., -c safe,destructive)"
148         echo "  -D disk_dir     directory used to store created backing disk "
149         echo "          when using -b (not compatible with -d)"
150         echo "  -d device       backing device to mount lower filesystem, such "
151         echo "          as /dev/sdd3 (not compatible with -b)"
152         echo "  -f lower_fses   comma-separated lower filesystem types" \
153                                 "(e.g., -f ext4,btrfs)"
154         echo "          defaults to $default_lower_fses" \
155                         "(not compatible with -d)"
156         echo "  -h              display this help and exit"
157         echo "  -K              run tests relating to the kernel module"
158         echo "  -l lower_mnt    destination path to mount lower filesystem"
159         echo "  -t tests        comma-separated list of tests to run"
160         echo "  -U              run tests relating to the userspace utilities"
161         echo "  -u upper_mnt    destination path to mount upper filesystem"
162 }
163
164 while getopts "b:c:D:d:f:hKl:t:Uu:" opt; do
165         case $opt in
166         b)
167                 blocks=$OPTARG
168                 ;;
169         c)
170                 categories=$OPTARG
171                 ;;
172         d)
173                 device=$OPTARG
174                 ;;
175         D)
176                 disk_dir=$OPTARG
177                 ;;
178         f)
179                 lower_fses=$OPTARG
180                 ;;
181         h)
182                 usage
183                 rc=0
184                 exit
185                 ;;
186         K)
187                 kernel=true
188                 ;;
189         l)
190                 lower_mnt=$OPTARG
191                 ;;
192         t)
193                 tests=$OPTARG
194                 ;;
195         U)
196                 userspace=true
197                 ;;
198         u)
199                 upper_mnt=$OPTARG
200                 ;;
201         \?)
202                 usage 1>&2
203                 exit
204                 ;;
205         :)
206                 usage 1>&2
207                 exit
208                 ;;
209         esac
210 done
211
212 if ! $kernel && ! $userspace ; then
213         # Must specify at least one of these
214         echo "Must specify one of -U or -K" 1>&2
215         usage 1>&2
216         exit
217 elif [ -z "$categories" ] && [ -z "$tests" ]; then
218         # Must either specify test categories or specific tests
219         echo "Must specify a list of test categories or a list of tests" 1>&2
220         usage 1>&2
221         exit
222 fi
223
224 if $kernel ; then
225         if [ "$blocks" -lt 1 ] && [ -z "$device" ]; then
226                 # Must specify blocks for disk creation *or* an existing device
227                 echo "Blocks for disk creation or an existing device must be" \
228                      "specified" 1>&2
229                 usage 1>&2
230                 exit
231         elif [ "$blocks" -gt 0 ] && [ -n "$device" ]; then
232                 # Can't specify blocks for disk *and* an existing device 
233                 echo "Cannot specify blocks for disk creation *and* also an" \
234                      "existing device" 1>&2
235                 usage 1>&2
236                 exit
237         elif [ -n "$disk_dir" ] && [ -n "$device" ]; then
238                 # Can't specify a dir for disk creation and an existing device
239                 echo "Cannot specify a directory for disk creation *and* also" \
240                      "an existing device" 1>&2
241                 usage 1>&2
242                 exit
243         elif [ -n "$device" ] && [ ! -b "$device" ]; then
244                 # A small attempt at making sure we're dealing with a block dev
245                 echo "Backing device must be a valid block device" 1>&2
246                 usage 1>&2
247                 exit
248         elif [ -n "$device" ] && [ -n "$lower_fses" ]; then
249                 # We currently don't reformat block devices so we shouldn't
250                 # accept a list of lower filesystems to test on
251                 echo "Lower filesystems cannot be specified when using" \
252                      "existing block devices" 1>&2
253                 usage 1>&2
254                 exit
255         elif [ -n "$lower_mnt" ] && [ ! -d "$lower_mnt" ]; then
256                 # A small attempt at making sure we're dealing with directories
257                 echo "Lower mount point must exist" 1>&2
258                 usage 1>&2
259                 exit
260         elif [ -n "$upper_mnt" ] && [ ! -d "$upper_mnt" ]; then
261                 # A small attempt at making sure we're dealing with directories
262                 echo "Upper mount point must exist" 1>&2
263                 usage 1>&2
264                 exit
265         elif [ -n "$disk_dir" ] && [ ! -d "$disk_dir" ]; then
266                 # A small attempt at making sure we're dealing with a directory
267                 echo "Directory used to store created backing disk must" \
268                      "exist" 1>&2
269                 usage 1>&2
270                 exit
271         fi
272 fi
273
274 if [ -n "$device" ]; then
275         export ETL_LMOUNT_SRC=$device
276 elif [ -z "$lower_fses" ]; then
277         lower_fses=$default_lower_fses
278 fi
279
280 if [ -z "$lower_mnt" ]; then
281         cleanup_lower_mnt=1
282         lower_mnt=$(mktemp -dq /tmp/etl-lower-XXXXXXXXXX)
283         if [ $? -ne 0 ]; then
284                 cleanup_lower_mnt=0
285                 rc=1
286                 exit
287         fi
288 fi
289 export ETL_LMOUNT_DST=$lower_mnt
290 export ETL_MOUNT_SRC=$lower_mnt
291
292 if [ -z "$upper_mnt" ]; then
293         cleanup_upper_mnt=1
294         upper_mnt=$(mktemp -dq /tmp/etl-upper-XXXXXXXXXX)
295         if [ $? -ne 0 ]; then
296                 cleanup_upper_mnt=0
297                 rc=1
298                 exit
299         fi
300 fi
301 export ETL_MOUNT_DST=$upper_mnt
302
303 # Source in the kernel and/or userspace tests.rc files to build the test lists
304 categories=$(echo $categories | tr ',' ' ')
305 if $kernel ; then
306         if [ -n "$tests" ]; then
307                 ktests=$(echo $tests | tr ',' ' ')
308         else
309                 . ${run_tests_dir}/kernel/tests.rc
310                 for cat in $categories ; do
311                         eval cat_tests=\$$cat
312                         ktests="$ktests $cat_tests"
313                 done
314         fi
315
316         if [ -n "$device" ]; then
317                 run_kernel_tests_on_existing_device
318         else
319                 run_kernel_tests_on_created_disk_image
320         fi
321 fi
322 if $userspace ; then
323         if [ -n "$tests" ]; then
324                 utests=$(echo $tests | tr ',' ' ')
325         else
326                 . ${run_tests_dir}/userspace/tests.rc
327                 for cat in $categories ; do
328                         eval cat_tests=\$$cat
329                         utests="$utests $cat_tests"
330                 done
331         fi
332
333         echo "Running eCryptfs userspace tests"
334
335         run_tests "${run_tests_dir}/userspace" "$utests"
336         if [ $? -ne 0 ]; then
337                 rc=1
338                 exit
339         fi
340 fi
341
342 echo ""
343 echo "Test Summary:"
344 echo "$passed passed"
345 echo "$failed failed"
346
347 rc=$failed
348 exit