summaryrefslogtreecommitdiff
path: root/config/config_test.go
blob: ef58bffb9dc5608325d1dc7ea67164bfc553b310 (plain)
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package config

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"reflect"
	"regexp"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/algorand/go-algorand/util/codecs"
)

var defaultConfig = Local{
	Archival:     false,
	GossipFanout: 4,

	IncomingConnectionsLimit: -1, // -1 marks no limit, otherwise marks limit
	BaseLoggerDebugLevel:     1,  //Info level
}

func TestLocal_SaveThenLoad(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	c1, err := loadWithoutDefaults(defaultConfig)
	require.NoError(t, err)
	c1, err = migrate(c1)
	require.NoError(t, err)
	var b1 bytes.Buffer
	ser1 := json.NewEncoder(&b1)
	ser1.Encode(c1)

	tempDir := t.TempDir()
	c1.SaveToDisk(tempDir)

	c2, err := LoadConfigFromDisk(tempDir)
	require.NoError(t, err)

	var b2 bytes.Buffer
	ser2 := json.NewEncoder(&b2)
	ser2.Encode(c2)

	require.True(t, bytes.Equal(b1.Bytes(), b2.Bytes()))
}

func TestConfig_LoadMissing(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	tempDir := t.TempDir()
	os.RemoveAll(tempDir)
	_, err := LoadConfigFromDisk(tempDir)
	require.True(t, os.IsNotExist(err))
}

func TestLocal_MergeConfig(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	tempDir := t.TempDir()

	c1 := struct {
		GossipFanout              int
		MaxNumberOfTxnsPerAccount int
		NetAddress                string
		ShouldNotExist            int // Ensure we don't panic when config has members we've removed
	}{}
	testInt := int(123)
	testString := "testing123"
	c1.GossipFanout = testInt
	c1.MaxNumberOfTxnsPerAccount = testInt
	c1.NetAddress = testString

	// write our reduced version of the Local struct
	fileToMerge := filepath.Join(tempDir, ConfigFilename)
	f, err := os.OpenFile(fileToMerge, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err == nil {
		enc := json.NewEncoder(f)
		err = enc.Encode(c1)
		f.Close()
	}

	require.NoError(t, err)

	// Take defaultConfig and merge with the saved custom settings.
	// This should result in c2 being the same as defaultConfig except for the value(s) in our custom c1
	c2, err := mergeConfigFromDir(tempDir, defaultConfig)

	require.NoError(t, err)
	require.Equal(t, defaultConfig.Archival || c1.NetAddress != "", c2.Archival)
	require.Equal(t, defaultConfig.IncomingConnectionsLimit, c2.IncomingConnectionsLimit)
	require.Equal(t, defaultConfig.BaseLoggerDebugLevel, c2.BaseLoggerDebugLevel)

	require.Equal(t, c1.NetAddress, c2.NetAddress)
	require.Equal(t, c1.GossipFanout, c2.GossipFanout)
}

func saveFullPhonebook(phonebook phonebookBlackWhiteList, saveToDir string) error {
	filename := filepath.Join(saveToDir, PhonebookFilename)
	f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err == nil {
		defer f.Close()
		enc := json.NewEncoder(f)
		err = enc.Encode(phonebook)
	}
	return err
}

var phonebook = phonebookBlackWhiteList{
	Include: []string{"Test1", "test2", "TEST3"},
}

var phonebookToMerge = phonebookBlackWhiteList{
	Include: []string{"test1", "addThisOne"},
}

func TestLoadPhonebook(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	tempDir := t.TempDir()

	err := saveFullPhonebook(phonebook, tempDir)
	require.NoError(t, err)

	phonebookEntries, err := LoadPhonebook(tempDir)
	require.NoError(t, err)
	require.Equal(t, 3, len(phonebookEntries))
	for index, entry := range phonebookEntries {
		require.Equal(t, phonebook.Include[index], entry)
	}
}

func TestLoadPhonebookMissing(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	tempDir := t.TempDir()
	_, err := LoadPhonebook(tempDir)
	require.True(t, os.IsNotExist(err))
}

func TestArchivalIfRelay(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	testArchivalIfRelay(t, true)
}

func TestArchivalIfNotRelay(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	testArchivalIfRelay(t, false)
}

func testArchivalIfRelay(t *testing.T, relay bool) {
	tempDir := t.TempDir()

	c1 := struct {
		NetAddress string
	}{}
	if relay {
		c1.NetAddress = ":1234"
	}

	// write our reduced version of the Local struct
	fileToMerge := filepath.Join(tempDir, ConfigFilename)
	f, err := os.OpenFile(fileToMerge, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err == nil {
		enc := json.NewEncoder(f)
		err = enc.Encode(c1)
		f.Close()
	}
	require.NoError(t, err)
	require.False(t, defaultConfig.Archival, "Default should be non-archival")

	c2, err := mergeConfigFromDir(tempDir, defaultConfig)
	require.NoError(t, err)
	if relay {
		require.True(t, c2.Archival, "Relay should be archival")
	} else {
		require.False(t, c2.Archival, "Non-relay should still be non-archival")
	}
}

func TestLocal_ConfigExampleIsCorrect(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	ourPath, err := os.Getwd()
	a.NoError(err)
	examplePath := filepath.Join(ourPath, "../installer/config.json.example")

	f, err := os.Open(examplePath)
	a.NoError(err)
	defer f.Close()

	dec := json.NewDecoder(f)
	var example Local
	err = dec.Decode(&example)
	a.NoError(err)
	a.Equal(example, defaultLocal)
}

// Returns the specified config prepared by
// serializing it (without default values) and
// unmarshaling it into a copy of defaultLocal.
// This mimics the behavior of saving an older config
// version (before new parameters exist), so we don't
// see their default (zero) values and instead see the
// new default because they won't exist in the old file.
func loadWithoutDefaults(cfg Local) (Local, error) {
	file, err := os.CreateTemp("", "lwd")
	if err != nil {
		return Local{}, err
	}
	name := file.Name()
	file.Close()
	defer os.Remove(name)
	err = cfg.SaveToFile(name)
	if err != nil {
		return Local{}, err
	}
	cfg, err = loadConfigFromFile(name)
	return cfg, err
}

func TestLocal_ConfigMigrate(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	c0, err := loadWithoutDefaults(GetVersionedDefaultLocalConfig(0))
	a.NoError(err)
	c0, err = migrate(c0)
	a.NoError(err)
	cLatest, err := migrate(defaultLocal)
	a.NoError(err)

	a.Equal(defaultLocal, c0)
	a.Equal(defaultLocal, cLatest)

	cLatest.Version = getLatestConfigVersion() + 1
	_, err = migrate(cLatest)
	a.Error(err)

	// Ensure we don't migrate values that aren't the default old version
	c0Modified := GetVersionedDefaultLocalConfig(0)
	c0Modified.BaseLoggerDebugLevel = GetVersionedDefaultLocalConfig(0).BaseLoggerDebugLevel + 1
	c0Modified, err = migrate(c0Modified)
	a.NoError(err)
	a.NotEqual(defaultLocal, c0Modified)
}

func TestLocal_ConfigMigrateFromDisk(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	ourPath, err := os.Getwd()
	a.NoError(err)
	configsPath := filepath.Join(ourPath, "../test/testdata/configs")

	for configVersion := uint32(0); configVersion <= getLatestConfigVersion(); configVersion++ {
		c, err := loadConfigFromFile(filepath.Join(configsPath, fmt.Sprintf("config-v%d.json", configVersion)))
		a.NoError(err)
		modified, err := migrate(c)
		a.NoError(err)
		a.Equal(defaultLocal, modified, "config-v%d.json", configVersion)
	}

	cNext := Local{Version: getLatestConfigVersion() + 1}
	_, err = migrate(cNext)
	a.Error(err)
}

// Verify that nobody is changing the shipping default configurations
func TestLocal_ConfigInvariant(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	ourPath, err := os.Getwd()
	a.NoError(err)
	configsPath := filepath.Join(ourPath, "../test/testdata/configs")

	// for configVersion := uint32(0); configVersion <= getLatestConfigVersion(); configVersion++ {
	for configVersion := uint32(27); configVersion <= 27; configVersion++ {
		c := Local{}
		err = codecs.LoadObjectFromFile(filepath.Join(configsPath, fmt.Sprintf("config-v%d.json", configVersion)), &c)
		a.NoError(err)
		a.Equal(GetVersionedDefaultLocalConfig(configVersion), c)
	}
}

func TestLocal_ConfigLatestVersion(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	a := require.New(t)

	// Make sure current version is correct for the assigned defaultLocal
	a.Equal(getLatestConfigVersion(), defaultLocal.Version)
}

func TestConsensusUpgrades(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	// Starting with v7, ensure we have a path to ConsensusCurrentVersion
	currentVersionName := protocol.ConsensusV7
	latestVersionName := protocol.ConsensusCurrentVersion

	leadsTo := consensusUpgradesTo(a, currentVersionName, latestVersionName, checkConsensusVersionName)
	a.True(leadsTo, "Consensus protocol must have upgrade path from %v to %v", currentVersionName, latestVersionName)
}

func checkConsensusVersionName(a *require.Assertions, name string) {
	// ensure versions come from official specs repo
	prefix1 := "https://github.com/algorandfoundation/specs/tree/"
	prefix2 := "https://github.com/algorand/spec/tree/"

	whitelist := map[string]bool{"v7": true, "v8": true, "v9": true, "v10": true, "v11": true, "v12": true}
	if !whitelist[name] {
		a.True(strings.HasPrefix(name, prefix1) || strings.HasPrefix(name, prefix2),
			"Consensus version %s does not start with allowed prefix", name)
	}
}

func consensusUpgradesTo(a *require.Assertions, currentName, targetName protocol.ConsensusVersion, nameCheckFn func(*require.Assertions, string)) bool {
	nameCheckFn(a, string(currentName))
	if currentName == targetName {
		return true
	}
	currentVersion, has := Consensus[currentName]
	a.True(has, "Consensus map should contain all references consensus versions: Missing '%v'", currentName)
	for upgrade := range currentVersion.ApprovedUpgrades {
		nameCheckFn(a, string(upgrade))
		if upgrade == targetName {
			return true
		}
		return consensusUpgradesTo(a, upgrade, targetName, nameCheckFn)
	}
	return false
}

func TestConsensusLatestVersion(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	latest, has := Consensus[protocol.ConsensusCurrentVersion]
	a.True(has, "ConsensusCurrentVersion doesn't appear to be a known version: %v", protocol.ConsensusCurrentVersion)
	a.Empty(latest.ApprovedUpgrades, "Latest ConsensusVersion should not have any upgrades - update ConsensusCurrentVersion")
}

func TestLocal_DNSBootstrapArray(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	type fields struct {
		DNSBootstrapID string
	}
	type args struct {
		networkID protocol.NetworkID
	}
	tests := []struct {
		name               string
		fields             fields
		args               args
		wantBootstrapArray []*DNSBootstrap
	}{
		{name: "test1",
			fields:             fields{DNSBootstrapID: "<network>.cloudflare.com"},
			args:               args{networkID: "devnet"},
			wantBootstrapArray: []*DNSBootstrap{{PrimarySRVBootstrap: "devnet.cloudflare.com"}},
		},
		{name: "test2",
			fields:             fields{DNSBootstrapID: "<network>.cloudflare.com;<network>.cloudfront.com"},
			args:               args{networkID: "devnet"},
			wantBootstrapArray: []*DNSBootstrap{{PrimarySRVBootstrap: "devnet.cloudflare.com"}, {PrimarySRVBootstrap: "devnet.cloudfront.com"}},
		},
		{name: "test3",
			fields:             fields{DNSBootstrapID: ""},
			args:               args{networkID: "devnet"},
			wantBootstrapArray: []*DNSBootstrap(nil),
		},
		{name: "test4 - intended to mismatch local template",
			fields: fields{DNSBootstrapID: "<network>.algorand.network?backup=<network>.algorand.net&dedup=<name>.algorand-<network>.(network|net)"},
			args:   args{networkID: "testnet"},
			wantBootstrapArray: []*DNSBootstrap{{PrimarySRVBootstrap: "testnet.algorand.network",
				BackupSRVBootstrap: "testnet.algorand.net",
				DedupExp:           regexp.MustCompile("(algorand-testnet.(network|net))")}},
		},
		{name: "test5 - intended to match legacy template",
			fields:             fields{DNSBootstrapID: "<network>.algorand.network"},
			args:               args{networkID: "testnet"},
			wantBootstrapArray: []*DNSBootstrap{{PrimarySRVBootstrap: "testnet.algorand.network"}},
		},
		{name: "test6 - exercise record append with full template",
			fields: fields{DNSBootstrapID: "<network>.algorand.network?backup=<network>.algorand.net&dedup=<name>.algorand-<network>.(network|net);<network>.cloudfront.com"},
			args:   args{networkID: "devnet"},
			wantBootstrapArray: []*DNSBootstrap{{PrimarySRVBootstrap: "devnet.algorand.network",
				BackupSRVBootstrap: "devnet.algorand.net",
				DedupExp:           regexp.MustCompile("(algorand-devnet.(network|net))")},
				{PrimarySRVBootstrap: "devnet.cloudfront.com"}},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cfg := Local{
				DNSBootstrapID: tt.fields.DNSBootstrapID,
			}
			if gotBootstrapArray := cfg.DNSBootstrapArray(tt.args.networkID); !reflect.DeepEqual(gotBootstrapArray, tt.wantBootstrapArray) {
				t.Errorf("Local.DNSBootstrapArray() = %#v, want %#v", gotBootstrapArray, tt.wantBootstrapArray)
			}
			// handling should be identical to DNSBootstrapArray method for all of these cases
			if gotBootstrapArray, _ := cfg.ValidateDNSBootstrapArray(tt.args.networkID); !reflect.DeepEqual(gotBootstrapArray, tt.wantBootstrapArray) {
				t.Errorf("Local.DNSBootstrapArray() = %#v, want %#v", gotBootstrapArray, tt.wantBootstrapArray)
			}
		})
	}
}

func TestLocal_ValidateDNSBootstrapArray_StopsOnError(t *testing.T) {
	partitiontest.PartitionTest(t)

	var dnsBootstrapIDWithInvalidNameMacroUsage = "<network>.algorand.network?backup=<network>.algorand.net&dedup=<name>.algorand-<network>.((network|net)"

	cfg := Local{
		DNSBootstrapID: dnsBootstrapIDWithInvalidNameMacroUsage,
	}

	_, err := cfg.ValidateDNSBootstrapArray(Mainnet)

	assert.ErrorContains(t, err, bootstrapDedupRegexDoesNotCompile)
}

func TestLocal_StructTags(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	localType := reflect.TypeOf(Local{})

	versionField, ok := localType.FieldByName("Version")
	require.True(t, ok)
	ver := 0
	versionTags := []string{}
	for {
		_, has := versionField.Tag.Lookup(fmt.Sprintf("version[%d]", ver))
		if !has {
			break
		}
		versionTags = append(versionTags, fmt.Sprintf("version[%d]", ver))
		ver++
	}

	for fieldNum := 0; fieldNum < localType.NumField(); fieldNum++ {
		field := localType.Field(fieldNum)
		if field.Tag == "" {
			require.Failf(t, "Field is missing versioning information", "Field Name: %s", field.Name)
		}
		// the field named "Version" is tested separately in TestLocalVersionField, so we'll be skipping
		// it on this test.
		if field.Name == "Version" {
			continue
		}
		// check to see if we have at least a single version from the versions tags above.
		foundTag := false
		expectedTag := ""
		for _, ver := range versionTags {
			if val, found := field.Tag.Lookup(ver); found {
				foundTag = true
				expectedTag = expectedTag + ver + ":\"" + val + "\" "
			}
		}
		require.True(t, foundTag)
		expectedTag = expectedTag[:len(expectedTag)-1]
		require.Equal(t, expectedTag, string(field.Tag))
	}
}

func TestLocal_GetVersionedDefaultLocalConfig(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	for i := uint32(0); i < getLatestConfigVersion(); i++ {
		localVersion := GetVersionedDefaultLocalConfig(i)
		require.Equal(t, uint32(i), localVersion.Version)
	}
}

// TestLocalVersionField - ensures the Version contains only versions tags, the versions are all contiguous, and that no non-version tags are included there.
func TestLocal_VersionField(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	localType := reflect.TypeOf(Local{})
	field, ok := localType.FieldByName("Version")
	require.True(t, true, ok)
	ver := 0
	expectedTag := ""
	for {
		val, has := field.Tag.Lookup(fmt.Sprintf("version[%d]", ver))
		if !has {
			break
		}
		expectedTag = fmt.Sprintf("%sversion[%d]:\"%s\" ", expectedTag, ver, val)
		ver++
	}
	expectedTag = expectedTag[:len(expectedTag)-1]
	require.Equal(t, expectedTag, string(field.Tag))
}

func TestLocal_GetNonDefaultConfigValues(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	cfg := GetDefaultLocal()

	// set 4 non-default values
	cfg.AgreementIncomingBundlesQueueLength = 2
	cfg.AgreementIncomingProposalsQueueLength = 200
	cfg.TxPoolSize = 30
	cfg.Archival = true

	// ask for 2 of them
	ndmap := GetNonDefaultConfigValues(cfg, []string{"AgreementIncomingBundlesQueueLength", "TxPoolSize"})

	// assert correct
	expected := map[string]interface{}{
		"AgreementIncomingBundlesQueueLength": uint64(2),
		"TxPoolSize":                          int(30),
	}
	assert.Equal(t, expected, ndmap)

	// ask for field that doesn't exist: should skip
	assert.Equal(t, expected, GetNonDefaultConfigValues(cfg, []string{"Blah", "AgreementIncomingBundlesQueueLength", "TxPoolSize"}))

	// check unmodified defaults
	assert.Empty(t, GetNonDefaultConfigValues(GetDefaultLocal(), []string{"AgreementIncomingBundlesQueueLength", "TxPoolSize"}))
}

func TestLocal_TxFiltering(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	cfg := GetDefaultLocal()

	// ensure the default
	require.True(t, cfg.TxFilterRawMsgEnabled())
	require.False(t, cfg.TxFilterCanonicalEnabled())

	cfg.TxIncomingFilteringFlags = 0
	require.False(t, cfg.TxFilterRawMsgEnabled())
	require.False(t, cfg.TxFilterCanonicalEnabled())

	cfg.TxIncomingFilteringFlags = 1
	require.True(t, cfg.TxFilterRawMsgEnabled())
	require.False(t, cfg.TxFilterCanonicalEnabled())

	cfg.TxIncomingFilteringFlags = 2
	require.False(t, cfg.TxFilterRawMsgEnabled())
	require.True(t, cfg.TxFilterCanonicalEnabled())

	cfg.TxIncomingFilteringFlags = 3
	require.True(t, cfg.TxFilterRawMsgEnabled())
	require.True(t, cfg.TxFilterCanonicalEnabled())
}

func TestLocal_IsGossipServer(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	cfg := GetDefaultLocal()
	require.False(t, cfg.IsGossipServer())

	cfg.NetAddress = ":4160"
	require.True(t, cfg.IsGossipServer())
}

func TestLocal_RecalculateConnectionLimits(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	var tests = []struct {
		maxFDs     uint64
		reservedIn uint64
		restSoftIn uint64
		restHardIn uint64
		incomingIn int

		updated     bool
		restSoftExp uint64
		restHardExp uint64
		incomingExp int
	}{
		{100, 10, 20, 40, 50, false, 20, 40, 50},               // no change
		{100, 10, 20, 50, 50, true, 20, 40, 50},                // borrow from rest
		{100, 10, 25, 50, 50, true, 25, 40, 50},                // borrow from rest
		{100, 10, 50, 50, 50, true, 40, 40, 50},                // borrow from rest, update soft
		{100, 10, 9, 19, 81, true, 9, 10, 80},                  // borrow from both rest and incoming
		{100, 10, 10, 20, 80, true, 10, 10, 80},                // borrow from both rest and incoming
		{100, 50, 10, 30, 40, true, 10, 10, 40},                // borrow from both rest and incoming
		{100, 90, 10, 30, 40, true, 10, 10, 0},                 // borrow from both rest and incoming, clear incoming
		{4096, 256, 1024, 2048, 2400, true, 1024, 1440, 2400},  // real numbers
		{5000, 256, 1024, 2048, 2400, false, 1024, 2048, 2400}, // real numbers
	}

	for i, test := range tests {
		test := test
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			t.Parallel()

			c := Local{
				RestConnectionsSoftLimit: test.restSoftIn,
				RestConnectionsHardLimit: test.restHardIn,
				IncomingConnectionsLimit: test.incomingIn,
			}
			requireFDs := test.reservedIn + test.restHardIn + uint64(test.incomingIn)
			res := c.AdjustConnectionLimits(requireFDs, test.maxFDs)
			require.Equal(t, test.updated, res)
			require.Equal(t, test.restSoftExp, c.RestConnectionsSoftLimit)
			require.Equal(t, test.restHardExp, c.RestConnectionsHardLimit)
			require.Equal(t, test.incomingExp, c.IncomingConnectionsLimit)
		})
	}
}

// Tests that ensureAbsGenesisDir resolves a path to an absolute path, appends the genesis directory, and creates any needed directories
func TestEnsureAbsDir(t *testing.T) {
	partitiontest.PartitionTest(t)

	testDirectory := t.TempDir()

	t1 := filepath.Join(testDirectory, "test1")
	t1Abs, err := ensureAbsGenesisDir(t1, "myGenesisID")
	require.NoError(t, err)
	require.DirExists(t, t1Abs)
	require.Equal(t, testDirectory+"/test1/myGenesisID", t1Abs)

	// confirm that relative paths become absolute
	t2 := filepath.Join(testDirectory, "test2", "..")
	t2Abs, err := ensureAbsGenesisDir(t2, "myGenesisID")
	require.NoError(t, err)
	require.DirExists(t, t2Abs)
	require.Equal(t, testDirectory+"/myGenesisID", t2Abs)
}

type tLogger struct{ t *testing.T }

func (l tLogger) Infof(fmts string, args ...interface{}) {
	l.t.Logf(fmts, args...)
}

// TestEnsureAndResolveGenesisDirs confirms that paths provided in the config are resolved to absolute paths and are created if relevant
func TestEnsureAndResolveGenesisDirs(t *testing.T) {
	partitiontest.PartitionTest(t)

	cfg := GetDefaultLocal()

	testDirectory := t.TempDir()
	// insert some "Bad" path elements to see them removed when converted to absolute
	cfg.TrackerDBDir = filepath.Join(testDirectory, "BAD/../custom_tracker")
	cfg.BlockDBDir = filepath.Join(testDirectory, "/BAD/BAD/../../custom_block")
	cfg.CrashDBDir = filepath.Join(testDirectory, "custom_crash")
	cfg.StateproofDir = filepath.Join(testDirectory, "/RELATIVEPATHS/../RELATIVE/../custom_stateproof")
	cfg.CatchpointDir = filepath.Join(testDirectory, "custom_catchpoint")

	paths, err := cfg.EnsureAndResolveGenesisDirs(testDirectory, "myGenesisID", tLogger{t: t})
	require.NoError(t, err)

	// confirm that the paths are absolute, and contain the genesisID
	require.Equal(t, testDirectory+"/custom_tracker/myGenesisID", paths.TrackerGenesisDir)
	require.DirExists(t, paths.TrackerGenesisDir)
	require.Equal(t, testDirectory+"/custom_block/myGenesisID", paths.BlockGenesisDir)
	require.DirExists(t, paths.BlockGenesisDir)
	require.Equal(t, testDirectory+"/custom_crash/myGenesisID", paths.CrashGenesisDir)
	require.DirExists(t, paths.CrashGenesisDir)
	require.Equal(t, testDirectory+"/custom_stateproof/myGenesisID", paths.StateproofGenesisDir)
	require.DirExists(t, paths.StateproofGenesisDir)
	require.Equal(t, testDirectory+"/custom_catchpoint/myGenesisID", paths.CatchpointGenesisDir)
	require.DirExists(t, paths.CatchpointGenesisDir)
}

// TestEnsureAndResolveGenesisDirs_hierarchy confirms that when only some directories are specified, other directories defer to them
func TestEnsureAndResolveGenesisDirs_hierarchy(t *testing.T) {
	partitiontest.PartitionTest(t)

	cfg := GetDefaultLocal()
	testDirectory := t.TempDir()
	paths, err := cfg.EnsureAndResolveGenesisDirs(testDirectory, "myGenesisID", tLogger{t: t})
	require.NoError(t, err)
	// confirm that if only the root is specified, it is used for all directories
	require.Equal(t, testDirectory+"/myGenesisID", paths.TrackerGenesisDir)
	require.DirExists(t, paths.TrackerGenesisDir)
	require.Equal(t, testDirectory+"/myGenesisID", paths.BlockGenesisDir)
	require.DirExists(t, paths.BlockGenesisDir)
	require.Equal(t, testDirectory+"/myGenesisID", paths.CrashGenesisDir)
	require.DirExists(t, paths.CrashGenesisDir)
	require.Equal(t, testDirectory+"/myGenesisID", paths.StateproofGenesisDir)
	require.DirExists(t, paths.StateproofGenesisDir)
	require.Equal(t, testDirectory+"/myGenesisID", paths.CatchpointGenesisDir)
	require.DirExists(t, paths.CatchpointGenesisDir)

	cfg = GetDefaultLocal()
	testDirectory = t.TempDir()
	hot := filepath.Join(testDirectory, "hot")
	cold := filepath.Join(testDirectory, "cold")
	cfg.HotDataDir = hot
	cfg.ColdDataDir = cold
	paths, err = cfg.EnsureAndResolveGenesisDirs(testDirectory, "myGenesisID", tLogger{t: t})
	require.NoError(t, err)
	// confirm that if hot/cold are specified, hot/cold are used for appropriate directories
	require.Equal(t, hot+"/myGenesisID", paths.TrackerGenesisDir)
	require.DirExists(t, paths.TrackerGenesisDir)
	require.Equal(t, cold+"/myGenesisID", paths.BlockGenesisDir)
	require.DirExists(t, paths.BlockGenesisDir)
	require.Equal(t, hot+"/myGenesisID", paths.CrashGenesisDir)
	require.DirExists(t, paths.CrashGenesisDir)
	require.Equal(t, hot+"/myGenesisID", paths.StateproofGenesisDir)
	require.DirExists(t, paths.StateproofGenesisDir)
	require.Equal(t, cold+"/myGenesisID", paths.CatchpointGenesisDir)
	require.DirExists(t, paths.CatchpointGenesisDir)
}

func TestEnsureAndResolveGenesisDirs_migrate(t *testing.T) {
	partitiontest.PartitionTest(t)

	cfg := GetDefaultLocal()
	testDirectory := t.TempDir()
	cfg.HotDataDir = filepath.Join(testDirectory, "hot")
	cfg.ColdDataDir = filepath.Join(testDirectory, "cold")
	coldDir := filepath.Join(cfg.ColdDataDir, "myGenesisID")
	hotDir := filepath.Join(cfg.HotDataDir, "myGenesisID")
	err := os.MkdirAll(coldDir, 0755)
	require.NoError(t, err)
	// put a crash.sqlite file in the ColdDataDir
	err = os.WriteFile(filepath.Join(coldDir, "crash.sqlite"), []byte("test"), 0644)
	require.NoError(t, err)
	err = os.WriteFile(filepath.Join(coldDir, "crash.sqlite-shm"), []byte("test"), 0644)
	require.NoError(t, err)
	// put a stateproof.sqlite file in the ColdDataDir
	err = os.WriteFile(filepath.Join(coldDir, "stateproof.sqlite"), []byte("test"), 0644)
	require.NoError(t, err)
	err = os.WriteFile(filepath.Join(coldDir, "stateproof.sqlite-wal"), []byte("test"), 0644)
	require.NoError(t, err)
	// Resolve
	paths, err := cfg.EnsureAndResolveGenesisDirs(testDirectory, "myGenesisID", tLogger{t: t})
	require.NoError(t, err)
	// Confirm that crash.sqlite was moved to HotDataDir
	require.DirExists(t, paths.CrashGenesisDir)
	require.Equal(t, hotDir, paths.CrashGenesisDir)
	require.NoFileExists(t, filepath.Join(coldDir, "crash.sqlite"))
	require.NoFileExists(t, filepath.Join(coldDir, "crash.sqlite-shm"))
	require.FileExists(t, filepath.Join(hotDir, "crash.sqlite"))
	require.FileExists(t, filepath.Join(hotDir, "crash.sqlite-shm"))
	// Confirm that stateproof.sqlite was moved to HotDataDir
	require.DirExists(t, paths.StateproofGenesisDir)
	require.Equal(t, hotDir, paths.StateproofGenesisDir)
	require.NoFileExists(t, filepath.Join(coldDir, "stateproof.sqlite"))
	require.NoFileExists(t, filepath.Join(coldDir, "stateproof.sqlite-wal"))
	require.FileExists(t, filepath.Join(hotDir, "stateproof.sqlite"))
	require.FileExists(t, filepath.Join(hotDir, "stateproof.sqlite-wal"))
}

func TestEnsureAndResolveGenesisDirs_migrateCrashFail(t *testing.T) {
	partitiontest.PartitionTest(t)

	cfg := GetDefaultLocal()
	testDirectory := t.TempDir()
	cfg.HotDataDir = filepath.Join(testDirectory, "hot")
	cfg.ColdDataDir = filepath.Join(testDirectory, "cold")
	coldDir := filepath.Join(cfg.ColdDataDir, "myGenesisID")
	hotDir := filepath.Join(cfg.HotDataDir, "myGenesisID")
	err := os.MkdirAll(coldDir, 0755)
	require.NoError(t, err)
	err = os.MkdirAll(hotDir, 0755)
	require.NoError(t, err)
	// put a crash.sqlite file in the ColdDataDir
	err = os.WriteFile(filepath.Join(coldDir, "crash.sqlite"), []byte("test"), 0644)
	require.NoError(t, err)
	err = os.WriteFile(filepath.Join(coldDir, "crash.sqlite-shm"), []byte("test"), 0644)
	require.NoError(t, err)
	// also put a crash.sqlite file in the HotDataDir
	err = os.WriteFile(filepath.Join(hotDir, "crash.sqlite"), []byte("test"), 0644)
	require.NoError(t, err)
	// Resolve
	paths, err := cfg.EnsureAndResolveGenesisDirs(testDirectory, "myGenesisID", tLogger{t: t})
	require.Error(t, err)
	require.Empty(t, paths)
	// Confirm that crash.sqlite was not moved to HotDataDir
	require.FileExists(t, filepath.Join(coldDir, "crash.sqlite"))
	require.FileExists(t, filepath.Join(coldDir, "crash.sqlite-shm"))
	require.FileExists(t, filepath.Join(hotDir, "crash.sqlite"))
	require.NoFileExists(t, filepath.Join(hotDir, "crash.sqlite-shm"))
}

func TestEnsureAndResolveGenesisDirs_migrateSPFail(t *testing.T) {
	partitiontest.PartitionTest(t)

	cfg := GetDefaultLocal()
	testDirectory := t.TempDir()
	cfg.HotDataDir = filepath.Join(testDirectory, "hot")
	cfg.ColdDataDir = filepath.Join(testDirectory, "cold")
	coldDir := filepath.Join(cfg.ColdDataDir, "myGenesisID")
	hotDir := filepath.Join(cfg.HotDataDir, "myGenesisID")
	err := os.MkdirAll(coldDir, 0755)
	require.NoError(t, err)
	err = os.MkdirAll(hotDir, 0755)
	require.NoError(t, err)
	// put a stateproof.sqlite file in the ColdDataDir
	err = os.WriteFile(filepath.Join(coldDir, "stateproof.sqlite"), []byte("test"), 0644)
	require.NoError(t, err)
	err = os.WriteFile(filepath.Join(coldDir, "stateproof.sqlite-wal"), []byte("test"), 0644)
	require.NoError(t, err)
	// also put a stateproof.sqlite-wal file in the HotDataDir
	err = os.WriteFile(filepath.Join(hotDir, "stateproof.sqlite-wal"), []byte("test"), 0644)
	require.NoError(t, err)
	// Resolve
	paths, err := cfg.EnsureAndResolveGenesisDirs(testDirectory, "myGenesisID", tLogger{t: t})
	require.Error(t, err)
	require.Empty(t, paths)
	// Confirm that stateproof.sqlite was not moved to HotDataDir
	require.FileExists(t, filepath.Join(coldDir, "stateproof.sqlite"))
	require.FileExists(t, filepath.Join(coldDir, "stateproof.sqlite-wal"))
	require.NoFileExists(t, filepath.Join(hotDir, "stateproof.sqlite"))
	require.FileExists(t, filepath.Join(hotDir, "stateproof.sqlite-wal"))
}

// TestEnsureAndResolveGenesisDirsError confirms that if a path can't be created, an error is returned
func TestEnsureAndResolveGenesisDirsError(t *testing.T) {
	partitiontest.PartitionTest(t)

	cfg := GetDefaultLocal()

	testDirectory := t.TempDir()
	// insert some "Bad" path elements to see them removed when converted to absolute
	cfg.TrackerDBDir = filepath.Join(testDirectory, "BAD/../custom_tracker")
	cfg.BlockDBDir = filepath.Join(testDirectory, "/BAD/BAD/../../custom_block")
	cfg.CrashDBDir = filepath.Join(testDirectory, "custom_crash")
	cfg.StateproofDir = filepath.Join(testDirectory, "/RELATIVEPATHS/../RELATIVE/../custom_stateproof")
	cfg.CatchpointDir = filepath.Join(testDirectory, "custom_catchpoint")

	// first try an error with an empty root dir
	paths, err := cfg.EnsureAndResolveGenesisDirs("", "myGenesisID", tLogger{t: t})
	require.Empty(t, paths)
	require.Error(t, err)
	require.Contains(t, err.Error(), "rootDir is required")

	require.NoError(t, os.Chmod(testDirectory, 0200))

	// now try an error with a root dir that can't be written to
	paths, err = cfg.EnsureAndResolveGenesisDirs(testDirectory, "myGenesisID", tLogger{t: t})
	require.Empty(t, paths)
	require.Error(t, err)
	require.Contains(t, err.Error(), "permission denied")
}

// TestResolveLogPaths confirms that log paths are resolved to the most appropriate data directory of the supplied config
func TestResolveLogPaths(t *testing.T) {
	partitiontest.PartitionTest(t)

	// on default settings, the log paths should be in the root directory
	cfg := GetDefaultLocal()
	log, archive := cfg.ResolveLogPaths("root")
	require.Equal(t, "root/node.log", log)
	require.Equal(t, "root/node.archive.log", archive)

	// with supplied hot/cold data directories, they resolve to hot/cold
	cfg = GetDefaultLocal()
	cfg.HotDataDir = "hot"
	cfg.ColdDataDir = "cold"
	log, archive = cfg.ResolveLogPaths("root")
	require.Equal(t, "hot/node.log", log)
	require.Equal(t, "cold/node.archive.log", archive)

	// with supplied hot/cold data AND specific paths directories, they resolve to the specific paths
	cfg = GetDefaultLocal()
	cfg.HotDataDir = "hot"
	cfg.ColdDataDir = "cold"
	cfg.LogFileDir = "mycoolLogDir"
	cfg.LogArchiveDir = "myCoolLogArchive"
	log, archive = cfg.ResolveLogPaths("root")
	require.Equal(t, "mycoolLogDir/node.log", log)
	require.Equal(t, "myCoolLogArchive/node.archive.log", archive)
}

func TestStoresCatchpoints(t *testing.T) {
	partitiontest.PartitionTest(t)

	var tests = []struct {
		name               string
		catchpointTracking int64
		catchpointInterval uint64
		archival           bool
		expected           bool
	}{
		{
			name:               "-1 w/ no catchpoint interval expects false",
			catchpointTracking: CatchpointTrackingModeUntracked,
			catchpointInterval: 0,
			expected:           false,
		},
		{
			name:               "-1 expects false",
			catchpointTracking: CatchpointTrackingModeUntracked,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           GetDefaultLocal().Archival,
			expected:           false,
		},
		{
			name:               "0 expects false",
			catchpointTracking: CatchpointTrackingModeAutomatic,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           GetDefaultLocal().Archival,
			expected:           false,
		},
		{
			name:               "0 w/ archival expects true",
			catchpointTracking: CatchpointTrackingModeAutomatic,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           true,
			expected:           true,
		},
		{
			name:               "0 w/ archival & catchpointInterval=0 expects false",
			catchpointTracking: CatchpointTrackingModeAutomatic,
			catchpointInterval: 0,
			archival:           true,
			expected:           false,
		},
		{
			name:               "1 expects false",
			catchpointTracking: CatchpointTrackingModeTracked,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           GetDefaultLocal().Archival,
			expected:           false,
		},
		{
			name:               "1 w/ archival expects true",
			catchpointTracking: CatchpointTrackingModeTracked,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           true,
			expected:           true,
		},
		{
			name:               "1 w/ archival & catchpointInterval=0 expects false",
			catchpointTracking: CatchpointTrackingModeTracked,
			catchpointInterval: 0,
			archival:           true,
			expected:           false,
		},
		{
			name:               "2 w/ catchpointInterval=0 expects false",
			catchpointTracking: CatchpointTrackingModeStored,
			catchpointInterval: 0,
			archival:           GetDefaultLocal().Archival,
			expected:           false,
		},
		{
			name:               "2 expects true",
			catchpointTracking: CatchpointTrackingModeStored,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           GetDefaultLocal().Archival,
			expected:           true,
		},
		{
			name:               "99 expects false",
			catchpointTracking: 99,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           GetDefaultLocal().Archival,
			expected:           false,
		},
		{
			name:               "99 w/ catchpointInterval=0 expects false",
			catchpointTracking: 99,
			catchpointInterval: 0,
			archival:           GetDefaultLocal().Archival,
			expected:           false,
		},
		{
			name:               "27 expects false",
			catchpointTracking: 27,
			catchpointInterval: GetDefaultLocal().CatchpointInterval,
			archival:           GetDefaultLocal().Archival,
			expected:           false,
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			cfg := GetDefaultLocal()
			cfg.CatchpointTracking = test.catchpointTracking
			cfg.CatchpointInterval = test.catchpointInterval
			cfg.Archival = test.archival
			require.Equal(t, test.expected, cfg.StoresCatchpoints())
			if cfg.StoresCatchpoints() {
				require.Equal(t, true, cfg.TracksCatchpoints())
			}
		})
	}
}

func TestTracksCatchpointsWithoutStoring(t *testing.T) {
	partitiontest.PartitionTest(t)

	cfg := GetDefaultLocal()
	cfg.CatchpointTracking = CatchpointTrackingModeTracked
	cfg.CatchpointInterval = 10000
	cfg.Archival = false
	require.Equal(t, true, cfg.TracksCatchpoints())
	require.Equal(t, false, cfg.StoresCatchpoints())
}