Debian Codegraph @

secrets/localsecrets/localsecrets_test.go

raw
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 localsecrets
16
17 import (
18 "context"
19 "errors"
20 "log"
21 "strings"
22 "testing"
23
24 "gocloud.dev/secrets"
25 "gocloud.dev/secrets/driver"
26 "gocloud.dev/secrets/drivertest"
27 )
28
29 type harness struct{}
30
31 func (h *harness) MakeDriver(ctx context.Context) (driver.Keeper, driver.Keeper, error) {
32 secret1, err := NewRandomKey()
33 if err != nil {
34 log.Fatal(err)
35 }
36 secret2, err := NewRandomKey()
37 if err != nil {
38 log.Fatal(err)
39 }
40 return &keeper{secretKey: secret1}, &keeper{secretKey: secret2}, nil
41 }
42
43 func (h *harness) Close() {}
44
45 func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) {
46 t.Helper()
47
48 return &harness{}, nil
49 }
50
51 func TestConformance(t *testing.T) {
52 drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyAs{}})
53 }
54
55 type verifyAs struct{}
56
57 func (v verifyAs) Name() string {
58 return "verify As function"
59 }
60
61 func (v verifyAs) ErrorCheck(k *secrets.Keeper, err error) error {
62 var s string
63 if k.ErrorAs(err, &s) {
64 return errors.New("Keeper.ErrorAs expected to fail")
65 }
66 return nil
67 }
68
69 func TestSmallData(t *testing.T) {
70 key, err := NewRandomKey()
71 if err != nil {
72 t.Fatal(err)
73 }
74 keeper := NewKeeper(key)
75 defer keeper.Close()
76
77 ctx := context.Background()
78 const plaintext = "hello world"
79 ciphertext, err := keeper.Encrypt(ctx, []byte(plaintext))
80 if err != nil {
81 t.Fatal(err)
82 }
83
84 tests := []struct {
85 Ciphertext []byte
86 Want string
87 WantErr bool
88 WantInvalidLengthErr bool // true if we want the error to be that the ciphertext length is invalid
89 }{
90 {nil, "", true, true},
91 {[]byte{}, "", true, true},
92 {[]byte{0}, "", true, true},
93 {ciphertext[:1], "", true, true},
94 {ciphertext[:nonceSize-1], "", true, true},
95 {ciphertext[:nonceSize], "", true, false}, // not invalid, but Decrypt will fail
96 {ciphertext, plaintext, false, false}, // works
97 }
98
99 for _, test := range tests {
100 got, err := keeper.Decrypt(ctx, test.Ciphertext)
101 if (err != nil) != test.WantErr {
102 t.Errorf("got err %v from Decrypt, want error? %v", err, test.WantErr)
103 }
104 if err == nil {
105 if gotStr := string(got); gotStr != test.Want {
106 t.Errorf("got %s want %s", gotStr, test.Want)
107 }
108 } else {
109 if gotInvalid := strings.Contains(err.Error(), "invalid message length"); gotInvalid != test.WantInvalidLengthErr {
110 t.Errorf("got invalid message length error? %v want %v", gotInvalid, test.WantInvalidLengthErr)
111 }
112 }
113 // Encrypt should always work.
114 if _, err := keeper.Encrypt(ctx, test.Ciphertext); err != nil {
115 t.Errorf("got error %v from Encrypt, want nil", err)
116 }
117 }
118 }
119
120 func TestOpenKeeper(t *testing.T) {
121 tests := []struct {
122 URL string
123 WantErr bool
124 }{
125 // OK.
126 {"base64key://", false},
127 // OK.
128 {"base64key://smGbjm71Nxd1Ig5FS0wj9SlbzAIrnolCz9bQQ6uAhl4=", false},
129 // Valid base64, but < 32 bytes.
130 {"base64key://c2VjcmV0", true},
131 // Valid base64, but > 32 bytes.
132 {"base64key://c2VjcmV0c2VjcmV0c2VjcmV0c2VjcmV0c2VjcmV0c3NlY3JldHNlY3JldHNlY3JldHNlY3JldHNlY3JldHM=", true},
133 // Invalid base64 key.
134 {"base64key://not-valid-base64", true},
135 // Valid base64 key (but invalid if using Std encoding instead of URL encoding).
136 {"base64Key://UKcmEoZW7nKl0uPHr8yV__KJm0ANhiFz8PzDN-gYWq8=", false},
137 // Invalid parameter.
138 {"base64key://?param=value", true},
139 }
140
141 ctx := context.Background()
142 for _, test := range tests {
143 keeper, err := secrets.OpenKeeper(ctx, test.URL)
144 if (err != nil) != test.WantErr {
145 t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr)
146 }
147 if err == nil {
148 if err = keeper.Close(); err != nil {
149 t.Errorf("%s: got error during close: %v", test.URL, err)
150 }
151 }
152 }
153 }