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# Copyright 2020 Joyent, Inc.
20#
21
22. $STF_SUITE/include/libtest.shlib
23
24#
25# DESCRIPTION:
26# ZFS should receive streams from raw sends.
27#
28# STRATEGY:
29# 1. Create an encrypted dataset
30# 2. Create a file and get its checksum
31# 3. Snapshot the dataset
32# 4. Attempt to receive a raw send stream as a child of an unencrypted dataset
33# 5. Verify the key is unavailable
34# 6. Attempt to load the key and mount the dataset
35# 7. Verify the cheksum of the file is the same as the original
36# 8. Attempt to receive a raw send stream as a child of an encrypted dataset
37# 9. Verify the key is unavailable
38# 10. Attempt to load the key and mount the dataset
39# 11. Verify the cheksum of the file is the same as the original
40#
41
42verify_runnable "both"
43
44function cleanup
45{
46	datasetexists $TESTPOOL/$TESTFS1 && \
47		log_must zfs destroy -r $TESTPOOL/$TESTFS1
48
49	datasetexists $TESTPOOL/$TESTFS2 && \
50		log_must zfs destroy -r $TESTPOOL/$TESTFS2
51}
52
53log_onexit cleanup
54
55log_assert "ZFS should receive streams from raw sends"
56
57typeset passphrase="password"
58typeset snap="$TESTPOOL/$TESTFS1@snap"
59
60log_must eval "echo $passphrase | zfs create -o encryption=on" \
61	"-o keyformat=passphrase $TESTPOOL/$TESTFS1"
62
63log_must mkfile 1M /$TESTPOOL/$TESTFS1/$TESTFILE0
64typeset checksum=$(digest -a md5 /$TESTPOOL/$TESTFS1/$TESTFILE0)
65
66log_must zfs snapshot $snap
67
68log_note "Verify ZFS can receive a raw send stream from an encrypted dataset"
69log_must eval "zfs send -w $snap | zfs receive $TESTPOOL/$TESTFS2"
70
71keystatus=$(get_prop keystatus $TESTPOOL/$TESTFS2)
72[[ "$keystatus" == "unavailable" ]] || \
73	log_fail "Expected keystatus unavailable, got $keystatus"
74
75log_must eval "echo $passphrase | zfs mount -l $TESTPOOL/$TESTFS2"
76
77typeset cksum1=$(digest -a md5 /$TESTPOOL/$TESTFS2/$TESTFILE0)
78[[ "$cksum1" == "$checksum" ]] || \
79	log_fail "Checksums differ ($cksum1 != $checksum)"
80
81log_must eval "zfs send -w $snap | zfs receive $TESTPOOL/$TESTFS1/c1"
82
83keystatus=$(get_prop keystatus $TESTPOOL/$TESTFS1/c1)
84[[ "$keystatus" == "unavailable" ]] || \
85	log_fail "Expected keystatus unavailable, got $keystatus"
86
87log_must eval "echo $passphrase | zfs mount -l $TESTPOOL/$TESTFS1/c1"
88typeset cksum2=$(digest -a md5 /$TESTPOOL/$TESTFS1/c1/$TESTFILE0)
89[[ "$cksum2" == "$checksum" ]] || \
90	log_fail "Checksums differ ($cksum2 != $checksum)"
91
92log_pass "ZFS can receive streams from raw sends"
93