github.com/btcsuite/btcd@v0.24.0/btcjson/chainsvrresults_test.go (about) 1 // Copyright (c) 2014 The btcsuite 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 btcjson_test 6 7 import ( 8 "encoding/json" 9 "net/url" 10 "reflect" 11 "testing" 12 13 "github.com/btcsuite/btcd/btcjson" 14 "github.com/btcsuite/btcd/btcutil" 15 "github.com/btcsuite/btcd/chaincfg/chainhash" 16 "github.com/davecgh/go-spew/spew" 17 ) 18 19 // TestChainSvrCustomResults ensures any results that have custom marshalling 20 // work as inteded. 21 // and unmarshal code of results are as expected. 22 func TestChainSvrCustomResults(t *testing.T) { 23 t.Parallel() 24 25 tests := []struct { 26 name string 27 result interface{} 28 expected string 29 }{ 30 { 31 name: "custom vin marshal with coinbase", 32 result: &btcjson.Vin{ 33 Coinbase: "021234", 34 Sequence: 4294967295, 35 }, 36 expected: `{"coinbase":"021234","sequence":4294967295}`, 37 }, 38 { 39 name: "custom vin marshal without coinbase", 40 result: &btcjson.Vin{ 41 Txid: "123", 42 Vout: 1, 43 ScriptSig: &btcjson.ScriptSig{ 44 Asm: "0", 45 Hex: "00", 46 }, 47 Sequence: 4294967295, 48 }, 49 expected: `{"txid":"123","vout":1,"scriptSig":{"asm":"0","hex":"00"},"sequence":4294967295}`, 50 }, 51 { 52 name: "custom vinprevout marshal with coinbase", 53 result: &btcjson.VinPrevOut{ 54 Coinbase: "021234", 55 Sequence: 4294967295, 56 }, 57 expected: `{"coinbase":"021234","sequence":4294967295}`, 58 }, 59 { 60 name: "custom vinprevout marshal without coinbase", 61 result: &btcjson.VinPrevOut{ 62 Txid: "123", 63 Vout: 1, 64 ScriptSig: &btcjson.ScriptSig{ 65 Asm: "0", 66 Hex: "00", 67 }, 68 PrevOut: &btcjson.PrevOut{ 69 Addresses: []string{"addr1"}, 70 Value: 0, 71 }, 72 Sequence: 4294967295, 73 }, 74 expected: `{"txid":"123","vout":1,"scriptSig":{"asm":"0","hex":"00"},"prevOut":{"addresses":["addr1"],"value":0},"sequence":4294967295}`, 75 }, 76 { 77 name: "zmq notification", 78 result: &btcjson.GetZmqNotificationResult{{ 79 Type: "pubrawblock", 80 Address: func() *url.URL { 81 u, err := url.Parse("tcp://127.0.0.1:1238") 82 if err != nil { 83 panic(err) 84 } 85 return u 86 }(), 87 HighWaterMark: 1337, 88 }}, 89 expected: `[{"address":"tcp://127.0.0.1:1238","hwm":1337,"type":"pubrawblock"}]`, 90 }, 91 } 92 93 t.Logf("Running %d tests", len(tests)) 94 for i, test := range tests { 95 marshalled, err := json.Marshal(test.result) 96 if err != nil { 97 t.Errorf("Test #%d (%s) unexpected error: %v", i, 98 test.name, err) 99 continue 100 } 101 if string(marshalled) != test.expected { 102 t.Errorf("Test #%d (%s) unexpected marhsalled data - "+ 103 "got %s, want %s", i, test.name, marshalled, 104 test.expected) 105 continue 106 } 107 } 108 } 109 110 // TestGetTxOutSetInfoResult ensures that custom unmarshalling of 111 // GetTxOutSetInfoResult works as intended. 112 func TestGetTxOutSetInfoResult(t *testing.T) { 113 t.Parallel() 114 115 tests := []struct { 116 name string 117 result string 118 want btcjson.GetTxOutSetInfoResult 119 }{ 120 { 121 name: "GetTxOutSetInfoResult - not scanning", 122 result: `{"height":123,"bestblock":"000000000000005f94116250e2407310463c0a7cf950f1af9ebe935b1c0687ab","transactions":1,"txouts":1,"bogosize":1,"hash_serialized_2":"9a0a561203ff052182993bc5d0cb2c620880bfafdbd80331f65fd9546c3e5c3e","disk_size":1,"total_amount":0.2}`, 123 want: btcjson.GetTxOutSetInfoResult{ 124 Height: 123, 125 BestBlock: func() chainhash.Hash { 126 h, err := chainhash.NewHashFromStr("000000000000005f94116250e2407310463c0a7cf950f1af9ebe935b1c0687ab") 127 if err != nil { 128 panic(err) 129 } 130 131 return *h 132 }(), 133 Transactions: 1, 134 TxOuts: 1, 135 BogoSize: 1, 136 HashSerialized: func() chainhash.Hash { 137 h, err := chainhash.NewHashFromStr("9a0a561203ff052182993bc5d0cb2c620880bfafdbd80331f65fd9546c3e5c3e") 138 if err != nil { 139 panic(err) 140 } 141 142 return *h 143 }(), 144 DiskSize: 1, 145 TotalAmount: func() btcutil.Amount { 146 a, err := btcutil.NewAmount(0.2) 147 if err != nil { 148 panic(err) 149 } 150 151 return a 152 }(), 153 }, 154 }, 155 } 156 157 t.Logf("Running %d tests", len(tests)) 158 for i, test := range tests { 159 var out btcjson.GetTxOutSetInfoResult 160 err := json.Unmarshal([]byte(test.result), &out) 161 if err != nil { 162 t.Errorf("Test #%d (%s) unexpected error: %v", i, 163 test.name, err) 164 continue 165 } 166 167 if !reflect.DeepEqual(out, test.want) { 168 t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+ 169 "got %v, want %v", i, test.name, spew.Sdump(out), 170 spew.Sdump(test.want)) 171 continue 172 } 173 } 174 } 175 176 // TestChainSvrMiningInfoResults ensures GetMiningInfoResults are unmarshalled correctly 177 func TestChainSvrMiningInfoResults(t *testing.T) { 178 t.Parallel() 179 180 tests := []struct { 181 name string 182 result string 183 expected btcjson.GetMiningInfoResult 184 }{ 185 { 186 name: "mining info with integer networkhashps", 187 result: `{"networkhashps": 89790618491361}`, 188 expected: btcjson.GetMiningInfoResult{ 189 NetworkHashPS: 89790618491361, 190 }, 191 }, 192 { 193 name: "mining info with scientific notation networkhashps", 194 result: `{"networkhashps": 8.9790618491361e+13}`, 195 expected: btcjson.GetMiningInfoResult{ 196 NetworkHashPS: 89790618491361, 197 }, 198 }, 199 } 200 201 t.Logf("Running %d tests", len(tests)) 202 for i, test := range tests { 203 var miningInfoResult btcjson.GetMiningInfoResult 204 err := json.Unmarshal([]byte(test.result), &miningInfoResult) 205 if err != nil { 206 t.Errorf("Test #%d (%s) unexpected error: %v", i, 207 test.name, err) 208 continue 209 } 210 if miningInfoResult != test.expected { 211 t.Errorf("Test #%d (%s) unexpected marhsalled data - "+ 212 "got %+v, want %+v", i, test.name, miningInfoResult, 213 test.expected) 214 continue 215 } 216 } 217 }