forked from meltwater/secretary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox_test.go
141 lines (110 loc) · 4.55 KB
/
box_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/nacl/box"
)
const privateKey = `Q1PuWtB1E7F1sLpvfBGjL+ZuH+fSCOvMDqTyRQE4GTg=`
const publicKey = `UNlPHu0seDm6He2clMI5QHSaRGrXBdsMiWsamIF85l8=`
func TestAsKey(t *testing.T) {
buf, err := decode(privateKey)
assert.Nil(t, err)
key, err := asKey(buf)
assert.Nil(t, err)
assert.Equal(t, buf, key[:])
_, err = asKey([]byte("abc"))
assert.NotNil(t, err)
_, err = asKey(nil)
assert.NotNil(t, err)
}
func TestAsNonce(t *testing.T) {
buf, err := decode(`WLWwVUGVX7tTJd84mRioKQflzoTUWMj+PMtrO+c2oxEbnJba3ILzlyqhBKbd2Q==`)
assert.Nil(t, err)
nonce, err := asNonce(buf[0:24])
assert.Nil(t, err)
assert.Equal(t, buf[0:24], nonce[:])
_, err = asNonce([]byte("abc"))
assert.NotNil(t, err)
_, err = asNonce(nil)
assert.NotNil(t, err)
}
func TestFindKey(t *testing.T) {
expected := encode(pemRead("./resources/test/keys/config-public-key.pem")[:])
assert.Equal(t, expected, encode(findKey("", "RANDOM_ENVVAR_THAT_DOESNT_EXIST", "./resources/test/keys/config-public-key.pem")[:]))
assert.Nil(t, findKey("", "RANDOM_ENVVAR_THAT_DOESNT_EXIST", "./resources/test/keys/nonexist-public-key.pem"))
}
func TestExtractEnvelopes(t *testing.T) {
envelopes := extractEnvelopes("amqp://ENC[NACL,uSr123+/=]:ENC[NACL,pWd123+/=]@rabbit:5672/")
assert.Equal(t, 2, len(envelopes))
assert.Equal(t, []string{"ENC[NACL,uSr123+/=]", "ENC[NACL,pWd123+/=]"}, envelopes)
envelopes = extractEnvelopes("amqp://ENC[NACL,uSr123+/=]:ENC[NACL,pWd123+/=]@rabbit:5672/ENC[NACL,def123+/=]")
assert.Equal(t, 3, len(envelopes))
assert.Equal(t, []string{"ENC[NACL,uSr123+/=]", "ENC[NACL,pWd123+/=]", "ENC[NACL,def123+/=]"}, envelopes)
envelopes = extractEnvelopes("amqp://ENC[NACL,]:ENC[NACL,pWd123+/=]@rabbit:5672/")
assert.Equal(t, 1, len(envelopes))
assert.Equal(t, []string{"ENC[NACL,pWd123+/=]"}, envelopes)
envelopes = extractEnvelopes("amqp://ENC[NACL,:ENC[NACL,pWd123+/=]@rabbit:5672/")
assert.Equal(t, 1, len(envelopes))
assert.Equal(t, []string{"ENC[NACL,pWd123+/=]"}, envelopes)
envelopes = extractEnvelopes("amqp://NC[NACL,]:ENC[NACL,pWd123+/=]@rabbit:5672/")
assert.Equal(t, 1, len(envelopes))
assert.Equal(t, []string{"ENC[NACL,pWd123+/=]"}, envelopes)
envelopes = extractEnvelopes("amqp://ENC[NACL,abc:ENC[NACL,pWd123+/=]@rabbit:5672/")
assert.Equal(t, 1, len(envelopes))
assert.Equal(t, []string{"ENC[NACL,pWd123+/=]"}, envelopes)
}
func TestExtractEnvelopeType(t *testing.T) {
assert.Equal(t, "", extractEnvelopeType("ENC[NACL,]"))
assert.Equal(t, "NACL", extractEnvelopeType("ENC[NACL,abc]"))
assert.Equal(t, "", extractEnvelopeType("ENC[KMS,]"))
assert.Equal(t, "KMS", extractEnvelopeType("ENC[KMS,abc]"))
assert.Equal(t, "", extractEnvelopeType("ENC[NACL,"))
assert.Equal(t, "", extractEnvelopeType("NC[NACL,]"))
assert.Equal(t, "", extractEnvelopeType("ENC[NACL,abc"))
assert.Equal(t, "", extractEnvelopeType("ENC[ACL,abc"))
}
func TestEncodeDecode(t *testing.T) {
publicKey, _, err := box.GenerateKey(rand.Reader)
encoded := pemEncode(publicKey, "NACL PUBLIC KEY")
decoded, err := pemDecode(encoded)
assert.Nil(t, err)
assert.Equal(t, publicKey, decoded, "Key must be same after encode/decode cycle")
}
func TestDecodeWithoutHeader(t *testing.T) {
completeKey := `-----BEGIN NACL PUBLIC KEY-----
/1fbWGMTaR+lLQJnEsmxdfwWybKOpPQpyWB3FpNmOF4=
-----END NACL PUBLIC KEY-----`
strippedKey := `/1fbWGMTaR+lLQJnEsmxdfwWybKOpPQpyWB3FpNmOF4=`
decoded, err := pemDecode(completeKey)
assert.Nil(t, err)
decoded2, err := pemDecode(strippedKey)
assert.Nil(t, err)
assert.Equal(t, decoded, decoded2, "Keys must be decode to same value")
}
func TestDecryptEnvelope(t *testing.T) {
envelope := `ENC[NACL,WLWwVUGVX7tTJd84mRioKQflzoTUWMj+PMtrO+c2oxEbnJba3ILzlyqhBKbd2Q==]`
privkey, err := pemDecode(privateKey)
assert.Nil(t, err)
pubkey, err := pemDecode(publicKey)
assert.Nil(t, err)
plaintext, err := decryptEnvelope(pubkey, privkey, envelope)
assert.Nil(t, err)
assert.Equal(t, "secret", string(plaintext), "Should decrypt plaintext")
}
func TestEncryptEnvelope(t *testing.T) {
privkey, err := pemDecode(privateKey)
assert.Nil(t, err)
pubkey, err := pemDecode(publicKey)
assert.Nil(t, err)
envelope, err := encryptEnvelope(pubkey, privkey, []byte("secret"))
assert.Nil(t, err)
plaintext, err := decryptEnvelope(pubkey, privkey, envelope)
assert.Nil(t, err)
assert.Equal(t, "secret", string(plaintext), "Should decrypt plaintext")
}
func BenchmarkExtractEnvelopes(b *testing.B) {
for n := 0; n < b.N; n++ {
extractEnvelopes("amqp://ENC[NACL,uSr123+/=]:ENC[NACL,pWd123+/=]@rabbit:5672/")
}
}