-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
219 lines (194 loc) · 5.83 KB
/
Copy pathconfig_test.go
File metadata and controls
219 lines (194 loc) · 5.83 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
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
// Copyright (c) 2024-2026 Blink Labs Software
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"net"
"os"
"path/filepath"
"regexp"
"runtime"
"slices"
"testing"
"time"
)
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-handshake-node.conf")
// Setup a temporary directory
tmpDir := t.TempDir()
testpath := filepath.Join(tmpDir, "test.conf")
// copy config file to location of handshake-node 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-handshake-node.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.")
}
}
func TestDefaultRPCPorts(t *testing.T) {
if mainNetParams.rpcPort != "12037" {
t.Fatalf("mainnet RPC port: got %q, want %q",
mainNetParams.rpcPort, "12037")
}
if regressionNetParams.rpcPort != "14037" {
t.Fatalf("regtest RPC port: got %q, want %q",
regressionNetParams.rpcPort, "14037")
}
if defaultMetricsPort != "12039" {
t.Fatalf("metrics port: got %q, want %q",
defaultMetricsPort, "12039")
}
if defaultStratumPort != "12040" {
t.Fatalf("stratum port: got %q, want %q",
defaultStratumPort, "12040")
}
}
func TestApplyConfigEnvOverrides(t *testing.T) {
cfg := config{
RPCUser: "fileuser",
Generate: false,
BanDuration: time.Second,
AddPeers: []string{"from-file"},
BlockMaxSize: 1,
MaxProofRPS: 100,
Prune: 1,
ConfigFile: "from-file.conf",
}
env := map[string]string{
"HANDSHAKE_NODE_RPCUSER": "envuser",
"HANDSHAKE_NODE_GENERATE": "true",
"HANDSHAKE_NODE_BANDURATION": "2m",
"HANDSHAKE_NODE_ADDPEER": "127.0.0.1,127.0.0.2",
"HANDSHAKE_NODE_BLOCKMAXSIZE": "010",
"HANDSHAKE_NODE_MAXPROOFRPS": "25",
"HANDSHAKE_NODE_PRUNE": "010",
"HANDSHAKE_NODE_CONFIGFILE": "from-env.conf",
"HANDSHAKE_NODE_VERSION": "true",
}
lookup := func(key string) (string, bool) {
value, ok := env[key]
return value, ok
}
if err := applyConfigEnvOverrides(&cfg, lookup); err != nil {
t.Fatalf("applyConfigEnvOverrides: %v", err)
}
if cfg.RPCUser != "envuser" {
t.Fatalf("RPCUser: got %q, want %q", cfg.RPCUser, "envuser")
}
if !cfg.Generate {
t.Fatalf("Generate: got false, want true")
}
if cfg.BanDuration != 2*time.Minute {
t.Fatalf("BanDuration: got %v, want %v", cfg.BanDuration,
2*time.Minute)
}
if got, want := cfg.AddPeers, []string{"127.0.0.1", "127.0.0.2"}; !slices.Equal(got, want) {
t.Fatalf("AddPeers: got %v, want %v", got, want)
}
if cfg.BlockMaxSize != 10 {
t.Fatalf("BlockMaxSize: got %d, want %d", cfg.BlockMaxSize,
10)
}
if cfg.MaxProofRPS != 25 {
t.Fatalf("MaxProofRPS: got %d, want %d", cfg.MaxProofRPS, 25)
}
if cfg.Prune != 10 {
t.Fatalf("Prune: got %d, want %d", cfg.Prune, uint64(10))
}
if cfg.ConfigFile != "from-file.conf" {
t.Fatalf("ConfigFile: got %q, want %q", cfg.ConfigFile,
"from-file.conf")
}
if cfg.ShowVersion {
t.Fatalf("ShowVersion: got true, want false")
}
}
func TestParseIPNets(t *testing.T) {
nets, err := parseIPNets([]string{"127.0.0.1", "10.0.0.0/8"},
"rpcallowip")
if err != nil {
t.Fatalf("parseIPNets: %v", err)
}
if len(nets) != 2 {
t.Fatalf("parseIPNets len: got %d, want %d", len(nets), 2)
}
if !nets[0].Contains(net.ParseIP("127.0.0.1")) {
t.Fatalf("single-IP net does not contain 127.0.0.1")
}
if !nets[1].Contains(net.ParseIP("10.1.2.3")) {
t.Fatalf("CIDR net does not contain 10.1.2.3")
}
if _, err := parseIPNets([]string{"not-an-ip"}, "rpcallowip"); err == nil {
t.Fatalf("parseIPNets accepted invalid IP")
}
}
func TestParseAssumeValid(t *testing.T) {
assumeValid := "5b6ef2d3c1f3cdcadfd9a030ba1811efdd17740f14e166489760741d075992e0"
hash, err := parseAssumeValid(assumeValid)
if err != nil {
t.Fatalf("parseAssumeValid: %v", err)
}
if hash == nil {
t.Fatalf("parseAssumeValid returned nil hash")
}
if hash.String() != assumeValid {
t.Fatalf("parseAssumeValid hash: got %q, want %q",
hash.String(), assumeValid)
}
hash, err = parseAssumeValid("")
if err != nil {
t.Fatalf("parseAssumeValid empty: %v", err)
}
if hash != nil {
t.Fatalf("parseAssumeValid empty: got %v, want nil", hash)
}
if _, err := parseAssumeValid("not-a-hash"); err == nil {
t.Fatalf("parseAssumeValid malformed hash unexpectedly succeeded")
}
}
func TestParseCheckpointHashOrder(t *testing.T) {
const (
checkpoint = "1008:0000000000001013c28fa079b545fb805f04c496687799b98e35e83cbbb8953e"
wantHash = "0000000000001013c28fa079b545fb805f04c496687799b98e35e83cbbb8953e"
)
got, err := newCheckpointFromStr(checkpoint)
if err != nil {
t.Fatalf("newCheckpointFromStr: %v", err)
}
if got.Height != 1008 {
t.Fatalf("checkpoint height: got %d, want %d", got.Height, 1008)
}
if got.Hash.String() != wantHash {
t.Fatalf("checkpoint hash: got %q, want %q", got.Hash.String(), wantHash)
}
}