forked from iotexproject/iotex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
484 lines (413 loc) · 12.7 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package config
import (
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/iotexproject/go-pkgs/crypto"
)
const (
overwritePath = "_overwritePath"
secretPath = "_secretPath"
subChainPath = "_subChainPath"
)
func makePathAndWriteFile(cfgStr, flagForPath string) (err error) {
switch flagForPath {
case overwritePath:
_overwritePath = filepath.Join(os.TempDir(), "config.yaml")
err = ioutil.WriteFile(_overwritePath, []byte(cfgStr), 0666)
case secretPath:
_secretPath = filepath.Join(os.TempDir(), "secret.yaml")
err = ioutil.WriteFile(_secretPath, []byte(cfgStr), 0666)
case subChainPath:
_subChainPath = filepath.Join(os.TempDir(), "config.yaml")
err = ioutil.WriteFile(_subChainPath, []byte(cfgStr), 0666)
}
return err
}
func resetPathValues(t *testing.T, flagForPath []string) {
for _, pathValue := range flagForPath {
switch pathValue {
case overwritePath:
err := os.Remove(_overwritePath)
_overwritePath = ""
require.NoError(t, err)
case secretPath:
err := os.Remove(_secretPath)
_secretPath = ""
require.NoError(t, err)
case subChainPath:
err := os.Remove(_subChainPath)
_subChainPath = ""
require.NoError(t, err)
}
}
}
func resetPathValuesWithLookupEnv(t *testing.T, oldEnv string, oldExist bool, flagForPath string) {
switch flagForPath {
case overwritePath:
err := os.Remove(_overwritePath)
require.NoError(t, err)
_overwritePath = ""
if oldExist {
err = os.Setenv("IOTEX_TEST_NODE_TYPE", oldEnv)
} else {
err = os.Unsetenv("IOTEX_TEST_NODE_TYPE")
}
require.NoError(t, err)
case subChainPath:
err := os.Remove(_subChainPath)
require.NoError(t, err)
_subChainPath = ""
if oldExist {
err = os.Setenv("IOTEX_TEST_NODE_TYPE", oldEnv)
} else {
err = os.Unsetenv("IOTEX_TEST_NODE_TYPE")
}
require.NoError(t, err)
}
}
func generateProducerPrivKey() (crypto.PrivateKey, string, error) {
sk, err := crypto.GenerateKey()
cfgStr := fmt.Sprintf(`
chain:
producerPrivKey: "%s"
`,
sk.HexString(),
)
return sk, cfgStr, err
}
func TestDB_SplitDBSize(t *testing.T) {
var db = DB{SplitDBSizeMB: uint64(1)}
var expected = uint64(1 * 1024 * 1024)
require.Equal(t, expected, db.SplitDBSize())
}
func TestStrs_String(t *testing.T) {
ss := strs{"test"}
str := "TEST"
require.Nil(t, ss.Set(str))
}
func TestNewDefaultConfig(t *testing.T) {
_, err := New()
require.NoError(t, err)
}
func TestNewConfigWithoutValidation(t *testing.T) {
cfg, err := New(DoNotValidate)
require.NoError(t, err)
require.NotNil(t, cfg)
exp := Default
exp.Network.MasterKey = cfg.Chain.ProducerPrivKey
require.Equal(t, exp, cfg)
}
func TestNewConfigWithWrongConfigPath(t *testing.T) {
_overwritePath = "wrong_path"
defer func() { _overwritePath = "" }()
cfg, err := New()
require.Error(t, err)
require.Equal(t, Config{}, cfg)
if strings.Contains(err.Error(),
"open wrong_path: The system cannot find the file specified") == false { // for Windows
require.Contains(t, err.Error(), "open wrong_path: no such file or directory")
}
}
func TestNewConfigWithPlugins(t *testing.T) {
_plugins = strs{
"gateway",
}
cfg, err := New()
require.Nil(t, cfg.Plugins[GatewayPlugin])
require.NoError(t, err)
_plugins = strs{
"trick",
}
cfg, err = New()
require.Equal(t, Config{}, cfg)
require.Error(t, err)
defer func() {
_plugins = nil
}()
}
func TestNewConfigWithOverride(t *testing.T) {
sk, cfgStr, err := generateProducerPrivKey()
require.NoError(t, err)
require.NoError(t, makePathAndWriteFile(cfgStr, "_overwritePath"))
defer resetPathValues(t, []string{"_overwritePath"})
cfg, err := New()
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, sk.HexString(), cfg.Chain.ProducerPrivKey)
}
func TestNewConfigWithSecret(t *testing.T) {
sk, cfgStr, err := generateProducerPrivKey()
require.NoError(t, err)
require.NoError(t, makePathAndWriteFile(cfgStr, "_overwritePath"))
require.NoError(t, makePathAndWriteFile(cfgStr, "_secretPath"))
defer resetPathValues(t, []string{"_overwritePath", "_secretPath"})
cfg, err := New()
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, sk.HexString(), cfg.Chain.ProducerPrivKey)
}
func TestNewConfigWithLookupEnv(t *testing.T) {
oldEnv, oldExist := os.LookupEnv("IOTEX_TEST_NODE_TYPE")
_, cfgStr, err := generateProducerPrivKey()
require.NoError(t, err)
require.NoError(t, makePathAndWriteFile(cfgStr, "_overwritePath"))
defer resetPathValuesWithLookupEnv(t, oldEnv, oldExist, "_overwritePath")
cfg, err := New()
require.NoError(t, err)
require.NotNil(t, cfg)
err = os.Unsetenv("IOTEX_TEST_NODE_TYPE")
require.NoError(t, err)
cfg, err = New()
require.NoError(t, err)
require.NotNil(t, cfg)
}
func TestValidateDispatcher(t *testing.T) {
cfg := Default
cfg.Dispatcher.EventChanSize = 0
err := ValidateDispatcher(cfg)
require.Error(t, err)
require.Equal(t, ErrInvalidCfg, errors.Cause(err))
require.True(
t,
strings.Contains(err.Error(), "dispatcher event chan size should be greater than 0"),
)
}
func TestValidateRollDPoS(t *testing.T) {
cfg := Default
cfg.Consensus.Scheme = RollDPoSScheme
cfg.Consensus.RollDPoS.FSM.EventChanSize = 0
err := ValidateRollDPoS(cfg)
require.Error(t, err)
require.Equal(t, ErrInvalidCfg, errors.Cause(err))
require.True(
t,
strings.Contains(err.Error(), "roll-DPoS event chan size should be greater than 0"),
)
}
func TestValidateArchiveMode(t *testing.T) {
cfg := Default
cfg.Chain.EnableArchiveMode = true
cfg.Chain.EnableTrielessStateDB = true
require.Error(t, ErrInvalidCfg, errors.Cause(ValidateArchiveMode(cfg)))
require.EqualError(t, ValidateArchiveMode(cfg), "Archive mode is incompatible with trieless state DB: invalid config value")
cfg.Chain.EnableArchiveMode = false
cfg.Chain.EnableTrielessStateDB = true
require.NoError(t, errors.Cause(ValidateArchiveMode(cfg)))
cfg.Chain.EnableArchiveMode = true
cfg.Chain.EnableTrielessStateDB = false
require.NoError(t, errors.Cause(ValidateArchiveMode(cfg)))
cfg.Chain.EnableArchiveMode = false
cfg.Chain.EnableTrielessStateDB = false
require.NoError(t, errors.Cause(ValidateArchiveMode(cfg)))
}
func TestValidateActPool(t *testing.T) {
cfg := Default
cfg.ActPool.MaxNumActsPerAcct = 0
err := ValidateActPool(cfg)
require.Error(t, err)
require.Equal(t, ErrInvalidCfg, errors.Cause(err))
require.True(
t,
strings.Contains(
err.Error(),
"maximum number of actions per pool or per account cannot be zero or negative",
),
)
cfg.ActPool.MaxNumActsPerAcct = 100
cfg.ActPool.MaxNumActsPerPool = 0
err = ValidateActPool(cfg)
require.Error(t, err)
require.Equal(t, ErrInvalidCfg, errors.Cause(err))
require.True(
t,
strings.Contains(
err.Error(),
"maximum number of actions per pool or per account cannot be zero or negative",
),
)
cfg.ActPool.MaxNumActsPerPool = 99
err = ValidateActPool(cfg)
require.Error(t, err)
require.Equal(t, ErrInvalidCfg, errors.Cause(err))
require.True(
t,
strings.Contains(
err.Error(),
"maximum number of actions per pool cannot be less than maximum number of actions per account",
),
)
}
func TestValidateMinGasPrice(t *testing.T) {
ap := ActPool{MinGasPriceStr: Default.ActPool.MinGasPriceStr}
mgp := ap.MinGasPrice()
fmt.Printf("%T,%v", mgp, mgp)
require.IsType(t, &big.Int{}, mgp)
}
func TestValidateProducerPrivateKey(t *testing.T) {
cfg := Default
sk := cfg.ProducerPrivateKey()
require.NotNil(t, sk)
}
func TestValidateProducerAddress(t *testing.T) {
cfg := Default
addr := cfg.ProducerAddress()
require.NotNil(t, addr)
}
func TestValidateForkHeights(t *testing.T) {
r := require.New(t)
tests := []struct {
fork string
err error
errMsg string
}{
{
"Pacific", ErrInvalidCfg, "Pacific is heigher than Aleutian",
},
{
"Aleutian", ErrInvalidCfg, "Aleutian is heigher than Bering",
},
{
"Bering", ErrInvalidCfg, "Bering is heigher than Cook",
},
{
"Cook", ErrInvalidCfg, "Cook is heigher than Dardanelles",
},
{
"Dardanelles", ErrInvalidCfg, "Dardanelles is heigher than Daytona",
},
{
"Daytona", ErrInvalidCfg, "Daytona is heigher than Easter",
},
{
"Easter", ErrInvalidCfg, "Easter is heigher than FairbankMigration",
},
{
"FbkMigration", ErrInvalidCfg, "FairbankMigration is heigher than Fairbank",
},
{
"Fairbank", ErrInvalidCfg, "Fairbank is heigher than Greenland",
},
{
"", nil, "",
},
}
for _, v := range tests {
cfg := newTestCfg(v.fork)
err := ValidateForkHeights(cfg)
r.Equal(v.err, errors.Cause(err))
if err != nil {
r.True(strings.Contains(err.Error(), v.errMsg))
}
}
}
func newTestCfg(fork string) Config {
cfg := Default
switch fork {
case "Pacific":
cfg.Genesis.PacificBlockHeight = cfg.Genesis.AleutianBlockHeight + 1
case "Aleutian":
cfg.Genesis.AleutianBlockHeight = cfg.Genesis.BeringBlockHeight + 1
case "Bering":
cfg.Genesis.BeringBlockHeight = cfg.Genesis.CookBlockHeight + 1
case "Cook":
cfg.Genesis.CookBlockHeight = cfg.Genesis.DardanellesBlockHeight + 1
case "Dardanelles":
cfg.Genesis.DardanellesBlockHeight = cfg.Genesis.DaytonaBlockHeight + 1
case "Daytona":
cfg.Genesis.DaytonaBlockHeight = cfg.Genesis.EasterBlockHeight + 1
case "Easter":
cfg.Genesis.EasterBlockHeight = cfg.Genesis.FbkMigrationBlockHeight + 1
case "FbkMigration":
cfg.Genesis.FbkMigrationBlockHeight = cfg.Genesis.FairbankBlockHeight + 1
case "Fairbank":
cfg.Genesis.FairbankBlockHeight = cfg.Genesis.GreenlandBlockHeight + 1
}
return cfg
}
func TestNewSubDefaultConfig(t *testing.T) {
_, err := NewSub()
require.NoError(t, err)
}
func TestNewSubConfigWithoutValidation(t *testing.T) {
cfg, err := NewSub(DoNotValidate)
require.NoError(t, err)
require.NotNil(t, cfg)
}
func TestNewSubConfigWithWrongConfigPath(t *testing.T) {
_subChainPath = "wrong_path"
defer func() { _subChainPath = "" }()
cfg, err := NewSub()
require.Error(t, err)
require.Equal(t, Config{}, cfg)
if strings.Contains(err.Error(),
"open wrong_path: The system cannot find the file specified") == false { // for Windows
require.Contains(t, err.Error(), "open wrong_path: no such file or directory")
}
}
func TestNewSubConfigWithSubChainPath(t *testing.T) {
sk, cfgStr, err := generateProducerPrivKey()
require.NoError(t, err)
require.NoError(t, makePathAndWriteFile(cfgStr, "_subChainPath"))
defer resetPathValues(t, []string{"_subChainPath"})
cfg, err := NewSub()
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, sk.HexString(), cfg.Chain.ProducerPrivKey)
}
func TestNewSubConfigWithSecret(t *testing.T) {
sk, cfgStr, err := generateProducerPrivKey()
require.NoError(t, err)
require.NoError(t, makePathAndWriteFile(cfgStr, "_subChainPath"))
require.NoError(t, makePathAndWriteFile(cfgStr, "_secretPath"))
defer resetPathValues(t, []string{"_subChainPath", "_secretPath"})
cfg, err := NewSub()
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, sk.HexString(), cfg.Chain.ProducerPrivKey)
}
func TestNewSubConfigWithLookupEnv(t *testing.T) {
oldEnv, oldExist := os.LookupEnv("IOTEX_TEST_NODE_TYPE")
_, cfgStr, err := generateProducerPrivKey()
require.NoError(t, err)
require.NoError(t, makePathAndWriteFile(cfgStr, "_subChainPath"))
defer resetPathValuesWithLookupEnv(t, oldEnv, oldExist, "_subChainPath")
cfg, err := NewSub()
require.NoError(t, err)
require.NotNil(t, cfg)
err = os.Unsetenv("IOTEX_TEST_NODE_TYPE")
require.NoError(t, err)
cfg, err = NewSub()
require.NoError(t, err)
require.NotNil(t, cfg)
}
func TestNewSubConfigWithoutSubChainPath(t *testing.T) {
_subChainPath = ""
cfg, err := NewSub()
require.Equal(t, Config{}, cfg)
require.Nil(t, err)
}
func TestWhitelist(t *testing.T) {
require := require.New(t)
cfg, err := NewSub()
require.NoError(err)
require.NotNil(cfg)
sk, err := crypto.HexStringToPrivateKey("308193020100301306072a8648ce3d020106082a811ccf5501822d0479307702010104202d57ec7da578b98dad465997748ed02af0c69092ad809598073e5a2356c20492a00a06082a811ccf5501822da14403420004223356f0c6f40822ade24d47b0cd10e9285402cbc8a5028a8eec9efba44b8dfe1a7e8bc44953e557b32ec17039fb8018a58d48c8ffa54933fac8030c9a169bf6")
require.NoError(err)
require.False(cfg.whitelistSignatureScheme(sk))
cfg.Chain.ProducerPrivKey = sk.HexString()
require.Panics(func() { cfg.ProducerPrivateKey() })
cfg.Chain.SignatureScheme = append(cfg.Chain.SignatureScheme, SigP256sm2)
require.Equal(sk, cfg.ProducerPrivateKey())
}