-
Notifications
You must be signed in to change notification settings - Fork 0
/
localFileHandler_test.go
143 lines (115 loc) · 3.36 KB
/
localFileHandler_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
package configHandler
import (
"bytes"
"testing"
)
type Configuration struct {
Test bool `json:"Test"`
}
func TestWriteAndReadKeyFile(t *testing.T) {
newKey, err := generateNewKey()
if err != nil {
t.Fatalf("got error %s ", err)
}
salt := "test"
estimatedKey := prepareKey(newKey, salt)
tempFilePath := t.TempDir() + "/testKey.key"
writeKeyToFile(tempFilePath, estimatedKey)
readFileResult, err := readKeyFile(tempFilePath, salt)
if err != nil {
t.Fatalf("Error reading Testfile: %v", err)
}
if !bytes.Equal(estimatedKey, readFileResult) {
t.Fatalf("got wrong value from Keyfile")
}
}
func TestReadConfigFile(t *testing.T) {
plaintext := "{\"Test\":true}"
tempFilePath := t.TempDir() + "/testKey.key"
writeFile(tempFilePath, []byte(plaintext))
readFileResult, err := readConfigFile(tempFilePath)
if err != nil {
t.Fatalf("Error reading Testfile: %v", err)
}
if plaintext != string(readFileResult) {
t.Fatalf("got wrong value from Configfile: %s", string(readFileResult))
}
}
func TestWriteConfigFileWithKey(t *testing.T) {
plaintext := "{\"Test\":true}"
tempFilePath := t.TempDir()
tempConfigFile := tempFilePath + "/config.json"
salt := "test"
newKey, err := generateNewKey()
if err != nil {
t.Fatalf("got error %s ", err)
}
preparedKey := prepareKey(newKey, salt)
tempKeyFile := tempFilePath + "/config.key"
writeKeyToFile(tempKeyFile, newKey)
config := Configuration{}
config.Test = true
err = exportConfigToLocalFile(tempConfigFile, &config, true, salt)
if err != nil {
t.Fatalf("Error writing Testfile: %v", err)
}
writtenConfigFile, err := readConfigFile(tempConfigFile)
if err != nil {
t.Fatalf("got error %s ", err)
}
writtenConfigFile, err = decrypt(preparedKey, writtenConfigFile)
if err != nil {
t.Fatalf("got error %s ", err)
}
if string(writtenConfigFile) != plaintext {
t.Fatalf("got wrong value from Configfile: %s", writtenConfigFile)
}
}
func TestWriteConfigFileWithoutKey(t *testing.T) {
plaintext := "{\"Test\":true}"
tempFilePath := t.TempDir()
tempConfigFile := tempFilePath + "/config.json"
tempKeyFile := tempFilePath + "/config.key"
salt := "test"
config := Configuration{}
config.Test = true
err := exportConfigToLocalFile(tempConfigFile, &config, true, salt)
if err != nil {
t.Fatalf("Error writing Testfile: %v", err)
}
writtenConfigFile, err := readConfigFile(tempConfigFile)
if err != nil {
t.Fatalf("got error %s ", err)
}
writtenKey, err := readKeyFile(tempKeyFile, salt)
if err != nil {
t.Fatalf("got error %s ", err)
}
preparedKey := prepareKey(writtenKey, salt)
writtenConfigFile, err = decrypt(preparedKey, writtenConfigFile)
if err != nil {
t.Fatalf("got error %s ", err)
}
if string(writtenConfigFile) != plaintext {
t.Fatalf("got wrong value from Configfile: %s", writtenConfigFile)
}
}
func TestWriteConfigFileUnencrypted(t *testing.T) {
plaintext := "{\"Test\":true}"
tempFilePath := t.TempDir()
tempConfigFile := tempFilePath + "/config.json"
salt := "test"
config := Configuration{}
config.Test = true
err := exportConfigToLocalFile(tempConfigFile, &config, false, salt)
if err != nil {
t.Fatalf("Error writing Testfile: %v", err)
}
writtenConfigFile, err := readConfigFile(tempConfigFile)
if err != nil {
t.Fatalf("got error %s ", err)
}
if string(writtenConfigFile) != plaintext {
t.Fatalf("got wrong value from Configfile: %s", writtenConfigFile)
}
}