| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package drivertest provides a conformance test for implementations of |
| 16 |
// the secrets driver. |
| 17 |
package drivertest // import "gocloud.dev/secrets/drivertest" |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"context" |
| 22 |
"errors" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/google/go-cmp/cmp" |
| 26 |
"gocloud.dev/secrets" |
| 27 |
"gocloud.dev/secrets/driver" |
| 28 |
) |
| 29 |
|
| 30 |
// Harness descibes the functionality test harnesses must provide to run |
| 31 |
// conformance tests. |
| 32 |
type Harness interface { |
| 33 |
// MakeDriver returns a pair of driver.Keeper, each backed by a different key. |
| 34 |
MakeDriver(ctx context.Context) (driver.Keeper, driver.Keeper, error) |
| 35 |
|
| 36 |
// Close is called when the test is complete. |
| 37 |
Close() |
| 38 |
} |
| 39 |
|
| 40 |
// HarnessMaker describes functions that construct a harness for running tests. |
| 41 |
// It is called exactly once per test. |
| 42 |
type HarnessMaker func(ctx context.Context, t *testing.T) (Harness, error) |
| 43 |
|
| 44 |
// AsTest represents a test of As functionality. |
| 45 |
// The conformance test: |
| 46 |
// 1. Tries to decrypt malformed message, and calls ErrorCheck with the error. |
| 47 |
type AsTest interface { |
| 48 |
// Name returns a descriptive name for the test. |
| 49 |
Name() string |
| 50 |
// ErrorCheck is called to allow verification of Keeper.ErrorAs. |
| 51 |
ErrorCheck(k *secrets.Keeper, err error) error |
| 52 |
} |
| 53 |
|
| 54 |
type verifyAsFailsOnNil struct{} |
| 55 |
|
| 56 |
func (v verifyAsFailsOnNil) Name() string { |
| 57 |
return "verify As returns false when passed nil" |
| 58 |
} |
| 59 |
|
| 60 |
func (v verifyAsFailsOnNil) ErrorCheck(k *secrets.Keeper, err error) (ret error) { |
| 61 |
defer func() { |
| 62 |
if recover() == nil { |
| 63 |
ret = errors.New("want ErrorAs to panic when passed nil") |
| 64 |
} |
| 65 |
}() |
| 66 |
k.ErrorAs(err, nil) |
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
// RunConformanceTests runs conformance tests for driver implementations of secret management. |
| 71 |
func RunConformanceTests(t *testing.T, newHarness HarnessMaker, asTests []AsTest) { |
| 72 |
t.Helper() |
| 73 |
|
| 74 |
t.Run("TestEncryptDecrypt", func(t *testing.T) { |
| 75 |
testEncryptDecrypt(t, newHarness) |
| 76 |
}) |
| 77 |
t.Run("TestMultipleEncryptionsNotEqual", func(t *testing.T) { |
| 78 |
testMultipleEncryptionsNotEqual(t, newHarness) |
| 79 |
}) |
| 80 |
t.Run("TestMultipleKeys", func(t *testing.T) { |
| 81 |
testMultipleKeys(t, newHarness) |
| 82 |
}) |
| 83 |
t.Run("TestDecryptMalformedError", func(t *testing.T) { |
| 84 |
testDecryptMalformedError(t, newHarness) |
| 85 |
}) |
| 86 |
asTests = append(asTests, verifyAsFailsOnNil{}) |
| 87 |
t.Run("TestAs", func(t *testing.T) { |
| 88 |
for _, tc := range asTests { |
| 89 |
if tc.Name() == "" { |
| 90 |
t.Fatal("AsTest.Name is required") |
| 91 |
} |
| 92 |
t.Run(tc.Name(), func(t *testing.T) { |
| 93 |
testAs(t, newHarness, tc) |
| 94 |
}) |
| 95 |
} |
| 96 |
}) |
| 97 |
} |
| 98 |
|
| 99 |
// testEncryptDecrypt tests the functionality of encryption and decryption |
| 100 |
func testEncryptDecrypt(t *testing.T, newHarness HarnessMaker) { |
| 101 |
t.Helper() |
| 102 |
|
| 103 |
ctx := context.Background() |
| 104 |
harness, err := newHarness(ctx, t) |
| 105 |
if err != nil { |
| 106 |
t.Fatal(err) |
| 107 |
} |
| 108 |
defer harness.Close() |
| 109 |
|
| 110 |
drv, _, err := harness.MakeDriver(ctx) |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
keeper := secrets.NewKeeper(drv) |
| 115 |
defer keeper.Close() |
| 116 |
|
| 117 |
msg := []byte("I'm a secret message!") |
| 118 |
encryptedMsg, err := keeper.Encrypt(ctx, msg) |
| 119 |
if err != nil { |
| 120 |
t.Fatal(err) |
| 121 |
} |
| 122 |
if cmp.Equal(msg, encryptedMsg) { |
| 123 |
t.Errorf("Got encrypted message %v, want it to differ from original message %v", string(msg), string(encryptedMsg)) |
| 124 |
} |
| 125 |
decryptedMsg, err := keeper.Decrypt(ctx, encryptedMsg) |
| 126 |
if err != nil { |
| 127 |
t.Fatal(err) |
| 128 |
} |
| 129 |
if !cmp.Equal(msg, decryptedMsg) { |
| 130 |
t.Errorf("Got decrypted message %v, want it to match original message %v", string(msg), string(decryptedMsg)) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// testMultipleEncryptionsNotEqual tests that encrypting a plaintext multiple |
| 135 |
// times with the same key works, and that the encrypted bytes are different. |
| 136 |
func testMultipleEncryptionsNotEqual(t *testing.T, newHarness HarnessMaker) { |
| 137 |
t.Helper() |
| 138 |
|
| 139 |
ctx := context.Background() |
| 140 |
harness, err := newHarness(ctx, t) |
| 141 |
if err != nil { |
| 142 |
t.Fatal(err) |
| 143 |
} |
| 144 |
defer harness.Close() |
| 145 |
|
| 146 |
drv, _, err := harness.MakeDriver(ctx) |
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
keeper := secrets.NewKeeper(drv) |
| 151 |
defer keeper.Close() |
| 152 |
|
| 153 |
msg := []byte("I'm a secret message!") |
| 154 |
encryptedMsg1, err := keeper.Encrypt(ctx, msg) |
| 155 |
if err != nil { |
| 156 |
t.Fatal(err) |
| 157 |
} |
| 158 |
encryptedMsg2, err := keeper.Encrypt(ctx, msg) |
| 159 |
if err != nil { |
| 160 |
t.Fatal(err) |
| 161 |
} |
| 162 |
if cmp.Equal(encryptedMsg1, encryptedMsg2) { |
| 163 |
t.Errorf("Got same encrypted messages from multiple encryptions %v, want them to be different", string(encryptedMsg1)) |
| 164 |
} |
| 165 |
decryptedMsg, err := keeper.Decrypt(ctx, encryptedMsg1) |
| 166 |
if err != nil { |
| 167 |
t.Fatal(err) |
| 168 |
} |
| 169 |
if !bytes.Equal(decryptedMsg, msg) { |
| 170 |
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg)) |
| 171 |
} |
| 172 |
decryptedMsg, err = keeper.Decrypt(ctx, encryptedMsg2) |
| 173 |
if err != nil { |
| 174 |
t.Fatal(err) |
| 175 |
} |
| 176 |
if !bytes.Equal(decryptedMsg, msg) { |
| 177 |
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg)) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// testMultipleKeys tests that encrypting the same text with different |
| 182 |
// keys works, and that the encrypted bytes are different. |
| 183 |
func testMultipleKeys(t *testing.T, newHarness HarnessMaker) { |
| 184 |
t.Helper() |
| 185 |
|
| 186 |
ctx := context.Background() |
| 187 |
harness, err := newHarness(ctx, t) |
| 188 |
if err != nil { |
| 189 |
t.Fatal(err) |
| 190 |
} |
| 191 |
defer harness.Close() |
| 192 |
|
| 193 |
drv1, drv2, err := harness.MakeDriver(ctx) |
| 194 |
if err != nil { |
| 195 |
t.Fatal(err) |
| 196 |
} |
| 197 |
keeper1 := secrets.NewKeeper(drv1) |
| 198 |
defer keeper1.Close() |
| 199 |
keeper2 := secrets.NewKeeper(drv2) |
| 200 |
defer keeper2.Close() |
| 201 |
|
| 202 |
msg := []byte("I'm a secret message!") |
| 203 |
encryptedMsg1, err := keeper1.Encrypt(ctx, msg) |
| 204 |
if err != nil { |
| 205 |
t.Fatal(err) |
| 206 |
} |
| 207 |
encryptedMsg2, err := keeper2.Encrypt(ctx, msg) |
| 208 |
if err != nil { |
| 209 |
t.Fatal(err) |
| 210 |
} |
| 211 |
if cmp.Equal(encryptedMsg1, encryptedMsg2) { |
| 212 |
t.Errorf("Got same encrypted messages from multiple encryptions %v, want them to be different", string(encryptedMsg1)) |
| 213 |
} |
| 214 |
|
| 215 |
// We cannot assert that decrypting encryptedMsg1 with keeper2 fails, |
| 216 |
// or that decrypting encryptedMsg2 with keeper1 fails, as Decrypt is allowed |
| 217 |
// to decrypt using a different key than the one given to Keeper. |
| 218 |
|
| 219 |
decryptedMsg, err := keeper1.Decrypt(ctx, encryptedMsg1) |
| 220 |
if err != nil { |
| 221 |
t.Fatal(err) |
| 222 |
} |
| 223 |
if !bytes.Equal(decryptedMsg, msg) { |
| 224 |
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg)) |
| 225 |
} |
| 226 |
|
| 227 |
decryptedMsg, err = keeper2.Decrypt(ctx, encryptedMsg2) |
| 228 |
if err != nil { |
| 229 |
t.Fatal(err) |
| 230 |
} |
| 231 |
if !bytes.Equal(decryptedMsg, msg) { |
| 232 |
t.Errorf("got decrypted %q want %q", string(decryptedMsg), string(msg)) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// testDecryptMalformedError tests decryption returns an error when the |
| 237 |
// ciphertext is malformed. |
| 238 |
func testDecryptMalformedError(t *testing.T, newHarness HarnessMaker) { |
| 239 |
t.Helper() |
| 240 |
|
| 241 |
ctx := context.Background() |
| 242 |
harness, err := newHarness(ctx, t) |
| 243 |
if err != nil { |
| 244 |
t.Fatal(err) |
| 245 |
} |
| 246 |
defer harness.Close() |
| 247 |
|
| 248 |
drv, _, err := harness.MakeDriver(ctx) |
| 249 |
if err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
keeper := secrets.NewKeeper(drv) |
| 253 |
defer keeper.Close() |
| 254 |
|
| 255 |
msg := []byte("I'm a secret message!") |
| 256 |
encryptedMsg, err := keeper.Encrypt(ctx, msg) |
| 257 |
if err != nil { |
| 258 |
t.Fatal(err) |
| 259 |
} |
| 260 |
copyEncryptedMsg := func() []byte { |
| 261 |
return append([]byte{}, encryptedMsg...) |
| 262 |
} |
| 263 |
|
| 264 |
l := len(encryptedMsg) |
| 265 |
for _, tc := range []struct { |
| 266 |
name string |
| 267 |
malformed []byte |
| 268 |
}{ |
| 269 |
{ |
| 270 |
name: "wrong first byte", |
| 271 |
malformed: append([]byte{encryptedMsg[0] + 1}, encryptedMsg[1:]...), |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "missing second byte", |
| 275 |
malformed: append(copyEncryptedMsg()[:1], encryptedMsg[2:]...), |
| 276 |
}, |
| 277 |
{ |
| 278 |
name: "wrong last byte", |
| 279 |
malformed: append(copyEncryptedMsg()[:l-2], encryptedMsg[l-1]-1), |
| 280 |
}, |
| 281 |
} { |
| 282 |
t.Run(tc.name, func(t *testing.T) { |
| 283 |
if _, err := keeper.Decrypt(ctx, []byte(tc.malformed)); err == nil { |
| 284 |
t.Error("Got nil, want decrypt error") |
| 285 |
} |
| 286 |
}) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func testAs(t *testing.T, newHarness HarnessMaker, tc AsTest) { |
| 291 |
t.Helper() |
| 292 |
|
| 293 |
ctx := context.Background() |
| 294 |
harness, err := newHarness(ctx, t) |
| 295 |
if err != nil { |
| 296 |
t.Fatal(err) |
| 297 |
} |
| 298 |
defer harness.Close() |
| 299 |
|
| 300 |
drv, _, err := harness.MakeDriver(ctx) |
| 301 |
if err != nil { |
| 302 |
t.Fatal(err) |
| 303 |
} |
| 304 |
keeper := secrets.NewKeeper(drv) |
| 305 |
defer keeper.Close() |
| 306 |
|
| 307 |
_, gotErr := keeper.Decrypt(ctx, []byte("malformed cipher message")) |
| 308 |
if gotErr == nil { |
| 309 |
t.Error("Got nil, want decrypt error") |
| 310 |
} |
| 311 |
if err := tc.ErrorCheck(keeper, gotErr); err != nil { |
| 312 |
t.Error(err) |
| 313 |
} |
| 314 |
} |