1# Copyright (c) 2018 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/part/misc.sh 329275 2018-02-14 20:26:09Z asomers $
26
27MD_DEVS="md.devs"
28
29atf_test_case blank_physpath cleanup
30blank_physpath_head()
31{
32	atf_set "descr" "gpart shouldn't add physical paths to underlying providers that have none"
33	atf_set "require.user" "root"
34}
35blank_physpath_body()
36{
37	load_gnop
38	load_gpart
39	md=$(alloc_md)
40	atf_check -o empty -e ignore diskinfo -p ${md}
41	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}
42	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}
43	atf_check -o empty -e ignore diskinfo -p ${md}a
44}
45blank_physpath_cleanup()
46{
47	common_cleanup
48}
49
50
51atf_test_case bsd_physpath cleanup
52bsd_physpath_head()
53{
54	atf_set "descr" "BSD partitions should append /X to the underlying device's physical path"
55	atf_set "require.user" "root"
56}
57bsd_physpath_body()
58{
59	load_gnop
60	load_gpart
61	md=$(alloc_md)
62	physpath="some/physical/path"
63	atf_check gnop create -z $physpath /dev/${md}
64	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}.nop
65	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}.nop
66	gpart_physpath=$(diskinfo -p ${md}.nopa)
67	atf_check_equal "${physpath}/a" "$gpart_physpath"
68}
69bsd_physpath_cleanup()
70{
71	common_cleanup
72}
73
74atf_test_case gpt_physpath cleanup
75gpt_physpath_head()
76{
77	atf_set "descr" "GPT partitions should append /pX to the underlying device's physical path"
78	atf_set "require.user" "root"
79}
80gpt_physpath_body()
81{
82	load_gnop
83	load_gpart
84	md=$(alloc_md)
85	physpath="some/physical/path"
86	atf_check gnop create -z $physpath /dev/${md}
87	atf_check -s exit:0 -o ignore gpart create -s gpt ${md}.nop
88	atf_check -s exit:0 -o ignore gpart add -t efi ${md}.nop
89	gpart_physpath=$(diskinfo -p ${md}.nopp1)
90	atf_check_equal "${physpath}/p1" "$gpart_physpath"
91}
92gpt_physpath_cleanup()
93{
94	common_cleanup
95}
96
97atf_test_case mbr_physpath cleanup
98mbr_physpath_head()
99{
100	atf_set "descr" "MBR partitions should append /sX to the underlying device's physical path"
101	atf_set "require.user" "root"
102}
103mbr_physpath_body()
104{
105	load_gnop
106	load_gpart
107	md=$(alloc_md)
108	physpath="some/physical/path"
109	atf_check gnop create -z $physpath /dev/${md}
110	atf_check -s exit:0 -o ignore gpart create -s mbr ${md}.nop
111	atf_check -s exit:0 -o ignore gpart add -t freebsd ${md}.nop
112	gpart_physpath=$(diskinfo -p ${md}.nops1)
113	atf_check_equal "${physpath}/s1" "$gpart_physpath"
114}
115mbr_physpath_cleanup()
116{
117	common_cleanup
118}
119
120atf_test_case mbr_bsd_physpath cleanup
121mbr_bsd_physpath_head()
122{
123	atf_set "descr" "BSD partitions nested within MBR partitions should append /sX/Y to the underlying device's physical path"
124	atf_set "require.user" "root"
125}
126mbr_bsd_physpath_body()
127{
128	load_gnop
129	load_gpart
130	md=$(alloc_md)
131	physpath="some/physical/path"
132	atf_check gnop create -z $physpath /dev/${md}
133	atf_check -s exit:0 -o ignore gpart create -s mbr ${md}.nop
134	atf_check -s exit:0 -o ignore gpart add -t freebsd ${md}.nop
135	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}.nops1
136	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}.nops1
137	gpart_physpath=$(diskinfo -p ${md}.nops1a)
138	atf_check_equal "${physpath}/s1/a" "$gpart_physpath"
139}
140mbr_bsd_physpath_cleanup()
141{
142	common_cleanup
143}
144
145atf_init_test_cases()
146{
147	atf_add_test_case blank_physpath
148	atf_add_test_case bsd_physpath
149	atf_add_test_case gpt_physpath
150	atf_add_test_case mbr_physpath
151	atf_add_test_case mbr_bsd_physpath
152}
153
154alloc_md()
155{
156	local md
157
158	md=$(mdconfig -a -t swap -s 1M) || atf_fail "mdconfig -a failed"
159	echo ${md} >> $MD_DEVS
160	echo ${md}
161}
162
163common_cleanup()
164{
165	if [ -f "$MD_DEVS" ]; then
166		while read test_md; do
167			gnop destroy -f ${test_md}.nop 2>/dev/null
168			mdconfig -d -u $test_md 2>/dev/null
169		done < $MD_DEVS
170		rm $MD_DEVS
171	fi
172	true
173}
174
175load_gpart()
176{
177	if ! kldstat -q -m g_part; then
178		geom part load || atf_skip "could not load module for geom part"
179	fi
180}
181
182load_gnop()
183{
184	if ! kldstat -q -m g_nop; then
185		geom nop load || atf_skip "could not load module for geom nop"
186	fi
187}
188