1#	$NetBSD: t_cgd.sh,v 1.14 2022/11/30 17:49:09 martin Exp $
2#
3# Copyright (c) 2010 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28rawpart=$( set -- a b c d e f g h i j k l m n o p q r s t u v w x y z;
29	shift $( sysctl -n kern.rawpartition ); printf %s "$1" )
30rawcgd=/dev/rcgd0${rawpart}
31cgdserver=\
32"rump_server -lrumpvfs -lrumpkern_crypto -lrumpdev -lrumpdev_disk -lrumpdev_cgd"
33
34atf_test_case basic cleanup
35basic_head()
36{
37
38	atf_set descr "Tests that encrypt/decrypt works"
39	atf_set require.progs rump_server
40}
41
42basic_body()
43{
44	d=$(atf_get_srcdir)
45	atf_check -s exit:0 \
46	    ${cgdserver} -d key=/dev/dk,hostpath=dk.img,size=1m unix://csock
47
48	export RUMP_SERVER=unix://csock
49	atf_check -s exit:0 -x "echo 12345 | \
50	    rump.cgdconfig -p cgd0 /dev/dk ${d}/paramsfile"
51	atf_check -s exit:0 -e ignore -x \
52	    "dd if=${d}/t_cgd count=2 | rump.dd of=${rawcgd}"
53	atf_check -s exit:0 -e ignore dd if=${d}/t_cgd of=testfile count=2
54	atf_check -s exit:0 -e ignore -o file:testfile \
55	    rump.dd if=${rawcgd} count=2
56}
57
58basic_cleanup()
59{
60	env RUMP_SERVER=unix://csock rump.halt || true
61}
62
63atf_test_case wrongpass cleanup
64wrongpass_head()
65{
66
67	atf_set descr \
68	    "Tests that wrong password does not give original plaintext"
69	atf_set require.progs rump_server
70}
71
72wrongpass_body()
73{
74	d=$(atf_get_srcdir)
75	atf_check -s exit:0 \
76	    ${cgdserver} -d key=/dev/dk,hostpath=dk.img,size=1m unix://csock
77
78	export RUMP_SERVER=unix://csock
79	atf_check -s exit:0 -x \
80	    "echo 12345 | rump.cgdconfig -p cgd0 /dev/dk ${d}/paramsfile"
81	atf_check -s exit:0 -e ignore -x \
82	    "dd if=${d}/t_cgd | rump.dd of=${rawcgd} count=2"
83
84	# unconfig and reconfig cgd
85	atf_check -s exit:0 rump.cgdconfig -u cgd0
86	atf_check -s exit:0 -x \
87	    "echo 54321 | rump.cgdconfig -p cgd0 /dev/dk ${d}/paramsfile"
88
89	atf_check -s exit:0 -e ignore dd if=${d}/t_cgd of=testfile count=2
90	atf_check -s exit:0 -e ignore -o not-file:testfile \
91	    rump.dd if=${rawcgd} count=2
92}
93
94wrongpass_cleanup()
95{
96	env RUMP_SERVER=unix://csock rump.halt || true
97}
98
99
100atf_test_case unaligned_write cleanup
101unaligned_write_head()
102{
103	atf_set descr "Attempt unaligned writes to a raw cgd device"
104	atf_set require.progs rump_server
105}
106
107unaligned_write_body()
108{
109	d=$(atf_get_srcdir)
110	atf_check -s exit:0 \
111	    ${cgdserver} -d key=/dev/dk,hostpath=dk.img,size=1m unix://csock
112
113	export RUMP_SERVER=unix://csock
114	atf_check -s exit:0 -x \
115	    "echo 12345 | rump.cgdconfig -p cgd0 /dev/dk ${d}/paramsfile"
116
117	# Check that cgd rejects writes of totally bogus lengths.
118	atf_check -s not-exit:0 -e ignore -x \
119	    "echo die hard | rump.dd of=${rawcgd} bs=123 conv=sync"
120
121	# Check that cgd rejects non-sector-length writes even if they
122	# are integral multiples of the block size.
123	atf_check -s not-exit:0 -e ignore -x \
124	    "echo die hard | rump.dd of=${rawcgd} bs=64 conv=sync"
125	atf_check -s not-exit:0 -e ignore -x \
126	    "echo die hard | rump.dd of=${rawcgd} bs=256 conv=sync"
127
128	# Check that cgd rejects misaligned buffers, produced by
129	# packetizing the input on bogus boundaries and using the
130	# bizarre behaviour of `bs=N' in dd.
131	atf_check -s not-exit:0 -e ignore -x \
132	    "(echo -n x && sleep 1 && head -c 511 </dev/zero) |
133		rump.dd of=${rawcgd} bs=512"
134
135	# Check that cgd rejects sector-length writes if they are not
136	# on sector boundaries.  Doesn't work because dd can't be
137	# persuaded to seek a non-integral multiple of the output
138	# buffer size and I can't be arsed to find the another way to
139	# do that.
140	#atf_check -s not-exit:0 -e ignore -x \
141	#    "echo die hard | rump.dd of=${rawcgd} seek=1 bs=512 conv=sync"
142}
143
144unaligned_write_cleanup()
145{
146	env RUMP_SERVER=unix://csock rump.halt || true
147}
148
149vmeth_failure_body()
150{
151	local vmeth="$1"
152	local d=$(atf_get_srcdir)
153
154	atf_check -s exit:0 \
155	    ${cgdserver} -d key=/dev/dk,hostpath=dk.img,size=1m unix://csock
156	export RUMP_SERVER=unix://csock
157	atf_check -s not-exit:0 -e ignore -x \
158	    "echo 12345 |
159	     rump.cgdconfig -V '${vmeth}' -p cgd0 /dev/dk ${d}/paramsfile"
160	atf_check -s exit:0 -o not-match:'(^| )cgd0( |$)' \
161	    rump.sysctl -n hw.disknames
162}
163
164test_case_vmeth_failure()
165{
166	local vmeth="${1}"
167	local name="vmeth_failure_${vmeth}"
168
169	atf_test_case "${name}" cleanup
170	eval "${name}_head() {
171		atf_set descr 'Tests verification method \"${vmeth}\" failure'
172		atf_set require.progs rump_server
173	}"
174	eval "${name}_body() {
175		vmeth_failure_body '${vmeth}'
176	}"
177	eval "${name}_cleanup() {
178		rump.cgdconfig -u cgd0 2>/dev/null
179		env RUMP_SERVER=unix://csock rump.halt || true
180	}"
181}
182
183test_case_vmeth_failure disklabel
184test_case_vmeth_failure ffs
185test_case_vmeth_failure gpt
186test_case_vmeth_failure mbr
187
188atf_init_test_cases()
189{
190	atf_add_test_case basic
191	atf_add_test_case wrongpass
192	atf_add_test_case unaligned_write
193	atf_add_test_case vmeth_failure_disklabel
194	atf_add_test_case vmeth_failure_ffs
195	atf_add_test_case vmeth_failure_gpt
196	atf_add_test_case vmeth_failure_mbr
197}
198