-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt_test.go
88 lines (67 loc) · 2.1 KB
/
crypt_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
package main
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
plaintext = "Hello world"
key = "12345678901234567890123456789012"
)
var ciphertext = "yALnSjIiOiljHpQ="
func TestEncrypt(t *testing.T) {
t.Run("encrypt plaintext with 32 characters key value", func(t *testing.T) {
got, err := Encrypt([]byte(plaintext), []byte(key))
want := ciphertext
if err != nil {
t.Error(err)
}
t.Log(string(got))
assert.Equal(t, string(want), string(got))
})
t.Run("encrypt plaintext with incorrect size of key value", func(t *testing.T) {
_, got := Encrypt([]byte(plaintext), []byte("hello"))
want := "crypto/aes: invalid key size 5"
assert.EqualError(t, got, want)
})
}
func TestDecrypt(t *testing.T) {
t.Run("decrypt ciphertext with 32 characters key value", func(t *testing.T) {
got, err := Decrypt([]byte(ciphertext), []byte(key))
want := plaintext
if err != nil {
t.Error(err)
}
assert.Equal(t, want, string(got))
})
t.Run("encrypt plaintext with incorrect size of key value", func(t *testing.T) {
_, got := Decrypt([]byte(ciphertext), []byte("hello"))
want := "crypto/aes: invalid key size 5"
assert.EqualError(t, got, want)
})
}
func TestReadOnlyFilePassphrase(t *testing.T) {
t.Run("read only file passphrase when file doesn't exist", func(t *testing.T) {
_, got := ReadOnlyFilePassphrase("./notexistentfile")
assert.ErrorContains(t, got, "no such file or directory")
})
t.Run("read only file passphrase with passphrase length different than 32", func(t *testing.T) {
f := createTmpWithPerm(t, 0666)
if _, err := f.Write(Encode([]byte("random data"))); err != nil {
t.Fatal(err)
}
_, got := ReadOnlyFilePassphrase(f.Name())
assert.ErrorIs(t, ErrCipherTextTooShort, got)
})
t.Run("read only file passphrase correctly", func(t *testing.T) {
f := createTmpWithPerm(t, 0666)
if _, err := f.Write(Encode([]byte(strings.Repeat("a", 32)))); err != nil {
t.Fatal(err)
}
passphrase, err := ReadOnlyFilePassphrase(f.Name())
if err != nil {
t.Fatal(err)
}
assert.Equal(t, strings.Repeat("a", 32), passphrase)
})
}