forked from martinjungblut/go-cryptsetup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
91 lines (73 loc) · 1.98 KB
/
main_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
package cryptsetup
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"testing"
)
const DevicePath string = "testDevice"
const DeviceName string = "testDeviceName"
const PassKey string = "testPassKey"
type TestWrapper struct {
test *testing.T
}
func (testWrapper TestWrapper) AssertError(err error) {
if err == nil {
testWrapper.test.Error("Operation should have failed, but didn't.")
}
}
func (testWrapper TestWrapper) AssertNoError(err error) {
if err != nil {
testWrapper.test.Error(err)
}
}
func (testWrapper TestWrapper) AssertErrorCodeEquals(err error, expectedErrorCode int) {
actualErrorCode := err.(*Error).Code()
if actualErrorCode != expectedErrorCode {
testWrapper.test.Errorf("Error code should be '%d', but '%d' was returned instead.", expectedErrorCode, actualErrorCode)
}
}
func getFileMD5(filePath string, test *testing.T) string {
fileHandle, error := os.Open(filePath)
if error != nil {
test.Error(error)
}
defer fileHandle.Close()
hash := md5.New()
_, error = io.Copy(hash, fileHandle)
if error != nil {
test.Error(error)
}
return hex.EncodeToString(hash.Sum(nil)[:16])
}
func generateKey(length int, test *testing.T) string {
bytes := make([]byte, length)
_, err := rand.Read(bytes)
if err != nil {
test.Error("Error while generating key.")
}
return string(bytes[:])
}
func setup(devicePath string) {
exec.Command("/bin/dd", "if=/dev/zero", fmt.Sprintf("of=%s", devicePath), "bs=64M", "count=1").Run()
}
func teardown(devicePath string) {
exec.Command("/bin/rm", "-f", devicePath).Run()
}
func resize(devicePath string) {
exec.Command("/bin/dd", "if=/dev/zero", fmt.Sprintf("of=%s", devicePath), "bs=32M", "count=1", "oflag=append", "conv=notrunc").Run()
}
func TestMain(m *testing.M) {
if os.Getuid() != 0 {
fmt.Println("This test suite requires root privileges, as libcrypsetup uses the kernel's device mapper.")
os.Exit(1)
}
setup(DevicePath)
result := m.Run()
teardown(DevicePath)
os.Exit(result)
}