1# SPDX-License-Identifier: BSD-2-Clause
2#
3# Copyright (c) 2018 Alan Somers
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26
27. $(atf_get_srcdir)/conf.sh
28
29atf_test_case preserve_props cleanup
30preserve_props_head()
31{
32	atf_set "descr" "geli should preserve basic GEOM properties"
33	atf_set "require.user" "root"
34	atf_set "timeout" 15
35}
36preserve_props_body()
37{
38	geli_test_setup
39
40	attach_md md -s1m
41	atf_check geli onetime /dev/${md}
42	md_secsize=$(diskinfo ${md} | cut -wf 2)
43	md_stripesize=$(diskinfo ${md} | cut -wf 5)
44	eli_secsize=$(diskinfo ${md}.eli | cut -wf 2)
45	eli_stripesize=$(diskinfo ${md}.eli | cut -wf 5)
46	atf_check_equal "$md_secsize" "$eli_secsize"
47	atf_check_equal "$md_stripesize" "$eli_stripesize"
48}
49preserve_props_cleanup()
50{
51	geli_test_cleanup
52}
53
54atf_test_case preserve_disk_props cleanup
55preserve_disk_props_head()
56{
57	atf_set "descr" "geli should preserve properties for disks"
58	atf_set "require.user" "root"
59	atf_set "require.config" "disks"
60	atf_set "timeout" 15
61}
62preserve_disk_props_body()
63{
64	geli_test_setup
65
66	disks=`atf_config_get disks`
67	disk=${disks%% *}
68	if [ -z "$disk" ]; then
69		atf_skip "Must define disks (see tests(7))"
70	fi
71	atf_check geli onetime ${disk}
72
73	disk_ident=$(diskinfo -s ${disk})
74	disk_descr=$(diskinfo -v ${disk} | awk '/Disk descr/ {print $1}')
75	disk_rotrate=$(diskinfo -v ${disk} | awk '/Rotation rate/ {print $1}')
76	disk_zonemode=$(diskinfo -v ${disk} | awk '/Zone Mode/ {print $1}')
77	eli_ident=$(diskinfo -s ${disk}.eli)
78	eli_descr=$(diskinfo -v ${disk}.eli | awk '/Disk descr/ {print $1}')
79	eli_rotrate=$(diskinfo -v ${disk}.eli | awk '/Rotation/ {print $1}')
80	eli_zonemode=$(diskinfo -v ${disk}.eli | awk '/Zone Mode/ {print $1}')
81	atf_check_equal "$disk_ident" "$eli_ident"
82	atf_check_equal "$disk_descr" "$eli_descr"
83	atf_check_equal "$disk_rotrate" "$eli_rotrate"
84	atf_check_equal "$disk_zonemode" "$eli_zonemode"
85}
86preserve_disk_props_cleanup()
87{
88	disk_cleanup
89	geli_test_cleanup
90}
91
92atf_test_case physpath cleanup
93physpath_head()
94{
95	atf_set "descr" "geli should append /eli to the underlying device's physical path"
96	atf_set "require.user" "root"
97	atf_set "timeout" 15
98}
99physpath_body()
100{
101	geli_test_setup
102	if ! error_message=$(geom_load_class_if_needed nop); then
103		atf_skip "$error_message"
104	fi
105
106	attach_md md -s1m
107	# If the underlying device has no physical path, then geli should not
108	# create one.
109	atf_check -o empty -e ignore diskinfo -p $md
110	atf_check -s exit:0 geli onetime $md
111	atf_check -o empty -e ignore diskinfo -p $md.eli
112	atf_check -s exit:0 geli kill $md
113
114	# If the underlying device does have a physical path, then geli should
115	# append "/eli"
116	physpath="some/physical/path"
117	atf_check gnop create -z $physpath ${md}
118	atf_check -s exit:0 geli onetime $md.nop
119	atf_check -o match:"^${physpath}/eli$" diskinfo -p $md.nop.eli
120}
121physpath_cleanup()
122{
123	if [ -f "$TEST_MDS_FILE" ]; then
124		while read md; do
125			[ -c /dev/${md}.nop.eli ] && \
126				geli detach $md.nop.eli 2>/dev/null
127			[ -c /dev/${md}.nop ] && \
128				gnop destroy -f $md.nop 2>/dev/null
129			[ -c /dev/${md}.eli ] && \
130				geli detach $md.eli 2>/dev/null
131			mdconfig -d -u $md 2>/dev/null
132		done < $TEST_MDS_FILE
133	fi
134	true
135}
136
137unaligned_io_test()
138{
139	cipher=$1
140	secsize=$2
141	ealgo=${cipher%%:*}
142	keylen=${cipher##*:}
143
144	atf_check -s exit:0 -e ignore \
145		geli init -B none -e $ealgo -l $keylen -P -K keyfile \
146		-s $secsize ${md}
147	atf_check geli attach -p -k keyfile ${md}
148
149	atf_check $(atf_get_srcdir)/unaligned_io /dev/${md}.eli
150}
151
152atf_test_case unaligned_io cleanup
153unaligned_io_head()
154{
155	atf_set "descr" "regression test for PR 271766"
156	atf_set "require.user" "root"
157}
158unaligned_io_body()
159{
160	geli_test_setup
161
162	sectors=4
163
164	atf_check dd if=/dev/random of=keyfile bs=512 count=16 status=none
165	for_each_geli_config_nointegrity unaligned_io_test
166}
167unaligned_io_cleanup()
168{
169	geli_test_cleanup
170}
171
172atf_init_test_cases()
173{
174	atf_add_test_case physpath
175	atf_add_test_case preserve_props
176	atf_add_test_case preserve_disk_props
177	atf_add_test_case unaligned_io
178}
179
180common_cleanup()
181{
182	if [ -f "$MD_DEVS" ]; then
183		while read test_md; do
184			gnop destroy -f ${test_md}.nop 2>/dev/null
185			mdconfig -d -u $test_md 2>/dev/null
186		done < $MD_DEVS
187		rm $MD_DEVS
188	fi
189
190	if [ -f "$PLAINFILES" ]; then
191		while read f; do
192			rm -f ${f}
193		done < ${PLAINFILES}
194		rm ${PLAINFILES}
195	fi
196	true
197}
198
199disk_cleanup()
200{
201	disks=`atf_config_get disks`
202	disk=${disks%% *}
203	if [ -n "$disk" ]; then
204		geli kill ${disk} 2>/dev/null
205	fi
206}
207