This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswordlock_test.go
86 lines (73 loc) · 2.17 KB
/
passwordlock_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
package passlock
import (
"fmt"
"runtime/debug"
"testing"
testify "github.com/stretchr/testify/require"
)
func TestEncryptBytes(t *testing.T) {
assert := testify.New(t)
badPassword := []byte("s3cre+")
data := []byte("some data that needs to be encrypted")
encrypted, err := EncryptBytes(badPassword, data)
assert.NoError(err, "Should be no error from encryption")
assert.NotEqual(data, encrypted)
decrypted, err := DecryptBytes(badPassword, encrypted)
assert.NoError(err, "Should be no error from decryption")
assert.Equal(data, decrypted)
}
func TestEncryptBytes_Negative(t *testing.T) {
validPassword := []byte("s3cre+")
validData := []byte("some data that needs to be encrypted")
tests := map[string]struct {
pass []byte
data []byte
}{
"Nil pass": {pass: nil, data: validData},
"Nil data": {pass: validPassword, data: nil},
"Both nil": {pass: nil, data: nil},
"Empty pass": {pass: []byte{}, data: validData},
"Empty data": {pass: validPassword, data: []byte{}},
"Both empty": {pass: []byte{}, data: []byte{}},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
if _, err := EncryptBytes(tc.pass, tc.data); err == nil {
t.Fatal("Should have thrown an error")
}
})
}
}
func TestDecryptBytes_Negative(t *testing.T) {
validPassword := []byte("s3cre+")
validData, err := EncryptBytes(validPassword, []byte("some data that needs to be encrypted"))
if err != nil {
t.Fatalf("Failed to encrypt test data")
}
tests := map[string]struct {
pass []byte
data []byte
}{
"Nil pass": {pass: nil, data: validData},
"Nil data": {pass: validPassword, data: nil},
"Both nil": {pass: nil, data: nil},
"Empty pass": {pass: []byte{}, data: validData},
"Empty data": {pass: validPassword, data: []byte{}},
"Both empty": {pass: []byte{}, data: []byte{}},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
t.Fatalf("Panic recovered: %v\n", r)
}
}()
if _, err := DecryptBytes(tc.pass, tc.data); err == nil {
t.Fatal("Should have thrown an error")
} else {
fmt.Println("Error thrown as expected:", err)
}
})
}
}