github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/namecoin/namecoinparser_test.go (about) 1 // +build unittest 2 3 package namecoin 4 5 import ( 6 "blockbook/bchain/coins/btc" 7 "bytes" 8 "encoding/hex" 9 "fmt" 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "reflect" 14 "testing" 15 16 "github.com/martinboehm/btcutil/chaincfg" 17 ) 18 19 func TestMain(m *testing.M) { 20 c := m.Run() 21 chaincfg.ResetParams() 22 os.Exit(c) 23 } 24 25 func Test_GetAddrDescFromAddress_Mainnet(t *testing.T) { 26 type args struct { 27 address string 28 } 29 tests := []struct { 30 name string 31 args args 32 want string 33 wantErr bool 34 }{ 35 { 36 name: "P2PKH1", 37 args: args{address: "MzBvZ4F759X6wHTjzwkMEbKh12am3PHT6F"}, 38 want: "76a91427a1f12771de5cc3b73941664b2537c15316be4388ac", 39 wantErr: false, 40 }, 41 { 42 name: "P2PKH2", 43 args: args{address: "N8Jkcm44Uq55GdmPojkpuGyoW4Cm658TwW"}, 44 want: "76a91480ad90d403581fa3bf46086a91b2d9d4125db6c188ac", 45 wantErr: false, 46 }, 47 } 48 parser := NewNamecoinParser(GetChainParams("main"), &btc.Configuration{}) 49 50 for _, tt := range tests { 51 t.Run(tt.name, func(t *testing.T) { 52 got, err := parser.GetAddrDescFromAddress(tt.args.address) 53 if (err != nil) != tt.wantErr { 54 t.Errorf("GetAddrDescFromAddress() error = %v, wantErr %v", err, tt.wantErr) 55 return 56 } 57 h := hex.EncodeToString(got) 58 if !reflect.DeepEqual(h, tt.want) { 59 t.Errorf("GetAddrDescFromAddress() = %v, want %v", h, tt.want) 60 } 61 }) 62 } 63 } 64 65 type testBlock struct { 66 size int 67 time int64 68 txs []string 69 } 70 71 var testParseBlockTxs = map[int]testBlock{ 72 40000: { 73 size: 1385, 74 time: 1327728573, 75 txs: []string{ 76 "e193a821393b20b99f4a4e05a481368ef8a8cfd43d0c45bdad7f53bc9535e844", 77 "ddcbf95797e81dd04127885bd001e96695b717e11c52721f6e8ee53f6dea8a6f", 78 "31ff728f24200f59fa4958e6c26de03d172b320e6eef2b8abecf6f94d01dd4ae", 79 }, 80 }, 81 } 82 83 func helperLoadBlock(t *testing.T, height int) []byte { 84 name := fmt.Sprintf("block_dump.%d", height) 85 path := filepath.Join("testdata", name) 86 87 d, err := ioutil.ReadFile(path) 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 d = bytes.TrimSpace(d) 93 94 b := make([]byte, hex.DecodedLen(len(d))) 95 _, err = hex.Decode(b, d) 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 return b 101 } 102 103 func TestParseBlock(t *testing.T) { 104 p := NewNamecoinParser(GetChainParams("main"), &btc.Configuration{}) 105 106 for height, tb := range testParseBlockTxs { 107 b := helperLoadBlock(t, height) 108 109 blk, err := p.ParseBlock(b) 110 if err != nil { 111 t.Fatal(err) 112 } 113 114 if blk.Size != tb.size { 115 t.Errorf("ParseBlock() block size: got %d, want %d", blk.Size, tb.size) 116 } 117 118 if blk.Time != tb.time { 119 t.Errorf("ParseBlock() block time: got %d, want %d", blk.Time, tb.time) 120 } 121 122 if len(blk.Txs) != len(tb.txs) { 123 t.Errorf("ParseBlock() number of transactions: got %d, want %d", len(blk.Txs), len(tb.txs)) 124 } 125 126 for ti, tx := range tb.txs { 127 if blk.Txs[ti].Txid != tx { 128 t.Errorf("ParseBlock() transaction %d: got %s, want %s", ti, blk.Txs[ti].Txid, tx) 129 } 130 } 131 } 132 }