github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgheaders_test.go (about) 1 // Copyright (c) 2013-2015 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 wire_test 7 8 import ( 9 "bytes" 10 "io" 11 "reflect" 12 "testing" 13 14 "github.com/dashpay/godash/wire" 15 "github.com/davecgh/go-spew/spew" 16 ) 17 18 // TestHeaders tests the MsgHeaders API. 19 func TestHeaders(t *testing.T) { 20 pver := uint32(60002) 21 22 // Ensure the command is expected value. 23 wantCmd := "headers" 24 msg := wire.NewMsgHeaders() 25 if cmd := msg.Command(); cmd != wantCmd { 26 t.Errorf("NewMsgHeaders: wrong command - got %v want %v", 27 cmd, wantCmd) 28 } 29 30 // Ensure max payload is expected value for latest protocol version. 31 // Num headers (varInt) + max allowed headers (header length + 1 byte 32 // for the number of transactions which is always 0). 33 wantPayload := uint32(162009) 34 maxPayload := msg.MaxPayloadLength(pver) 35 if maxPayload != wantPayload { 36 t.Errorf("MaxPayloadLength: wrong max payload length for "+ 37 "protocol version %d - got %v, want %v", pver, 38 maxPayload, wantPayload) 39 } 40 41 // Ensure headers are added properly. 42 bh := &blockOne.Header 43 msg.AddBlockHeader(bh) 44 if !reflect.DeepEqual(msg.Headers[0], bh) { 45 t.Errorf("AddHeader: wrong header - got %v, want %v", 46 spew.Sdump(msg.Headers), 47 spew.Sdump(bh)) 48 } 49 50 // Ensure adding more than the max allowed headers per message returns 51 // error. 52 var err error 53 for i := 0; i < wire.MaxBlockHeadersPerMsg+1; i++ { 54 err = msg.AddBlockHeader(bh) 55 } 56 if reflect.TypeOf(err) != reflect.TypeOf(&wire.MessageError{}) { 57 t.Errorf("AddBlockHeader: expected error on too many headers " + 58 "not received") 59 } 60 61 return 62 } 63 64 // TestHeadersWire tests the MsgHeaders wire encode and decode for various 65 // numbers of headers and protocol versions. 66 func TestHeadersWire(t *testing.T) { 67 hash := mainNetGenesisHash 68 merkleHash := blockOne.Header.MerkleRoot 69 bits := uint32(0x1d00ffff) 70 nonce := uint32(0x9962e301) 71 bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce) 72 bh.Version = blockOne.Header.Version 73 bh.Timestamp = blockOne.Header.Timestamp 74 75 // Empty headers message. 76 noHeaders := wire.NewMsgHeaders() 77 noHeadersEncoded := []byte{ 78 0x00, // Varint for number of headers 79 } 80 81 // Headers message with one header. 82 oneHeader := wire.NewMsgHeaders() 83 oneHeader.AddBlockHeader(bh) 84 oneHeaderEncoded := []byte{ 85 0x01, // VarInt for number of headers. 86 0x01, 0x00, 0x00, 0x00, // Version 1 87 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 88 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 89 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 90 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock 91 0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, 92 0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67, 93 0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1, 94 0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot 95 0x61, 0xbc, 0x66, 0x49, // Timestamp 96 0xff, 0xff, 0x00, 0x1d, // Bits 97 0x01, 0xe3, 0x62, 0x99, // Nonce 98 0x00, // TxnCount (0 for headers message) 99 } 100 101 tests := []struct { 102 in *wire.MsgHeaders // Message to encode 103 out *wire.MsgHeaders // Expected decoded message 104 buf []byte // Wire encoding 105 pver uint32 // Protocol version for wire encoding 106 }{ 107 // Latest protocol version with no headers. 108 { 109 noHeaders, 110 noHeaders, 111 noHeadersEncoded, 112 wire.ProtocolVersion, 113 }, 114 115 // Latest protocol version with one header. 116 { 117 oneHeader, 118 oneHeader, 119 oneHeaderEncoded, 120 wire.ProtocolVersion, 121 }, 122 123 // Protocol version BIP0035Version with no headers. 124 { 125 noHeaders, 126 noHeaders, 127 noHeadersEncoded, 128 wire.BIP0035Version, 129 }, 130 131 // Protocol version BIP0035Version with one header. 132 { 133 oneHeader, 134 oneHeader, 135 oneHeaderEncoded, 136 wire.BIP0035Version, 137 }, 138 139 // Protocol version BIP0031Version with no headers. 140 { 141 noHeaders, 142 noHeaders, 143 noHeadersEncoded, 144 wire.BIP0031Version, 145 }, 146 147 // Protocol version BIP0031Version with one header. 148 { 149 oneHeader, 150 oneHeader, 151 oneHeaderEncoded, 152 wire.BIP0031Version, 153 }, 154 // Protocol version NetAddressTimeVersion with no headers. 155 { 156 noHeaders, 157 noHeaders, 158 noHeadersEncoded, 159 wire.NetAddressTimeVersion, 160 }, 161 162 // Protocol version NetAddressTimeVersion with one header. 163 { 164 oneHeader, 165 oneHeader, 166 oneHeaderEncoded, 167 wire.NetAddressTimeVersion, 168 }, 169 170 // Protocol version MultipleAddressVersion with no headers. 171 { 172 noHeaders, 173 noHeaders, 174 noHeadersEncoded, 175 wire.MultipleAddressVersion, 176 }, 177 178 // Protocol version MultipleAddressVersion with one header. 179 { 180 oneHeader, 181 oneHeader, 182 oneHeaderEncoded, 183 wire.MultipleAddressVersion, 184 }, 185 } 186 187 t.Logf("Running %d tests", len(tests)) 188 for i, test := range tests { 189 // Encode the message to wire format. 190 var buf bytes.Buffer 191 err := test.in.BtcEncode(&buf, test.pver) 192 if err != nil { 193 t.Errorf("BtcEncode #%d error %v", i, err) 194 continue 195 } 196 if !bytes.Equal(buf.Bytes(), test.buf) { 197 t.Errorf("BtcEncode #%d\n got: %s want: %s", i, 198 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) 199 continue 200 } 201 202 // Decode the message from wire format. 203 var msg wire.MsgHeaders 204 rbuf := bytes.NewReader(test.buf) 205 err = msg.BtcDecode(rbuf, test.pver) 206 if err != nil { 207 t.Errorf("BtcDecode #%d error %v", i, err) 208 continue 209 } 210 if !reflect.DeepEqual(&msg, test.out) { 211 t.Errorf("BtcDecode #%d\n got: %s want: %s", i, 212 spew.Sdump(&msg), spew.Sdump(test.out)) 213 continue 214 } 215 } 216 } 217 218 // TestHeadersWireErrors performs negative tests against wire encode and decode 219 // of MsgHeaders to confirm error paths work correctly. 220 func TestHeadersWireErrors(t *testing.T) { 221 pver := wire.ProtocolVersion 222 wireErr := &wire.MessageError{} 223 224 hash := mainNetGenesisHash 225 merkleHash := blockOne.Header.MerkleRoot 226 bits := uint32(0x1d00ffff) 227 nonce := uint32(0x9962e301) 228 bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce) 229 bh.Version = blockOne.Header.Version 230 bh.Timestamp = blockOne.Header.Timestamp 231 232 // Headers message with one header. 233 oneHeader := wire.NewMsgHeaders() 234 oneHeader.AddBlockHeader(bh) 235 oneHeaderEncoded := []byte{ 236 0x01, // VarInt for number of headers. 237 0x01, 0x00, 0x00, 0x00, // Version 1 238 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 239 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 240 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 241 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock 242 0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, 243 0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67, 244 0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1, 245 0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot 246 0x61, 0xbc, 0x66, 0x49, // Timestamp 247 0xff, 0xff, 0x00, 0x1d, // Bits 248 0x01, 0xe3, 0x62, 0x99, // Nonce 249 0x00, // TxnCount (0 for headers message) 250 } 251 252 // Message that forces an error by having more than the max allowed 253 // headers. 254 maxHeaders := wire.NewMsgHeaders() 255 for i := 0; i < wire.MaxBlockHeadersPerMsg; i++ { 256 maxHeaders.AddBlockHeader(bh) 257 } 258 maxHeaders.Headers = append(maxHeaders.Headers, bh) 259 maxHeadersEncoded := []byte{ 260 0xfd, 0xd1, 0x07, // Varint for number of addresses (2001)7D1 261 } 262 263 // Intentionally invalid block header that has a transaction count used 264 // to force errors. 265 bhTrans := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce) 266 bhTrans.Version = blockOne.Header.Version 267 bhTrans.Timestamp = blockOne.Header.Timestamp 268 269 transHeader := wire.NewMsgHeaders() 270 transHeader.AddBlockHeader(bhTrans) 271 transHeaderEncoded := []byte{ 272 0x01, // VarInt for number of headers. 273 0x01, 0x00, 0x00, 0x00, // Version 1 274 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 275 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 276 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 277 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock 278 0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, 279 0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67, 280 0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1, 281 0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot 282 0x61, 0xbc, 0x66, 0x49, // Timestamp 283 0xff, 0xff, 0x00, 0x1d, // Bits 284 0x01, 0xe3, 0x62, 0x99, // Nonce 285 0x01, // TxnCount (should be 0 for headers message, but 1 to force error) 286 } 287 288 tests := []struct { 289 in *wire.MsgHeaders // Value to encode 290 buf []byte // Wire encoding 291 pver uint32 // Protocol version for wire encoding 292 max int // Max size of fixed buffer to induce errors 293 writeErr error // Expected write error 294 readErr error // Expected read error 295 }{ 296 // Latest protocol version with intentional read/write errors. 297 // Force error in header count. 298 {oneHeader, oneHeaderEncoded, pver, 0, io.ErrShortWrite, io.EOF}, 299 // Force error in block header. 300 {oneHeader, oneHeaderEncoded, pver, 5, io.ErrShortWrite, io.EOF}, 301 // Force error with greater than max headers. 302 {maxHeaders, maxHeadersEncoded, pver, 3, wireErr, wireErr}, 303 // Force error with number of transactions. 304 {transHeader, transHeaderEncoded, pver, 81, io.ErrShortWrite, io.EOF}, 305 // Force error with included transactions. 306 {transHeader, transHeaderEncoded, pver, len(transHeaderEncoded), nil, wireErr}, 307 } 308 309 t.Logf("Running %d tests", len(tests)) 310 for i, test := range tests { 311 // Encode to wire format. 312 w := newFixedWriter(test.max) 313 err := test.in.BtcEncode(w, test.pver) 314 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { 315 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", 316 i, err, test.writeErr) 317 continue 318 } 319 320 // For errors which are not of type wire.MessageError, check 321 // them for equality. 322 if _, ok := err.(*wire.MessageError); !ok { 323 if err != test.writeErr { 324 t.Errorf("BtcEncode #%d wrong error got: %v, "+ 325 "want: %v", i, err, test.writeErr) 326 continue 327 } 328 } 329 330 // Decode from wire format. 331 var msg wire.MsgHeaders 332 r := newFixedReader(test.max, test.buf) 333 err = msg.BtcDecode(r, test.pver) 334 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { 335 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", 336 i, err, test.readErr) 337 continue 338 } 339 340 // For errors which are not of type wire.MessageError, check 341 // them for equality. 342 if _, ok := err.(*wire.MessageError); !ok { 343 if err != test.readErr { 344 t.Errorf("BtcDecode #%d wrong error got: %v, "+ 345 "want: %v", i, err, test.readErr) 346 continue 347 } 348 } 349 350 } 351 }