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 rename' should not move an encrypted child dataset outside of its
27# encryption root.
28#
29# STRATEGY:
30# 1. Create two encryption roots, and a child and grandchild of the first
31#    encryption root
32# 2. Attempt to rename the grandchild under an unencrypted parent
33# 3. Attempt to rename the grandchild under a different encrypted parent
34# 4. Attempt to rename the grandchild under the current parent
35# 5. Verify the encryption root of the dataset
36# 6. Attempt to rename the grandchild to a child
37# 7. Verify the encryption root of the dataset
38#
39
40verify_runnable "both"
41
42function cleanup
43{
44	datasetexists $TESTPOOL/$TESTFS2 && \
45		log_must zfs destroy -r $TESTPOOL/$TESTFS2
46	datasetexists $TESTPOOL/$TESTFS3 && \
47		log_must zfs destroy -r $TESTPOOL/$TESTFS3
48}
49log_onexit cleanup
50
51log_assert "'zfs rename' should not move an encrypted child outside of its" \
52	"encryption root"
53
54log_must eval "echo $PASSPHRASE | zfs create -o encryption=on" \
55	"-o keyformat=passphrase -o keylocation=prompt $TESTPOOL/$TESTFS2"
56log_must zfs create $TESTPOOL/$TESTFS2/child
57log_must zfs create $TESTPOOL/$TESTFS2/child/grandchild
58log_must eval "echo $PASSPHRASE1 | zfs create -o encryption=on" \
59	"-o keyformat=passphrase -o keylocation=prompt $TESTPOOL/$TESTFS3"
60
61log_mustnot zfs rename $TESTPOOL/$TESTFS2/child/grandchild \
62	$TESTPOOL/grandchild
63
64log_mustnot zfs rename $TESTPOOL/$TESTFS2/child/grandchild \
65	$TESTPOOL/$TESTFS3/grandchild
66
67log_must zfs rename $TESTPOOL/$TESTFS2/child/grandchild \
68	$TESTPOOL/$TESTFS2/child/grandchild2
69log_must verify_encryption_root $TESTPOOL/$TESTFS2/child/grandchild2 \
70	$TESTPOOL/$TESTFS2
71
72log_must zfs rename $TESTPOOL/$TESTFS2/child/grandchild2 \
73	$TESTPOOL/$TESTFS2/grandchild2
74log_must verify_encryption_root $TESTPOOL/$TESTFS2/grandchild2 \
75	$TESTPOOL/$TESTFS2
76
77log_pass "'zfs rename' does not move an encrypted child outside of its" \
78	"encryption root"
79