-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathnode_config_test.go
316 lines (279 loc) · 10.3 KB
/
node_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
package appdatabase
import (
"crypto/rand"
"database/sql"
"fmt"
"math"
"math/big"
"sort"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/nodecfg"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/t/helpers"
)
func setupTestDB(t *testing.T) (*sql.DB, func()) {
db, cleanup, err := helpers.SetupTestSQLDB(DbInitializer{}, "settings-tests-")
require.NoError(t, err)
return db, func() { require.NoError(t, cleanup()) }
}
func TestGetNodeConfig(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
nodeConfig := randomNodeConfig()
require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig))
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err)
require.Equal(t, nodeConfig, dbNodeConfig)
}
func TestSaveNodeConfig(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
newNodeConfig := randomNodeConfig()
require.NoError(t, nodecfg.SaveNodeConfig(db, newNodeConfig))
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err)
require.Equal(t, *newNodeConfig, *dbNodeConfig)
}
func TestMigrateNodeConfig(t *testing.T) {
// Migration will be run in setupTestDB. If there's an error, that function will fail
db, stop := setupTestDB(t)
defer stop()
// node_config column should be empty
var result string
err := db.QueryRow("SELECT COALESCE(NULL, 'empty')").Scan(&result)
require.NoError(t, err)
require.Equal(t, "empty", result)
}
func randomString() string {
b := make([]byte, 10)
_, _ = rand.Read(b)
return fmt.Sprintf("%x", b)[:10]
}
func randomBool() bool {
return randomInt(2) == 1
}
func randomInt(max int64) int {
r, _ := rand.Int(rand.Reader, big.NewInt(max))
return int(r.Int64())
}
func randomFloat(max int64) float64 {
r, _ := rand.Int(rand.Reader, big.NewInt(max))
return float64(r.Int64()) / (1 << 63)
}
func randomStringSlice() []string {
m := randomInt(7)
var result []string
for i := 0; i < m; i++ {
result = append(result, randomString())
}
sort.Strings(result)
return result
}
func randomTopicSlice() []discv5.Topic {
randomValues := randomStringSlice()
var result []discv5.Topic
for _, v := range randomValues {
result = append(result, discv5.Topic(v))
}
return result
}
func randomTopicLimits() map[discv5.Topic]params.Limits {
result := make(map[discv5.Topic]params.Limits)
m := randomInt(7) + 1
for i := 0; i < m; i++ {
result[discv5.Topic(fmt.Sprint(i))] = params.Limits{Min: randomInt(2), Max: randomInt(10)}
}
return result
}
func randomCustomNodes() map[string]string {
result := make(map[string]string)
m := randomInt(7)
for i := 0; i < m; i++ {
result[randomString()] = randomString()
}
return result
}
func randomNodeConfig() *params.NodeConfig {
privK, _ := crypto.GenerateKey()
return ¶ms.NodeConfig{
NetworkID: uint64(int64(randomInt(math.MaxInt64))),
DataDir: randomString(),
KeyStoreDir: randomString(),
NodeKey: randomString(),
NoDiscovery: randomBool(),
ListenAddr: randomString(),
AdvertiseAddr: randomString(),
Name: randomString(),
Version: randomString(),
APIModules: randomString(),
TLSEnabled: randomBool(),
MaxPeers: randomInt(math.MaxInt64),
MaxPendingPeers: randomInt(math.MaxInt64),
EnableStatusService: randomBool(),
BridgeConfig: params.BridgeConfig{Enabled: randomBool()},
WalletConfig: params.WalletConfig{Enabled: randomBool()},
LocalNotificationsConfig: params.LocalNotificationsConfig{Enabled: randomBool()},
BrowsersConfig: params.BrowsersConfig{Enabled: randomBool()},
PermissionsConfig: params.PermissionsConfig{Enabled: randomBool()},
MailserversConfig: params.MailserversConfig{Enabled: randomBool()},
Web3ProviderConfig: params.Web3ProviderConfig{Enabled: randomBool()},
ConnectorConfig: params.ConnectorConfig{Enabled: randomBool()},
SwarmConfig: params.SwarmConfig{Enabled: randomBool()},
MailServerRegistryAddress: randomString(),
HTTPEnabled: randomBool(),
HTTPHost: randomString(),
HTTPPort: randomInt(math.MaxInt64),
HTTPVirtualHosts: randomStringSlice(),
HTTPCors: randomStringSlice(),
WSEnabled: false, // NOTE: leaving ws field idle since we are moving away from the storing the whole config
WSHost: "",
WSPort: 0,
IPCEnabled: randomBool(),
IPCFile: randomString(),
LogEnabled: randomBool(),
LogDir: randomString(),
LogFile: randomString(),
LogLevel: randomString(),
LogMaxBackups: randomInt(math.MaxInt64),
LogMaxSize: randomInt(math.MaxInt64),
LogCompressRotated: randomBool(),
LogToStderr: randomBool(),
ClusterConfig: params.ClusterConfig{
Enabled: randomBool(),
Fleet: randomString(),
StaticNodes: randomStringSlice(),
BootNodes: randomStringSlice(),
},
LightEthConfig: params.LightEthConfig{
Enabled: randomBool(),
DatabaseCache: randomInt(math.MaxInt64),
TrustedNodes: randomStringSlice(),
MinTrustedFraction: randomInt(math.MaxInt64),
},
RegisterTopics: randomTopicSlice(),
RequireTopics: randomTopicLimits(),
PushNotificationServerConfig: params.PushNotificationServerConfig{
Enabled: randomBool(),
GorushURL: randomString(),
Identity: privK,
},
ShhextConfig: params.ShhextConfig{
PFSEnabled: randomBool(),
InstallationID: randomString(),
MailServerConfirmations: randomBool(),
EnableConnectionManager: randomBool(),
EnableLastUsedMonitor: randomBool(),
ConnectionTarget: randomInt(math.MaxInt64),
RequestsDelay: time.Duration(randomInt(math.MaxInt64)),
MaxServerFailures: randomInt(math.MaxInt64),
MaxMessageDeliveryAttempts: randomInt(math.MaxInt64),
WhisperCacheDir: randomString(),
DisableGenericDiscoveryTopic: randomBool(),
SendV1Messages: randomBool(),
DataSyncEnabled: randomBool(),
VerifyTransactionURL: randomString(),
VerifyENSURL: randomString(),
VerifyENSContractAddress: randomString(),
VerifyTransactionChainID: int64(randomInt(math.MaxInt64)),
AnonMetricsSendID: randomString(),
AnonMetricsServerEnabled: randomBool(),
AnonMetricsServerPostgresURI: randomString(),
BandwidthStatsEnabled: randomBool(),
},
WakuV2Config: params.WakuV2Config{
Enabled: randomBool(),
Host: randomString(),
Port: randomInt(math.MaxInt64),
LightClient: randomBool(),
FullNode: randomBool(),
DiscoveryLimit: randomInt(math.MaxInt64),
DataDir: randomString(),
MaxMessageSize: uint32(randomInt(math.MaxInt64)),
EnableConfirmations: randomBool(),
CustomNodes: randomCustomNodes(),
EnableDiscV5: randomBool(),
UDPPort: randomInt(math.MaxInt64),
AutoUpdate: randomBool(),
},
WakuConfig: params.WakuConfig{
Enabled: randomBool(),
LightClient: randomBool(),
FullNode: randomBool(),
EnableMailServer: randomBool(),
DataDir: randomString(),
MinimumPoW: randomFloat(math.MaxInt64),
MailServerPassword: randomString(),
MailServerRateLimit: randomInt(math.MaxInt64),
MailServerDataRetention: randomInt(math.MaxInt64),
TTL: randomInt(math.MaxInt64),
MaxMessageSize: uint32(randomInt(math.MaxInt64)),
DatabaseConfig: params.DatabaseConfig{
PGConfig: params.PGConfig{
Enabled: randomBool(),
URI: randomString(),
},
},
EnableRateLimiter: randomBool(),
PacketRateLimitIP: int64(randomInt(math.MaxInt64)),
PacketRateLimitPeerID: int64(randomInt(math.MaxInt64)),
BytesRateLimitIP: int64(randomInt(math.MaxInt64)),
BytesRateLimitPeerID: int64(randomInt(math.MaxInt64)),
RateLimitTolerance: int64(randomInt(math.MaxInt64)),
BloomFilterMode: randomBool(),
SoftBlacklistedPeerIDs: randomStringSlice(),
EnableConfirmations: randomBool(),
},
}
}
func TestConfigValidate(t *testing.T) {
// GIVEN
db, stop := setupTestDB(t)
defer stop()
tmpdir := t.TempDir()
nodeConfig, err := params.NewNodeConfig(tmpdir, 1777)
require.NoError(t, err)
require.NoError(t, nodeConfig.Validate())
require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig))
// WHEN
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err)
// THEN
require.NoError(t, dbNodeConfig.Validate())
}
func TestRepairLoadedTorrentConfig(t *testing.T) {
// GIVEN
db, stop := setupTestDB(t)
defer stop()
tmpdir := t.TempDir()
nodeConfig, err := params.NewNodeConfig(tmpdir, 1777)
require.NoError(t, err)
require.NoError(t, nodeConfig.Validate())
// Write config to db
require.NoError(t, nodecfg.SaveNodeConfig(db, nodeConfig))
// WHEN: Corrupt the torrent config data as described in the ticket
// (https://github.com/status-im/status-desktop/issues/14643)
// Write invalid torrent config to database
nodeConfig.TorrentConfig.DataDir = ""
nodeConfig.TorrentConfig.TorrentDir = ""
nodeConfig.TorrentConfig.Enabled = true
require.Error(t, nodeConfig.Validate())
_, err = db.Exec(`INSERT OR REPLACE INTO torrent_config (
enabled, port, data_dir, torrent_dir, synthetic_id
) VALUES (?, ?, ?, ?, 'id')`,
nodeConfig.TorrentConfig.Enabled,
nodeConfig.TorrentConfig.Port,
nodeConfig.TorrentConfig.DataDir,
nodeConfig.TorrentConfig.TorrentDir,
)
require.NoError(t, err)
dbNodeConfig, err := nodecfg.GetNodeConfigFromDB(db)
require.NoError(t, err)
// THEN The invalid torrent config should be repaired
require.Error(t, dbNodeConfig.Validate())
require.NoError(t, dbNodeConfig.UpdateWithDefaults())
require.NoError(t, dbNodeConfig.Validate())
}