1# Copyright (c) 2016 Alan Somers
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25# $FreeBSD: stable/11/tests/sys/geom/class/nop/nop_test.sh 329789 2018-02-22 02:12:47Z asomers $
26
27MD_DEVS="md.devs"
28PLAINFILES=plainfiles
29
30atf_test_case preserve_props cleanup
31preserve_props_head()
32{
33	atf_set "descr" "gnop should preserve basic GEOM properties"
34	atf_set "require.user" "root"
35	atf_set "timeout" 15
36}
37preserve_props_body()
38{
39	load_gnop
40	us=$(alloc_md)
41	atf_check gnop create /dev/${us}
42	md_secsize=$(diskinfo ${us} | cut -wf 2)
43	md_mediasize=$(diskinfo ${us} | cut -wf 3)
44	md_stripesize=$(diskinfo ${us} | cut -wf 5)
45	nop_secsize=$(diskinfo ${us}.nop | cut -wf 2)
46	nop_mediasize=$(diskinfo ${us}.nop | cut -wf 3)
47	nop_stripesize=$(diskinfo ${us}.nop | cut -wf 5)
48	atf_check_equal "$md_secsize" "$nop_secsize"
49	atf_check_equal "$md_mediasize" "$nop_mediasize"
50	atf_check_equal "$md_stripesize" "$nop_stripesize"
51}
52preserve_props_cleanup()
53{
54	common_cleanup
55}
56
57atf_test_case preserve_disk_props cleanup
58preserve_disk_props_head()
59{
60	atf_set "descr" "gnop should preserve properties for disks"
61	atf_set "require.user" "root"
62	atf_set "require.config" "disks"
63	atf_set "timeout" 15
64}
65preserve_disk_props_body()
66{
67	load_gnop
68	disks=`atf_config_get disks`
69	disk=${disks%% *}
70	if [ -z "$disk" ]; then
71		atf_skip "Must define disks (see tests(7))"
72	fi
73	atf_check gnop create ${disk}
74
75	disk_ident=$(diskinfo -s ${disk})
76	disk_physpath=$(diskinfo -p ${disk})
77	disk_descr=$(diskinfo -v ${disk} | awk '/Disk descr/ {print $1}')
78	disk_trim=$(diskinfo -v ${disk} | awk '/TRIM.UNMAP/ {print $1}')
79	disk_rotrate=$(diskinfo -v ${disk} | awk '/Rotation rate/ {print $1}')
80	disk_zonemode=$(diskinfo -v ${disk} | awk '/Zone Mode/ {print $1}')
81	nop_ident=$(diskinfo -s ${disk}.nop)
82	nop_physpath=$(diskinfo -p ${disk}.nop)
83	nop_descr=$(diskinfo -v ${disk}.nop | awk '/Disk descr/ {print $1}')
84	nop_trim=$(diskinfo -v ${disk}.nop | awk '/TRIM.UNMAP/ {print $1}')
85	nop_rotrate=$(diskinfo -v ${disk}.nop | awk '/Rotation/ {print $1}')
86	nop_zonemode=$(diskinfo -v ${disk}.nop | awk '/Zone Mode/ {print $1}')
87	atf_check_equal "$disk_ident" "$nop_ident"
88	atf_check_equal "$disk_physpath" "$nop_physpath"
89	atf_check_equal "$disk_descr" "$nop_descr"
90	atf_check_equal "$disk_trim" "$nop_trim"
91	atf_check_equal "$disk_rotrate" "$nop_rotrate"
92	atf_check_equal "$disk_zonemode" "$nop_zonemode"
93}
94preserve_disk_props_cleanup()
95{
96	disk_cleanup
97	common_cleanup
98}
99
100atf_test_case io cleanup
101io_head()
102{
103	atf_set "descr" "I/O works on gnop devices"
104	atf_set "require.user" "root"
105	atf_set "timeout" 15
106}
107io_body()
108{
109	load_gnop
110	us=$(alloc_md)
111	atf_check gnop create /dev/${us}
112
113	echo src >> $PLAINFILES
114	echo dst >> $PLAINFILES
115	dd if=/dev/random of=src bs=1m count=1 >/dev/null 2>&1
116	dd if=src of=/dev/${us}.nop bs=1m count=1 > /dev/null 2>&1
117	dd if=/dev/${us}.nop of=dst bs=1m count=1 > /dev/null 2>&1
118
119	atf_check_equal `md5 -q src` `md5 -q dst`
120}
121io_cleanup()
122{
123	common_cleanup
124}
125
126atf_test_case physpath cleanup
127physpath_head()
128{
129	atf_set "descr" "Test gnop's -z option"
130	atf_set "require.user" "root"
131	atf_set "timeout" 15
132}
133physpath_body()
134{
135	load_gnop
136	us=$(alloc_md)
137	physpath="some/physical/path"
138	atf_check gnop create -z $physpath /dev/${us}
139	gnop_physpath=$(diskinfo -p ${us}.nop)
140	atf_check_equal "$physpath" "$gnop_physpath"
141}
142physpath_cleanup()
143{
144	common_cleanup
145}
146
147atf_test_case physpath_blank cleanup
148physpath_blank_head()
149{
150	atf_set "descr" "gnop can set physical path to the empty string"
151	atf_set "require.user" "root"
152	atf_set "require.config" "disks"
153	atf_set "timeout" 15
154}
155physpath_blank_body()
156{
157	load_gnop
158	disks=`atf_config_get disks`
159	disk=${disks%% *}
160	if [ -z "$disk" ]; then
161		atf_skip "Must define disks (see tests(7))"
162	fi
163
164	atf_check gnop create -z "" ${disk}
165	gnop_physpath=$(diskinfo -p ${disk}.nop)
166	atf_check_equal "" "$gnop_physpath"
167}
168physpath_blank_cleanup()
169{
170	disk_cleanup
171	common_cleanup
172}
173
174atf_test_case size cleanup
175size_head()
176{
177	atf_set "descr" "Test gnop's -s option"
178	atf_set "require.user" "root"
179	atf_set "timeout" 15
180}
181size_body()
182{
183	load_gnop
184	us=$(alloc_md)
185	for mediasize in 65536 524288 1048576; do
186		atf_check gnop create -s ${mediasize} /dev/${us}
187		gnop_mediasize=`diskinfo /dev/${us}.nop | cut -wf 3`
188		atf_check_equal "${mediasize}" "${gnop_mediasize}"
189		atf_check gnop destroy /dev/${us}.nop
190	done
191	# We shouldn't be able to extend the provider's size
192	atf_check -s not-exit:0 -e ignore gnop create -s 2097152 /dev/${us}
193}
194size_cleanup()
195{
196	common_cleanup
197}
198
199atf_test_case stripesize cleanup
200stripesize_head()
201{
202	atf_set "descr" "Test gnop's -p and -P options"
203	atf_set "require.user" "root"
204	atf_set "timeout" 15
205}
206stripesize_body()
207{
208	load_gnop
209	us=$(alloc_md)
210	for ss in 512 1024 2048 4096 8192; do
211		for sofs in `seq 0 512 ${ss}`; do
212			[ "$sofs" -eq "$ss" ] && continue
213			atf_check gnop create -p ${ss} -P ${sofs} /dev/${us}
214			gnop_ss=`diskinfo /dev/${us}.nop | cut -wf 5`
215			gnop_sofs=`diskinfo /dev/${us}.nop | cut -wf 6`
216			atf_check_equal "${ss}" "${gnop_ss}"
217			atf_check_equal "${sofs}" "${gnop_sofs}"
218			atf_check gnop destroy /dev/${us}.nop
219		done
220	done
221}
222stripesize_cleanup()
223{
224	common_cleanup
225}
226
227atf_init_test_cases()
228{
229	atf_add_test_case io
230	atf_add_test_case physpath
231	atf_add_test_case physpath_blank
232	atf_add_test_case preserve_props
233	atf_add_test_case preserve_disk_props
234	atf_add_test_case stripesize
235	atf_add_test_case size
236}
237
238alloc_md()
239{
240	local md
241
242	md=$(mdconfig -a -t swap -s 1M) || atf_fail "mdconfig -a failed"
243	echo ${md} >> $MD_DEVS
244	echo ${md}
245}
246
247common_cleanup()
248{
249	if [ -f "$MD_DEVS" ]; then
250		while read test_md; do
251			gnop destroy -f ${test_md}.nop 2>/dev/null
252			mdconfig -d -u $test_md 2>/dev/null
253		done < $MD_DEVS
254		rm $MD_DEVS
255	fi
256
257	if [ -f "$PLAINFILES" ]; then
258		while read f; do
259			rm -f ${f}
260		done < ${PLAINFILES}
261		rm ${PLAINFILES}
262	fi
263	true
264}
265
266disk_cleanup()
267{
268	disks=`atf_config_get disks`
269	disk=${disks%% *}
270	if [ -n "$disk" ]; then
271		gnop destroy -f ${disk}.nop 2>/dev/null
272	fi
273}
274
275load_gnop()
276{
277	if ! kldstat -q -m g_nop; then
278		geom nop load || atf_skip "could not load module for geom nop"
279	fi
280}
281