decred.org/dcrwallet/v3@v3.1.0/wallet/wallet_test.go (about) 1 // Copyright (c) 2018 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package wallet 6 7 import ( 8 "encoding/hex" 9 "math" 10 "testing" 11 12 "decred.org/dcrwallet/v3/errors" 13 "github.com/decred/dcrd/chaincfg/v3" 14 ) 15 16 func TestCoinbaseMatured(t *testing.T) { 17 t.Parallel() 18 params := chaincfg.MainNetParams() 19 maturity := int32(params.CoinbaseMaturity) 20 tests := []struct { 21 txHeight, tipHeight int32 22 matured bool 23 }{ 24 {0, 0, false}, 25 {0, maturity - 1, false}, 26 {0, maturity, true}, 27 {0, maturity + 1, true}, 28 {1, 0, false}, 29 {maturity, 0, false}, 30 {0, -1, false}, 31 {-1, maturity, false}, 32 {-1, math.MaxInt32, false}, 33 } 34 35 for i, test := range tests { 36 result := coinbaseMatured(params, test.txHeight, test.tipHeight) 37 if result != test.matured { 38 t.Errorf("test %d: result (%v) != expected (%v)", i, result, test.matured) 39 } 40 } 41 } 42 43 func TestTicketMatured(t *testing.T) { 44 t.Parallel() 45 params := chaincfg.MainNetParams() 46 maturity := int32(params.TicketMaturity) 47 tests := []struct { 48 txHeight, tipHeight int32 49 matured bool 50 }{ 51 {0, 0, false}, 52 {0, maturity - 1, false}, 53 {0, maturity, false}, // dcrd off-by-one results in this being false 54 {0, maturity + 1, true}, 55 {1, 0, false}, 56 {maturity, 0, false}, 57 {0, -1, false}, 58 {-1, maturity, false}, 59 {-1, math.MaxInt32, false}, 60 } 61 62 for i, test := range tests { 63 result := ticketMatured(params, test.txHeight, test.tipHeight) 64 if result != test.matured { 65 t.Errorf("test %d: result (%v) != expected (%v)", i, result, test.matured) 66 } 67 } 68 } 69 70 func TestTicketExpired(t *testing.T) { 71 t.Parallel() 72 params := chaincfg.MainNetParams() 73 expiry := int32(params.TicketMaturity) + int32(params.TicketExpiry) 74 tests := []struct { 75 txHeight, tipHeight int32 76 expired bool 77 }{ 78 {0, 0, false}, 79 {0, expiry - 1, false}, 80 {0, expiry, false}, // dcrd off-by-one results in this being false 81 {0, expiry + 1, true}, 82 {1, 0, false}, 83 {expiry, 0, false}, 84 {0, -1, false}, 85 {-1, expiry, false}, 86 {-1, math.MaxInt32, false}, 87 } 88 89 for i, test := range tests { 90 result := ticketExpired(params, test.txHeight, test.tipHeight) 91 if result != test.expired { 92 t.Errorf("test %d: result (%v) != expected (%v)", i, result, test.expired) 93 } 94 } 95 } 96 97 // TestVotingXprivFromSeed tests that creating voting xprivs works properly. 98 func TestVotingXprivFromSeed(t *testing.T) { 99 seed, err := hex.DecodeString("0000000000000000000000000000000000000" + 100 "000000000000000000000000000") 101 if err != nil { 102 t.Fatalf("unable to create seed: %v", err) 103 } 104 wantXpriv := "dprv3oaf1h1hLxLZNHn6Gn9AaUaMJxhpHAj2KGMMHTi2AnoiHdRhCc" + 105 "iKbwf3dB6zpPEq8ffdT4NZ7gjtrZBQhSWDm2RVXbphmpdPnbq299ddB8a" 106 107 tests := []struct { 108 name, want string 109 seed []byte 110 wantErr *errors.Error 111 }{{ 112 name: "ok", 113 seed: seed, 114 want: wantXpriv, 115 }, { 116 name: "bad seed", 117 seed: seed[:1], 118 wantErr: &errors.Error{Kind: errors.Invalid}, 119 }} 120 121 for _, test := range tests { 122 got, err := votingXprivFromSeed(test.seed, chaincfg.MainNetParams()) 123 if test.wantErr != nil { 124 if err == nil { 125 t.Fatalf("wanted error %v but got none", test.wantErr) 126 } 127 kind := err.(*errors.Error).Kind 128 if !test.wantErr.Is(kind) { 129 t.Fatalf("wanted error %v but got %v", test.wantErr, kind) 130 } 131 continue 132 } 133 if err != nil { 134 t.Fatalf("unexpected error: %v", err) 135 } 136 if got.String() != test.want { 137 t.Fatalf("wanted xpriv %v but got %v", test.want, got) 138 } 139 } 140 }