~ubuntu-branches/ubuntu/trusty/ecryptfs-utils/trusty

« back to all changes in this revision

Viewing changes to tests/lib/etl_funcs.sh

  • Committer: Package Import Robot
  • Author(s): Dustin Kirkland, Dustin Kirkland, Tyler Hicks, Eddie Garcia, Colin King
  • Date: 2012-02-16 14:22:09 UTC
  • mfrom: (1.1.41)
  • Revision ID: package-import@ubuntu.com-20120216142209-n1r1a7dwbbq49xzs
Tags: 96-0ubuntu1
[ Dustin Kirkland ]
* CONTRIBUTING:
  - added a new file to describe how to contribute to ecryptfs
* === added directory img/old, img/old/ecryptfs_14.png,
  img/old/ecryptfs_192.png, img/old/ecryptfs_64.png:
  - saving the old logos/branding for posterity
* debian/copyright, img/COPYING:
  - added CC-by-SA 3.0 license
  - use the text version
* img/ecryptfs_14.png, img/ecryptfs_192.png, img/ecryptfs_64.png:
  - added scaled copies of images used for Launchpad.net branding
* src/utils/ecryptfs-recover-private: LP: #847505
  - add an option to allow user to enter the mount passphrase,
    in case they've recorded that, but forgotten their login
    passphrase
* src/libecryptfs/sysfs.c: LP: #802197
  - default sysfs to /sys, if not found in /etc/mtab
  - it seems that reading /etc/mtab for this is outdated
  - ensure that ecryptfs works even if there is no sysfs entry
    in /etc/mtab
* src/key_mod/ecryptfs_key_mod_tspi.c: LP: #462225
  - fix TPM and string_to_uuid 64bits issue
  - thanks to Janos for the patch
* precise

[ Tyler Hicks ]
* CONTRIBUTING:
  - clarified how to contribute to the ecryptfs kernel module
* tests/lib/etl_funcs.sh:
  - created eCryptfs test library of bash functions for use in test
    cases and test harnesses
* test/etl_add_passphrase_key_to_keyring.c:
  - created a C helper program to allow bash scripts to interface to
    the libecryptfs function that adds passphrase-based keys to the
    kernel keyring
* tests/kernel/tests.rc, tests/userspace/tests.rc:
  - created a test case category files for test harnesses to source
    when running testcases of a certain category (destructive, safe,
    etc.)
* tests/run_tests.sh:
  - created a test harness to run eCryptfs test cases
* tests/kernel/miscdev-bad-count.sh,
  tests/kernel/miscdev-bad-count/test.c:
  - created test case for miscdev issue reported to mailing list
* tests/kernel/lp-885744.sh:
  - created test case for pathconf bug
* tests/kernel/lp-926292.sh:
  - created test case for checking stale inode attrs after setxattr
* tests/new.sh:
  - created new test case template to copy from
* tests/userspace/verify-passphrase-sig.sh,
  tests/userspace/verify-passphrase-sig/test.c:
  - created test case, for make check, to test the creation of
    passphrase-based fekeks and signatures
* configure.ac, Makefile.am, tests/Makefile.am, tests/lib/Makefile.am,
  tests/kernel/Makefile.am, tests/userspace/Makefile.am:
  - updated and created autoconf/automake files to build the new tests
    directory
  - added make check target

[ Eddie Garcia ]
* img/*: LP: #907131
  - contributing a new set of logos and branding under the CC-by-SA3.0
    license

[ Colin King ]
* tests/kernel/extend-file-random.sh,
  tests/kernel/extend-file-random/test.c:
  - Test to randomly extend file size, read/write + unlink
* tests/kernel/trunc-file.sh, tests/kernel/trunc-file/test.c:
  - Test to exercise file truncation
* tests/kernel/directory-concurrent.sh,
  tests/kernel/directory-concurrent/test.c:
  - test for directory creation/deletion races with multiple processes
* tests/kernel/file-concurrent.sh,
  tests/kernel/file-concurrent/test.c:
  - test for file creation/truncation/unlink races with multiple
    processes
* tests/kernel/inotify.sh, tests/kernel/inotify/test.c:
  - test for proper inotify support
* tests/kernel/mmap-dir.sh, tests/kernel/mmap-dir/test.c:
  - test that directory files cannot be mmap'ed
* tests/kernel/read-dir.sh, tests/kernel/read-dir/test.c:
  - test that read() on directory files returns the right error
* tests/kernel/setattr-flush-dirty.sh:
  - test that the modified timestamp isn't clobbered in writeback
* tests/kernel/inode-race-stat.sh, tests/kernel/inode-race-stat/test.c:
  - test for inode initialization race condition

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#
 
3
# etl_funcs.sh: eCryptfs test library (etl) helper functions
 
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 version 2
 
11
# of the License.
 
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
etl=$(dirname $BASH_SOURCE[0])
 
23
 
 
24
default_fekek_pass="foo"
 
25
default_fekek_salt_hex="0011223344556677"
 
26
 
 
27
default_fnek_pass="$default_fekek_pass"
 
28
default_fnek_salt_hex="9988776655443322"
 
29
 
 
30
default_lfs="ext4"
 
31
default_lmount_opts="rw,relatime"
 
32
default_ext_opts="user_xattr,acl,commit=600,barrier=1,data=ordered"
 
33
default_mount_opts="rw,relatime,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_sig=\${ETL_FEKEK_SIG}"
 
34
default_fne_mount_opts="${default_mount_opts},ecryptfs_fnek_sig=\${ETL_FNEK_SIG}"
 
35
 
 
36
 
 
37
#
 
38
# etl_add_fekek_passphrase [PASS] [SALT_HEX]
 
39
#
 
40
# Adds a passphrase-based file encryption key to the kernel keyring. A default
 
41
# PASS and SALT_HEX will be used if they are not specified. The key signature
 
42
# is exported into ETL_FEKEK_SIG upon success.
 
43
#
 
44
# Only call this directly if your test needs to add a specific fekek.
 
45
#
 
46
etl_add_fekek_passphrase()
 
47
{
 
48
        if [ -z "$1" ]; then
 
49
                pass=$default_fekek_pass
 
50
        else
 
51
                pass=$1
 
52
        fi
 
53
        if [ -z "$2" ]; then
 
54
                salt_hex=$default_fekek_salt_hex
 
55
        else
 
56
                salt_hex=$2
 
57
        fi
 
58
 
 
59
        sig=$(${etl}/etl-add-passphrase-key-to-keyring $pass $salt_hex)
 
60
        if [ $? -ne 0 ]; then
 
61
                return 1
 
62
        fi
 
63
 
 
64
        export ETL_FEKEK_SIG=$sig
 
65
        return 0
 
66
}
 
67
 
 
68
#
 
69
# etl_add_fnek_passphrase [PASS] [SALT_HEX]
 
70
#
 
71
# Adds a passphrase-based filename encryption key to the kernel keyring. A
 
72
# default PASS and SALT_HEX will be used if they are not specified. The key
 
73
# signature is exported into ETL_FNEK_SIG upon success.
 
74
#
 
75
# Only call this directly if your test needs to add a specific fnek.
 
76
#
 
77
etl_add_fnek_passphrase()
 
78
{
 
79
        if [ -z "$1" ]; then
 
80
                pass=$default_fnek_pass
 
81
        else
 
82
                pass=$1
 
83
        fi
 
84
        if [ -z "$2" ]; then
 
85
                salt_hex=$default_fnek_salt_hex
 
86
        else
 
87
                salt_hex=$2
 
88
        fi
 
89
 
 
90
        sig=$(${etl}/etl-add-passphrase-key-to-keyring $pass $salt_hex)
 
91
        if [ $? -ne 0 ]; then
 
92
                return 1
 
93
        fi
 
94
 
 
95
        export ETL_FNEK_SIG=$sig
 
96
        return 0
 
97
}
 
98
 
 
99
#
 
100
# etl_add_keys
 
101
#
 
102
# Adds a fekek and, if appropriate, a fnek to the kernel keyring using the
 
103
# default values defined above. Most test cases requiring a generic mount will
 
104
# use this rather than the lower level functions that this calls.
 
105
#
 
106
# Set ETL_TEST_FNE to true if you want filename encryption enabled (it is best
 
107
# to lest the test harness handle that). ETL_FEKEK_SIG and, if appropriate,
 
108
# ETL_FNEK_SIG will contain the key signatures upon success.
 
109
#
 
110
etl_add_keys()
 
111
{
 
112
        # TODO: This should support non-passphrase based keys, too
 
113
 
 
114
        etl_add_fekek_passphrase
 
115
        if [ $? -ne 0 ]; then
 
116
                return 1
 
117
        fi
 
118
 
 
119
        if $ETL_TEST_FNE ; then
 
120
                etl_add_fnek_passphrase
 
121
                return $?
 
122
        fi
 
123
 
 
124
        return 0
 
125
}
 
126
 
 
127
#
 
128
# etl_unlink_key_sig SIGNATURE
 
129
#
 
130
# Unlinks the key corresponding to the specified signature.
 
131
#
 
132
etl_unlink_key_sig()
 
133
{
 
134
        if [ -z "$1" ]; then
 
135
                return 1
 
136
        fi
 
137
 
 
138
        show_line=$(keyctl list @u | grep -s $1)
 
139
        if [ $? -ne 0 ]; then
 
140
                return 1
 
141
        fi
 
142
 
 
143
        key=$(printf $show_line | awk -F ':' '{ print $1 }')
 
144
        keyctl unlink $key &>/dev/null
 
145
}
 
146
 
 
147
#
 
148
# etl_unlink_fekek
 
149
#
 
150
# Unlinks the key corresponding to the value of ETL_FEKEK_SIG. Unsets
 
151
# that variable upon success.
 
152
#
 
153
etl_unlink_fekek()
 
154
{
 
155
        if [ -z "$ETL_FEKEK_SIG" ]; then
 
156
               return 1
 
157
        fi
 
158
 
 
159
        etl_unlink_key_sig $ETL_FEKEK_SIG
 
160
        if [ $? -ne 0 ]; then
 
161
                return 1
 
162
        fi
 
163
 
 
164
        unset ETL_FEKEK_SIG
 
165
}
 
166
 
 
167
#
 
168
# etl_unlink_fnek
 
169
#
 
170
# Unlinks the key corresponding to the value of ETL_FNEK_SIG. Unsets
 
171
# that variable upon success.
 
172
#
 
173
etl_unlink_fnek()
 
174
{
 
175
        if [ -z "$ETL_FNEK_SIG" ]; then
 
176
                return 1
 
177
        fi
 
178
 
 
179
        etl_unlink_key_sig $ETL_FNEK_SIG
 
180
        if [ $? -ne 0 ]; then
 
181
                return 1
 
182
        fi
 
183
 
 
184
        unset ETL_FNEK_SIG
 
185
}
 
186
 
 
187
#
 
188
# etl_unlink_keys
 
189
#
 
190
# Unlinks the fekek and, if appropriate, the fnek from the kernel keyring. See
 
191
# the functions called by etl_unlink_keys() for more information.
 
192
#
 
193
# Most test cases requiring a generic mount will use this rather than the lower
 
194
# level functions that this calls.
 
195
#
 
196
etl_unlink_keys()
 
197
{
 
198
        etl_unlink_fekek
 
199
        if [ $? -ne 0 ]; then
 
200
                return 1
 
201
        fi
 
202
 
 
203
        if $ETL_TEST_FNE ; then
 
204
                etl_unlink_fnek
 
205
                return $?
 
206
        fi
 
207
 
 
208
        return 0
 
209
}
 
210
 
 
211
#
 
212
# etl_create_disk DISK_SIZE [DIR_PATH]
 
213
#
 
214
# Creates a disk image for testing. This disk image will be formatted and ready
 
215
# for mounting as the lower filesystem.
 
216
#
 
217
# DISK_SIZE must be specified in 1K block sizes. DIR_PATH can be specified so
 
218
# that the image file is stored somewhere other than the /tmp/ directory.
 
219
#
 
220
etl_create_disk()
 
221
{
 
222
        if [ -z "$1" ]; then
 
223
                return 1
 
224
        fi
 
225
        if [ -z "$2" ]; then
 
226
                dir_path="/tmp"
 
227
        else
 
228
                dir_path="$2"
 
229
        fi
 
230
        if [ -z "$ETL_LFS" ]; then
 
231
                lfs=$default_lfs
 
232
        else
 
233
                lfs=$ETL_LFS
 
234
        fi
 
235
 
 
236
        img=$(mktemp -q /${dir_path}/etl-img-XXXXXXXXXX)
 
237
        if [ $? -ne 0 ]; then
 
238
                return 1
 
239
        fi
 
240
 
 
241
        dd if=/dev/zero of=$img bs=1024 count=$1 &>/dev/null
 
242
        if [ $? -ne 0 ]; then
 
243
                rm $img &>/dev/null
 
244
                return 1
 
245
        fi
 
246
 
 
247
        mkfs -F -t $lfs $img &>/dev/null
 
248
        if [ $? -ne 0 ]; then
 
249
                rm $img &>/dev/null
 
250
                return 1
 
251
        fi
 
252
 
 
253
        export ETL_DISK=$img
 
254
        export ETL_LMOUNT_SRC=$img
 
255
        export ETL_LFS=$lfs
 
256
}
 
257
 
 
258
#
 
259
# etl_remove_disk
 
260
#
 
261
# Removes any lower test disk created by etl_create_disk().
 
262
#
 
263
etl_remove_disk()
 
264
{
 
265
        if [ -z "$ETL_DISK" ] || [ ! -f "$ETL_DISK" ]; then
 
266
                return 1
 
267
        fi
 
268
        if grep -q $ETL_DISK /proc/mounts; then
 
269
                return 1
 
270
        fi
 
271
 
 
272
        rm -f $ETL_DISK &>/dev/null
 
273
}
 
274
 
 
275
#
 
276
# etl_load_ecryptfs
 
277
#
 
278
# Ensures that the eCryptfs kernel code is either loaded, if a module, or
 
279
# compiled in.
 
280
#
 
281
# If your test only needs an eCryptfs mount, don't call this function. The mount
 
282
# process will autoload the module for you. If you need access to something like
 
283
# /dev/ecryptfs, but don't need an eCryptfs mount, this function is for you.
 
284
#
 
285
etl_load_ecryptfs()
 
286
{
 
287
        if ! grep -q ecryptfs /proc/filesystems; then
 
288
                modprobe ecryptfs
 
289
                return $?
 
290
        fi
 
291
 
 
292
        return 0
 
293
}
 
294
 
 
295
#
 
296
# etl_construct_lmount_opts
 
297
#
 
298
# Construct the lower filesystem mount options. If mount options are already
 
299
# set, nothing is done. Otherwise, the default mount options for the lower
 
300
# filesystem are set.
 
301
#
 
302
# If you need specific options, you should probably construct them yourself and
 
303
# simply export them as ETL_LMOUNT_OPTS. This function is mostly a helper for
 
304
# other etl functions.
 
305
#
 
306
etl_construct_lmount_opts()
 
307
{
 
308
        if [ -n "$ETL_LMOUNT_OPTS" ]; then
 
309
                return 0
 
310
        fi
 
311
        if [ -z "$ETL_LFS" ]; then
 
312
                export ETL_LFS=$default_lfs
 
313
        fi
 
314
 
 
315
        # TODO: Add support for more filesystems
 
316
        case $LFS in
 
317
        ext2|ext3|ext4)
 
318
                lmount_opts=${default_lmount_opts},${default_ext_opts}
 
319
                ;;
 
320
        *)
 
321
                lmount_opts=$default_lmount_opts
 
322
                ;;
 
323
        esac
 
324
 
 
325
        if [ -f "$ETL_LMOUNT_SRC" ]; then
 
326
                lmount_opts="${lmount_opts},loop"
 
327
        fi
 
328
 
 
329
        export ETL_LMOUNT_OPTS=$lmount_opts
 
330
        return 0
 
331
}
 
332
 
 
333
#
 
334
# etl_lmount
 
335
#
 
336
# Mounts the lower filesystem based upon the various env variables.
 
337
#
 
338
etl_lmount()
 
339
{
 
340
        if [ -z "$ETL_LMOUNT_SRC" ] || [ -z "$ETL_LMOUNT_DST" ]; then
 
341
                return 1
 
342
        fi
 
343
        if ! etl_construct_lmount_opts; then
 
344
                return 1
 
345
        fi
 
346
 
 
347
        mount -t "$ETL_LFS" -o "$ETL_LMOUNT_OPTS" \
 
348
                "$ETL_LMOUNT_SRC" "$ETL_LMOUNT_DST" &>/dev/null
 
349
}
 
350
 
 
351
#
 
352
# etl_lumount
 
353
#
 
354
# Unmounts the lower filesystem.
 
355
#
 
356
etl_lumount()
 
357
{
 
358
        if [ -z "$ETL_LMOUNT_SRC" ]; then
 
359
                return 1
 
360
        fi
 
361
 
 
362
        sync
 
363
        umount "$ETL_LMOUNT_SRC" &>/dev/null
 
364
}
 
365
 
 
366
#
 
367
# etl_mount_i
 
368
#
 
369
# Performs an eCryptfs mount, bypassing the eCryptfs mount helper.
 
370
#
 
371
# If you're fine with the default eCryptfs mount options, or have constructed
 
372
# your own mount options, and have already added the appropriate keys to the
 
373
# kernel keyring, this is the easiest way to do an eCryptfs mount.
 
374
#
 
375
etl_mount_i()
 
376
{
 
377
        if [ -z "$ETL_MOUNT_SRC" ] || [ -z "$ETL_MOUNT_DST" ]; then
 
378
                return 1
 
379
        fi
 
380
        if [ -z "$ETL_MOUNT_OPTS" ]; then
 
381
                if [ -n "ETL_FNEK_SIG" ]; then
 
382
                        export ETL_MOUNT_OPTS=$(eval \
 
383
                                                "echo $default_fne_mount_opts")
 
384
                else
 
385
                        export ETL_MOUNT_OPTS=$(eval "echo $default_mount_opts")
 
386
                fi
 
387
        fi
 
388
        
 
389
        mount -it ecryptfs -o "$ETL_MOUNT_OPTS" \
 
390
                "$ETL_MOUNT_SRC" "$ETL_MOUNT_DST"
 
391
}
 
392
 
 
393
#
 
394
# etl_umount_i
 
395
#
 
396
# Unmounts the eCryptfs mount point specified by ETL_MOUNT_DST. Note that the
 
397
# eCryptfs umount helper will not be called.
 
398
#
 
399
etl_umount_i()
 
400
{
 
401
        if [ -z "$ETL_MOUNT_DST" ]; then
 
402
                return 1
 
403
        fi
 
404
 
 
405
        if ! grep -q $ETL_MOUNT_DST /proc/mounts; then
 
406
                return 1
 
407
        fi
 
408
 
 
409
        sync
 
410
        umount -i "$ETL_MOUNT_DST" &>/dev/null
 
411
}
 
412
 
 
413
#
 
414
# etl_umount
 
415
#
 
416
# Unmounts the eCryptfs mount point specified by ETL_MOUNT_DST. Note that the
 
417
# eCryptfs umount helper will be called.
 
418
#
 
419
etl_umount()
 
420
{
 
421
        if [ -z "$ETL_MOUNT_DST" ]; then
 
422
                return 1
 
423
        fi
 
424
 
 
425
        if ! grep -q $ETL_MOUNT_DST /proc/mounts; then
 
426
                return 1
 
427
        fi
 
428
 
 
429
        sync
 
430
        umount "$ETL_MOUNT_DST" &>/dev/null
 
431
}
 
432
 
 
433
#
 
434
# etl_create_test_dir
 
435
#
 
436
# Creates a directory for carrying out tests inside of the eCryptfs mount point
 
437
# (ETL_MOUNT_DST).
 
438
#
 
439
# Upon success, the newly created directory's name is echoed to stdout.
 
440
#
 
441
etl_create_test_dir()
 
442
{
 
443
        if [ -z "$ETL_MOUNT_DST" ] || [ -z "$1" ]; then
 
444
                return 1
 
445
        fi
 
446
 
 
447
        test_basename=$(basename $0)
 
448
        test_dir=$(mktemp -qd /${ETL_MOUNT_DST}/etl-${test_basename}-XXXXXXXXXX)
 
449
        if [ $? -ne 0 ]; then
 
450
                return 1;
 
451
        fi
 
452
 
 
453
        echo $test_dir
 
454
        return 0
 
455
}
 
456
 
 
457
#
 
458
# etl_remove_test_dir TEST_DIR
 
459
#
 
460
# Removes the specified test directory.
 
461
#
 
462
# For now, it is nothing much more than a wrapper around rm -rf, but it may
 
463
# gain more functionality and/or safety checks in the future, so please use it.
 
464
#
 
465
etl_remove_test_dir()
 
466
{
 
467
        if [ -z "$1" ]; then
 
468
                return 0
 
469
        elif [ ! -d "$1" ]; then
 
470
                return 1
 
471
        elif [ "$1" = "/" ]; then
 
472
                return 1
 
473
        fi
 
474
 
 
475
        rm -rf $1 &>/dev/null
 
476
}