]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/tests/basic.sh
MFC r365967:
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / tests / basic.sh
1 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
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}
39 INV_OPT="-z"
40 INV_OPT_MSG="invalid option -- z"
41
42
43 atf_test_case fake_lib cleanup
44 fake_lib_head()
45 {
46         atf_set "descr" "check loading of a library from /lib"
47         atf_set "require.user" "root"
48 }
49 fake_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 }
61 fake_lib_cleanup()
62 {
63         rm -f /lib/nvmecontrol/fake.so
64 }
65
66 atf_test_case fake_local_lib cleanup
67 fake_local_lib_head()
68 {
69         atf_set "descr" "check loading of a library from /usr/local/lib"
70         atf_set "require.user" "root"
71 }
72 fake_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 }
84 fake_local_lib_cleanup()
85 {
86         rm -f /usr/local/lib/nvmecontrol/fake.so
87 }
88
89 atf_test_case admin_passthru
90 admin_passthru_head()
91 {
92         atf_set "descr" "check the admin-passthru command"
93         atf_set "require.user" "root"
94 }
95 admin_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
105 atf_test_case devlist
106 devlist_head()
107 {
108         atf_set "descr" "check the devlist command"
109         atf_set "require.user" "root"
110 }
111 devlist_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
121 atf_test_case identify
122 identify_head()
123 {
124         atf_set "descr" "check the identify command"
125         atf_set "require.user" "root"
126 }
127 identify_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
137 atf_test_case io_passthru
138 io_passthru_head()
139 {
140         atf_set "descr" "check the io-passthru command"
141         atf_set "require.user" "root"
142 }
143 io_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
153 atf_test_case logpage
154 logpage_head()
155 {
156         atf_set "descr" "check the logpage command"
157         atf_set "require.user" "root"
158 }
159 logpage_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
169 atf_test_case nsid
170 nsid_head()
171 {
172         atf_set "descr" "check the nsid command"
173         atf_set "require.user" "root"
174 }
175 nsid_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
185 atf_test_case power
186 power_head()
187 {
188         atf_set "descr" "check the power command"
189         atf_set "require.user" "root"
190 }
191 power_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
201 atf_test_case reset
202 reset_head()
203 {
204         atf_set "descr" "check the reset command"
205         atf_set "require.user" "root"
206 }
207 reset_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
224 atf_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 }