github.com/bchainhub/blockbook@v0.3.2/bchain/coins/gamecredits/gamecreditsparser_test.go (about) 1 // +build unittest 2 3 package gamecredits 4 5 import ( 6 "blockbook/bchain" 7 "blockbook/bchain/coins/btc" 8 "encoding/hex" 9 "math/big" 10 "os" 11 "reflect" 12 "testing" 13 14 "github.com/martinboehm/btcutil/chaincfg" 15 ) 16 17 func TestMain(m *testing.M) { 18 c := m.Run() 19 chaincfg.ResetParams() 20 os.Exit(c) 21 } 22 23 func Test_GetAddrDescFromAddress_Testnet(t *testing.T) { 24 type args struct { 25 address string 26 } 27 tests := []struct { 28 name string 29 args args 30 want string 31 wantErr bool 32 }{ 33 { 34 name: "P2PKH1", 35 args: args{address: "GcGBy77CCfZJJhGLALohdahf9eAc7jo7Yk"}, 36 want: "76a914ca093a938a0e19e86b36859d9423a475d45eb3a288ac", 37 wantErr: false, 38 }, 39 { 40 name: "P2SH1", 41 args: args{address: "S84eckDshWupTwErdLKkyDauNwtWfa9rPL"}, 42 want: "a9146edfea548a7d6c25aa28e37bf2ea382891882fa687", 43 wantErr: false, 44 }, 45 } 46 parser := NewGameCreditsParser(GetChainParams("test"), &btc.Configuration{}) 47 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 got, err := parser.GetAddrDescFromAddress(tt.args.address) 51 if (err != nil) != tt.wantErr { 52 t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 53 return 54 } 55 h := hex.EncodeToString(got) 56 if !reflect.DeepEqual(h, tt.want) { 57 t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) 58 } 59 }) 60 } 61 } 62 63 func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { 64 type args struct { 65 address string 66 } 67 tests := []struct { 68 name string 69 args args 70 want string 71 wantErr bool 72 }{ 73 { 74 name: "P2PKH1", 75 args: args{address: "GcGBy77CCfZJJhGLALohdahf9eAc7jo7Yk"}, 76 want: "76a914ca093a938a0e19e86b36859d9423a475d45eb3a288ac", 77 wantErr: false, 78 }, 79 { 80 name: "P2SH1", 81 args: args{address: "S84eckDshWupTwErdLKkyDauNwtWfa9rPL"}, 82 want: "a9146edfea548a7d6c25aa28e37bf2ea382891882fa687", 83 wantErr: false, 84 }, 85 } 86 parser := NewGameCreditsParser(GetChainParams("main"), &btc.Configuration{}) 87 88 for _, tt := range tests { 89 t.Run(tt.name, func(t *testing.T) { 90 got, err := parser.GetAddrDescFromAddress(tt.args.address) 91 if (err != nil) != tt.wantErr { 92 t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 93 return 94 } 95 h := hex.EncodeToString(got) 96 if !reflect.DeepEqual(h, tt.want) { 97 t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) 98 } 99 }) 100 } 101 } 102 103 var ( 104 testTx1 bchain.Tx 105 106 testTxPacked1 = "002151148bbcaa8406010000000123c41ad26dd5782635638effbc9e31c9b4a3b757591a52c83d2770ad82b33e93000000006b483045022100a20302bde6d2fb194bb9c0a8d7beb52ed0b5b72b912da75364efe169d5b74c67022065632d4032673a6093f513b93e380323487ad2708003e161a12e7b7362bf9f4a01210325c1b08d90a016cb73f4e8d37614cac7da00cb78121f21b7b6e0a7d4a03fbae4fdffffff0100f4aa01000000001976a914ca093a938a0e19e86b36859d9423a475d45eb3a288acc54f2100" 107 ) 108 109 func init() { 110 testTx1 = bchain.Tx{ 111 Hex: "010000000123c41ad26dd5782635638effbc9e31c9b4a3b757591a52c83d2770ad82b33e93000000006b483045022100a20302bde6d2fb194bb9c0a8d7beb52ed0b5b72b912da75364efe169d5b74c67022065632d4032673a6093f513b93e380323487ad2708003e161a12e7b7362bf9f4a01210325c1b08d90a016cb73f4e8d37614cac7da00cb78121f21b7b6e0a7d4a03fbae4fdffffff0100f4aa01000000001976a914ca093a938a0e19e86b36859d9423a475d45eb3a288acc54f2100", 112 Blocktime: 1539653891, 113 Txid: "983da8317fff45afb17290d4dd8da6ec1cd8ffbbfa98e53a0754e9b60f8cc0f9", 114 LockTime: 2183109, 115 Version: 1, 116 Vin: []bchain.Vin{ 117 { 118 ScriptSig: bchain.ScriptSig{ 119 Hex: "483045022100a20302bde6d2fb194bb9c0a8d7beb52ed0b5b72b912da75364efe169d5b74c67022065632d4032673a6093f513b93e380323487ad2708003e161a12e7b7362bf9f4a01210325c1b08d90a016cb73f4e8d37614cac7da00cb78121f21b7b6e0a7d4a03fbae4", 120 }, 121 Txid: "933eb382ad70273dc8521a5957b7a3b4c9319ebcff8e63352678d56dd21ac423", 122 Vout: 0, 123 Sequence: 4294967293, 124 }, 125 }, 126 Vout: []bchain.Vout{ 127 { 128 ValueSat: *big.NewInt(27980800), 129 N: 0, 130 ScriptPubKey: bchain.ScriptPubKey{ 131 Hex: "76a914ca093a938a0e19e86b36859d9423a475d45eb3a288ac", 132 Addresses: []string{ 133 "GcGBy77CCfZJJhGLALohdahf9eAc7jo7Yk", 134 }, 135 }, 136 }, 137 }, 138 } 139 } 140 141 func Test_PackTx(t *testing.T) { 142 type args struct { 143 tx bchain.Tx 144 height uint32 145 blockTime int64 146 parser *GameCreditsParser 147 } 148 tests := []struct { 149 name string 150 args args 151 want string 152 wantErr bool 153 }{ 154 { 155 name: "gamecredits-1", 156 args: args{ 157 tx: testTx1, 158 height: 2183444, 159 blockTime: 1539653891, 160 parser: NewGameCreditsParser(GetChainParams("main"), &btc.Configuration{}), 161 }, 162 want: testTxPacked1, 163 wantErr: false, 164 }, 165 } 166 for _, tt := range tests { 167 t.Run(tt.name, func(t *testing.T) { 168 got, err := tt.args.parser.PackTx(&tt.args.tx, tt.args.height, tt.args.blockTime) 169 if (err != nil) != tt.wantErr { 170 t.Errorf("packTx() error = %v, wantErr %v", err, tt.wantErr) 171 return 172 } 173 h := hex.EncodeToString(got) 174 if !reflect.DeepEqual(h, tt.want) { 175 t.Errorf("packTx() = %v, want %v", h, tt.want) 176 } 177 }) 178 } 179 } 180 181 func Test_UnpackTx(t *testing.T) { 182 type args struct { 183 packedTx string 184 parser *GameCreditsParser 185 } 186 tests := []struct { 187 name string 188 args args 189 want *bchain.Tx 190 want1 uint32 191 wantErr bool 192 }{ 193 { 194 name: "gamecredits-1", 195 args: args{ 196 packedTx: testTxPacked1, 197 parser: NewGameCreditsParser(GetChainParams("main"), &btc.Configuration{}), 198 }, 199 want: &testTx1, 200 want1: 2183444, 201 wantErr: false, 202 }, 203 } 204 for _, tt := range tests { 205 t.Run(tt.name, func(t *testing.T) { 206 b, _ := hex.DecodeString(tt.args.packedTx) 207 got, got1, err := tt.args.parser.UnpackTx(b) 208 if (err != nil) != tt.wantErr { 209 t.Errorf("unpackTx() error = %v, wantErr %v", err, tt.wantErr) 210 return 211 } 212 if !reflect.DeepEqual(got, tt.want) { 213 t.Errorf("unpackTx() got = %v, want %v", got, tt.want) 214 } 215 if got1 != tt.want1 { 216 t.Errorf("unpackTx() got1 = %v, want %v", got1, tt.want1) 217 } 218 }) 219 } 220 }