xref: /freebsd/sbin/nvmecontrol/tests/basic.sh (revision 02e9120893770924227138ba49df1edb3896112a)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# Copyright (c) 2020 David A. Bright
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#
28# A basic test for nvmecontrol. This isn't a thorough or complete test
29# of nvmecontrol functionality; it is more of a sanity check that
30# nvmecontrol basically works.
31#
32
33DANGEROUS=false # Set to true to run "dangerous" tests
34# Select a nvme device to use for testing. If none exist, use nvme0.
35TEST_DEV=$(cd /dev/; ls -1 nvme[0-9]* 2> /dev/null | grep -E 'nvme[0-9][0-9]*$' | head -n 1)
36TEST_DEV=${TEST_DEV:-nvme0}
37TEST_DEV_PATH=/dev/${TEST_DEV}
38INV_OPT="-z"
39INV_OPT_MSG="invalid option -- z"
40
41
42atf_test_case fake_lib cleanup
43fake_lib_head()
44{
45	atf_set "descr" "check loading of a library from /lib"
46	atf_set "require.user" "root"
47}
48fake_lib_body()
49{
50	local libdir="/lib/nvmecontrol"
51	local fakelib="${libdir}/fake.so"
52	if [ -d ${libdir} ] ; then
53		touch ${fakelib}
54		atf_check -s not-exit:0 -o ignore -e match:"Can't load ${fakelib}" nvmecontrol
55		rm -f ${fakelib}
56	else
57		atf_skip "Skipping; directory ${libdir} does not exist"
58	fi
59}
60fake_lib_cleanup()
61{
62	rm -f /lib/nvmecontrol/fake.so
63}
64
65atf_test_case fake_local_lib cleanup
66fake_local_lib_head()
67{
68	atf_set "descr" "check loading of a library from /usr/local/lib"
69	atf_set "require.user" "root"
70}
71fake_local_lib_body()
72{
73	local libdir="/usr/local/lib/nvmecontrol"
74	local fakelib="${libdir}/fake.so"
75	if [ -d ${libdir} ] ; then
76		touch ${fakelib}
77		atf_check -s not-exit:0 -o ignore -e match:"Can't load ${fakelib}" nvmecontrol
78		rm -f ${fakelib}
79	else
80		atf_skip "Skipping; directory ${libdir} does not exist"
81	fi
82}
83fake_local_lib_cleanup()
84{
85	rm -f /usr/local/lib/nvmecontrol/fake.so
86}
87
88atf_test_case admin_passthru
89admin_passthru_head()
90{
91	atf_set "descr" "check the admin-passthru command"
92	atf_set "require.user" "root"
93}
94admin_passthru_body()
95{
96	if [ -c "${TEST_DEV_PATH}" ] ; then
97		atf_check -o not-empty -e empty nvmecontrol admin-passthru --opcode=06 --data-len=4096 --cdw10=1 -r 0 ${TEST_DEV}
98	else
99		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol admin-passthru --opcode=06 --data-len=4096 --cdw10=1 -r 0 ${TEST_DEV}
100	fi
101	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol admin-passthru ${INV_OPT} --opcode=06 --data-len=4096 --cdw10=1 -r 0 ${TEST_DEV}
102}
103
104atf_test_case devlist
105devlist_head()
106{
107	atf_set "descr" "check the devlist command"
108	atf_set "require.user" "root"
109}
110devlist_body()
111{
112	if [ -c "${TEST_DEV_PATH}" ] ; then
113		atf_check -o not-empty -e empty nvmecontrol devlist
114	else
115		atf_check -s not-exit:0 -o ignore -e ignore nvmecontrol devlist
116	fi
117	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol devlist ${INV_OPT}
118}
119
120atf_test_case identify
121identify_head()
122{
123	atf_set "descr" "check the identify command"
124	atf_set "require.user" "root"
125}
126identify_body()
127{
128	if [ -c "${TEST_DEV_PATH}" ] ; then
129		atf_check -o not-empty -e empty nvmecontrol identify ${TEST_DEV}
130	else
131		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol identify ${TEST_DEV}
132	fi
133	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol identify ${INV_OPT} ${TEST_DEV}
134}
135
136atf_test_case io_passthru
137io_passthru_head()
138{
139	atf_set "descr" "check the io-passthru command"
140	atf_set "require.user" "root"
141}
142io_passthru_body()
143{
144	if [ -c "${TEST_DEV_PATH}" ] ; then
145		atf_check -o not-empty -e empty nvmecontrol io-passthru --opcode=02 --data-len=4096 --cdw10=0 --cdw11=0 --cdw12=0x70000 -r 0 nvme0 ${TEST_DEV}
146	else
147		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol io-passthru --opcode=02 --data-len=4096 --cdw10=0 --cdw11=0 --cdw12=0x70000 -r 0 nvme0 ${TEST_DEV}
148	fi
149	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol io-passthru ${INV_OPT} --opcode=02 --data-len=4096 --cdw10=0 --cdw11=0 --cdw12=0x70000 -r 0 nvme0 ${TEST_DEV}
150}
151
152atf_test_case logpage
153logpage_head()
154{
155	atf_set "descr" "check the logpage command"
156	atf_set "require.user" "root"
157}
158logpage_body()
159{
160	if [ -c "${TEST_DEV_PATH}" ] ; then
161		atf_check -o not-empty -e empty nvmecontrol logpage -p 1 ${TEST_DEV}
162	else
163		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol logpage -p 1 ${TEST_DEV}
164	fi
165	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol logpage -p 1 ${INV_OPT} ${TEST_DEV}
166}
167
168atf_test_case nsid
169nsid_head()
170{
171	atf_set "descr" "check the nsid command"
172	atf_set "require.user" "root"
173}
174nsid_body()
175{
176	if [ -c "${TEST_DEV_PATH}" ] ; then
177		atf_check -o not-empty -e empty nvmecontrol nsid ${TEST_DEV}
178	else
179		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol nsid ${TEST_DEV}
180	fi
181	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol nsid ${INV_OPT} ${TEST_DEV}
182}
183
184atf_test_case power
185power_head()
186{
187	atf_set "descr" "check the power command"
188	atf_set "require.user" "root"
189}
190power_body()
191{
192	if [ -c "${TEST_DEV_PATH}" ] ; then
193		atf_check -o not-empty -e empty nvmecontrol power ${TEST_DEV}
194	else
195		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol power ${TEST_DEV}
196	fi
197	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol power ${INV_OPT} ${TEST_DEV}
198}
199
200atf_test_case reset
201reset_head()
202{
203	atf_set "descr" "check the reset command"
204	atf_set "require.user" "root"
205}
206reset_body()
207{
208	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol reset ${INV_OPT} ${TEST_DEV}
209	if [ -c "${TEST_DEV_PATH}" ] ; then
210		# Reset of an active device seems a little dangerous,
211		# therefore, this is normally disabled.
212	    	if ${DANGEROUS} ; then
213			atf_check -o not-empty -e empty nvmecontrol reset ${TEST_DEV}
214		else
215			atf_skip "Skipping reset test"
216		fi
217	else
218		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol reset ${TEST_DEV}
219	fi
220}
221
222
223atf_init_test_cases()
224{
225	atf_add_test_case fake_lib
226	atf_add_test_case fake_local_lib
227	atf_add_test_case admin_passthru
228	atf_add_test_case devlist
229	atf_add_test_case identify
230	atf_add_test_case io_passthru
231	atf_add_test_case logpage
232	atf_add_test_case nsid
233	atf_add_test_case power
234	atf_add_test_case reset
235}
236