-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencrypted_bytes_test.go
More file actions
100 lines (80 loc) · 2.07 KB
/
encrypted_bytes_test.go
File metadata and controls
100 lines (80 loc) · 2.07 KB
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
package sqlcrypter
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_NewEncryptedBytes(t *testing.T) {
t.Run("nil string", func(t *testing.T) {
var b []byte
e := NewEncryptedBytes("")
assert.Equal(t, b, e.Bytes())
assert.Nil(t, e)
})
t.Run("success", func(t *testing.T) {
s := "Hello World"
e := NewEncryptedBytes(s)
assert.Equal(t, s, e.String())
})
}
func Test_EncryptedBytes_Scan(t *testing.T) {
Init(&base64Crypter{})
t.Run("nil value", func(t *testing.T) {
e := NewEncryptedBytes("")
var b []byte
err := e.Scan(b)
require.NoError(t, err)
assert.Nil(t, e)
})
t.Run("not bytes", func(t *testing.T) {
e := NewEncryptedBytes("")
err := e.Scan("string, not bytes")
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to read value as bytes")
})
t.Run("decrypt", func(t *testing.T) {
e := &EncryptedBytes{}
err := e.Scan([]byte("SGVsbG8gV29ybGQ="))
require.NoError(t, err)
assert.Equal(t, "Hello World", e.String())
})
}
func Test_EncryptedBytes_Value(t *testing.T) {
Init(&base64Crypter{})
t.Run("nil value", func(t *testing.T) {
e := &EncryptedBytes{}
var b []byte
d, err := e.Value()
require.NoError(t, err)
assert.Equal(t, b, d)
})
t.Run("encrypt", func(t *testing.T) {
e := NewEncryptedBytes("Hello World")
d, err := e.Value()
require.NoError(t, err)
b, ok := d.([]byte)
assert.True(t, ok)
assert.Equal(t, "SGVsbG8gV29ybGQ=", string(b))
})
}
func Test_EncryptedBytes_MarshalJSON(t *testing.T) {
Init(&base64Crypter{})
m := map[string]EncryptedBytes{
"v": NewEncryptedBytes("Hello World"),
}
b, err := json.Marshal(m)
require.NoError(t, err)
assert.JSONEq(t, `{"v":"U0dWc2JHOGdWMjl5YkdRPQ=="}`, string(b))
}
func Test_EncryptedBytes_UnmarshalJSON(t *testing.T) {
Init(&base64Crypter{})
data := []byte(`{"secret":"U0dWc2JHOGdWMjl5YkdRPQ=="}`)
type Example struct {
Secret EncryptedBytes
}
var e Example
err := json.Unmarshal(data, &e)
require.NoError(t, err)
assert.Equal(t, "Hello World", e.Secret.String())
}