-
Notifications
You must be signed in to change notification settings - Fork 2
/
encrypt_test.go
148 lines (126 loc) · 3.64 KB
/
encrypt_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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
log "github.com/sirupsen/logrus"
)
var update = flag.Bool("update", false, "update golden test data")
func TestEncrypt(t *testing.T) {
tests, err := filepath.Glob("testdata/encrypt_*.yaml")
if err != nil {
t.Fatalf("failed to parse test data :: %v", err)
}
for _, test := range tests {
keyfile := fmt.Sprintf("%v.key", test)
encrypted := fmt.Sprintf("%v.enc", test)
content, err := ioutil.ReadFile(test)
if err != nil {
t.Fatalf("failed to read file %v :: %v", test, err)
}
// update golden data for test if flag given
if *update {
key, nonce, err := newKey()
fmt.Printf("%v\n%v\n\n", key, nonce)
ioutil.WriteFile(keyfile, []byte(fmt.Sprintf("%v\n%v", key, nonce)), 0644)
result, err := encrypt(key, nonce, content)
if err != nil {
t.Fatalf("failed to encrypt data %v :: %v", test, err)
}
ioutil.WriteFile(encrypted, result, 0644)
}
keycontent, err := ioutil.ReadFile(keyfile)
if err != nil {
t.Fatalf("failed to read key %v :: %v", test, err)
}
key := strings.Split(string(keycontent), "\n")
result, err := encrypt(string(key[0]), string(key[1]), content)
if err != nil {
t.Fatalf("failed to encrypt data %v :: %v", test, err)
}
expected, err := ioutil.ReadFile(encrypted)
if err != nil {
t.Fatalf("failed to read encrypted data %v :: %v", test, err)
}
if bytes.Compare(result, expected) != 0 {
t.Errorf("expected: %v :: result: %v", expected, result)
}
}
}
func TestDecrypt(t *testing.T) {
tests, err := filepath.Glob("testdata/encrypt_*.yaml")
if err != nil {
t.Fatalf("failed to parse test data :: %v", err)
}
for _, test := range tests {
keyfile := fmt.Sprintf("%v.key", test)
encryptedfile := fmt.Sprintf("%v.enc", test)
content, err := ioutil.ReadFile(encryptedfile)
if err != nil {
t.Fatalf("failed to read file %v :: %v", test, err)
}
keycontent, err := ioutil.ReadFile(keyfile)
if err != nil {
t.Fatalf("failed to read key %v :: %v", test, err)
}
key := strings.Split(string(keycontent), "\n")
result, err := decrypt(string(key[0]), string(key[1]), string(content))
if err != nil {
t.Fatalf("failed to encrypt data %v :: %v", test, err)
}
expected, err := ioutil.ReadFile(test)
if err != nil {
t.Fatalf("failed to read encrypted data %v :: %v", test, err)
}
if bytes.Compare(result, expected) != 0 {
t.Errorf("expected: %v :: result: %v", expected, result)
}
}
}
func TestReleaseName(t *testing.T) {
fullwd, _ := os.Getwd()
wd := filepath.Base(fullwd)
r := releaseName()
if r != wd {
t.Fatalf("'%v' does not match expected '%v'", r, wd)
}
}
func Example_encrypt() {
// Sample yaml content
content := `
group:
value: 1
other: 2
`
// Sample AES-256 GCM key and nonce, use output from newKey()
key := "s928HkkJKVCGO1q1aIFq1iWG3ZDh6LB7utsZ1mRqjKg="
nonce := "cWcmxHPcuG0O0hY3"
result, err := encrypt(key, nonce, []byte(content))
if err != nil {
log.Fatalf("encrypt failed : %v", err)
}
fmt.Printf("%v", string(result))
// Output:
// EetswXTplHOz9LXemnt4cglcWhp9/Uv2vTif1kiSSzuWY/Gyp953iL1X7JMDTBtpAo5W0Bo=
}
func Example_decrypt() {
// Content to decrypt must be base64 encoded
b64content := "EetswXTplHOz9LXemnt4cglcWhp9/Uv2vTif1kiSSzuWY/Gyp953iL1X7JMDTBtpAo5W0Bo="
// Sample AES-256 GCM key and nonce, use output from newKey()
key := "s928HkkJKVCGO1q1aIFq1iWG3ZDh6LB7utsZ1mRqjKg="
nonce := "cWcmxHPcuG0O0hY3"
result, err := decrypt(key, nonce, b64content)
if err != nil {
log.Fatalf("decrypt failed : %v", err)
}
fmt.Printf("%v", string(result))
// Output:
// group:
// value: 1
// other: 2
}