forked from soteria-dag/soterd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
77 lines (65 loc) · 1.92 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
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2018-2019 The Soteria DAG developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"testing"
)
var (
rpcuserRegexp = regexp.MustCompile("(?m)^rpcuser=.+$")
rpcpassRegexp = regexp.MustCompile("(?m)^rpcpass=.+$")
)
func TestCreateDefaultConfigFile(t *testing.T) {
// find out where the sample config lives
_, path, _, ok := runtime.Caller(0)
if !ok {
t.Fatalf("Failed finding config file path")
}
sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-soterd.conf")
// Setup a temporary directory
tmpDir, err := ioutil.TempDir("", "soterd")
if err != nil {
t.Fatalf("Failed creating a temporary directory: %v", err)
}
testpath := filepath.Join(tmpDir, "test.conf")
// copy config file to location of soterd binary
data, err := ioutil.ReadFile(sampleConfigFile)
if err != nil {
t.Fatalf("Failed reading sample config file: %v", err)
}
appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
t.Fatalf("Failed obtaining app path: %v", err)
}
tmpConfigFile := filepath.Join(appPath, "sample-soterd.conf")
err = ioutil.WriteFile(tmpConfigFile, data, 0644)
if err != nil {
t.Fatalf("Failed copying sample config file: %v", err)
}
// Clean-up
defer func() {
os.Remove(testpath)
os.Remove(tmpConfigFile)
os.Remove(tmpDir)
}()
err = createDefaultConfigFile(testpath)
if err != nil {
t.Fatalf("Failed to create a default config file: %v", err)
}
content, err := ioutil.ReadFile(testpath)
if err != nil {
t.Fatalf("Failed to read generated default config file: %v", err)
}
if !rpcuserRegexp.Match(content) {
t.Error("Could not find rpcuser in generated default config file.")
}
if !rpcpassRegexp.Match(content) {
t.Error("Could not find rpcpass in generated default config file.")
}
}