forked from decred/dcrdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
320 lines (272 loc) · 9.58 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/decred/dcrd/dcrutil/v2"
)
var tempConfigFile *os.File
var tempAppDataDir string
func TestMain(m *testing.M) {
// Temp config file is used to ensure there are no external influences
// from previously set env variables or default config files.
tempConfigFile, _ = ioutil.TempFile("", "dcrdata_test_file.cfg")
defer os.Remove(tempConfigFile.Name())
os.Setenv("DCRDATA_CONFIG_FILE", tempConfigFile.Name())
// Make an empty folder for appdata tests.
tempAppDataDir, _ = ioutil.TempDir("", "dcrdata_test_appdata")
defer os.RemoveAll(tempAppDataDir)
// Parse the -test.* flags before removing them from the command line
// arguments list, which we do to allow go-flags to succeed.
flag.Parse()
os.Args = os.Args[:1]
// Run the tests now that the testing package flags have been parsed.
retCode := m.Run()
os.Unsetenv("DCRDATA_CONFIG_FILE")
os.Exit(retCode)
}
// disableConfigFileEnv checks if the DCRDATA_CONFIG_FILE environment variable
// is set, unsets it, and returns a function that will return
// DCRDATA_CONFIG_FILE to its state before calling disableConfigFileEnv.
func disableConfigFileEnv() func() {
loc, wasSet := os.LookupEnv("DCRDATA_CONFIG_FILE")
if wasSet {
os.Unsetenv("DCRDATA_CONFIG_FILE")
return func() { os.Setenv("DCRDATA_CONFIG_FILE", loc) }
}
return func() {}
}
func TestLoadCustomConfigPresent(t *testing.T) {
// Load using the empty config file set via environment variable in
// TestMain. Since the file exists, it should not cause an error.
_, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
}
func TestLoadDefaultConfigMissing(t *testing.T) {
// Unset the custom config file.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Use the empty appdata dir.
os.Setenv("DCRDATA_APPDATA_DIR", tempAppDataDir)
defer os.Unsetenv("DCRDATA_APPDATA_DIR")
// Load using the the empty appdata directory (with no config file). Since
// this is the default config file, it should not cause an error.
_, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
}
func TestLoadCustomConfigMissing(t *testing.T) {
// Unset the custom config file.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Set a path to a non-existent config file. Use TempFile followed by Remove
// to guarantee the file does not exist.
goneFile, _ := ioutil.TempFile("", "blah")
os.Remove(goneFile.Name())
os.Setenv("DCRDATA_CONFIG_FILE", goneFile.Name())
// Attempt to load using the non-existent non-default config file, which
// should return an error.
_, err := loadConfig()
if err == nil {
t.Errorf("Loaded dcrdata config, but the explicitly set config file"+
"%s does not exist.", goneFile.Name())
}
}
// TestLoadDefaultConfigPathCustomAppdata ensures that setting appdata while the
// config file is not explicitly set will change the default config file
// location, and that there is no error if this new default config file does not
// exist as missing config files are only an error when explicitly set.
func TestLoadDefaultConfigPathCustomAppdata(t *testing.T) {
// Unset the custom config file.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Use the empty appdata dir.
os.Setenv("DCRDATA_APPDATA_DIR", tempAppDataDir)
defer os.Unsetenv("DCRDATA_APPDATA_DIR")
// Load using the the empty appdata directory (with no config file). Since
// this is the default config file, it should not cause an error.
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
// Verify that the default config file is located in the specified appdata
// directory rather than the default appdata directory.
expected := filepath.Join(tempAppDataDir, defaultConfigFilename)
if cfg.ConfigFile != expected {
t.Errorf("Default config file expected at %s, got %s", expected, cfg.ConfigFile)
}
}
func TestDefaultConfigAPIListen(t *testing.T) {
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
defaultAddr := defaultHost + ":" + defaultMainnetPort
if cfg.APIListen != defaultAddr {
t.Errorf("Expected API listen URL %s, got %s", defaultAddr, cfg.APIListen)
}
}
func TestDefaultConfigAPIListenWithEnv(t *testing.T) {
customListenPath := "0.0.0.0:7777"
os.Setenv("DCRDATA_LISTEN_URL", customListenPath)
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
if cfg.APIListen != customListenPath {
t.Errorf("Expected API listen URL %s, got %s", customListenPath, cfg.APIListen)
}
}
func TestDefaultConfigAppDataDir(t *testing.T) {
expected := dcrutil.AppDataDir("dcrdata", false)
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
if cfg.HomeDir != expected {
t.Errorf("Expected appdata directory %s, got %s", expected, cfg.HomeDir)
}
}
func TestCustomHomeDirWithEnv(t *testing.T) {
// Do not override config file as appdata changes its location.
restoreConfigFileLoc := disableConfigFileEnv()
defer restoreConfigFileLoc()
// Use the empty appdata dir made for the tests.
os.Setenv("DCRDATA_APPDATA_DIR", tempAppDataDir)
defer os.Unsetenv("DCRDATA_APPDATA_DIR")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
if cfg.HomeDir != tempAppDataDir {
t.Errorf("Expected appdata directory %s, got %s", tempAppDataDir, cfg.HomeDir)
}
}
// Ensure that command line flags override env variables.
func TestDefaultConfigHomeDirWithEnvAndFlag(t *testing.T) {
tmp2 := "dcrdata_test_appdata2"
cliOverride, err := ioutil.TempDir("", tmp2)
if err != nil {
t.Fatalf("Unable to create temporary folder %s: %v", tmp2, err)
}
defer os.RemoveAll(cliOverride)
os.Args = append(os.Args, "--appdata="+cliOverride)
os.Setenv("DCRDATA_APPDATA_DIR", cliOverride)
defer os.Unsetenv("DCRDATA_APPDATA_DIR")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
if cfg.HomeDir != cliOverride {
t.Errorf("Expected appdata directory %s, got %s", cliOverride, cfg.HomeDir)
}
}
func TestDefaultConfigNetwork(t *testing.T) {
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
if cfg.TestNet || cfg.SimNet {
t.Errorf("Default config should be for mainnet but was not.")
}
}
func TestDefaultConfigTestNetWithEnv(t *testing.T) {
os.Setenv("DCRDATA_USE_TESTNET", "true")
defer os.Unsetenv("DCRDATA_USE_TESTNET")
cfg, err := loadConfig()
if err != nil {
t.Fatalf("Failed to load dcrdata config: %v", err)
}
if !cfg.TestNet {
t.Errorf("Testnet was specified via environment variable, but not using testnet.")
}
}
func TestDefaultConfigTestNetWithEnvAndBadValue(t *testing.T) {
os.Setenv("DCRDATA_USE_TESTNET", "no")
defer os.Unsetenv("DCRDATA_USE_TESTNET")
_, err := loadConfig()
if err == nil {
t.Errorf("Invalid boolean value for DCRDATA_USE_TESTNET did not cause an error.")
}
}
func TestDisablePiparserValueOnSimnet(t *testing.T) {
os.Args = append(os.Args, "--simnet")
cfg, err := loadConfig()
if err != nil {
t.Errorf("expected to find no error but found: %v", err)
}
if !cfg.DisablePiParser {
t.Fatal("expected DisablePiParser value to be activated but it wasn't")
}
}
func TestRetrieveRootPath(t *testing.T) {
type testData struct {
RawURL string
FinalURL string
isError bool
}
td := []testData{
// :1234 is consided invalid url since the root url is also used as a
// hyperlink reference on the frontend.
{":1234", "", true},
// 192.168.10.12:1234 is consided invalid url by net/url package.
// https://github.com/golang/go/issues/21415#issuecomment-321966574
{"192.168.10.12:1234/xxxxx", "", true},
{"mydomain.com/", "mydomain.com", false},
{"mydomain.com?id=1", "mydomain.com", false},
{"192.168.10.12/xxxxx", "192.168.10.12", false},
{"localhost:1234/api/", "localhost:1234", false},
{"mydomain.com/xxxxx?id=1", "mydomain.com", false},
{"www.mydomain.com/xxxxx", "www.mydomain.com", false},
{"http://www.mydomain.com/xxxxx", "http://www.mydomain.com", false},
{"https://www.mydomain.com/xxxxx", "https://www.mydomain.com", false},
{"https://www.mydomain.com/xxxxx?id=1", "https://www.mydomain.com", false},
}
for _, val := range td {
result, err := retrieveRootPath(val.RawURL)
if err != nil && !val.isError {
t.Fatalf("expected no error but found: %v", err)
}
if result != val.FinalURL {
t.Fatalf("expected the returned url to be '%s' but found '%s'",
val.FinalURL, result)
}
}
}
func TestNormalizeNetworkAddress(t *testing.T) {
defaultPort := "1234"
defaultHost := "localhost"
type test struct {
input string
expectation string
shouldBeError bool
}
tests := []test{
{":1234", "localhost:1234", false},
{"some.name", "some.name:1234", false},
{"192.168.0.2", "192.168.0.2:1234", false},
{"192.168.0.2:5678", "192.168.0.2:5678", false},
{"http://remote.com:5678", "http://remote.com:5678", true}, // Only local addresses supported.
{"", "localhost:1234", false},
{":", "localhost:1234", false},
}
for _, test := range tests {
translated, err := normalizeNetworkAddress(test.input, defaultHost, defaultPort)
if translated != test.expectation {
t.Errorf("Unexpected result. input: %s, returned: %s, expected: %s", test.input, translated, test.expectation)
}
if err != nil {
if test.shouldBeError {
continue
}
t.Errorf("Unexpected error parsing %s: %v", test.input, err)
} else if test.shouldBeError {
t.Errorf("Error expected but not seen for %s", test.input)
}
}
}