Repository navigation
Expand file tree
/
Copy pathconfig_test.go
More file actions
115 lines (101 loc) · 2.72 KB
/
Copy pathconfig_test.go
File metadata and controls
115 lines (101 loc) · 2.72 KB
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
package main
import (
"os"
"path/filepath"
"regexp"
"runtime"
"testing"
)
func TestValidateMaxPeers(t *testing.T) {
tests := []struct {
name string
maxPeers int
wantErr bool
}{
{name: "negative", maxPeers: -1, wantErr: true},
{name: "zero", wantErr: true},
{name: "positive", maxPeers: 1},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validateMaxPeers(test.maxPeers)
if test.wantErr && err == nil {
t.Fatal("expected validation error")
}
if !test.wantErr && err != nil {
t.Fatalf("unexpected validation error: %v", err)
}
})
}
}
// TestProxyTorIsolation checks when --torisolation applies to --proxy: always,
// unless --onion is set too, in which case it applies to the onion proxy.
func TestProxyTorIsolation(t *testing.T) {
tests := []struct {
name string
torIsolation bool
onionProxy string
want bool
}{
{name: "isolation", torIsolation: true, want: true},
{name: "no isolation"},
{
name: "bridge mode",
torIsolation: true,
onionProxy: "127.0.0.1:9050",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := proxyTorIsolation(
test.torIsolation, test.onionProxy,
)
if got != test.want {
t.Fatalf("got %v, want %v", got, test.want)
}
})
}
}
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-btcd.conf")
// Setup a temporary directory
tmpDir := t.TempDir()
testpath := filepath.Join(tmpDir, "test.conf")
// copy config file to location of btcd binary
data, err := os.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-btcd.conf")
err = os.WriteFile(tmpConfigFile, data, 0644)
if err != nil {
t.Fatalf("Failed copying sample config file: %v", err)
}
err = createDefaultConfigFile(testpath)
if err != nil {
t.Fatalf("Failed to create a default config file: %v", err)
}
content, err := os.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.")
}
}