6
6
Tests end-user scenarios for sedcli binary.
7
7
This suite needs to be run against Python 3.x and the sedcli binary must be
8
8
in your $PATH.
9
+ The user must provide:
10
+ 1. Device Path
11
+ 2. Device PSID
9
12
"""
10
13
11
14
import os
@@ -27,42 +30,133 @@ def gen_rand_key(key_len):
27
30
letters = string .ascii_lowercase
28
31
return '' .join (random .choice (letters ) for i in range (key_len )).encode ('ascii' )
29
32
33
+ def read_opal_drive (device_path ):
34
+ # Read from the disk
35
+ pipe = subprocess .Popen (["dd" , "if=" + device_path , "of=/dev/null" , "bs=4k" , "count=100" , "iflag=direct" ], stdout = subprocess .PIPE , stdin = subprocess .PIPE , stderr = subprocess .PIPE )
36
+ err = pipe .communicate ()
37
+ return pipe .returncode
38
+
39
+ def write_opal_drive (device_path ):
40
+ # Write to the disk
41
+ pipe = subprocess .Popen (["dd" , "if=/dev/zero" , "of=" + device_path , "bs=4k" , "count=100" , "oflag=direct" ], stdout = subprocess .PIPE , stdin = subprocess .PIPE , stderr = subprocess .PIPE )
42
+ err = pipe .communicate ()
43
+ return pipe .returncode
44
+
45
+ nvme_sid_key = gen_rand_key (32 )
46
+ print ("\n Key used to initial provision drive:" + str (nvme_sid_key ) + "\n " )
47
+ f = open ("random_pswd.txt" , "w+" )
48
+ f .write ("Randomly Generated SID password to initially provision drive: " + str (nvme_sid_key ) + "\n " )
49
+
30
50
class TestSedcliBasic (unittest .TestCase ):
31
51
32
52
NVME_DEV_PATH = '/dev/nvme0n1'
33
53
34
54
def __init__ (self , * args , ** kwargs ):
35
55
super (TestSedcliBasic , self ).__init__ (* args , ** kwargs )
36
56
37
- self .nvme_sid_key = gen_rand_key (32 )
38
-
39
57
def test_initial_setup (self ):
40
58
"""
41
59
Tests for: sedcli intial provision of drive (that is take ownerhisp,
42
60
activate Locking SP and setup global locking range)
43
61
"""
44
- print ("Key used to initial provision drive:" + str (self .nvme_sid_key ))
45
-
46
62
# Test taking ownership (test is assuming that drive is in Manufactured-inactive state)
47
- outs , errs = run_cmd (["sedcli" , "--ownership" , "--device" , self .NVME_DEV_PATH ], self . nvme_sid_key + b'\n ' + self . nvme_sid_key + b'\n ' )
63
+ outs , errs = run_cmd (["sedcli" , "--ownership" , "--device" , self .NVME_DEV_PATH ], nvme_sid_key + b'\n ' + nvme_sid_key + b'\n ' )
48
64
49
65
self .assertEqual (pipe .returncode , 0 , "sedcli returned error while taking ownership of the drive:\n " + str (errs ))
50
66
self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
51
67
52
68
# Activate Locking SP
53
- outs , errs = run_cmd (["sedcli" , "--activate-lsp" , "--device" , self .NVME_DEV_PATH ], self . nvme_sid_key + b'\n ' )
69
+ outs , errs = run_cmd (["sedcli" , "--activate-lsp" , "--device" , self .NVME_DEV_PATH ], nvme_sid_key + b'\n ' )
54
70
55
71
self .assertEqual (pipe .returncode , 0 , "sedcli returned error while activating Locking SP:\n " + str (errs ))
56
72
self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
57
73
58
74
# Set RLE and WLE on global locking range
59
- outs , errs = run_cmd (["sedcli" , "--setup-global-range" , "--device" , self .NVME_DEV_PATH ], self . nvme_sid_key + b'\n ' )
75
+ outs , errs = run_cmd (["sedcli" , "--setup-global-range" , "--device" , self .NVME_DEV_PATH ], nvme_sid_key + b'\n ' )
60
76
61
77
self .assertEqual (pipe .returncode , 0 , "sedcli returned error while setting RLE and WLE on global range:\n " + str (errs ))
62
78
self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
63
79
80
+ def test_locking_range_test (self ):
81
+ # Read Lock Disk
82
+ outs , errs = run_cmd (["sedcli" , "--lock-unlock" , "--device" , self .NVME_DEV_PATH , "--accesstype" , "RO" ], nvme_sid_key + b'\n ' )
83
+
84
+ self .assertEqual (pipe .returncode , 0 , "sedcli returned error while Read-Only locking the disk:\n " + str (errs ))
85
+ self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
86
+
87
+ read = read_opal_drive (TestSedcliBasic .NVME_DEV_PATH )
88
+ self .assertEqual (read , 0 , "Unable to read data from the disk when the user is supposed to: " + str (errs ))
89
+
90
+ write = write_opal_drive (TestSedcliBasic .NVME_DEV_PATH )
91
+ self .assertEqual (write , 1 , "Able to write data to the disk when the user is NOT supposed to: " + str (errs ))
92
+
93
+ # ReadWrite Lock Disk
94
+ outs , errs = run_cmd (["sedcli" , "--lock-unlock" , "--device" , self .NVME_DEV_PATH , "--accesstype" , "RW" ], nvme_sid_key + b'\n ' )
95
+
96
+ self .assertEqual (pipe .returncode , 0 , "sedcli returned error while Read-Write locking the disk:\n " + str (errs ))
97
+ self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
98
+
99
+ read = read_opal_drive (TestSedcliBasic .NVME_DEV_PATH )
100
+ self .assertEqual (read , 0 , "Unable to read data from the disk when the user is supposed to: " + str (errs ))
101
+
102
+ write = write_opal_drive (TestSedcliBasic .NVME_DEV_PATH )
103
+ self .assertEqual (write , 0 , "Unable to write data to the disk when the user is supposed to: " + str (errs ))
104
+
105
+ # Lock Disk
106
+ outs , errs = run_cmd (["sedcli" , "--lock-unlock" , "--device" , self .NVME_DEV_PATH , "--accesstype" , "LK" ], nvme_sid_key + b'\n ' )
107
+
108
+ self .assertEqual (pipe .returncode , 0 , "sedcli returned error while Locking the disk:\n " + str (errs ))
109
+ self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
110
+
111
+ read = read_opal_drive (TestSedcliBasic .NVME_DEV_PATH )
112
+ self .assertEqual (read , 1 , "Able to read data from the disk when the user is NOT supposed to: " + str (errs ))
113
+
114
+ write = write_opal_drive (TestSedcliBasic .NVME_DEV_PATH )
115
+ self .assertEqual (write , 1 , "Able to write data to the disk when the user is NOT supposed to: " + str (errs ))
116
+
117
+ # Randomly generate the new amin1 password
118
+ self .new_admin1_paswd = gen_rand_key (32 )
119
+ f .write ("Radomly generated new Admin-1 Passwd: " + str (self .new_admin1_paswd ) + "\n " )
120
+ f .close ()
121
+
122
+ # Set password (Update/Change password for Admin1 authority in Locking SP)
123
+ outs , errs = run_cmd (["sedcli" , "--set-password" , "--device" , self .NVME_DEV_PATH ], nvme_sid_key + b'\n ' + self .new_admin1_paswd + b'\n ' + self .new_admin1_paswd + b'\n ' )
124
+
125
+ self .assertEqual (pipe .returncode , 0 , "sedcli returned error while Setting new password for the disk:\n " + str (errs ))
126
+ self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
127
+
128
+ # Now check if the newly set password works fine
129
+ # 1. Try to Set RLE and WLE on global locking range using old Admin-1 password
130
+ outs , errs = run_cmd (["sedcli" , "--setup-global-range" , "--device" , self .NVME_DEV_PATH ], nvme_sid_key + b'\n ' )
131
+
132
+ self .assertEqual (pipe .returncode , 1 , "Able to access LSP using the old admin-1 password: " + str (errs ))
133
+
134
+ # 2. Try to Set RLE and WLE on global locking range usign new Admin-1 password
135
+ outs , errs = run_cmd (["sedcli" , "--setup-global-range" , "--device" , self .NVME_DEV_PATH ], self .new_admin1_paswd + b'\n ' )
136
+
137
+ self .assertEqual (pipe .returncode , 0 , "sedcli returned error while setting RLE and WLE on global range with new admin-1 password:\n " + str (errs ))
138
+ self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
139
+
140
+ def test_revert_drive (self ):
141
+ # Revert the TPer with SID authority
142
+ outs , errs = run_cmd (["sedcli" , "--revert" , "--device" , self .NVME_DEV_PATH ], nvme_sid_key + b'\n ' )
143
+
144
+ self .assertEqual (pipe .returncode , 0 , "sedcli returned error while Reverting TPer using SID authority:\n " + str (errs ))
145
+ self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
146
+
147
+ # Revert the TPer with PSID authority
148
+ outs , errs = run_cmd (["sedcli" , "--revert" , "--device" , self .NVME_DEV_PATH , "--psid" ], TestSedcliBasic .NVME_DEV_PSID + b'\n ' )
149
+
150
+ self .assertEqual (pipe .returncode , 0 , "sedcli returned error while Reverting TPer using PSID authority:\n " + str (errs ))
151
+ self .assertEqual (len (errs ), 0 , "sedcli returned success code but error message has been printed on stderr:\n " + str (errs ))
152
+
64
153
# -----------------------------------------------------------------------------
65
154
if __name__ == '__main__' :
66
- if len (sys .argv ) > 1 :
155
+ if len (sys .argv ) == 3 :
156
+ TestSedcliBasic .NVME_DEV_PSID = bytes (sys .argv .pop (), 'utf-8' )
67
157
TestSedcliBasic .NVME_DEV_PATH = sys .argv .pop ()
68
- unittest .main ()
158
+ print ("Provided Device-Path: " + TestSedcliBasic .NVME_DEV_PATH )
159
+ print (b'Provided Device-PSID: ' + TestSedcliBasic .NVME_DEV_PSID )
160
+ unittest .main ()
161
+ else :
162
+ print ("The user must provide\n \t 1. Device Path\n \t 2. Device PSID\n " )
0 commit comments