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
23#
24# DESCRIPTION:
25# ZFS should receive streams from raw incremental sends.
26#
27# STRATEGY:
28# 1. Create an encrypted dataset
29# 2. Snapshot the dataset
30# 3. Create a file and get its checksum
31# 4. Snapshot the dataset
32# 5. Attempt to receive a raw send stream of the first snapshot
33# 6. Change the passphrase required to unlock the original filesystem
34# 7. Attempt and intentionally fail to receive the second snapshot
35# 8. Verify that the required passphrase hasn't changed on the receive side
36# 9. Attempt a real raw incremental send stream of the second snapshot
37# 10. Attempt load the key and mount the dataset
38# 11. Verify the checksum of the file is the same as the original
39#
40
41verify_runnable "both"
42
43function cleanup
44{
45	datasetexists $TESTPOOL/$TESTFS1 && \
46		destroy_dataset $TESTPOOL/$TESTFS1 -r
47
48	datasetexists $TESTPOOL/$TESTFS2 && \
49		destroy_dataset $TESTPOOL/$TESTFS2 -r
50
51	[[ -f $ibackup ]] && log_must rm -f $ibackup
52	[[ -f $ibackup_trunc ]] && log_must rm -f $ibackup_trunc
53}
54
55log_onexit cleanup
56
57log_assert "ZFS should receive streams from raw incremental sends"
58
59typeset ibackup="$TEST_BASE_DIR/ibackup.$$"
60typeset ibackup_trunc="$TEST_BASE_DIR/ibackup_trunc.$$"
61typeset passphrase="password"
62typeset passphrase2="password2"
63typeset snap1="$TESTPOOL/$TESTFS1@snap1"
64typeset snap2="$TESTPOOL/$TESTFS1@snap2"
65
66log_must eval "echo $passphrase | zfs create -o encryption=on" \
67	"-o keyformat=passphrase $TESTPOOL/$TESTFS1"
68
69log_must zfs snapshot $snap1
70
71log_must mkfile 1M /$TESTPOOL/$TESTFS1/$TESTFILE0
72typeset checksum=$(md5digest /$TESTPOOL/$TESTFS1/$TESTFILE0)
73
74log_must zfs snapshot $snap2
75
76log_must eval "zfs send -w $snap1 | zfs receive $TESTPOOL/$TESTFS2"
77log_must eval "echo $passphrase2 | zfs change-key $TESTPOOL/$TESTFS1"
78log_must eval "zfs send -w -i $snap1 $snap2 > $ibackup"
79
80typeset trunc_size=$(stat_size $ibackup)
81trunc_size=$(expr $trunc_size - 64)
82log_must cp $ibackup $ibackup_trunc
83log_must truncate -s $trunc_size $ibackup_trunc
84log_mustnot eval "zfs receive $TESTPOOL/$TESTFS2 < $ibackup_trunc"
85log_mustnot eval "echo $passphrase2 | zfs load-key $TESTPOOL/$TESTFS2"
86log_must eval "echo $passphrase | zfs load-key $TESTPOOL/$TESTFS2"
87log_must zfs unload-key $TESTPOOL/$TESTFS2
88
89log_must eval "zfs receive $TESTPOOL/$TESTFS2 < $ibackup"
90log_must eval "echo $passphrase2 | zfs mount -l $TESTPOOL/$TESTFS2"
91
92typeset cksum1=$(md5digest /$TESTPOOL/$TESTFS2/$TESTFILE0)
93[[ "$cksum1" == "$checksum" ]] || \
94	log_fail "Checksums differ ($cksum1 != $checksum)"
95
96log_pass "ZFS can receive streams from raw incremental sends"
97