github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcjson/chainsvrwsntfns_test.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package btcjson_test 7 8 import ( 9 "bytes" 10 "encoding/json" 11 "fmt" 12 "reflect" 13 "testing" 14 15 "github.com/dashpay/godash/btcjson" 16 ) 17 18 // TestChainSvrWsNtfns tests all of the chain server websocket-specific 19 // notifications marshal and unmarshal into valid results include handling of 20 // optional fields being omitted in the marshalled command, while optional 21 // fields with defaults have the default assigned on unmarshalled commands. 22 func TestChainSvrWsNtfns(t *testing.T) { 23 t.Parallel() 24 25 tests := []struct { 26 name string 27 newNtfn func() (interface{}, error) 28 staticNtfn func() interface{} 29 marshalled string 30 unmarshalled interface{} 31 }{ 32 { 33 name: "blockconnected", 34 newNtfn: func() (interface{}, error) { 35 return btcjson.NewCmd("blockconnected", "123", 100000, 123456789) 36 }, 37 staticNtfn: func() interface{} { 38 return btcjson.NewBlockConnectedNtfn("123", 100000, 123456789) 39 }, 40 marshalled: `{"jsonrpc":"1.0","method":"blockconnected","params":["123",100000,123456789],"id":null}`, 41 unmarshalled: &btcjson.BlockConnectedNtfn{ 42 Hash: "123", 43 Height: 100000, 44 Time: 123456789, 45 }, 46 }, 47 { 48 name: "blockdisconnected", 49 newNtfn: func() (interface{}, error) { 50 return btcjson.NewCmd("blockdisconnected", "123", 100000, 123456789) 51 }, 52 staticNtfn: func() interface{} { 53 return btcjson.NewBlockDisconnectedNtfn("123", 100000, 123456789) 54 }, 55 marshalled: `{"jsonrpc":"1.0","method":"blockdisconnected","params":["123",100000,123456789],"id":null}`, 56 unmarshalled: &btcjson.BlockDisconnectedNtfn{ 57 Hash: "123", 58 Height: 100000, 59 Time: 123456789, 60 }, 61 }, 62 { 63 name: "recvtx", 64 newNtfn: func() (interface{}, error) { 65 return btcjson.NewCmd("recvtx", "001122", `{"height":100000,"hash":"123","index":0,"time":12345678}`) 66 }, 67 staticNtfn: func() interface{} { 68 blockDetails := btcjson.BlockDetails{ 69 Height: 100000, 70 Hash: "123", 71 Index: 0, 72 Time: 12345678, 73 } 74 return btcjson.NewRecvTxNtfn("001122", &blockDetails) 75 }, 76 marshalled: `{"jsonrpc":"1.0","method":"recvtx","params":["001122",{"height":100000,"hash":"123","index":0,"time":12345678}],"id":null}`, 77 unmarshalled: &btcjson.RecvTxNtfn{ 78 HexTx: "001122", 79 Block: &btcjson.BlockDetails{ 80 Height: 100000, 81 Hash: "123", 82 Index: 0, 83 Time: 12345678, 84 }, 85 }, 86 }, 87 { 88 name: "redeemingtx", 89 newNtfn: func() (interface{}, error) { 90 return btcjson.NewCmd("redeemingtx", "001122", `{"height":100000,"hash":"123","index":0,"time":12345678}`) 91 }, 92 staticNtfn: func() interface{} { 93 blockDetails := btcjson.BlockDetails{ 94 Height: 100000, 95 Hash: "123", 96 Index: 0, 97 Time: 12345678, 98 } 99 return btcjson.NewRedeemingTxNtfn("001122", &blockDetails) 100 }, 101 marshalled: `{"jsonrpc":"1.0","method":"redeemingtx","params":["001122",{"height":100000,"hash":"123","index":0,"time":12345678}],"id":null}`, 102 unmarshalled: &btcjson.RedeemingTxNtfn{ 103 HexTx: "001122", 104 Block: &btcjson.BlockDetails{ 105 Height: 100000, 106 Hash: "123", 107 Index: 0, 108 Time: 12345678, 109 }, 110 }, 111 }, 112 { 113 name: "rescanfinished", 114 newNtfn: func() (interface{}, error) { 115 return btcjson.NewCmd("rescanfinished", "123", 100000, 12345678) 116 }, 117 staticNtfn: func() interface{} { 118 return btcjson.NewRescanFinishedNtfn("123", 100000, 12345678) 119 }, 120 marshalled: `{"jsonrpc":"1.0","method":"rescanfinished","params":["123",100000,12345678],"id":null}`, 121 unmarshalled: &btcjson.RescanFinishedNtfn{ 122 Hash: "123", 123 Height: 100000, 124 Time: 12345678, 125 }, 126 }, 127 { 128 name: "rescanprogress", 129 newNtfn: func() (interface{}, error) { 130 return btcjson.NewCmd("rescanprogress", "123", 100000, 12345678) 131 }, 132 staticNtfn: func() interface{} { 133 return btcjson.NewRescanProgressNtfn("123", 100000, 12345678) 134 }, 135 marshalled: `{"jsonrpc":"1.0","method":"rescanprogress","params":["123",100000,12345678],"id":null}`, 136 unmarshalled: &btcjson.RescanProgressNtfn{ 137 Hash: "123", 138 Height: 100000, 139 Time: 12345678, 140 }, 141 }, 142 { 143 name: "txaccepted", 144 newNtfn: func() (interface{}, error) { 145 return btcjson.NewCmd("txaccepted", "123", 1.5) 146 }, 147 staticNtfn: func() interface{} { 148 return btcjson.NewTxAcceptedNtfn("123", 1.5) 149 }, 150 marshalled: `{"jsonrpc":"1.0","method":"txaccepted","params":["123",1.5],"id":null}`, 151 unmarshalled: &btcjson.TxAcceptedNtfn{ 152 TxID: "123", 153 Amount: 1.5, 154 }, 155 }, 156 { 157 name: "txacceptedverbose", 158 newNtfn: func() (interface{}, error) { 159 return btcjson.NewCmd("txacceptedverbose", `{"hex":"001122","txid":"123","version":1,"locktime":4294967295,"vin":null,"vout":null,"confirmations":0}`) 160 }, 161 staticNtfn: func() interface{} { 162 txResult := btcjson.TxRawResult{ 163 Hex: "001122", 164 Txid: "123", 165 Version: 1, 166 LockTime: 4294967295, 167 Vin: nil, 168 Vout: nil, 169 Confirmations: 0, 170 } 171 return btcjson.NewTxAcceptedVerboseNtfn(txResult) 172 }, 173 marshalled: `{"jsonrpc":"1.0","method":"txacceptedverbose","params":[{"hex":"001122","txid":"123","version":1,"locktime":4294967295,"vin":null,"vout":null}],"id":null}`, 174 unmarshalled: &btcjson.TxAcceptedVerboseNtfn{ 175 RawTx: btcjson.TxRawResult{ 176 Hex: "001122", 177 Txid: "123", 178 Version: 1, 179 LockTime: 4294967295, 180 Vin: nil, 181 Vout: nil, 182 Confirmations: 0, 183 }, 184 }, 185 }, 186 } 187 188 t.Logf("Running %d tests", len(tests)) 189 for i, test := range tests { 190 // Marshal the notification as created by the new static 191 // creation function. The ID is nil for notifications. 192 marshalled, err := btcjson.MarshalCmd(nil, test.staticNtfn()) 193 if err != nil { 194 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 195 test.name, err) 196 continue 197 } 198 199 if !bytes.Equal(marshalled, []byte(test.marshalled)) { 200 t.Errorf("Test #%d (%s) unexpected marshalled data - "+ 201 "got %s, want %s", i, test.name, marshalled, 202 test.marshalled) 203 continue 204 } 205 206 // Ensure the notification is created without error via the 207 // generic new notification creation function. 208 cmd, err := test.newNtfn() 209 if err != nil { 210 t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ", 211 i, test.name, err) 212 } 213 214 // Marshal the notification as created by the generic new 215 // notification creation function. The ID is nil for 216 // notifications. 217 marshalled, err = btcjson.MarshalCmd(nil, cmd) 218 if err != nil { 219 t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i, 220 test.name, err) 221 continue 222 } 223 224 if !bytes.Equal(marshalled, []byte(test.marshalled)) { 225 t.Errorf("Test #%d (%s) unexpected marshalled data - "+ 226 "got %s, want %s", i, test.name, marshalled, 227 test.marshalled) 228 continue 229 } 230 231 var request btcjson.Request 232 if err := json.Unmarshal(marshalled, &request); err != nil { 233 t.Errorf("Test #%d (%s) unexpected error while "+ 234 "unmarshalling JSON-RPC request: %v", i, 235 test.name, err) 236 continue 237 } 238 239 cmd, err = btcjson.UnmarshalCmd(&request) 240 if err != nil { 241 t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i, 242 test.name, err) 243 continue 244 } 245 246 if !reflect.DeepEqual(cmd, test.unmarshalled) { 247 t.Errorf("Test #%d (%s) unexpected unmarshalled command "+ 248 "- got %s, want %s", i, test.name, 249 fmt.Sprintf("(%T) %+[1]v", cmd), 250 fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled)) 251 continue 252 } 253 } 254 }