-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilestore_test.go
More file actions
872 lines (769 loc) · 24.4 KB
/
filestore_test.go
File metadata and controls
872 lines (769 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
package paywall
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/opd-ai/paywall/wallet"
)
// Helper function to create a temporary directory for testing
func createTempDir(t *testing.T) string {
t.Helper()
tempDir, err := os.MkdirTemp("", "filestore_test_")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
return tempDir
}
// Helper function to create a test payment
func createTestPayment(id string) *Payment {
return &Payment{
ID: id,
Addresses: map[wallet.WalletType]string{
wallet.Bitcoin: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
wallet.Monero: "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD",
},
Amounts: map[wallet.WalletType]float64{
wallet.Bitcoin: 0.0001,
wallet.Monero: 0.001,
},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(2 * time.Hour),
Status: StatusPending,
Confirmations: 0,
}
}
func TestNewFileStore(t *testing.T) {
tests := []struct {
name string
baseDir string
want string
}{
{
name: "custom base directory",
baseDir: "/tmp/test-payments",
want: "/tmp/test-payments",
},
{
name: "empty base directory uses default",
baseDir: "",
want: "./payments",
},
{
name: "relative path",
baseDir: "./custom-payments",
want: "./custom-payments",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clean up any existing directory first
if tt.baseDir != "" {
os.RemoveAll(tt.baseDir)
} else {
os.RemoveAll("./payments")
}
store := NewFileStore(tt.baseDir)
if store == nil {
t.Error("NewFileStore() returned nil")
return
}
if store.baseDir != tt.want {
t.Errorf("NewFileStore() baseDir = %v, want %v", store.baseDir, tt.want)
}
// Verify directory was created
if _, err := os.Stat(store.baseDir); os.IsNotExist(err) {
t.Errorf("NewFileStore() did not create directory %v", store.baseDir)
}
// Clean up
os.RemoveAll(store.baseDir)
})
}
}
func TestFileStore_CreatePayment(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
tests := []struct {
name string
payment *Payment
wantErr bool
}{
{
name: "valid payment",
payment: createTestPayment("test-payment-1"),
wantErr: false,
},
{
name: "payment with special characters in ID",
payment: createTestPayment("test-payment-special-#@$"),
wantErr: false,
},
{
name: "payment with empty addresses map",
payment: &Payment{
ID: "empty-addresses",
Addresses: map[wallet.WalletType]string{},
Amounts: map[wallet.WalletType]float64{},
CreatedAt: time.Now(),
Status: StatusPending,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := store.CreatePayment(tt.payment)
if (err != nil) != tt.wantErr {
t.Errorf("FileStore.CreatePayment() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
// Verify file was created
filename := filepath.Join(tempDir, tt.payment.ID+".json")
if _, err := os.Stat(filename); os.IsNotExist(err) {
t.Errorf("FileStore.CreatePayment() did not create file %v", filename)
}
// Verify file permissions
fileInfo, err := os.Stat(filename)
if err != nil {
t.Errorf("Failed to get file info: %v", err)
} else {
if fileInfo.Mode().Perm() != 0o600 {
t.Errorf("FileStore.CreatePayment() file permissions = %v, want %v", fileInfo.Mode().Perm(), 0o600)
}
}
}
})
}
}
func TestFileStore_CreatePayment_NilPayment(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
// This should cause a panic or error due to nil payment
defer func() {
if r := recover(); r == nil {
t.Error("FileStore.CreatePayment() with nil payment should panic")
}
}()
err := store.CreatePayment(nil)
if err == nil {
t.Error("FileStore.CreatePayment() with nil payment should return error")
}
}
func TestFileStore_GetPayment(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
testPayment := createTestPayment("test-payment-get")
// First create a payment
err := store.CreatePayment(testPayment)
if err != nil {
t.Fatalf("Failed to create test payment: %v", err)
}
tests := []struct {
name string
paymentID string
wantNil bool
wantErr bool
}{
{
name: "existing payment",
paymentID: "test-payment-get",
wantNil: false,
wantErr: false,
},
{
name: "non-existing payment",
paymentID: "non-existing-payment",
wantNil: true,
wantErr: false,
},
{
name: "empty payment ID",
paymentID: "",
wantNil: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
payment, err := store.GetPayment(tt.paymentID)
if (err != nil) != tt.wantErr {
t.Errorf("FileStore.GetPayment() error = %v, wantErr %v", err, tt.wantErr)
return
}
if (payment == nil) != tt.wantNil {
t.Errorf("FileStore.GetPayment() payment is nil = %v, wantNil %v", payment == nil, tt.wantNil)
return
}
if !tt.wantNil && payment != nil {
if payment.ID != tt.paymentID {
t.Errorf("FileStore.GetPayment() payment.ID = %v, want %v", payment.ID, tt.paymentID)
}
if payment.Addresses[wallet.Bitcoin] != testPayment.Addresses[wallet.Bitcoin] {
t.Errorf("FileStore.GetPayment() Bitcoin address = %v, want %v",
payment.Addresses[wallet.Bitcoin], testPayment.Addresses[wallet.Bitcoin])
}
}
})
}
}
func TestFileStore_UpdatePayment(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
testPayment := createTestPayment("test-payment-update")
// First create a payment
err := store.CreatePayment(testPayment)
if err != nil {
t.Fatalf("Failed to create test payment: %v", err)
}
tests := []struct {
name string
setup func() *Payment
wantErr bool
validate func(*testing.T, *Payment)
}{
{
name: "update existing payment status",
setup: func() *Payment {
updated := createTestPayment("test-payment-update")
updated.Status = StatusConfirmed
updated.Confirmations = 6
return updated
},
wantErr: false,
validate: func(t *testing.T, p *Payment) {
if p.Status != StatusConfirmed {
t.Errorf("Updated payment status = %v, want %v", p.Status, StatusConfirmed)
}
if p.Confirmations != 6 {
t.Errorf("Updated payment confirmations = %v, want %v", p.Confirmations, 6)
}
},
},
{
name: "update non-existing payment creates new file",
setup: func() *Payment {
return createTestPayment("new-payment-via-update")
},
wantErr: false,
validate: func(t *testing.T, p *Payment) {
if p.ID != "new-payment-via-update" {
t.Errorf("New payment ID = %v, want %v", p.ID, "new-payment-via-update")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
payment := tt.setup()
err := store.UpdatePayment(payment)
if (err != nil) != tt.wantErr {
t.Errorf("FileStore.UpdatePayment() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
// Verify the update by reading the payment back
retrieved, err := store.GetPayment(payment.ID)
if err != nil {
t.Errorf("Failed to retrieve updated payment: %v", err)
return
}
if retrieved == nil {
t.Error("Updated payment is nil")
return
}
tt.validate(t, retrieved)
}
})
}
}
func TestFileStore_ListPendingPayments(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
// Create test payments with different confirmation counts
payments := []*Payment{
{
ID: "payment-0-confirmations",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.0001},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(2 * time.Hour),
Status: StatusPending,
Confirmations: 0,
},
{
ID: "payment-1-confirmation",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.0002},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(2 * time.Hour),
Status: StatusPending,
Confirmations: 1,
},
{
ID: "payment-3-confirmations",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.0003},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(2 * time.Hour),
Status: StatusPending,
Confirmations: 3,
},
{
ID: "payment-6-confirmations",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.0004},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(2 * time.Hour),
Status: StatusConfirmed,
Confirmations: 6,
},
}
// Store all test payments
for _, payment := range payments {
err := store.CreatePayment(payment)
if err != nil {
t.Fatalf("Failed to create test payment %s: %v", payment.ID, err)
}
}
tests := []struct {
name string
wantErr bool
expectedCount int
expectedMaxConfirms int
}{
{
name: "list pending payments with less than 1 confirmation",
wantErr: false,
expectedCount: 1, // payments with 0 confirmations only
expectedMaxConfirms: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pendingPayments, err := store.ListPendingPayments()
if (err != nil) != tt.wantErr {
t.Errorf("FileStore.ListPendingPayments() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if len(pendingPayments) != tt.expectedCount {
t.Errorf("FileStore.ListPendingPayments() count = %v, want %v", len(pendingPayments), tt.expectedCount)
}
// Verify all returned payments have less than 1 confirmation
for _, payment := range pendingPayments {
if payment.Confirmations >= 1 {
t.Errorf("FileStore.ListPendingPayments() returned payment with %v confirmations, want < 1", payment.Confirmations)
}
}
}
})
}
}
func TestFileStore_ListPendingPayments_EmptyDirectory(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
pendingPayments, err := store.ListPendingPayments()
if err != nil {
t.Errorf("FileStore.ListPendingPayments() on empty directory error = %v, want nil", err)
}
if len(pendingPayments) != 0 {
t.Errorf("FileStore.ListPendingPayments() on empty directory count = %v, want 0", len(pendingPayments))
}
}
func TestFileStore_ListPendingPayments_NonJSONFiles(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
// Create some non-JSON files
nonJSONFiles := []string{"readme.txt", "config.xml", "data.csv", "script.sh"}
for _, filename := range nonJSONFiles {
filePath := filepath.Join(tempDir, filename)
err := os.WriteFile(filePath, []byte("test content"), 0o644)
if err != nil {
t.Fatalf("Failed to create test file %s: %v", filename, err)
}
}
// Create one valid payment
testPayment := createTestPayment("valid-payment")
testPayment.Confirmations = 0
err := store.CreatePayment(testPayment)
if err != nil {
t.Fatalf("Failed to create test payment: %v", err)
}
pendingPayments, err := store.ListPendingPayments()
if err != nil {
t.Errorf("FileStore.ListPendingPayments() error = %v, want nil", err)
}
if len(pendingPayments) != 1 {
t.Errorf("FileStore.ListPendingPayments() count = %v, want 1", len(pendingPayments))
}
if len(pendingPayments) > 0 && pendingPayments[0].ID != "valid-payment" {
t.Errorf("FileStore.ListPendingPayments() payment ID = %v, want valid-payment", pendingPayments[0].ID)
}
}
func TestFileStore_GetPaymentByAddress(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
// Create test payments with different addresses
btcAddr := "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
xmrAddr := "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD"
payments := []*Payment{
{
ID: "payment-btc-only",
Addresses: map[wallet.WalletType]string{
wallet.Bitcoin: btcAddr,
},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.0001},
CreatedAt: time.Now(),
Status: StatusPending,
},
{
ID: "payment-xmr-only",
Addresses: map[wallet.WalletType]string{
wallet.Monero: xmrAddr,
},
Amounts: map[wallet.WalletType]float64{wallet.Monero: 0.001},
CreatedAt: time.Now(),
Status: StatusPending,
},
{
ID: "payment-both",
Addresses: map[wallet.WalletType]string{
wallet.Bitcoin: "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2",
wallet.Monero: "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A",
},
Amounts: map[wallet.WalletType]float64{
wallet.Bitcoin: 0.0002,
wallet.Monero: 0.002,
},
CreatedAt: time.Now(),
Status: StatusPending,
},
}
// Store all test payments
for _, payment := range payments {
err := store.CreatePayment(payment)
if err != nil {
t.Fatalf("Failed to create test payment %s: %v", payment.ID, err)
}
}
tests := []struct {
name string
address string
wantPayment string
wantNil bool
wantErr bool
}{
{
name: "find payment by Bitcoin address",
address: btcAddr,
wantPayment: "payment-btc-only",
wantNil: false,
wantErr: false,
},
{
name: "find payment by Monero address",
address: xmrAddr,
wantPayment: "payment-xmr-only",
wantNil: false,
wantErr: false,
},
{
name: "find payment by Bitcoin address in mixed payment",
address: "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2",
wantPayment: "payment-both",
wantNil: false,
wantErr: false,
},
{
name: "find payment by Monero address in mixed payment",
address: "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A",
wantPayment: "payment-both",
wantNil: false,
wantErr: false,
},
{
name: "address not found",
address: "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy",
wantPayment: "",
wantNil: true,
wantErr: false,
},
{
name: "empty address",
address: "",
wantPayment: "",
wantNil: true,
wantErr: false,
},
{
name: "invalid address",
address: "invalid-address",
wantPayment: "",
wantNil: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
payment, err := store.GetPaymentByAddress(tt.address)
if (err != nil) != tt.wantErr {
t.Errorf("FileStore.GetPaymentByAddress() error = %v, wantErr %v", err, tt.wantErr)
return
}
if (payment == nil) != tt.wantNil {
t.Errorf("FileStore.GetPaymentByAddress() payment is nil = %v, wantNil %v", payment == nil, tt.wantNil)
return
}
if !tt.wantNil && payment != nil {
if payment.ID != tt.wantPayment {
t.Errorf("FileStore.GetPaymentByAddress() payment.ID = %v, want %v", payment.ID, tt.wantPayment)
}
}
})
}
}
func TestFileStore_GetPaymentByAddress_EmptyDirectory(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
payment, err := store.GetPaymentByAddress("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")
if err != nil {
t.Errorf("FileStore.GetPaymentByAddress() on empty directory error = %v, want nil", err)
}
if payment != nil {
t.Errorf("FileStore.GetPaymentByAddress() on empty directory payment = %v, want nil", payment)
}
}
// TestFileStore_ConcurrentAccess tests thread safety of FileStore operations
func TestFileStore_ConcurrentAccess(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
// Test concurrent operations
const numGoroutines = 10
const numOperations = 5
errChan := make(chan error, numGoroutines*numOperations)
// Concurrent writes
for i := 0; i < numGoroutines; i++ {
go func(id int) {
for j := 0; j < numOperations; j++ {
payment := createTestPayment(fmt.Sprintf("concurrent-payment-%d-%d", id, j))
if err := store.CreatePayment(payment); err != nil {
errChan <- fmt.Errorf("goroutine %d operation %d: %w", id, j, err)
} else {
errChan <- nil
}
}
}(i)
}
// Collect results
for i := 0; i < numGoroutines*numOperations; i++ {
if err := <-errChan; err != nil {
t.Errorf("Concurrent operation failed: %v", err)
}
}
// Verify all payments were created
payments, err := store.ListPendingPayments()
if err != nil {
t.Errorf("Failed to list payments after concurrent operations: %v", err)
}
// We can't guarantee exact count due to filtering (>1 confirmations), but should have some
if len(payments) > numGoroutines*numOperations {
t.Errorf("Too many payments found: %d, expected <= %d", len(payments), numGoroutines*numOperations)
}
}
// TestFileStore_CorruptedJSONHandling tests how the store handles corrupted JSON files
func TestFileStore_CorruptedJSONHandling(t *testing.T) {
tempDir := createTempDir(t)
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
// Create a corrupted JSON file
corruptedFile := filepath.Join(tempDir, "corrupted-payment.json")
err := os.WriteFile(corruptedFile, []byte("{invalid json content"), 0o600)
if err != nil {
t.Fatalf("Failed to create corrupted file: %v", err)
}
// Create a valid payment file
validPayment := createTestPayment("valid-payment")
validPayment.Confirmations = 0
err = store.CreatePayment(validPayment)
if err != nil {
t.Fatalf("Failed to create valid payment: %v", err)
}
// ListPendingPayments should skip corrupted files and return valid ones
payments, err := store.ListPendingPayments()
if err != nil {
t.Errorf("FileStore.ListPendingPayments() with corrupted file error = %v, want nil", err)
}
if len(payments) != 1 {
t.Errorf("FileStore.ListPendingPayments() with corrupted file count = %v, want 1", len(payments))
}
// GetPaymentByAddress should skip corrupted files and find valid ones
payment, err := store.GetPaymentByAddress(validPayment.Addresses[wallet.Bitcoin])
if err != nil {
t.Errorf("FileStore.GetPaymentByAddress() with corrupted file error = %v, want nil", err)
}
if payment == nil {
t.Error("FileStore.GetPaymentByAddress() should find valid payment despite corrupted file")
} else if payment.ID != "valid-payment" {
t.Errorf("FileStore.GetPaymentByAddress() payment.ID = %v, want valid-payment", payment.ID)
}
}
// Benchmark tests for performance validation
func BenchmarkFileStore_CreatePayment(b *testing.B) {
tempDir, err := os.MkdirTemp("", "filestore_bench_")
if err != nil {
b.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
b.ResetTimer()
for i := 0; i < b.N; i++ {
payment := createTestPayment(fmt.Sprintf("bench-payment-%d", i))
if err := store.CreatePayment(payment); err != nil {
b.Fatalf("CreatePayment failed: %v", err)
}
}
}
func BenchmarkFileStore_GetPayment(b *testing.B) {
tempDir, err := os.MkdirTemp("", "filestore_bench_")
if err != nil {
b.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)
store := NewFileStore(tempDir)
// Create a test payment
payment := createTestPayment("bench-payment")
if err := store.CreatePayment(payment); err != nil {
b.Fatalf("Failed to create test payment: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := store.GetPayment("bench-payment")
if err != nil {
b.Fatalf("GetPayment failed: %v", err)
}
}
}
// TestStoreImplementations_PendingPaymentConsistency validates that FileStore and
// EncryptedFileStore implement identical pending payment logic.
// This addresses PRIORITY 5 from ROADMAP.md - ensuring Liskov Substitution Principle
// compliance across storage backend implementations.
func TestStoreImplementations_PendingPaymentConsistency(t *testing.T) {
// Test that both stores exclude payments with 1 confirmation from pending list
t.Run("ExcludeOneConfirmation", func(t *testing.T) {
// Create FileStore
tempDir1 := createTempDir(t)
defer os.RemoveAll(tempDir1)
fileStore := NewFileStore(tempDir1)
// Create EncryptedFileStore
tempDir2 := createTempDir(t)
defer os.RemoveAll(tempDir2)
encKey, err := wallet.GenerateEncryptionKey()
if err != nil {
t.Fatalf("Failed to generate encryption key: %v", err)
}
encStore, err := NewFileStoreWithConfig(FileStoreConfig{
DataDir: tempDir2,
EncryptionKey: encKey,
})
if err != nil {
t.Fatalf("Failed to create encrypted file store: %v", err)
}
// Create identical payment with 1 confirmation
payment1 := createTestPayment("test-payment-1")
payment1.Confirmations = 1
payment2 := createTestPayment("test-payment-1")
payment2.Confirmations = 1
// Store in both backends
if err := fileStore.CreatePayment(payment1); err != nil {
t.Fatalf("FileStore.CreatePayment() error = %v", err)
}
if err := encStore.CreatePayment(payment2); err != nil {
t.Fatalf("EncryptedFileStore.CreatePayment() error = %v", err)
}
// Get pending payments from both stores
filePending, err := fileStore.ListPendingPayments()
if err != nil {
t.Fatalf("FileStore.ListPendingPayments() error = %v", err)
}
encPending, err := encStore.ListPendingPayments()
if err != nil {
t.Fatalf("EncryptedFileStore.ListPendingPayments() error = %v", err)
}
// Both should return empty list (exclude payments with 1 confirmation)
if len(filePending) != 0 {
t.Errorf("FileStore.ListPendingPayments() returned %d payments, want 0 (should exclude confirmations=1)", len(filePending))
}
if len(encPending) != 0 {
t.Errorf("EncryptedFileStore.ListPendingPayments() returned %d payments, want 0 (should exclude confirmations=1)", len(encPending))
}
// Verify both stores behave identically
if len(filePending) != len(encPending) {
t.Errorf("Store implementations inconsistent: FileStore returned %d, EncryptedFileStore returned %d", len(filePending), len(encPending))
}
})
// Test that both stores include payments with 0 confirmations in pending list
t.Run("IncludeZeroConfirmations", func(t *testing.T) {
// Create FileStore
tempDir1 := createTempDir(t)
defer os.RemoveAll(tempDir1)
fileStore := NewFileStore(tempDir1)
// Create EncryptedFileStore
tempDir2 := createTempDir(t)
defer os.RemoveAll(tempDir2)
encKey, err := wallet.GenerateEncryptionKey()
if err != nil {
t.Fatalf("Failed to generate encryption key: %v", err)
}
encStore, err := NewFileStoreWithConfig(FileStoreConfig{
DataDir: tempDir2,
EncryptionKey: encKey,
})
if err != nil {
t.Fatalf("Failed to create encrypted file store: %v", err)
}
// Create identical payment with 0 confirmations
payment1 := createTestPayment("test-payment-0")
payment1.Confirmations = 0
payment2 := createTestPayment("test-payment-0")
payment2.Confirmations = 0
// Store in both backends
if err := fileStore.CreatePayment(payment1); err != nil {
t.Fatalf("FileStore.CreatePayment() error = %v", err)
}
if err := encStore.CreatePayment(payment2); err != nil {
t.Fatalf("EncryptedFileStore.CreatePayment() error = %v", err)
}
// Get pending payments from both stores
filePending, err := fileStore.ListPendingPayments()
if err != nil {
t.Fatalf("FileStore.ListPendingPayments() error = %v", err)
}
encPending, err := encStore.ListPendingPayments()
if err != nil {
t.Fatalf("EncryptedFileStore.ListPendingPayments() error = %v", err)
}
// Both should return 1 payment (include payments with 0 confirmations)
if len(filePending) != 1 {
t.Errorf("FileStore.ListPendingPayments() returned %d payments, want 1 (should include confirmations=0)", len(filePending))
}
if len(encPending) != 1 {
t.Errorf("EncryptedFileStore.ListPendingPayments() returned %d payments, want 1 (should include confirmations=0)", len(encPending))
}
// Verify both stores behave identically
if len(filePending) != len(encPending) {
t.Errorf("Store implementations inconsistent: FileStore returned %d, EncryptedFileStore returned %d", len(filePending), len(encPending))
}
})
}