forked from codahale/sneaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackaging_test.go
113 lines (93 loc) · 2.24 KB
/
packaging_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
package sneaker
import (
"archive/tar"
"bytes"
"io"
"io/ioutil"
"reflect"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
)
func TestPackagingRoundTrip(t *testing.T) {
fakeKMS := &FakeKMS{
GenerateOutputs: []kms.GenerateDataKeyOutput{
{
Plaintext: make([]byte, 32),
KeyId: aws.String("key1"),
CiphertextBlob: []byte("encrypted key"),
},
},
DecryptOutputs: []kms.DecryptOutput{
{
KeyId: aws.String("key1"),
Plaintext: make([]byte, 32),
},
},
}
man := Manager{
Envelope: Envelope{
KMS: fakeKMS,
},
KeyId: "key1",
}
input := map[string][]byte{
"example.txt": []byte("hello world"),
}
context := map[string]string{
"hostname": "example.com",
}
buf := bytes.NewBuffer(nil)
if err := man.Pack(input, context, "", buf); err != nil {
t.Fatal(err)
}
r, err := man.Unpack(context, bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatal(err)
}
output := map[string][]byte{}
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(tr)
if err != nil {
t.Fatal(err)
}
output[hdr.Name] = b
}
if !reflect.DeepEqual(input, output) {
t.Errorf("Input was %#v, but output was %#v", input, output)
}
genReq := fakeKMS.GenerateInputs[0]
if v, want := *genReq.KeyId, "key1"; v != want {
t.Errorf("Key ID was %q, but expected %q", v, want)
}
if v, want := *genReq.KeySpec, "AES_256"; v != want {
t.Errorf("Key spec was %v, but expected %v", v, want)
}
if v, want := fromAWS(genReq.EncryptionContext), context; !reflect.DeepEqual(v, want) {
t.Errorf("Encryption context was %#v, but expected %#v", v, want)
}
decReq := fakeKMS.DecryptInputs[0]
if v, want := decReq.CiphertextBlob, []byte("encrypted key"); !bytes.Equal(v, want) {
t.Errorf("Ciphertext Blob was %v, but expected %v", v, want)
}
if v, want := fromAWS(decReq.EncryptionContext), context; !reflect.DeepEqual(v, want) {
t.Errorf("Encryption context was %#v, but expected %#v", v, want)
}
}
func fromAWS(m map[string]*string) map[string]string {
if m == nil {
return nil
}
res := make(map[string]string, len(m))
for k, v := range m {
res[k] = *v
}
return res
}