forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
175 lines (158 loc) · 4.4 KB
/
config_test.go
File metadata and controls
175 lines (158 loc) · 4.4 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
package lnd
import (
"fmt"
"testing"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/routing"
"github.com/stretchr/testify/require"
)
var (
testPassword = "testpassword"
redactedPassword = "[redacted]"
)
// TestConfigToFlatMap tests that the configToFlatMap function works as
// expected on the default configuration.
func TestConfigToFlatMap(t *testing.T) {
cfg := DefaultConfig()
cfg.BitcoindMode.RPCPass = testPassword
cfg.BtcdMode.RPCPass = testPassword
cfg.Tor.Password = testPassword
cfg.DB.Etcd.Pass = testPassword
cfg.DB.Postgres.Dsn = testPassword
// Set deprecated fields.
cfg.Bitcoin.Active = true
cfg.Tor.V2 = true
result, deprecated, err := configToFlatMap(cfg)
require.NoError(t, err)
// Check that the deprecated option has been parsed out.
require.Contains(t, deprecated, "bitcoin.active")
require.Contains(t, deprecated, "tor.v2")
// Pick a couple of random values to check.
require.Equal(t, DefaultLndDir, result["lnddir"])
require.Equal(
t, fmt.Sprintf("%v", chainreg.DefaultBitcoinTimeLockDelta),
result["bitcoin.timelockdelta"],
)
require.Equal(
t, fmt.Sprintf("%v", routing.DefaultAprioriWeight),
result["routerrpc.apriori.weight"],
)
require.Contains(t, result, "routerrpc.routermacaroonpath")
// Check that sensitive values are not included.
require.Equal(t, redactedPassword, result["bitcoind.rpcpass"])
require.Equal(t, redactedPassword, result["btcd.rpcpass"])
require.Equal(t, redactedPassword, result["tor.password"])
require.Equal(t, redactedPassword, result["db.etcd.pass"])
require.Equal(t, redactedPassword, result["db.postgres.dsn"])
}
// TestSupplyEnvValue tests that the supplyEnvValue function works as
// expected on the passed inputs.
func TestSupplyEnvValue(t *testing.T) {
// Mock environment variables for testing.
t.Setenv("EXISTING_VAR", "existing_value")
t.Setenv("EMPTY_VAR", "")
tests := []struct {
input string
expected string
description string
}{
{
input: "$EXISTING_VAR",
expected: "existing_value",
description: "Valid environment variable without " +
"default value",
},
{
input: "${EXISTING_VAR:-default_value}",
expected: "existing_value",
description: "Valid environment variable with " +
"default value",
},
{
input: "$NON_EXISTENT_VAR",
expected: "",
description: "Non-existent environment variable " +
"without default value",
},
{
input: "${NON_EXISTENT_VAR:-default_value}",
expected: "default_value",
description: "Non-existent environment variable " +
"with default value",
},
{
input: "$EMPTY_VAR",
expected: "",
description: "Empty environment variable without " +
"default value",
},
{
input: "${EMPTY_VAR:-default_value}",
expected: "default_value",
description: "Empty environment variable with " +
"default value",
},
{
input: "raw_input",
expected: "raw_input",
description: "Raw input - no matching format",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
result := supplyEnvValue(test.input)
require.Equal(t, test.expected, result)
})
}
}
// TestValidateConfigTrickleDelay tests that the TrickleDelay configuration
// is properly validated and defaulted in ValidateConfig. This test directly
// verifies the validation logic without going through the full ValidateConfig
// function which has many dependencies.
func TestValidateConfigTrickleDelay(t *testing.T) {
t.Parallel()
tests := []struct {
name string
trickleDelay int
expectedDelay int
}{
{
name: "zero delay defaults to 1ms",
trickleDelay: 0,
expectedDelay: 1,
},
{
name: "negative delay defaults to 1ms",
trickleDelay: -1000,
expectedDelay: 1,
},
{
name: "positive delay unchanged",
trickleDelay: 5000,
expectedDelay: 5000,
},
{
name: "minimum valid delay (1ms)",
trickleDelay: 1,
expectedDelay: 1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create a config with the test's TrickleDelay.
cfg := Config{
TrickleDelay: tc.trickleDelay,
}
// Simulate the validation logic from ValidateConfig.
if cfg.TrickleDelay <= 0 {
cfg.TrickleDelay = 1
}
// Verify the TrickleDelay was set to the expected
// value.
require.Equal(
t, tc.expectedDelay, cfg.TrickleDelay,
"TrickleDelay mismatch",
)
})
}
}