1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2017 Datto, Inc. All rights reserved.
19#
20
21. $STF_SUITE/include/libtest.shlib
22. $STF_SUITE/tests/functional/cli_root/zfs_load-key/zfs_load-key_common.kshlib
23
24#
25# DESCRIPTION:
26# 'zfs load-key' should only load a key for an unloaded encrypted dataset.
27#
28# STRATEGY:
29# 1. Attempt to load the default dataset's key
30# 2. Unmount the dataset
31# 3. Attempt to load the default dataset's key
32# 4. Create an encrypted dataset
33# 5. Unmount the dataset and unload its key
34# 6. Attempt to load the dataset's key
35# 7. Verify the dataset's key is loaded
36# 8. Attempt to load the dataset's key again
37# 9. Create an encrypted pool
38# 10. Unmount the pool and unload its key
39# 11. Attempt to load the pool's key
40# 12. Verify the pool's key is loaded
41# 13. Attempt to load the pool's key again
42#
43
44verify_runnable "both"
45
46function cleanup
47{
48	datasetexists $TESTPOOL/$TESTFS1 && \
49		log_must zfs destroy $TESTPOOL/$TESTFS1
50	poolexists $TESTPOOL1 && log_must destroy_pool $TESTPOOL1
51}
52log_onexit cleanup
53
54log_assert "'zfs load-key' should only load the key for an" \
55	"unloaded encrypted dataset"
56
57log_mustnot eval "echo $PASSPHRASE | zfs load-key $TESTPOOL/$TESTFS"
58
59log_must zfs unmount $TESTPOOL/$TESTFS
60log_mustnot eval "echo $PASSPHRASE | zfs load-key $TESTPOOL/$TESTFS"
61
62log_must eval "echo $PASSPHRASE | zfs create -o encryption=on" \
63	"-o keyformat=passphrase -o keylocation=prompt $TESTPOOL/$TESTFS1"
64
65log_must zfs unmount $TESTPOOL/$TESTFS1
66log_must zfs unload-key $TESTPOOL/$TESTFS1
67
68log_must eval "echo $PASSPHRASE | zfs load-key $TESTPOOL/$TESTFS1"
69log_must key_available $TESTPOOL/$TESTFS1
70
71log_mustnot eval "echo $PASSPHRASE | zfs load-key $TESTPOOL/$TESTFS1"
72
73typeset DISK2="$(echo $DISKS | awk '{ print $2 }')"
74log_must eval "echo $PASSPHRASE | zpool create -O encryption=on" \
75	"-O keyformat=passphrase -O keylocation=prompt $TESTPOOL1 $DISK2"
76
77log_must zfs unmount $TESTPOOL1
78log_must zfs unload-key $TESTPOOL1
79
80log_must eval "echo $PASSPHRASE | zfs load-key $TESTPOOL1"
81log_must key_available $TESTPOOL1
82
83log_mustnot eval "echo $PASSPHRASE | zfs load-key $TESTPOOL1"
84
85log_pass "'zfs load-key' only loads the key for an unloaded encrypted dataset"
86