-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptex_test.go
192 lines (147 loc) · 4.21 KB
/
cryptex_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
//
// Copyright (C) 2020 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/cryptex
//
package cryptex_test
import (
"encoding/json"
"errors"
"os"
"testing"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
"github.com/fogfish/cryptex"
"github.com/fogfish/cryptex/cipher"
"github.com/fogfish/golem/generic"
"github.com/fogfish/it"
)
type MyString struct {
Secret cryptex.String `json:"secret"`
}
type MyJSON struct {
Secret cryptex.AnyT `json:"secret"`
}
func TestStringUnmarshalJSON(t *testing.T) {
cipher.Default = cipher.NewNone()
value := MyString{}
input := []byte("{\"secret\":\"cGxhaW50ZXh0\"}")
it.Ok(t).
If(json.Unmarshal(input, &value)).Should().Equal(nil).
If(value.Secret).Should().Equal(cryptex.String("plaintext")).
If(value.Secret.PlainText()).Should().Equal("plaintext")
}
func TestStringUnmarshalFail(t *testing.T) {
cipher.Default = cipher.NewKMS(fail{})
value := MyString{}
input := []byte("{\"secret\":\"cGxhaW50ZXh0\"}")
it.Ok(t).
If(
func() error {
return json.Unmarshal(input, &value)
},
).Should().Intercept(ErrDecrypt)
}
func TestStringMarshalJSON(t *testing.T) {
cipher.Default = cipher.NewNone()
cipher.Default.UseKey("alias/mykms/key")
value := MyString{cryptex.String("plaintext")}
bytes, err := json.Marshal(value)
it.Ok(t).
If(err).Should().Equal(nil).
If(bytes).Should().Equal([]byte("{\"secret\":\"cGxhaW50ZXh0\"}"))
}
func TestStringMarshalFail(t *testing.T) {
cipher.Default = cipher.NewKMS(fail{})
value := MyString{cryptex.String("plaintext")}
it.Ok(t).
If(
func() error {
_, err := json.Marshal(value)
return err
},
).Should().Intercept(ErrEncrypt)
}
func TestAnyTUnmarshalJSON(t *testing.T) {
cipher.Default = cipher.NewNone()
value := MyJSON{}
input := []byte("{\"secret\":\"eyJ0ZXh0IjoicGxhaW50ZXh0In0\"}")
it.Ok(t).
If(json.Unmarshal(input, &value)).Should().Equal(nil).
If(value.Secret).Should().Equal(cryptex.AnyT{"text": "plaintext"}).
If(value.Secret.PlainText()).Should().Equal(generic.L{"text": "plaintext"})
}
func TestAnyTUnmarshalFail(t *testing.T) {
cipher.Default = cipher.NewKMS(fail{})
cipher.Default.UseKey("alias/mykms/key")
value := MyJSON{}
input := []byte("{\"secret\":\"eyJ0ZXh0IjoicGxhaW50ZXh0In0\"}")
it.Ok(t).
If(
func() error {
return json.Unmarshal(input, &value)
},
).Should().Intercept(ErrDecrypt)
}
func TestAnyTMarshalJSON(t *testing.T) {
cipher.Default = cipher.NewNone()
value := MyJSON{cryptex.AnyT{"text": "plaintext"}}
bytes, err := json.Marshal(value)
it.Ok(t).
If(err).Should().Equal(nil).
If(bytes).Should().Equal([]byte("{\"secret\":\"eyJ0ZXh0IjoicGxhaW50ZXh0In0\"}"))
}
func TestAnyTMarshalFail(t *testing.T) {
cipher.Default = cipher.NewKMS(fail{})
value := MyJSON{cryptex.AnyT{"text": "plaintext"}}
it.Ok(t).
If(
func() error {
_, err := json.Marshal(value)
return err
},
).Should().Intercept(ErrEncrypt)
}
func TestSetenv(t *testing.T) {
cipher.Default = cipher.NewNone()
cipher.Default.UseKey("alias/mykms/key")
err := cryptex.Setenv("SET_KEY", "plaintext")
val := os.Getenv("SET_KEY")
it.Ok(t).
If(err).Should().Equal(nil).
If(val).Should().Equal("cGxhaW50ZXh0")
}
func TestSetenvFail(t *testing.T) {
cipher.Default = cipher.NewKMS(fail{})
err := cryptex.Setenv("SET_KEY", "plaintext")
it.Ok(t).
If(err).Should().Be().Like(ErrEncrypt)
}
func TestGetenv(t *testing.T) {
cipher.Default = cipher.NewNone()
cipher.Default.UseKey("alias/mykms/key")
os.Setenv("GET_KEY", "cGxhaW50ZXh0")
it.Ok(t).
If(cryptex.Getenv("GET_KEY")).Should().Equal("plaintext")
}
func TestGetenvFail(t *testing.T) {
cipher.Default = cipher.NewKMS(fail{})
os.Setenv("GET_KEY", "cGxhaW50ZXh0")
it.Ok(t).
If(cryptex.Getenv("GET_KEY")).Should().Equal("")
}
//
//
type fail struct {
kmsiface.KMSAPI
}
var ErrDecrypt = errors.New("Unable to decrypt")
var ErrEncrypt = errors.New("Unable to encrypt")
func (fail) Decrypt(input *kms.DecryptInput) (*kms.DecryptOutput, error) {
return nil, ErrDecrypt
}
func (fail) Encrypt(input *kms.EncryptInput) (*kms.EncryptOutput, error) {
return nil, ErrEncrypt
}