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