forked from google/go-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivertest.go
307 lines (276 loc) · 8.45 KB
/
drivertest.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2018 The Go Cloud Development Kit Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package drivertest provides a conformance test for implementations of
// the secrets driver.
package drivertest // import "github.com/kainoaseto/go-cloud/secrets/drivertest"
import (
"bytes"
"context"
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/kainoaseto/go-cloud/secrets"
"github.com/kainoaseto/go-cloud/secrets/driver"
)
// Harness descibes the functionality test harnesses must provide to run
// conformance tests.
type Harness interface {
// MakeDriver returns a pair of driver.Keeper, each backed by a different key.
MakeDriver(ctx context.Context) (driver.Keeper, driver.Keeper, error)
// Close is called when the test is complete.
Close()
}
// HarnessMaker describes functions that construct a harness for running tests.
// It is called exactly once per test.
type HarnessMaker func(ctx context.Context, t *testing.T) (Harness, error)
// AsTest represents a test of As functionality.
// The conformance test:
// 1. Tries to decrypt malformed message, and calls ErrorCheck with the error.
type AsTest interface {
// Name returns a descriptive name for the test.
Name() string
// ErrorCheck is called to allow verification of Keeper.ErrorAs.
ErrorCheck(k *secrets.Keeper, err error) error
}
type verifyAsFailsOnNil struct{}
func (v verifyAsFailsOnNil) Name() string {
return "verify As returns false when passed nil"
}
func (v verifyAsFailsOnNil) ErrorCheck(k *secrets.Keeper, err error) (ret error) {
defer func() {
if recover() == nil {
ret = errors.New("want ErrorAs to panic when passed nil")
}
}()
k.ErrorAs(err, nil)
return nil
}
// RunConformanceTests runs conformance tests for driver implementations of secret management.
func RunConformanceTests(t *testing.T, newHarness HarnessMaker, asTests []AsTest) {
t.Run("TestEncryptDecrypt", func(t *testing.T) {
testEncryptDecrypt(t, newHarness)
})
t.Run("TestMultipleEncryptionsNotEqual", func(t *testing.T) {
testMultipleEncryptionsNotEqual(t, newHarness)
})
t.Run("TestMultipleKeys", func(t *testing.T) {
testMultipleKeys(t, newHarness)
})
t.Run("TestDecryptMalformedError", func(t *testing.T) {
testDecryptMalformedError(t, newHarness)
})
asTests = append(asTests, verifyAsFailsOnNil{})
t.Run("TestAs", func(t *testing.T) {
for _, tc := range asTests {
if tc.Name() == "" {
t.Fatal("AsTest.Name is required")
}
t.Run(tc.Name(), func(t *testing.T) {
testAs(t, newHarness, tc)
})
}
})
}
// testEncryptDecrypt tests the functionality of encryption and decryption
func testEncryptDecrypt(t *testing.T, newHarness HarnessMaker) {
ctx := context.Background()
harness, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
}
defer harness.Close()
drv, _, err := harness.MakeDriver(ctx)
if err != nil {
t.Fatal(err)
}
keeper := secrets.NewKeeper(drv)
defer keeper.Close()
msg := []byte("I'm a secret message!")
encryptedMsg, err := keeper.Encrypt(ctx, msg)
if err != nil {
t.Fatal(err)
}
if cmp.Equal(msg, encryptedMsg) {
t.Errorf("Got encrypted message %v, want it to differ from original message %v", string(msg), string(encryptedMsg))
}
decryptedMsg, err := keeper.Decrypt(ctx, encryptedMsg)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(msg, decryptedMsg) {
t.Errorf("Got decrypted message %v, want it to match original message %v", string(msg), string(decryptedMsg))
}
}
// testMultipleEncryptionsNotEqual tests that encrypting a plaintext multiple
// times with the same key works, and that the encrypted bytes are different.
func testMultipleEncryptionsNotEqual(t *testing.T, newHarness HarnessMaker) {
ctx := context.Background()
harness, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
}
defer harness.Close()
drv, _, err := harness.MakeDriver(ctx)
if err != nil {
t.Fatal(err)
}
keeper := secrets.NewKeeper(drv)
defer keeper.Close()
msg := []byte("I'm a secret message!")
encryptedMsg1, err := keeper.Encrypt(ctx, msg)
if err != nil {
t.Fatal(err)
}
encryptedMsg2, err := keeper.Encrypt(ctx, msg)
if err != nil {
t.Fatal(err)
}
if cmp.Equal(encryptedMsg1, encryptedMsg2) {
t.Errorf("Got same encrypted messages from multiple encryptions %v, want them to be different", string(encryptedMsg1))
}
decryptedMsg, err := keeper.Decrypt(ctx, encryptedMsg1)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(decryptedMsg, msg) {
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg))
}
decryptedMsg, err = keeper.Decrypt(ctx, encryptedMsg2)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(decryptedMsg, msg) {
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg))
}
}
// testMultipleKeys tests that encrypting the same text with different
// keys works, and that the encrypted bytes are different.
func testMultipleKeys(t *testing.T, newHarness HarnessMaker) {
ctx := context.Background()
harness, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
}
defer harness.Close()
drv1, drv2, err := harness.MakeDriver(ctx)
if err != nil {
t.Fatal(err)
}
keeper1 := secrets.NewKeeper(drv1)
defer keeper1.Close()
keeper2 := secrets.NewKeeper(drv2)
defer keeper2.Close()
msg := []byte("I'm a secret message!")
encryptedMsg1, err := keeper1.Encrypt(ctx, msg)
if err != nil {
t.Fatal(err)
}
encryptedMsg2, err := keeper2.Encrypt(ctx, msg)
if err != nil {
t.Fatal(err)
}
if cmp.Equal(encryptedMsg1, encryptedMsg2) {
t.Errorf("Got same encrypted messages from multiple encryptions %v, want them to be different", string(encryptedMsg1))
}
// We cannot assert that decrypting encryptedMsg1 with keeper2 fails,
// or that decrypting encryptedMsg2 with keeper1 fails, as Decrypt is allowed
// to decrypt using a different key than the one given to Keeper.
decryptedMsg, err := keeper1.Decrypt(ctx, encryptedMsg1)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(decryptedMsg, msg) {
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg))
}
decryptedMsg, err = keeper2.Decrypt(ctx, encryptedMsg2)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(decryptedMsg, msg) {
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg))
}
}
// testDecryptMalformedError tests decryption returns an error when the
// ciphertext is malformed.
func testDecryptMalformedError(t *testing.T, newHarness HarnessMaker) {
ctx := context.Background()
harness, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
}
defer harness.Close()
drv, _, err := harness.MakeDriver(ctx)
if err != nil {
t.Fatal(err)
}
keeper := secrets.NewKeeper(drv)
defer keeper.Close()
msg := []byte("I'm a secret message!")
encryptedMsg, err := keeper.Encrypt(ctx, msg)
if err != nil {
t.Fatal(err)
}
copyEncryptedMsg := func() []byte {
return append([]byte{}, encryptedMsg...)
}
l := len(encryptedMsg)
for _, tc := range []struct {
name string
malformed []byte
}{
{
name: "wrong first byte",
malformed: append([]byte{encryptedMsg[0] + 1}, encryptedMsg[1:]...),
},
{
name: "missing second byte",
malformed: append(copyEncryptedMsg()[:1], encryptedMsg[2:]...),
},
{
name: "wrong last byte",
malformed: append(copyEncryptedMsg()[:l-2], encryptedMsg[l-1]-1),
},
{
name: "one more byte",
malformed: append(encryptedMsg, 4),
},
} {
t.Run(tc.name, func(t *testing.T) {
if _, err := keeper.Decrypt(ctx, []byte(tc.malformed)); err == nil {
t.Error("Got nil, want decrypt error")
}
})
}
}
func testAs(t *testing.T, newHarness HarnessMaker, tc AsTest) {
ctx := context.Background()
harness, err := newHarness(ctx, t)
if err != nil {
t.Fatal(err)
}
defer harness.Close()
drv, _, err := harness.MakeDriver(ctx)
if err != nil {
t.Fatal(err)
}
keeper := secrets.NewKeeper(drv)
defer keeper.Close()
_, gotErr := keeper.Decrypt(ctx, []byte("malformed cipher message"))
if gotErr == nil {
t.Error("Got nil, want decrypt error")
}
if err := tc.ErrorCheck(keeper, gotErr); err != nil {
t.Error(err)
}
}