-
Notifications
You must be signed in to change notification settings - Fork 14
/
device_test.go
262 lines (200 loc) · 7.33 KB
/
device_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package cryptsetup
import (
"testing"
)
func Test_Device_Init_Works_If_Device_Is_Found(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
if device.Type() != "" {
test.Error("Device should have no type.")
}
}
func Test_Device_Init_Fails_If_Device_Is_Not_Found(test *testing.T) {
testWrapper := TestWrapper{test}
_, err := Init("nonExistingDevicePath")
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -15)
}
func Test_Device_InitByName_Fails_If_Device_Is_Not_Active(test *testing.T) {
testWrapper := TestWrapper{test}
_, err := InitByName("nonExistingMappedDevice")
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -19)
}
func Test_Device_Free_Works(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.Format(
LUKS1{Hash: "sha256"},
GenericParams{Cipher: "aes", CipherMode: "xts-plain64", VolumeKeySize: 512 / 8},
)
testWrapper.AssertNoError(err)
code := device.Dump()
if code != 0 {
test.Error("Dump() should have returned `0`.")
}
if device.Free() != true {
test.Error("Free should have returned `true`.")
}
code = device.Dump()
if code != -22 {
test.Error("Dump() should have returned `-22`.")
}
}
func Test_Device_Free_Doesnt_Fail_For_Empty_Device(test *testing.T) {
device := &Device{}
if device.Free() != true {
test.Error("Free should have returned `true`.")
}
if device.Free() != false {
test.Error("Free should have returned `false`.")
}
}
func Test_Device_Free_Doesnt_Fail_If_Called_Multiple_Times(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.Format(
LUKS1{Hash: "sha256"},
GenericParams{Cipher: "aes", CipherMode: "xts-plain64", VolumeKeySize: 512 / 8},
)
testWrapper.AssertNoError(err)
if device.Free() != true {
test.Error("Free should have returned `true`.")
}
if device.Free() != false {
test.Error("Free should have returned `false`.")
}
}
func Test_Device_Deactivate_Fails_If_Device_Is_Not_Active(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.Deactivate(DeviceName)
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -19)
}
func Test_Device_ActivateByPassphrase_Fails_If_Device_Has_No_Type(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.ActivateByPassphrase(DeviceName, 0, "testPassphrase", CRYPT_ACTIVATE_READONLY)
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -22)
}
func Test_Device_ActivateByVolumeKey_Fails_If_Device_Has_No_Type(test *testing.T) {
testWrapper := TestWrapper{test}
genericParams := GenericParams{
Cipher: "aes",
CipherMode: "xts-plain64",
VolumeKey: generateKey(32, test),
VolumeKeySize: 32,
}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.ActivateByVolumeKey(DeviceName, genericParams.VolumeKey, genericParams.VolumeKeySize, CRYPT_ACTIVATE_READONLY)
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -22)
}
func Test_Device_KeyslotAddByVolumeKey_Fails_If_Device_Has_No_Type(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.KeyslotAddByVolumeKey(0, "", "testPassphrase")
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -22)
}
func Test_Device_KeyslotAddByPassphrase_Fails_If_Device_Has_No_Type(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.KeyslotAddByPassphrase(0, "testPassphrase", "secondTestPassphrase")
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -22)
}
func Test_Device_KeyslotChangeByPassphrase_Fails_If_Device_Has_No_Type(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.KeyslotChangeByPassphrase(0, 0, "testPassphrase", "secondTestPassphrase")
testWrapper.AssertError(err)
testWrapper.AssertErrorCodeEquals(err, -22)
}
func Test_Device_VolumeKeyGet(test *testing.T) {
volumeKeySize := 512 / 8
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.Format(LUKS2{SectorSize: 512}, GenericParams{Cipher: "aes", CipherMode: "xts-plain64", VolumeKeySize: volumeKeySize})
testWrapper.AssertNoError(err)
defer device.Free()
err = device.KeyslotAddByVolumeKey(0, "", "firstPassphrase")
testWrapper.AssertNoError(err)
err = device.KeyslotAddByPassphrase(1, "firstPassphrase", "secondPassphrase")
testWrapper.AssertNoError(err)
volumeKey, volumeKeySlot, err := device.VolumeKeyGet(CRYPT_ANY_SLOT, "secondPassphrase")
testWrapper.AssertNoError(err)
if len(volumeKey) != volumeKeySize {
test.Errorf("Invalid volume key size length: %d", len(volumeKey))
} else if volumeKeySlot != 1 {
test.Errorf("Volume key slot should have been 1, but was: %d", volumeKeySlot)
}
}
func Test_Device_VolumeKeyGet_Fails_If_Wrong_Passphrase(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.Format(LUKS2{SectorSize: 512}, GenericParams{Cipher: "aes", CipherMode: "xts-plain64", VolumeKeySize: 512 / 8})
testWrapper.AssertNoError(err)
defer device.Free()
err = device.KeyslotAddByVolumeKey(0, "", "testPassphrase")
testWrapper.AssertNoError(err)
volumeKey, volumeKeySlot, err := device.VolumeKeyGet(CRYPT_ANY_SLOT, "secondTestPassphrase")
testWrapper.AssertError(err)
if len(volumeKey) != 0 {
test.Errorf("Invalid volume key size length: %d", len(volumeKey))
} else if volumeKeySlot != 0 {
test.Errorf("Volume key slot should have been zero, but was: %d", volumeKeySlot)
}
}
func Test_Device_GetDeviceName(test *testing.T) {
testWrapper := TestWrapper{test}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
defer device.Free()
err = device.Format(LUKS2{SectorSize: 512}, GenericParams{Cipher: "aes", CipherMode: "xts-plain64", VolumeKeySize: 512 / 8})
testWrapper.AssertNoError(err)
devicePath := device.GetDeviceName()
if devicePath != DevicePath {
test.Errorf("Returned the wrong device path: got %s, expected %s", devicePath, DevicePath)
}
}
func Test_Device_GetUUID(test *testing.T) {
testWrapper := TestWrapper{test}
device := &Device{}
uid := device.GetUUID()
if uid != "" {
test.Error("UUID should be empty")
}
device, err := Init(DevicePath)
testWrapper.AssertNoError(err)
defer device.Free()
err = device.Format(LUKS2{SectorSize: 512}, GenericParams{Cipher: "aes", CipherMode: "xts-plain64", VolumeKeySize: 512 / 8})
testWrapper.AssertNoError(err)
uid = device.GetUUID()
if uid == "" {
test.Error("Should have generated a UUID")
}
newUUID := "12345678-1234-1234-1234-12345678abcd"
device.Free()
device, err = Init(DevicePath)
testWrapper.AssertNoError(err)
err = device.Format(LUKS2{SectorSize: 512}, GenericParams{Cipher: "aes", CipherMode: "xts-plain64", UUID: newUUID, VolumeKeySize: 512 / 8})
testWrapper.AssertNoError(err)
uid = device.GetUUID()
if uid != newUUID {
test.Errorf("Returned a different UUID than was set for the device: got %s, expected %s", uid, newUUID)
}
}