-
Notifications
You must be signed in to change notification settings - Fork 1
/
secrets_test.go
210 lines (153 loc) · 3.62 KB
/
secrets_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package secrets
import (
"fmt"
"testing"
)
func ExampleNewSecret() {
// allocate a new 8-byte secret
secret, err := NewSecret(8)
if err != nil {
return
}
// allow the secret to be read; good hygeine dictates that the
// Lock() should be deferred, so it is guaranteed to happen
// when the method returns
secret.Read()
defer secret.Lock()
fmt.Printf("0x%x", secret.Slice())
// while not strictly necessary, explicitly wiping the secret
// when done is also good hygeine; this will happen when
// the secret is garbage collected, but doing it explicitly
// causes the memory to be zeroed immediately
secret.Wipe()
// Output: 0x0000000000000000
}
func ExampleNewSecretFromBytes() {
var (
bytes = []byte("secrets!")
secret, err = NewSecretFromBytes(bytes)
)
if err != nil {
return
}
secret.Read()
defer secret.Lock()
fmt.Printf("0x%x", secret.Slice())
secret.Wipe()
// Output: 0x7365637265747321
}
func ExampleNewSecretFromBytes_zeroing() {
var (
bytes = []byte("secrets!")
_, err = NewSecretFromBytes(bytes)
)
if err != nil {
return
}
fmt.Printf("0x%x", bytes)
// Output: 0x0000000000000000
}
func ExampleSecret_Equal() {
var (
secret1, _ = NewSecretFromBytes([]byte("secret"))
secret2, _ = NewSecretFromBytes([]byte("secret"))
secret3, _ = NewSecretFromBytes([]byte("secrex"))
)
fmt.Printf("%t %t", secret1.Equal(secret2), secret1.Equal(secret3))
// Output: true false
}
func ExampleSecret_Trim() {
secret, err := NewSecretFromBytes([]byte("secret!"))
if err != nil {
return
}
secret.Trim(4)
secret.Read()
defer secret.Lock()
fmt.Printf("%s", secret.Slice())
secret.Wipe()
// Output: secr
}
func ExampleSecret_Split() {
var (
secret1, _ = NewSecretFromBytes([]byte("secret!"))
secret2, _ = secret1.Split(4)
)
secret1.Read()
defer secret1.Lock()
secret2.Read()
defer secret2.Lock()
fmt.Printf("%s %s", secret1.Slice(), secret2.Slice())
// Output: secr et!
}
func ExampleSecret_Wipe() {
secret, err := NewSecretFromBytes([]byte("secret!"))
if err != nil {
return
}
secret.Wipe()
// The pointer to the Secret should be NULL
fmt.Printf("%x\n", secret.Pointer())
// Output: 0
}
func TestNewSecret(t *testing.T) {
var (
secret1 *Secret
secret2 *Secret
err error
)
secret1, err = NewSecret(32)
if err != nil {
t.Error("NewSecret(32) = _, err; want nil")
}
secret1.Write()
defer secret1.Lock()
copy(secret1.Slice(), "cryptographic secrets are secret")
secret2, err = secret1.Copy()
if err != nil {
t.Error("Copy() = _, err; want nil")
}
if !secret1.Equal(secret2) {
t.Error("sec1.Equal(sec2) = false; want true")
}
}
func TestEmptySecret(t *testing.T) {
secret, err := NewSecret(0)
if err != nil {
t.Error("NewSecret(0) = _, err; want nil")
}
if secret.Pointer() != nil {
t.Errorf("secret.Pointer() = %x; want 0", secret.Pointer())
}
}
func TestSecretCanary(t *testing.T) {
var (
secret, _ = NewSecret(32)
slice = secret.Slice()
)
// trim the secret by one byte
secret.Trim(31)
secret.Write()
defer secret.Lock()
defer func() {
if recover() == nil {
t.Error("secret's canary should have triggered")
}
}()
// attempt to write past the secret's reduced length
slice[31] = 42
// the canary should trigger when the secret is wiped
secret.Wipe()
}
func TestSecretCopies(t *testing.T) {
var (
secret1, _ = NewSecretFromBytes([]byte("secret stuff"))
secret2, _ = NewSecretFromBytes([]byte("secret stuff"))
secret3, _ = secret1.Copy()
)
secret1.Wipe()
if !secret3.Equal(secret2) {
// oops, looks like we didn't actually copy the contents
t.Error("secret3.Equal(secret2) = false; want true")
}
}