-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencryption_test.go
100 lines (80 loc) · 2.86 KB
/
encryption_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
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).
package props
import (
"crypto/rand"
"errors"
"strings"
"testing"
)
type badReader struct{}
func (b *badReader) Read(p []byte) (n int, err error) { return 0, errors.New("bad read") }
func TestEncrypt(t *testing.T) {
val, err := Encrypt("xyz", "password", "plaintext")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
val, err = Encrypt(EncryptNone, "password", "plaintext")
if val != "[enc:0]plaintext" || err != nil {
t.Errorf("want: '[enc:0]plaintext', err == nil; got: %s, %v", val, err)
}
val, err = Encrypt(EncryptAESGCM, "small", "plaintext")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
val, err = Encrypt(EncryptAESGCM, "1234567890123456", "plaintext")
if len(val) < 58 || !strings.HasPrefix(val, "[enc:1]") || err != nil {
t.Errorf("want: '[enc:1]...', err == nil; got: %s, %v", val, err)
}
oldReader := rand.Reader
rand.Reader = &badReader{}
val, err = Encrypt(EncryptAESGCM, "1234567890123456", "plaintext")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
rand.Reader = oldReader
}
func TestDecrypt(t *testing.T) {
badpass := "123"
pass := "1234567890123456"
pass256 := "12345678901234567890123456789012"
val, err := Decrypt(badpass, "[enc:0]")
if val != "" || err != nil {
t.Errorf("want: '', err == nil; got: %s, %v", val, err)
}
val, err = Decrypt(badpass, "[enc:0]plaintext")
if val != "plaintext" || err != nil {
t.Errorf("want: 'plaintext', err == nil; got: %s, %v", val, err)
}
val, err = Decrypt(badpass, "[enc:1]qzp-F5Cw1G5n9c7D5LFw4NzanvZzdQzONRPlM3JpLLCO6swpBQ==")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
val, err = Decrypt(pass, "[enc:1]")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
val, err = Decrypt(pass, "[enc:1]$$$$")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
val, err = Decrypt(pass, "[enc:1]qzp-F5Cw1G5n9c7D5LFw4NzanvZzdQzONRPlM3JpLLCO6swpBQ==")
if val != "plaintext" || err != nil {
t.Errorf("want: 'plaintext', err == nil; got: %s, %v", val, err)
}
val, err = Decrypt(pass, "[enc:1]x-f72AdaTiZfRvX_6kekHpd4xUj1JmMFwbwiADbMZXgJjGpJNg==")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
val, err = Decrypt(pass256, "[enc:1]x-f72AdaTiZfRvX_6kekHpd4xUj1JmMFwbwiADbMZXgJjGpJNg==")
if val != "plaintext" || err != nil {
t.Errorf("want: 'plaintext', err == nil; got: %s, %v", val, err)
}
val, err = Decrypt(pass, "[enc:x]abcd")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
val, err = Decrypt(pass, "abcd")
if val != "" || err == nil {
t.Errorf("want: '', err != nil; got: %s, %v", val, err)
}
}