1#!/bin/ksh
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2018 by Delphix. All rights reserved.
16#
17
18. $STF_SUITE/tests/functional/redacted_send/redacted.kshlib
19
20#
21# Description:
22# Verify redaction works as expected for various scenarios.
23#
24# Strategy:
25# 1. An unmodified file does not get redacted at all.
26# 2. Empty redaction list redacts everything.
27# 3. A file removed in the clone redacts the whole file.
28# 4. A file moved in the clone does not redact the file.
29# 5. A copied, then removed file in the clone redacts the whole file.
30# 6. Overwriting a file with identical contents redacts the file.
31# 7. A partially modified block redacts the entire block.
32# 8. Only overlapping areas of modified ranges are redacted.
33# 9. Send from the root dataset of a pool work correctly.
34#
35
36typeset ds_name="contents"
37typeset sendfs="$POOL/$ds_name"
38typeset recvfs="$POOL2/$ds_name"
39typeset clone="$POOL/${ds_name}_clone"
40typeset tmpdir="$(get_prop mountpoint $POOL)/tmp"
41typeset stream=$(mktemp $tmpdir/stream.XXXX)
42setup_dataset $ds_name ''
43typeset clone_mnt="$(get_prop mountpoint $clone)"
44typeset send_mnt="$(get_prop mountpoint $sendfs)"
45typeset recv_mnt="/$POOL2/$ds_name"
46
47log_onexit redacted_cleanup $sendfs $recvfs
48
49# An unmodified file does not get redacted at all.
50log_must zfs snapshot $clone@snap1
51log_must zfs redact $sendfs@snap book1 $clone@snap1
52log_must eval "zfs send --redact book1 $sendfs@snap >$stream"
53log_must eval "zfs recv $recvfs <$stream"
54log_must mount_redacted -f $recvfs
55log_must diff $send_mnt/f1 $recv_mnt/f1
56log_must diff $send_mnt/f2 $recv_mnt/f2
57log_must zfs rollback -R $clone@snap
58log_must zfs destroy -R $recvfs
59
60# Removing a file in the clone redacts the entire file.
61log_must rm "$clone_mnt/f1"
62log_must zfs snapshot $clone@snap1
63log_must zfs redact $sendfs@snap book3 $clone@snap1
64log_must eval "zfs send --redact book3 $sendfs@snap >$stream"
65log_must eval "zfs recv $recvfs <$stream"
66compare_files $sendfs $recvfs "f1" "$RANGE0"
67log_must zfs rollback -R $clone@snap
68log_must zfs destroy -R $recvfs
69
70# Moving a file in the clone does not redact the file.
71log_must mv "$clone_mnt/f1" "$clone_mnt/f1.moved"
72log_must zfs snapshot $clone@snap1
73log_must zfs redact $sendfs@snap book4 $clone@snap1
74log_must eval "zfs send --redact book4 $sendfs@snap >$stream"
75log_must eval "zfs recv $recvfs <$stream"
76log_must mount_redacted -f $recvfs
77[[ -f $recv_mnt/f1.moved ]] && log_fail "Found moved file in redacted receive."
78log_must diff $send_mnt/f1 $recv_mnt/f1
79log_must zfs rollback -R $clone@snap
80log_must zfs destroy -R $recvfs
81
82# Copying, then removing a file in the clone does redact the file.
83log_must cp "$clone_mnt/f1" "$clone_mnt/f1.copied"
84log_must rm "$clone_mnt/f1"
85log_must zfs snapshot $clone@snap1
86log_must zfs redact $sendfs@snap book5 $clone@snap1
87log_must eval "zfs send --redact book5 $sendfs@snap >$stream"
88log_must eval "zfs recv $recvfs <$stream"
89compare_files $sendfs $recvfs "f1" "$RANGE0"
90log_must mount_redacted -f $recvfs
91[[ -f $recv_mnt/f1.copied ]] && log_fail "Found moved file in redacted receive."
92log_must zfs rollback -R $clone@snap
93log_must zfs destroy -R $recvfs
94
95# Overwriting the contents of a block with identical contents redacts the file.
96log_must cp "$clone_mnt/f1" "$clone_mnt/f1.copied"
97log_must cp "$clone_mnt/f1.copied" "$clone_mnt/f1"
98log_must zfs snapshot $clone@snap1
99log_must zfs redact $sendfs@snap book6 $clone@snap1
100log_must eval "zfs send --redact book6 $sendfs@snap >$stream"
101log_must eval "zfs recv $recvfs <$stream"
102compare_files $sendfs $recvfs "f1" "$RANGE0"
103log_must mount_redacted -f $recvfs
104[[ -f $recv_mnt/f1.copied ]] && log_fail "Found moved file in redacted receive."
105log_must zfs rollback -R $clone@snap
106log_must zfs destroy -R $recvfs
107
108# Modifying some of a block redacts the whole block.
109log_must dd if=/dev/urandom of=$clone_mnt/f1 conv=notrunc seek=2 count=1 bs=32k
110log_must zfs snapshot $clone@snap1
111log_must zfs redact $sendfs@snap book7 $clone@snap1
112log_must eval "zfs send --redact book7 $sendfs@snap >$stream"
113log_must eval "zfs recv $recvfs <$stream"
114compare_files $sendfs $recvfs "f1" "$RANGE1"
115log_must zfs rollback -R $clone@snap
116log_must zfs destroy -R $recvfs
117
118# Only overlapping areas of modified ranges are redacted.
119log_must dd if=/dev/urandom of=$clone_mnt/f2 bs=1024k count=3 conv=notrunc
120log_must zfs snapshot $clone@snap1
121log_must zfs clone $sendfs@snap $clone/new
122typeset mntpnt="$(get_prop mountpoint $clone/new)"
123log_must dd if=/dev/urandom of=$mntpnt/f2 bs=1024k seek=1 count=3 \
124    conv=notrunc
125log_must zfs snapshot $clone/new@snap
126log_must zfs redact $sendfs@snap book8 $clone@snap1 $clone/new@snap
127log_must eval "zfs send --redact book8 $sendfs@snap >$stream"
128log_must eval "zfs recv $recvfs <$stream"
129compare_files $sendfs $recvfs "f2" "$RANGE2"
130log_must zfs destroy -R $clone/new
131log_must zfs rollback -R $clone@snap
132log_must zfs destroy -R $recvfs
133
134# FizzBuzz version
135log_must zfs clone $sendfs@snap $POOL/stride3
136mntpnt="$(get_prop mountpoint $POOL/stride3)"
137log_must stride_dd -i /dev/urandom -o $mntpnt/f2 -b $((128 * 1024)) -c 11 -s 3
138log_must zfs snapshot $POOL/stride3@snap
139log_must zfs clone $sendfs@snap $POOL/stride5
140mntpnt="$(get_prop mountpoint $POOL/stride5)"
141log_must stride_dd -i /dev/urandom -o $mntpnt/f2 -b $((128 * 1024)) -c 7 -s 5
142log_must zfs snapshot $POOL/stride5@snap
143log_must zfs redact $sendfs@snap book8a $POOL/stride3@snap $POOL/stride5@snap
144log_must eval "zfs send --redact book8a $sendfs@snap >$stream"
145log_must eval "zfs recv $recvfs <$stream"
146compare_files $sendfs $recvfs "f2" "$RANGE3"
147log_must zfs rollback -R $clone@snap
148log_must zfs destroy -R $recvfs
149
150# Send from the root dataset of a pool work correctly.
151log_must dd if=/dev/urandom of=/$POOL/f1 bs=128k count=4
152log_must zfs snapshot $POOL@snap
153log_must zfs clone $POOL@snap $POOL/clone
154log_must dd if=/dev/urandom of=/$POOL/clone/f1 bs=128k count=1 conv=notrunc
155log_must zfs snapshot $POOL/clone@snap
156log_must zfs redact $POOL@snap book9 $POOL/clone@snap
157log_must eval "zfs send --redact book9 $POOL@snap >$stream"
158log_must eval "zfs recv $recvfs <$stream"
159compare_files $POOL $recvfs "f1" "$RANGE1"
160log_must zfs destroy -R $POOL@snap
161
162log_pass "Redaction works as expected for various scenarios."
163