/
config_test.go
189 lines (166 loc) · 5.81 KB
/
config_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
/*
* Copyright (c) 2014, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package psiphon
import (
"encoding/json"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/suite"
)
const (
_README = "../README.md"
_README_CONFIG_BEGIN = "<!--BEGIN-SAMPLE-CONFIG-->"
_README_CONFIG_END = "<!--END-SAMPLE-CONFIG-->"
)
type ConfigTestSuite struct {
suite.Suite
confStubBlob []byte
requiredFields []string
nonRequiredFields []string
}
func (suite *ConfigTestSuite) SetupSuite() {
readmeBlob, _ := ioutil.ReadFile(_README)
readmeString := string(readmeBlob)
readmeString = readmeString[strings.Index(readmeString, _README_CONFIG_BEGIN)+len(_README_CONFIG_BEGIN) : strings.Index(readmeString, _README_CONFIG_END)]
readmeString = strings.TrimSpace(readmeString)
readmeString = strings.Trim(readmeString, "`")
suite.confStubBlob = []byte(readmeString)
var obj map[string]interface{}
json.Unmarshal(suite.confStubBlob, &obj)
for k, v := range obj {
if v == "<placeholder>" {
suite.requiredFields = append(suite.requiredFields, k)
} else {
suite.nonRequiredFields = append(suite.nonRequiredFields, k)
}
}
}
func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTestSuite))
}
// Tests good config
func (suite *ConfigTestSuite) Test_LoadConfig_BasicGood() {
config, err := LoadConfig(suite.confStubBlob)
if err == nil {
err = config.Commit()
}
suite.Nil(err, "a basic config should succeed")
}
// Tests non-JSON file contents
func (suite *ConfigTestSuite) Test_LoadConfig_BadFileContents() {
_, err := LoadConfig([]byte(`this is not JSON`))
suite.NotNil(err, "bytes that are not JSON at all should give an error")
}
// Tests config file with JSON contents that don't match our structure
func (suite *ConfigTestSuite) Test_LoadConfig_BadJson() {
var testObj map[string]interface{}
var testObjJSON []byte
// JSON with none of our fields
config, err := LoadConfig([]byte(`{"f1": 11, "f2": "two"}`))
if err == nil {
err = config.Commit()
}
suite.NotNil(err, "JSON with none of our fields should fail")
// Test all required fields
for _, field := range suite.requiredFields {
// Missing a required field
json.Unmarshal(suite.confStubBlob, &testObj)
delete(testObj, field)
testObjJSON, _ = json.Marshal(testObj)
config, err = LoadConfig(testObjJSON)
if err == nil {
err = config.Commit()
}
suite.NotNil(err, "JSON with one of our required fields missing should fail: %s", field)
// Bad type for required field
json.Unmarshal(suite.confStubBlob, &testObj)
testObj[field] = false // basically guessing a wrong type
testObjJSON, _ = json.Marshal(testObj)
config, err = LoadConfig(testObjJSON)
if err == nil {
err = config.Commit()
}
suite.NotNil(err, "JSON with one of our required fields with the wrong type should fail: %s", field)
// One of our required fields is null
json.Unmarshal(suite.confStubBlob, &testObj)
testObj[field] = nil
testObjJSON, _ = json.Marshal(testObj)
config, err = LoadConfig(testObjJSON)
if err == nil {
err = config.Commit()
}
suite.NotNil(err, "JSON with one of our required fields set to null should fail: %s", field)
// One of our required fields is an empty string
json.Unmarshal(suite.confStubBlob, &testObj)
testObj[field] = ""
testObjJSON, _ = json.Marshal(testObj)
config, err = LoadConfig(testObjJSON)
if err == nil {
err = config.Commit()
}
suite.NotNil(err, "JSON with one of our required fields set to an empty string should fail: %s", field)
}
// Test optional fields
for _, field := range suite.nonRequiredFields {
// Has incorrect type for optional field
json.Unmarshal(suite.confStubBlob, &testObj)
testObj[field] = false // basically guessing a wrong type
testObjJSON, _ = json.Marshal(testObj)
config, err = LoadConfig(testObjJSON)
if err == nil {
err = config.Commit()
}
suite.NotNil(err, "JSON with one of our optional fields with the wrong type should fail: %s", field)
}
}
// Tests config file with JSON contents that don't match our structure
func (suite *ConfigTestSuite) Test_LoadConfig_GoodJson() {
var testObj map[string]interface{}
var testObjJSON []byte
// TODO: Test that the config actually gets the values we expect?
// Has all of our required fields, but no optional fields
json.Unmarshal(suite.confStubBlob, &testObj)
for i := range suite.nonRequiredFields {
delete(testObj, suite.nonRequiredFields[i])
}
testObjJSON, _ = json.Marshal(testObj)
config, err := LoadConfig(testObjJSON)
if err == nil {
err = config.Commit()
}
suite.Nil(err, "JSON with good values for our required fields but no optional fields should succeed")
// Has all of our required fields, and all optional fields
config, err = LoadConfig(suite.confStubBlob)
if err == nil {
err = config.Commit()
}
suite.Nil(err, "JSON with all good values for required and optional fields should succeed")
// Has null for optional fields
json.Unmarshal(suite.confStubBlob, &testObj)
for i := range suite.nonRequiredFields {
testObj[suite.nonRequiredFields[i]] = nil
}
testObjJSON, _ = json.Marshal(testObj)
config, err = LoadConfig(testObjJSON)
if err == nil {
err = config.Commit()
}
suite.Nil(err, "JSON with null for optional values should succeed")
}