Skip to content

Commit c88cd78

Browse files
committed
nvme/066: test NVMe host driver code for NVME SED operations
Add comprehensive test coverage for NVMe host driver code about NVMe Self-Encrypting Drive operations including discover, initialize, lock/unlock, password management, and revert functionality. This test verifies the complete SED lifecycle: - Discovery of SED capabilities and initial state - SED initialization with password setup - Device locking and unlocking operations - Password change functionality - Regular revert to disable SED - Destructive revert testing The test uses expect scripts to handle interactive password prompts and includes proper verification of each operation through discovery output parsing. Also adds helper functions to the nvme test framework for SED support detection and nvme-cli SED plugin availability checking. Signed-off-by: Yi Zhang <[email protected]>
1 parent 6b00475 commit c88cd78

3 files changed

Lines changed: 211 additions & 0 deletions

File tree

tests/nvme/066

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-3.0+
3+
# Copyright (C) 2025 Yi Zhang <[email protected]>
4+
#
5+
# Test NVMe host driver code for NVME SED (Self-Encryptiong Drive) operations
6+
# including discover, initialize, lock/unlock, password change and revert
7+
#
8+
9+
. tests/nvme/rc
10+
11+
DESCRIPTION="Test NVMe host driver code for NVME SED operations"
12+
QUICK=1
13+
14+
# SED test passwords
15+
SED_PASSWORD="password"
16+
SED_NEW_PASSWORD="PASSWORD"
17+
18+
requires() {
19+
_nvme_requires
20+
_have_kernel_option BLK_SED_OPAL
21+
_have_program expect
22+
_have_program nvme
23+
_require_nvme_cli_sed
24+
}
25+
26+
device_requires() {
27+
_require_test_dev_is_nvme
28+
_require_test_dev_support_sed
29+
}
30+
31+
nvme_sed_discover() {
32+
local device=$1
33+
34+
nvme sed discover "$device"
35+
}
36+
37+
nvme_sed_init() {
38+
local device=$1
39+
40+
expect <<EOF
41+
spawn nvme sed initialize $device
42+
expect "New Password:"
43+
send "$SED_PASSWORD\r"
44+
expect "Re-enter New Password:"
45+
send "$SED_PASSWORD\r"
46+
expect eof
47+
EOF
48+
}
49+
50+
nvme_sed_lock() {
51+
local device=$1
52+
53+
nvme sed lock "$device"
54+
}
55+
56+
nvme_sed_unlock() {
57+
local device=$1
58+
59+
nvme sed unlock "$device"
60+
}
61+
62+
nvme_sed_change_password() {
63+
local device=$1
64+
65+
expect <<EOF
66+
spawn nvme sed password $device
67+
expect "Password:"
68+
send "$SED_PASSWORD\r"
69+
expect "New Password:"
70+
send "$SED_NEW_PASSWORD\r"
71+
expect "Re-enter New Password:"
72+
send "$SED_NEW_PASSWORD\r"
73+
expect eof
74+
EOF
75+
}
76+
77+
nvme_sed_revert() {
78+
local device=$1
79+
80+
expect <<EOF
81+
spawn nvme sed revert $device
82+
expect "Password:"
83+
send "$SED_NEW_PASSWORD\r"
84+
expect eof
85+
EOF
86+
}
87+
88+
nvme_sed_revert_destructive() {
89+
local device=$1
90+
91+
expect <<EOF
92+
spawn nvme sed revert -e $device
93+
expect "Destructive revert erases drive data. Continue (y/n)?"
94+
send "y\r"
95+
expect "Are you sure (y/n)?"
96+
send "y\r"
97+
expect "Password:"
98+
send "$SED_PASSWORD\r"
99+
expect eof
100+
EOF
101+
}
102+
103+
test_device() {
104+
echo "Running ${TEST_NAME}"
105+
106+
# Test 1: Discover initial state
107+
echo "Initial SED state:"
108+
nvme_sed_discover "$TEST_DEV" | grep -E "Locking|Locked"
109+
110+
# Test 2: Initialize SED
111+
echo "Initializing SED..."
112+
nvme_sed_init "$TEST_DEV" > /dev/null 2>&1
113+
114+
# Verify initialization
115+
if nvme_sed_discover "$TEST_DEV" | grep -q "Locking Feature Enabled.*: yes"; then
116+
echo "SED initialization successful"
117+
else
118+
echo "SED initialization failed"
119+
return
120+
fi
121+
122+
# Test 3: Lock the device
123+
echo "Locking device..."
124+
nvme_sed_lock "$TEST_DEV"
125+
126+
# Verify lock
127+
if nvme_sed_discover "$TEST_DEV" | grep -q "Locked.*: yes"; then
128+
echo "Device locked successfully"
129+
else
130+
echo "Device lock failed"
131+
fi
132+
133+
# Test 4: Unlock the device
134+
echo "Unlocking device..."
135+
nvme_sed_unlock "$TEST_DEV"
136+
137+
# Verify unlock
138+
if nvme_sed_discover "$TEST_DEV" | grep -q "Locked.*: no"; then
139+
echo "Device unlocked successfully"
140+
else
141+
echo "Device unlock failed"
142+
fi
143+
144+
# Test 5: Change password
145+
echo "Changing password..."
146+
nvme_sed_change_password "$TEST_DEV" &> /dev/null
147+
148+
# Test 6: Revert to disable locking
149+
echo "Reverting SED..."
150+
nvme_sed_revert "$TEST_DEV" &> /dev/null
151+
152+
# Verify revert
153+
if nvme_sed_discover "$TEST_DEV" | grep -q "Locking Feature Enabled.*: no"; then
154+
echo "SED revert successful"
155+
else
156+
echo "SED revert failed"
157+
fi
158+
159+
# Test 7: Reset to disable locking with destructive revert
160+
echo "Destructive revert SED..."
161+
nvme_sed_init "$TEST_DEV" &> /dev/null
162+
nvme_sed_lock "$TEST_DEV"
163+
nvme_sed_revert_destructive "$TEST_DEV" &> /dev/null
164+
165+
# Verify destructive revert
166+
if nvme_sed_discover "$TEST_DEV" | grep -q "Locking Feature Enabled.*: no"; then
167+
echo "Destructive revert successful"
168+
else
169+
echo "Destructive revert failed"
170+
fi
171+
172+
echo "Test complete"
173+
}

tests/nvme/066.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Running nvme/066
2+
Initial SED state:
3+
Locking Features:
4+
Locking Supported : yes
5+
Locking Feature Enabled : no
6+
Locked : no
7+
Initializing SED...
8+
SED initialization successful
9+
Locking device...
10+
Device locked successfully
11+
Unlocking device...
12+
Device unlocked successfully
13+
Changing password...
14+
Reverting SED...
15+
SED revert successful
16+
Destructive revert SED...
17+
Destructive revert successful
18+
Test complete

tests/nvme/rc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ _require_test_dev_is_not_nvme_multipath() {
153153
return 0
154154
}
155155

156+
_require_test_dev_support_sed() {
157+
if ! nvme sed discover "$TEST_DEV" &> /dev/null; then
158+
SKIP_REASONS+=("$TEST_DEV doesn't support SED operations")
159+
return 1
160+
fi
161+
if nvme sed discover "$TEST_DEV" | grep -q "Locking Supported.*: yes"; then
162+
return 0
163+
fi
164+
SKIP_REASONS+=("$TEST_DEV doesn't support SED operations")
165+
return 1
166+
}
167+
156168
_test_dev_has_metadata() {
157169
if [ ! -e "${TEST_DEV_SYSFS}/metadata_bytes" ] || \
158170
(( ! $(<"${TEST_DEV_SYSFS}/metadata_bytes") )); then
@@ -216,6 +228,14 @@ _require_nvme_cli_tls() {
216228
return 0
217229
}
218230

231+
_require_nvme_cli_sed() {
232+
if ! nvme sed help &> /dev/null; then
233+
SKIP_REASONS+=("nvme doesn't support sed plugin")
234+
return 1
235+
fi
236+
return 0
237+
}
238+
219239
_require_kernel_nvme_fabrics_feature() {
220240
local feature="$1"
221241

0 commit comments

Comments
 (0)