-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwallet_test.go
More file actions
1067 lines (860 loc) · 29.3 KB
/
Copy pathwallet_test.go
File metadata and controls
1067 lines (860 loc) · 29.3 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
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
// Copyright 2025 HOLOGRAM Project. All rights reserved.
// Unit tests for wallet.go
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"unicode/utf8"
"github.com/deroproject/derohe/globals"
)
// ============== WalletManager Tests ==============
func TestNewWalletManager(t *testing.T) {
wm := NewWalletManager()
if wm == nil {
t.Fatal("NewWalletManager should not return nil")
}
if wm.recentWallets == nil {
t.Error("recentWallets should be initialized")
}
if wm.isOpen {
t.Error("isOpen should be false initially")
}
if wm.wallet != nil {
t.Error("wallet should be nil initially")
}
}
func TestWalletManager_InitialState(t *testing.T) {
// Create a fresh wallet manager for testing
wm := &WalletManager{
recentWallets: make([]string, 0),
}
if wm.isOpen {
t.Error("New WalletManager should not have isOpen=true")
}
if wm.walletPath != "" {
t.Error("walletPath should be empty initially")
}
if len(wm.recentWallets) != 0 {
t.Error("recentWallets should be empty initially")
}
}
// ============== No-Wallet Error Path Tests ==============
// TestApp is a minimal test struct to test wallet methods
type testWalletApp struct {
settings map[string]interface{}
consoleLogs []ConsoleLog
}
func (a *testWalletApp) logToConsole(msg string) {
a.consoleLogs = append(a.consoleLogs, ConsoleLog{Message: msg})
}
func TestCloseWallet_NotOpen(t *testing.T) {
// Reset global wallet manager state
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{
settings: make(map[string]interface{}),
consoleLogs: make([]ConsoleLog, 0),
}
result := app.CloseWallet()
if result["success"] != false {
t.Error("CloseWallet should return success=false when no wallet is open")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg == "" {
t.Error("CloseWallet should return an error message")
}
}
func TestGetWalletStatus_NoWallet(t *testing.T) {
// Reset global wallet manager state
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
result := app.GetWalletStatus()
if result["success"] != true {
t.Error("GetWalletStatus should return success=true even when no wallet")
}
if result["isOpen"] != false {
t.Error("isOpen should be false when no wallet is open")
}
}
func TestGetBalance_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
result := app.GetBalance()
if result["success"] != false {
t.Error("GetBalance should return success=false when no wallet is open")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg == "" {
t.Error("GetBalance should return an error message")
}
}
func TestGetAddress_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
result := app.GetAddress()
if result["success"] != false {
t.Error("GetAddress should return success=false when no wallet is open")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg == "" {
t.Error("GetAddress should return an error message")
}
}
func TestGetIntegratedAddress_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
result := app.GetIntegratedAddress(0, "", 0)
if result["success"] != false {
t.Error("GetIntegratedAddress should return success=false when no wallet is open")
}
}
func TestTransfer_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{consoleLogs: make([]ConsoleLog, 0)}
result := app.Transfer("dero1dest...", 1000000, "", 16)
if result["success"] != false {
t.Error("Transfer should return success=false when no wallet is open")
}
}
func TestGetTransactionHistory_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
result := app.GetTransactionHistory(50)
if result["success"] != false {
t.Error("GetTransactionHistory should return success=false when no wallet is open")
}
}
func TestGetTransactionHistory_DefaultLimit(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
// Test with 0 limit - should default to 50
result := app.GetTransactionHistory(0)
// Will fail because no wallet, but we're testing the limit isn't causing issues
if result["success"] != false {
t.Error("GetTransactionHistory should return success=false")
}
// Test with negative limit
result = app.GetTransactionHistory(-10)
if result["success"] != false {
t.Error("GetTransactionHistory should return success=false")
}
}
func TestGetWalletMiningEarnings_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
result := app.GetWalletMiningEarnings(100)
if result["success"] != false {
t.Error("GetWalletMiningEarnings should return success=false when no wallet")
}
// Should still return empty earnings array
earnings, ok := result["earnings"].([]map[string]interface{})
if !ok {
t.Error("earnings should be present even on error")
}
if len(earnings) != 0 {
t.Error("earnings should be empty when no wallet")
}
}
func TestGetMiningEarningsSummary_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
result := app.GetMiningEarningsSummary()
if result["success"] != false {
t.Error("GetMiningEarningsSummary should return success=false when no wallet")
}
// Should have zero total_amount
if result["total_amount"] != uint64(0) {
t.Error("total_amount should be 0 when no wallet")
}
}
func TestIsWalletOpen_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{}
if app.IsWalletOpen() {
t.Error("IsWalletOpen should return false when no wallet is open")
}
}
func TestGetWallet_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
wallet := GetWallet()
if wallet != nil {
t.Error("GetWallet should return nil when no wallet is open")
}
}
func TestGetCurrentWalletPath_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.walletPath = ""
walletManager.Unlock()
app := &App{}
path := app.GetCurrentWalletPath()
if path != "" {
t.Errorf("GetCurrentWalletPath should return empty string, got %s", path)
}
}
// ============== File Validation Tests ==============
func TestOpenWallet_FileNotFound(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{
settings: make(map[string]interface{}),
consoleLogs: make([]ConsoleLog, 0),
}
result := app.OpenWallet("/nonexistent/path/wallet.db", "password")
if result["success"] != false {
t.Error("OpenWallet should fail for nonexistent file")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg != "Wallet file not found" {
t.Errorf("Expected 'Wallet file not found' error, got: %v", result["error"])
}
}
func TestCreateWallet_FileExists(t *testing.T) {
// Create a temp file
tempDir, err := os.MkdirTemp("", "hologram_wallet_test_*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
existingFile := filepath.Join(tempDir, "existing.db")
os.WriteFile(existingFile, []byte("test"), 0600)
app := &App{consoleLogs: make([]ConsoleLog, 0)}
result := app.CreateWallet(existingFile, "password")
if result["success"] != false {
t.Error("CreateWallet should fail when file already exists")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg != "A wallet with this name already exists" {
t.Errorf("Expected 'A wallet with this name already exists' error, got: %v", result["error"])
}
}
func TestRestoreWallet_FileExists(t *testing.T) {
// Create a temp file
tempDir, err := os.MkdirTemp("", "hologram_wallet_test_*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
existingFile := filepath.Join(tempDir, "existing.db")
os.WriteFile(existingFile, []byte("test"), 0600)
app := &App{consoleLogs: make([]ConsoleLog, 0)}
result := app.RestoreWallet(existingFile, "password", "test seed phrase words")
if result["success"] != false {
t.Error("RestoreWallet should fail when file already exists")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg != "A wallet with this name already exists" {
t.Errorf("Expected 'A wallet with this name already exists' error, got: %v", result["error"])
}
}
func TestSwitchWallet_FileNotFound(t *testing.T) {
app := &App{
settings: make(map[string]interface{}),
consoleLogs: make([]ConsoleLog, 0),
}
result := app.SwitchWallet("/nonexistent/wallet.db", "password")
if result["success"] != false {
t.Error("SwitchWallet should fail for nonexistent file")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg != "Wallet file not found" {
t.Errorf("Expected 'Wallet file not found' error, got: %v", result["error"])
}
}
// ============== InternalWalletCall Validation Tests ==============
func TestInternalWalletCall_NoWallet(t *testing.T) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.walletPath = ""
walletManager.Unlock()
app := &App{
settings: make(map[string]interface{}),
consoleLogs: make([]ConsoleLog, 0),
}
result := app.InternalWalletCall("transfer", map[string]interface{}{}, "")
if result["success"] != false {
t.Error("InternalWalletCall should fail when no wallet is open")
}
errMsg, ok := result["error"].(string)
if !ok || errMsg != "Wallet not open" {
t.Errorf("Expected 'Wallet not open' error, got: %v", result["error"])
}
}
func TestInternalWalletCall_Transfer_NoTransfers(t *testing.T) {
// Simulate wallet open state but we can't actually make calls
walletManager.Lock()
originalIsOpen := walletManager.isOpen
originalWallet := walletManager.wallet
walletManager.isOpen = true
// Note: wallet is nil so actual call will fail, but we test param validation
walletManager.Unlock()
defer func() {
walletManager.Lock()
walletManager.isOpen = originalIsOpen
walletManager.wallet = originalWallet
walletManager.Unlock()
}()
app := &App{
settings: make(map[string]interface{}),
consoleLogs: make([]ConsoleLog, 0),
}
// Empty transfers should fail
result := app.InternalWalletCall("transfer", map[string]interface{}{}, "password")
if result["success"] != false {
t.Error("InternalWalletCall transfer with no transfers should fail")
}
}
func TestInternalWalletCall_SCInvoke_MissingSCID(t *testing.T) {
// Note: Without a real wallet, we can't test parameter validation in isolation
// because the code checks wallet.isOpen && wallet != nil first.
// This test verifies the error path when wallet state is inconsistent.
walletManager.Lock()
originalIsOpen := walletManager.isOpen
originalWallet := walletManager.wallet
walletManager.isOpen = true
walletManager.wallet = nil // Wallet object is nil even though isOpen is true
walletManager.Unlock()
defer func() {
walletManager.Lock()
walletManager.isOpen = originalIsOpen
walletManager.wallet = originalWallet
walletManager.Unlock()
}()
app := &App{
settings: make(map[string]interface{}),
consoleLogs: make([]ConsoleLog, 0),
}
// With isOpen=true but wallet=nil, should fail
result := app.InternalWalletCall("scinvoke", map[string]interface{}{}, "password")
if result["success"] != false {
t.Error("InternalWalletCall should fail when wallet object is nil")
}
// Error should indicate wallet not usable
_, ok := result["error"].(string)
if !ok {
t.Error("Should return an error message")
}
}
func TestInternalWalletCall_UnsupportedMethod(t *testing.T) {
// Note: Without a real wallet, we can't test the unsupported method path
// because the code checks wallet state first.
// This test verifies behavior when wallet object is nil.
walletManager.Lock()
originalIsOpen := walletManager.isOpen
originalWallet := walletManager.wallet
walletManager.isOpen = true
walletManager.wallet = nil
walletManager.Unlock()
defer func() {
walletManager.Lock()
walletManager.isOpen = originalIsOpen
walletManager.wallet = originalWallet
walletManager.Unlock()
}()
app := &App{
settings: make(map[string]interface{}),
consoleLogs: make([]ConsoleLog, 0),
}
result := app.InternalWalletCall("unsupported_method", map[string]interface{}{}, "")
if result["success"] != false {
t.Error("InternalWalletCall should fail when wallet object is nil")
}
// Should return some error
_, ok := result["error"].(string)
if !ok {
t.Error("Should return an error message")
}
}
// ============== Recent Wallets Tests ==============
func TestAddToRecentWalletsWithInfo_NoDuplicates(t *testing.T) {
// Create temp directory for settings
tempDir, err := os.MkdirTemp("", "hologram_recent_test_*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Override the data directory for this test
originalOverride := testDataDirOverride
testDataDirOverride = tempDir
defer func() { testDataDirOverride = originalOverride }()
// Reset wallet manager
walletManager.Lock()
walletManager.recentWallets = []string{}
walletManager.Unlock()
// Add same path twice (addToRecentWalletsWithInfo requires path + address)
addToRecentWalletsWithInfo("/path/to/wallet.db", "dero1qwerty1234567890abcdef")
addToRecentWalletsWithInfo("/other/wallet.db", "dero1other1234567890abcdef")
addToRecentWalletsWithInfo("/path/to/wallet.db", "dero1qwerty1234567890abcdef") // Duplicate
walletManager.RLock()
count := len(walletManager.recentWallets)
walletManager.RUnlock()
if count != 2 {
t.Errorf("Expected 2 wallets (no duplicates), got %d", count)
}
// The duplicate should move to front
walletManager.RLock()
first := walletManager.recentWallets[0]
walletManager.RUnlock()
if first != "/path/to/wallet.db" {
t.Error("Most recently added should be first")
}
}
func TestAddToRecentWalletsWithInfo_Max10(t *testing.T) {
tempDir, err := os.MkdirTemp("", "hologram_recent_test_*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Override the data directory for this test
originalOverride := testDataDirOverride
testDataDirOverride = tempDir
defer func() { testDataDirOverride = originalOverride }()
// Reset
walletManager.Lock()
walletManager.recentWallets = []string{}
walletManager.Unlock()
// Add 12 wallets (exceeds the max of 10)
for i := 0; i < 12; i++ {
addToRecentWalletsWithInfo(
filepath.Join(tempDir, "wallet"+string(rune('A'+i))+".db"),
"dero1testaddr1234567890abc",
)
}
walletManager.RLock()
count := len(walletManager.recentWallets)
walletManager.RUnlock()
if count != 10 {
t.Errorf("Expected max 10 recent wallets, got %d", count)
}
}
func TestLoadSaveRecentWalletsData_Roundtrip(t *testing.T) {
tempDir, err := os.MkdirTemp("", "hologram_recent_test_*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Override the data directory for this test
originalOverride := testDataDirOverride
testDataDirOverride = tempDir
defer func() { testDataDirOverride = originalOverride }()
// Save some wallets using the new data format
testWallets := []recentWalletData{
{Path: "/path/a.db", AddressPrefix: "dero1aaa...", LastUsed: 1000, Network: "mainnet"},
{Path: "/path/b.db", AddressPrefix: "dero1bbb...", LastUsed: 2000, Network: "mainnet"},
}
saveRecentWalletsData(testWallets)
// Load them back
loaded := loadRecentWalletsData()
if len(loaded) != 2 {
t.Errorf("Expected 2 wallets, got %d", len(loaded))
}
if loaded[0].Path != "/path/a.db" || loaded[1].Path != "/path/b.db" {
t.Errorf("Loaded wallet paths don't match: %v", loaded)
}
if loaded[0].Network != "mainnet" || loaded[1].Network != "mainnet" {
t.Errorf("Loaded wallet networks don't match: %v", loaded)
}
}
func TestLoadRecentWallets_NoFile(t *testing.T) {
tempDir, err := os.MkdirTemp("", "hologram_recent_test_*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Override the data directory for this test
originalOverride := testDataDirOverride
testDataDirOverride = tempDir
defer func() { testDataDirOverride = originalOverride }()
// No settings file exists
loaded := loadRecentWallets()
if len(loaded) != 0 {
t.Errorf("Expected empty slice when no file, got %d items", len(loaded))
}
}
func TestLoadRecentWallets_InvalidJSON(t *testing.T) {
tempDir, err := os.MkdirTemp("", "hologram_recent_test_*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Override the data directory for this test
originalOverride := testDataDirOverride
testDataDirOverride = tempDir
defer func() { testDataDirOverride = originalOverride }()
// Create invalid JSON file in the test data directory
settingsDir := filepath.Join(tempDir, "datashards", "settings")
os.MkdirAll(settingsDir, 0700)
os.WriteFile(filepath.Join(settingsDir, "recent_wallets.json"), []byte("invalid json"), 0600)
loaded := loadRecentWallets()
if len(loaded) != 0 {
t.Errorf("Expected empty slice for invalid JSON, got %d items", len(loaded))
}
}
// ============== WalletInfo Tests ==============
func TestWalletInfo_Structure(t *testing.T) {
info := WalletInfo{
Path: "/path/to/wallet.db",
Filename: "wallet.db",
AddressPrefix: "dero1abc123...",
LastUsed: 1234567890,
IsCurrent: true,
}
if info.Path != "/path/to/wallet.db" {
t.Error("Path mismatch")
}
if info.Filename != "wallet.db" {
t.Error("Filename mismatch")
}
if info.AddressPrefix != "dero1abc123..." {
t.Error("AddressPrefix mismatch")
}
if info.LastUsed != 1234567890 {
t.Error("LastUsed mismatch")
}
if !info.IsCurrent {
t.Error("IsCurrent mismatch")
}
}
func TestWalletInfo_JSONMarshal(t *testing.T) {
info := WalletInfo{
Path: "/test.db",
Filename: "test.db",
}
data, err := json.Marshal(info)
if err != nil {
t.Fatalf("JSON marshal failed: %v", err)
}
var decoded WalletInfo
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("JSON unmarshal failed: %v", err)
}
if decoded.Path != info.Path {
t.Error("JSON roundtrip failed for Path")
}
}
// ============== Mining Reward Classification Tests ==============
func TestMiningRewardType_Classification(t *testing.T) {
// Block rewards are >= 2 DERO (200000 atomic units with 5 decimals)
// Miniblock rewards are < 2 DERO
tests := []struct {
amount uint64
expectedType string
}{
{200000, "block"}, // Exactly 2 DERO
{300000, "block"}, // 3 DERO
{1000000, "block"}, // 10 DERO
{199999, "miniblock"}, // Just under 2 DERO
{100000, "miniblock"}, // 1 DERO
{50000, "miniblock"}, // 0.5 DERO
{0, "miniblock"}, // Zero
}
for _, tt := range tests {
t.Run(string(rune(tt.amount)), func(t *testing.T) {
rewardType := "miniblock"
if tt.amount >= 200000 {
rewardType = "block"
}
if rewardType != tt.expectedType {
t.Errorf("Amount %d: got %s, expected %s", tt.amount, rewardType, tt.expectedType)
}
})
}
}
// ============== Concurrent Access Tests ==============
func TestWalletManager_ConcurrentStatusCheck(t *testing.T) {
var wg sync.WaitGroup
iterations := 100
for i := 0; i < iterations; i++ {
wg.Add(1)
go func() {
defer wg.Done()
walletManager.RLock()
_ = walletManager.isOpen
_ = walletManager.walletPath
walletManager.RUnlock()
}()
}
wg.Wait()
// No race conditions = success
}
func TestListRecentWallets_Concurrent(t *testing.T) {
var wg sync.WaitGroup
iterations := 50
app := &App{}
for i := 0; i < iterations; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_ = app.ListRecentWallets()
}()
}
wg.Wait()
// No race conditions = success
}
// ============== Benchmark Tests ==============
func BenchmarkGetWalletStatus(b *testing.B) {
walletManager.Lock()
walletManager.isOpen = false
walletManager.Unlock()
app := &App{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.GetWalletStatus()
}
}
func BenchmarkIsWalletOpen(b *testing.B) {
app := &App{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.IsWalletOpen()
}
}
func BenchmarkListRecentWallets(b *testing.B) {
walletManager.Lock()
walletManager.recentWallets = []string{"a.db", "b.db", "c.db", "d.db", "e.db"}
walletManager.Unlock()
app := &App{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.ListRecentWallets()
}
}
// ============== Tracked-token concurrency ==============
// TestAddTrackedToken_ConcurrentNoLostUpdates verifies that trackedTokensMu
// serializes the load-modify-save of tracked_tokens.json: concurrent adds of
// distinct SCIDs must all persist, with none clobbered by a racing whole-file
// rewrite. Run under -race, it also flags any data race on the shared file.
func TestAddTrackedToken_ConcurrentNoLostUpdates(t *testing.T) {
dir := t.TempDir()
prev := testDataDirOverride
testDataDirOverride = dir
defer func() { testDataDirOverride = prev }()
// No wallet open: AddTrackedToken still runs the persistence path and skips
// the wallet-registration block.
walletManager.Lock()
walletManager.isOpen = false
walletManager.wallet = nil
walletManager.Unlock()
app := &App{settings: make(map[string]interface{}), consoleLogs: make([]ConsoleLog, 0)}
const n = 50
scids := make([]string, n)
for i := range scids {
// distinct, valid 64-hex SCIDs
scids[i] = fmt.Sprintf("%064x", i+1)
}
var wg sync.WaitGroup
for _, s := range scids {
wg.Add(1)
go func(scid string) {
defer wg.Done()
app.AddTrackedToken(scid, "T", "T")
}(s)
}
wg.Wait()
got := loadTrackedTokens()
if len(got) != n {
t.Fatalf("expected %d tracked tokens after concurrent adds, got %d (lost-update race?)", n, len(got))
}
seen := make(map[string]bool, n)
for _, tok := range got {
seen[tok.SCID] = true
}
for _, s := range scids {
if !seen[s] {
t.Errorf("SCID %s missing — clobbered by a concurrent write", s)
}
}
}
// ============== Token Metadata Sanitization ==============
// TestSanitizeTokenText pins the on-chain-metadata defenses: an NFA's name/symbol/
// description headers are attacker-controlled, so a crafted token must not be able
// to smuggle bidi-override or zero-width characters into the rendered identity, nor
// bloat tracked_tokens.json with an unbounded value. A regression here re-opens the
// spoofing/DoS surface, so it should fail loud.
func TestSanitizeTokenText(t *testing.T) {
const (
rlo = "\u202e" // right-to-left override (bidi spoofing)
lro = "\u202d" // left-to-right override
zwj = "\u200d" // zero-width joiner
zws = "\u200b" // zero-width space
bom = "\ufeff" // byte-order mark / zero-width no-break space
wj = "\u2060" // word joiner
)
cases := []struct {
name string
in string
limit int
want string
}{
{"plain passes through", "Lotto", tokenNameLimit, "Lotto"},
{"empty stays empty", "", tokenNameLimit, ""},
{"bidi override stripped", "abc" + rlo + "def" + lro, tokenNameLimit, "abcdef"},
{"zero-width family stripped", "DE" + zws + "RO" + zwj + bom + wj, tokenNameLimit, "DERO"},
{"control chars stripped", "a\x00b\x07c\x1bd", tokenNameLimit, "abcd"},
{"newlines/tabs collapse to space", "line1\nline2\ttail", tokenNameLimit, "line1line2 tail"},
{"surrounding space trimmed", " spaced ", tokenNameLimit, "spaced"},
{"all-junk degrades to empty", rlo + zws + bom + "\x00", tokenNameLimit, ""},
{"length cap by rune count", strings.Repeat("x", tokenNameLimit+50), tokenNameLimit, strings.Repeat("x", tokenNameLimit)},
{"description gets wider cap", strings.Repeat("y", tokenDescLimit+1), tokenDescLimit, strings.Repeat("y", tokenDescLimit)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := sanitizeTokenText(tc.in, tc.limit)
if got != tc.want {
t.Errorf("sanitizeTokenText(%q, %d) = %q; want %q", tc.in, tc.limit, got, tc.want)
}
})
}
}
// TestSanitizeTokenText_MultibyteCapBoundary guards that the length cap never splits
// a multi-byte rune (a byte-slice cap would corrupt the trailing character).
func TestSanitizeTokenText_MultibyteCapBoundary(t *testing.T) {
in := strings.Repeat("é", tokenNameLimit+10) // 2 bytes per rune
got := sanitizeTokenText(in, tokenNameLimit)
if rc := utf8.RuneCountInString(got); rc != tokenNameLimit {
t.Errorf("expected %d runes after cap, got %d", tokenNameLimit, rc)
}
if !utf8.ValidString(got) {
t.Error("cap split a multi-byte rune — result is not valid UTF-8")
}
}
// TestRefreshTokenMetadata_NeverBlanksExisting pins the merge rule: an empty
// fresh value (Gnomon off, or the chain has no header) must never blank a
// stored name/symbol — even in overwriteNames mode, which only lets non-empty
// chain values replace existing ones. A regression here would let a metadata
// refresh silently wipe a user's token labels.
func TestRefreshTokenMetadata_NeverBlanksExisting(t *testing.T) {
dir := t.TempDir()
prev := testDataDirOverride
testDataDirOverride = dir
defer func() { testDataDirOverride = prev }()
app := &App{settings: make(map[string]interface{}), consoleLogs: make([]ConsoleLog, 0)}
scid := fmt.Sprintf("%064x", 0xbeef)
saveTrackedTokens([]TrackedToken{{SCID: scid, Name: "MyToken", Symbol: "MTK"}})
// gnomonClient is nil → fetchTokenHeader returns the lock inputs unchanged.
// overwriteNames=true passes empty locks, so fresh comes back all-empty —
// the exact case that must not blank stored values.
result := app.RefreshTokenMetadata(scid, true, true)
if result["success"] != true {
t.Fatalf("expected success, got %v", result["error"])
}
if result["changed"] != false {
t.Error("all-empty fresh values must report changed=false")
}
got := loadTrackedTokens()
if len(got) != 1 || got[0].Name != "MyToken" || got[0].Symbol != "MTK" {
t.Errorf("stored metadata was modified: %+v", got)
}
}
// TestRefreshTokenMetadata_PreviewWritesNothing pins apply=false as a pure
// read: even if values would change, nothing reaches disk.
func TestRefreshTokenMetadata_PreviewWritesNothing(t *testing.T) {
dir := t.TempDir()
prev := testDataDirOverride
testDataDirOverride = dir
defer func() { testDataDirOverride = prev }()
app := &App{settings: make(map[string]interface{}), consoleLogs: make([]ConsoleLog, 0)}
scid := fmt.Sprintf("%064x", 0xcafe)
saveTrackedTokens([]TrackedToken{{SCID: scid, Name: "", Symbol: ""}})