xref: /freebsd/sbin/nvmecontrol/tests/basic.sh (revision a259b98fa211ed87bfee58c575de4e2de94ee0fa)
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	atf_set "require.progs" nvmecontrol
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	atf_set "require.progs" nvmecontrol
72}
73fake_local_lib_body()
74{
75	local libdir="/usr/local/lib/nvmecontrol"
76	local fakelib="${libdir}/fake.so"
77	if [ -d ${libdir} ] ; then
78		touch ${fakelib}
79		atf_check -s not-exit:0 -o ignore -e match:"Can't load ${fakelib}" nvmecontrol
80		rm -f ${fakelib}
81	else
82		atf_skip "Skipping; directory ${libdir} does not exist"
83	fi
84}
85fake_local_lib_cleanup()
86{
87	rm -f /usr/local/lib/nvmecontrol/fake.so
88}
89
90atf_test_case admin_passthru
91admin_passthru_head()
92{
93	atf_set "descr" "check the admin-passthru command"
94	atf_set "require.user" "root"
95	atf_set "require.progs" nvmecontrol
96}
97admin_passthru_body()
98{
99	if [ -c "${TEST_DEV_PATH}" ] ; then
100		atf_check -o not-empty -e empty nvmecontrol admin-passthru --opcode=06 --data-len=4096 --cdw10=1 -r ${TEST_DEV}
101	else
102		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol admin-passthru --opcode=06 --data-len=4096 --cdw10=1 -r ${TEST_DEV}
103	fi
104	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 ${TEST_DEV}
105}
106
107atf_test_case devlist
108devlist_head()
109{
110	atf_set "descr" "check the devlist command"
111	atf_set "require.user" "root"
112	atf_set "require.progs" nvmecontrol
113}
114devlist_body()
115{
116	if [ -c "${TEST_DEV_PATH}" ] ; then
117		atf_check -o not-empty -e empty nvmecontrol devlist
118	else
119		atf_check -s not-exit:0 -o ignore -e ignore nvmecontrol devlist
120	fi
121	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol devlist ${INV_OPT}
122}
123
124atf_test_case identify
125identify_head()
126{
127	atf_set "descr" "check the identify command"
128	atf_set "require.user" "root"
129	atf_set "require.progs" nvmecontrol
130}
131identify_body()
132{
133	if [ -c "${TEST_DEV_PATH}" ] ; then
134		atf_check -o not-empty -e empty nvmecontrol identify ${TEST_DEV}
135	else
136		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol identify ${TEST_DEV}
137	fi
138	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol identify ${INV_OPT} ${TEST_DEV}
139}
140
141atf_test_case io_passthru
142io_passthru_head()
143{
144	atf_set "descr" "check the io-passthru command"
145	atf_set "require.user" "root"
146	atf_set "require.progs" nvmecontrol
147}
148io_passthru_body()
149{
150	if [ -c "${TEST_DEV_PATH}" ] ; then
151		atf_check -o not-empty -e empty nvmecontrol io-passthru --opcode=02 --data-len=4096 --cdw10=0 --cdw11=0 --cdw12=0x70000 -r nvme0 ${TEST_DEV}
152	else
153		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 nvme0 ${TEST_DEV}
154	fi
155	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 nvme0 ${TEST_DEV}
156}
157
158atf_test_case logpage
159logpage_head()
160{
161	atf_set "descr" "check the logpage command"
162	atf_set "require.user" "root"
163	atf_set "require.progs" nvmecontrol
164}
165logpage_body()
166{
167	if [ -c "${TEST_DEV_PATH}" ] ; then
168		atf_check -o not-empty -e empty nvmecontrol logpage -p 1 ${TEST_DEV}
169	else
170		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol logpage -p 1 ${TEST_DEV}
171	fi
172	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol logpage -p 1 ${INV_OPT} ${TEST_DEV}
173}
174
175atf_test_case nsid
176nsid_head()
177{
178	atf_set "descr" "check the nsid command"
179	atf_set "require.user" "root"
180	atf_set "require.progs" nvmecontrol
181}
182nsid_body()
183{
184	if [ -c "${TEST_DEV_PATH}" ] ; then
185		atf_check -o not-empty -e empty nvmecontrol nsid ${TEST_DEV}
186	else
187		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol nsid ${TEST_DEV}
188	fi
189	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol nsid ${INV_OPT} ${TEST_DEV}
190}
191
192atf_test_case power
193power_head()
194{
195	atf_set "descr" "check the power command"
196	atf_set "require.user" "root"
197	atf_set "require.progs" nvmecontrol
198}
199power_body()
200{
201	if [ -c "${TEST_DEV_PATH}" ] ; then
202		atf_check -o not-empty -e empty nvmecontrol power ${TEST_DEV}
203	else
204		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol power ${TEST_DEV}
205	fi
206	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol power ${INV_OPT} ${TEST_DEV}
207}
208
209atf_test_case reset
210reset_head()
211{
212	atf_set "descr" "check the reset command"
213	atf_set "require.user" "root"
214	atf_set "require.progs" nvmecontrol
215}
216reset_body()
217{
218	atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol reset ${INV_OPT} ${TEST_DEV}
219	if [ -c "${TEST_DEV_PATH}" ] ; then
220		# Reset of an active device seems a little dangerous,
221		# therefore, this is normally disabled.
222	    	if ${DANGEROUS} ; then
223			atf_check -o not-empty -e empty nvmecontrol reset ${TEST_DEV}
224		else
225			atf_skip "Skipping reset test"
226		fi
227	else
228		atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol reset ${TEST_DEV}
229	fi
230}
231
232
233atf_init_test_cases()
234{
235	atf_add_test_case fake_lib
236	atf_add_test_case fake_local_lib
237	atf_add_test_case admin_passthru
238	atf_add_test_case devlist
239	atf_add_test_case identify
240	atf_add_test_case io_passthru
241	atf_add_test_case logpage
242	atf_add_test_case nsid
243	atf_add_test_case power
244	atf_add_test_case reset
245}
246