1 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 # Copyright (c) 2020 David A. Bright
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
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.
34 DANGEROUS=false # Set to true to run "dangerous" tests
35 # Select a nvme device to use for testing. If none exist, use nvme0.
36 TEST_DEV=$(cd /dev/; ls -1 nvme[0-9]* 2> /dev/null | grep -E 'nvme[0-9][0-9]*$' | head -n 1)
37 TEST_DEV=${TEST_DEV:-nvme0}
38 TEST_DEV_PATH=/dev/${TEST_DEV}
40 INV_OPT_MSG="invalid option -- z"
43 atf_test_case fake_lib cleanup
46 atf_set "descr" "check loading of a library from /lib"
47 atf_set "require.user" "root"
51 local libdir="/lib/nvmecontrol"
52 local fakelib="${libdir}/fake.so"
53 if [ -d ${libdir} ] ; then
55 atf_check -s not-exit:0 -o ignore -e match:"Can't load ${fakelib}" nvmecontrol
58 atf_skip "Skipping; directory ${libdir} does not exist"
63 rm -f /lib/nvmecontrol/fake.so
66 atf_test_case fake_local_lib cleanup
69 atf_set "descr" "check loading of a library from /usr/local/lib"
70 atf_set "require.user" "root"
74 local libdir="/usr/local/lib/nvmecontrol"
75 local fakelib="${libdir}/fake.so"
76 if [ -d ${libdir} ] ; then
78 atf_check -s not-exit:0 -o ignore -e match:"Can't load ${fakelib}" nvmecontrol
81 atf_skip "Skipping; directory ${libdir} does not exist"
84 fake_local_lib_cleanup()
86 rm -f /usr/local/lib/nvmecontrol/fake.so
89 atf_test_case admin_passthru
92 atf_set "descr" "check the admin-passthru command"
93 atf_set "require.user" "root"
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}
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}
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}
105 atf_test_case devlist
108 atf_set "descr" "check the devlist command"
109 atf_set "require.user" "root"
113 if [ -c "${TEST_DEV_PATH}" ] ; then
114 atf_check -o not-empty -e empty nvmecontrol devlist
116 atf_check -s not-exit:0 -o ignore -e ignore nvmecontrol devlist
118 atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol devlist ${INV_OPT}
121 atf_test_case identify
124 atf_set "descr" "check the identify command"
125 atf_set "require.user" "root"
129 if [ -c "${TEST_DEV_PATH}" ] ; then
130 atf_check -o not-empty -e empty nvmecontrol identify ${TEST_DEV}
132 atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol identify ${TEST_DEV}
134 atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol identify ${INV_OPT} ${TEST_DEV}
137 atf_test_case io_passthru
140 atf_set "descr" "check the io-passthru command"
141 atf_set "require.user" "root"
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}
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}
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}
153 atf_test_case logpage
156 atf_set "descr" "check the logpage command"
157 atf_set "require.user" "root"
161 if [ -c "${TEST_DEV_PATH}" ] ; then
162 atf_check -o not-empty -e empty nvmecontrol logpage -p 1 ${TEST_DEV}
164 atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol logpage -p 1 ${TEST_DEV}
166 atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol logpage -p 1 ${INV_OPT} ${TEST_DEV}
172 atf_set "descr" "check the nsid command"
173 atf_set "require.user" "root"
177 if [ -c "${TEST_DEV_PATH}" ] ; then
178 atf_check -o not-empty -e empty nvmecontrol nsid ${TEST_DEV}
180 atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol nsid ${TEST_DEV}
182 atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol nsid ${INV_OPT} ${TEST_DEV}
188 atf_set "descr" "check the power command"
189 atf_set "require.user" "root"
193 if [ -c "${TEST_DEV_PATH}" ] ; then
194 atf_check -o not-empty -e empty nvmecontrol power ${TEST_DEV}
196 atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol power ${TEST_DEV}
198 atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol power ${INV_OPT} ${TEST_DEV}
204 atf_set "descr" "check the reset command"
205 atf_set "require.user" "root"
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}
216 atf_skip "Skipping reset test"
219 atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol reset ${TEST_DEV}
224 atf_init_test_cases()
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