diff --git a/msgheaders_test.go b/msgheaders_test.go index c84ab3a1..07a58b3b 100644 --- a/msgheaders_test.go +++ b/msgheaders_test.go @@ -8,6 +8,7 @@ import ( "bytes" "github.com/conformal/btcwire" "github.com/davecgh/go-spew/spew" + "io" "reflect" "testing" ) @@ -211,3 +212,137 @@ func TestHeadersWire(t *testing.T) { } } } + +// TestHeadersWireErrors performs negative tests against wire encode and decode +// of MsgHeaders to confirm error paths work correctly. +func TestHeadersWireErrors(t *testing.T) { + pver := btcwire.ProtocolVersion + btcwireErr := &btcwire.MessageError{} + + hash := btcwire.GenesisHash + merkleHash := blockOne.Header.MerkleRoot + bits := uint32(0x1d00ffff) + nonce := uint32(0x9962e301) + bh := btcwire.NewBlockHeader(&hash, &merkleHash, bits, nonce) + bh.Version = blockOne.Header.Version + bh.Timestamp = blockOne.Header.Timestamp + + // Headers message with one header. + oneHeader := btcwire.NewMsgHeaders() + oneHeader.AddBlockHeader(bh) + oneHeaderEncoded := []byte{ + 0x01, // VarInt for number of headers. + 0x01, 0x00, 0x00, 0x00, // Version 1 + 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, + 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, + 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, + 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock + 0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, + 0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67, + 0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1, + 0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot + 0x61, 0xbc, 0x66, 0x49, // Timestamp + 0xff, 0xff, 0x00, 0x1d, // Bits + 0x01, 0xe3, 0x62, 0x99, // Nonce + 0x00, // TxnCount (0 for headers message) + } + + // Message that forces an error by having more than the max allowed + // headers. + maxHeaders := btcwire.NewMsgHeaders() + for i := 0; i < btcwire.MaxBlockHeadersPerMsg; i++ { + maxHeaders.AddBlockHeader(bh) + } + maxHeaders.Headers = append(maxHeaders.Headers, bh) + maxHeadersEncoded := []byte{ + 0xfd, 0xd1, 0x07, // Varint for number of addresses (2001)7D1 + } + + // Intentionally invalid block header that has a transaction count used + // to force errors. + bhTrans := btcwire.NewBlockHeader(&hash, &merkleHash, bits, nonce) + bhTrans.Version = blockOne.Header.Version + bhTrans.Timestamp = blockOne.Header.Timestamp + bhTrans.TxnCount = 1 + + transHeader := btcwire.NewMsgHeaders() + transHeader.AddBlockHeader(bhTrans) + transHeaderEncoded := []byte{ + 0x01, // VarInt for number of headers. + 0x01, 0x00, 0x00, 0x00, // Version 1 + 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, + 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, + 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, + 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock + 0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, + 0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67, + 0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1, + 0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e, // MerkleRoot + 0x61, 0xbc, 0x66, 0x49, // Timestamp + 0xff, 0xff, 0x00, 0x1d, // Bits + 0x01, 0xe3, 0x62, 0x99, // Nonce + 0x01, // TxnCount (should be 0 for headers message, but 1 to force error) + } + + tests := []struct { + in *btcwire.MsgHeaders // Value to encode + buf []byte // Wire encoding + pver uint32 // Protocol version for wire encoding + max int // Max size of fixed buffer to induce errors + writeErr error // Expected write error + readErr error // Expected read error + }{ + // Latest protocol version with intentional read/write errors. + // Force error in header count. + {oneHeader, oneHeaderEncoded, pver, 0, io.ErrShortWrite, io.EOF}, + // Force error in block header. + {oneHeader, oneHeaderEncoded, pver, 5, io.ErrShortWrite, io.EOF}, + // Force error with greater than max headers. + {maxHeaders, maxHeadersEncoded, pver, 3, btcwireErr, btcwireErr}, + // Force error with included transactions. + {transHeader, transHeaderEncoded, pver, len(transHeaderEncoded), btcwireErr, btcwireErr}, + } + + t.Logf("Running %d tests", len(tests)) + for i, test := range tests { + // Encode to wire format. + w := newFixedWriter(test.max) + err := test.in.BtcEncode(w, test.pver) + if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { + t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", + i, err, test.writeErr) + continue + } + + // For errors which are not of type btcwire.MessageError, check + // them for equality. + if _, ok := err.(*btcwire.MessageError); !ok { + if err != test.writeErr { + t.Errorf("BtcEncode #%d wrong error got: %v, "+ + "want: %v", i, err, test.writeErr) + continue + } + } + + // Decode from wire format. + var msg btcwire.MsgHeaders + r := newFixedReader(test.max, test.buf) + err = msg.BtcDecode(r, test.pver) + if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { + t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", + i, err, test.readErr) + continue + } + + // For errors which are not of type btcwire.MessageError, check + // them for equality. + if _, ok := err.(*btcwire.MessageError); !ok { + if err != test.readErr { + t.Errorf("BtcEncode #%d wrong error got: %v, "+ + "want: %v", i, err, test.readErr) + continue + } + } + + } +} diff --git a/test_coverage.txt b/test_coverage.txt index 20ee4632..d2e4e5c2 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -2,64 +2,66 @@ github.com/conformal/btcwire/message.go ReadMessage 100.00% (37/37) github.com/conformal/btcwire/msgtx.go MsgTx.Copy 100.00% (24/24) github.com/conformal/btcwire/common.go readVarInt 100.00% (24/24) -github.com/conformal/btcwire/message.go makeEmptyMessage 100.00% (20/20) github.com/conformal/btcwire/netaddress.go readNetAddress 100.00% (20/20) +github.com/conformal/btcwire/message.go makeEmptyMessage 100.00% (20/20) +github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 100.00% (16/16) github.com/conformal/btcwire/common.go writeVarInt 100.00% (16/16) -github.com/conformal/btcwire/shahash.go NewShaHashFromStr 100.00% (15/15) github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcEncode 100.00% (15/15) +github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 100.00% (15/15) +github.com/conformal/btcwire/shahash.go NewShaHashFromStr 100.00% (15/15) github.com/conformal/btcwire/netaddress.go writeNetAddress 100.00% (14/14) -github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 100.00% (13/13) -github.com/conformal/btcwire/msginv.go MsgInv.BtcDecode 100.00% (13/13) github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (13/13) github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13) +github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 100.00% (13/13) +github.com/conformal/btcwire/msginv.go MsgInv.BtcDecode 100.00% (13/13) github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12) -github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcEncode 100.00% (12/12) -github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12) github.com/conformal/btcwire/msginv.go MsgInv.BtcEncode 100.00% (12/12) -github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 100.00% (10/10) +github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12) +github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcEncode 100.00% (12/12) github.com/conformal/btcwire/message.go discardInput 100.00% (10/10) +github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 100.00% (10/10) github.com/conformal/btcwire/blockheader.go readBlockHeader 100.00% (10/10) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 100.00% (9/9) -github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8) github.com/conformal/btcwire/common.go readVarString 100.00% (8/8) -github.com/conformal/btcwire/common.go writeVarString 100.00% (7/7) -github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7) -github.com/conformal/btcwire/common.go DoubleSha256 100.00% (7/7) -github.com/conformal/btcwire/message.go readMessageHeader 100.00% (7/7) -github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 100.00% (7/7) -github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7) +github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8) github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7) -github.com/conformal/btcwire/common.go readElements 100.00% (5/5) -github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5) -github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5) -github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5) -github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5) +github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7) +github.com/conformal/btcwire/message.go readMessageHeader 100.00% (7/7) +github.com/conformal/btcwire/common.go DoubleSha256 100.00% (7/7) +github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 100.00% (7/7) +github.com/conformal/btcwire/common.go writeVarString 100.00% (7/7) +github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7) github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5) -github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5) -github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5) github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5) +github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5) +github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5) +github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5) +github.com/conformal/btcwire/common.go writeElements 100.00% (5/5) +github.com/conformal/btcwire/common.go readElements 100.00% (5/5) github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5) +github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5) github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5) github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5) -github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5) -github.com/conformal/btcwire/common.go writeElements 100.00% (5/5) -github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4) -github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4) -github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4) +github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5) +github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5) +github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5) github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcEncode 100.00% (4/4) +github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4) +github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4) github.com/conformal/btcwire/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4) github.com/conformal/btcwire/msgpong.go MsgPong.MaxPayloadLength 100.00% (4/4) github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4) github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4) +github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4) github.com/conformal/btcwire/invvect.go InvType.String 100.00% (3/3) github.com/conformal/btcwire/error.go MessageError.Error 100.00% (3/3) -github.com/conformal/btcwire/msgaddr.go MsgAddr.MaxPayloadLength 100.00% (3/3) github.com/conformal/btcwire/msgversion.go MsgVersion.HasService 100.00% (3/3) github.com/conformal/btcwire/msgblock.go MsgBlock.AddTransaction 100.00% (3/3) github.com/conformal/btcwire/netaddress.go NetAddress.HasService 100.00% (3/3) +github.com/conformal/btcwire/msgaddr.go MsgAddr.MaxPayloadLength 100.00% (3/3) github.com/conformal/btcwire/shahash.go ShaHash.Bytes 100.00% (3/3) -github.com/conformal/btcwire/msgblock.go MsgBlock.ClearTransactions 100.00% (2/2) github.com/conformal/btcwire/netaddress.go NetAddress.SetAddress 100.00% (2/2) +github.com/conformal/btcwire/msgblock.go MsgBlock.ClearTransactions 100.00% (2/2) github.com/conformal/btcwire/msgalert.go MsgAlert.Command 100.00% (1/1) github.com/conformal/btcwire/blockheader.go NewBlockHeader 100.00% (1/1) github.com/conformal/btcwire/common.go readElement 100.00% (1/1) @@ -132,22 +134,20 @@ github.com/conformal/btcwire/shahash.go NewShaHash 80.00% (4/5) github.com/conformal/btcwire/msgtx.go MsgTx.BtcEncode 78.26% (18/23) github.com/conformal/btcwire/msgtx.go readTxIn 76.47% (13/17) github.com/conformal/btcwire/msgtx.go readTxOut 75.00% (9/12) +github.com/conformal/btcwire/msgalert.go MsgAlert.BtcDecode 75.00% (6/8) github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 75.00% (6/8) github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 75.00% (6/8) -github.com/conformal/btcwire/msgalert.go MsgAlert.BtcDecode 75.00% (6/8) github.com/conformal/btcwire/msgtx.go readOutPoint 75.00% (3/4) github.com/conformal/btcwire/msgtx.go writeOutPoint 75.00% (3/4) github.com/conformal/btcwire/msgtx.go writeTxIn 73.33% (11/15) github.com/conformal/btcwire/msgtx.go writeTxOut 72.73% (8/11) github.com/conformal/btcwire/message.go WriteMessage 70.00% (21/30) -github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcDecode 68.42% (13/19) github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcDecode 68.42% (13/19) +github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcDecode 68.42% (13/19) github.com/conformal/btcwire/msgversion.go MsgVersion.BtcDecode 68.00% (17/25) -github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcEncode 66.67% (12/18) github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcEncode 66.67% (12/18) +github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcEncode 66.67% (12/18) github.com/conformal/btcwire/msgversion.go MsgVersion.BtcEncode 63.64% (14/22) -github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 62.50% (10/16) -github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 60.00% (9/15) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecodeTxLoc 0.00% (0/16) -github.com/conformal/btcwire --------------------------------- 88.00% (821/933) +github.com/conformal/btcwire --------------------------------- 89.28% (833/933)