From 2aee754448efbd583905aff39dedc5e4d1f1a323 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 9 May 2013 22:25:21 -0500 Subject: [PATCH] Add test for discarding invalid large messages. --- message.go | 1 + message_test.go | 13 ++++++++++ test_coverage.txt | 64 +++++++++++++++++++++++------------------------ 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/message.go b/message.go index 00c3b185..13d0d77a 100644 --- a/message.go +++ b/message.go @@ -226,6 +226,7 @@ func ReadMessage(r io.Reader, pver uint32, btcnet BitcoinNet) (Message, []byte, // Check for messages from the wrong bitcoin network. if hdr.magic != btcnet { + discardInput(r, hdr.length) str := fmt.Sprintf("message from other network [%v]", hdr.magic) return nil, nil, messageError("ReadMessage", str) } diff --git a/message_test.go b/message_test.go index fa7b6a85..ea0cd182 100644 --- a/message_test.go +++ b/message_test.go @@ -176,6 +176,10 @@ func TestReadMessageWireErrors(t *testing.T) { badMessageBytes := makeHeader(btcnet, "addr", 1, 0xeaadc31c) badMessageBytes = append(badMessageBytes, 0x2) + // Wire encoded bytes for a message which the header claims has 15k + // bytes of data to discard. + discardBytes := makeHeader(btcnet, "bogus", 15*1024, 0) + tests := []struct { buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding @@ -265,6 +269,15 @@ func TestReadMessageWireErrors(t *testing.T) { len(badMessageBytes), io.EOF, }, + + // 15k bytes of data to discard. + { + discardBytes, + pver, + btcnet, + len(discardBytes), + &btcwire.MessageError{}, + }, } t.Logf("Running %d tests", len(tests)) diff --git a/test_coverage.txt b/test_coverage.txt index 2163ef9b..92a62c0b 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,46 +1,47 @@ -github.com/conformal/btcwire/message.go ReadMessage 100.00% (36/36) -github.com/conformal/btcwire/common.go readVarInt 100.00% (24/24) +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/common.go writeVarInt 100.00% (16/16) github.com/conformal/btcwire/shahash.go NewShaHashFromStr 100.00% (15/15) github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12) +github.com/conformal/btcwire/message.go discardInput 100.00% (10/10) github.com/conformal/btcwire/common.go readVarString 100.00% (8/8) -github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7) -github.com/conformal/btcwire/message.go readMessageHeader 100.00% (7/7) -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 writeVarString 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/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5) -github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5) -github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 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/msgping.go MsgPing.BtcDecode 100.00% (5/5) -github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5) -github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5) +github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7) github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5) +github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5) +github.com/conformal/btcwire/common.go readElements 100.00% (5/5) github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5) github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5) +github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5) github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5) github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5) github.com/conformal/btcwire/common.go writeElements 100.00% (5/5) -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/netaddress.go maxNetAddressPayload 100.00% (4/4) -github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcEncode 100.00% (4/4) +github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5) +github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5) +github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5) +github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5) +github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5) github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4) +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/msgblock.go MsgBlock.AddTransaction 100.00% (3/3) -github.com/conformal/btcwire/error.go MessageError.Error 100.00% (3/3) -github.com/conformal/btcwire/shahash.go ShaHash.Bytes 100.00% (3/3) -github.com/conformal/btcwire/netaddress.go NetAddress.HasService 100.00% (3/3) -github.com/conformal/btcwire/invvect.go InvType.String 100.00% (3/3) +github.com/conformal/btcwire/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4) +github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4) +github.com/conformal/btcwire/msgpong.go MsgPong.MaxPayloadLength 100.00% (4/4) github.com/conformal/btcwire/msgversion.go MsgVersion.HasService 100.00% (3/3) github.com/conformal/btcwire/msgaddr.go MsgAddr.MaxPayloadLength 100.00% (3/3) -github.com/conformal/btcwire/msgblock.go MsgBlock.ClearTransactions 100.00% (2/2) +github.com/conformal/btcwire/netaddress.go NetAddress.HasService 100.00% (3/3) +github.com/conformal/btcwire/invvect.go InvType.String 100.00% (3/3) +github.com/conformal/btcwire/shahash.go ShaHash.Bytes 100.00% (3/3) +github.com/conformal/btcwire/error.go MessageError.Error 100.00% (3/3) +github.com/conformal/btcwire/msgblock.go MsgBlock.AddTransaction 100.00% (3/3) 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) @@ -107,13 +108,12 @@ github.com/conformal/btcwire/msgversion.go NewMsgVersion 100.00% (1/1) github.com/conformal/btcwire/netaddress.go NetAddress.AddService 100.00% (1/1) github.com/conformal/btcwire/shahash.go ShaHash.IsEqual 100.00% (1/1) github.com/conformal/btcwire/netaddress.go readNetAddress 90.00% (18/20) -github.com/conformal/btcwire/message.go discardInput 90.00% (9/10) -github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 85.71% (6/7) github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 85.71% (6/7) github.com/conformal/btcwire/msgblock.go MsgBlock.TxShas 85.71% (6/7) +github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 85.71% (6/7) github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 85.71% (6/7) -github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 80.00% (8/10) github.com/conformal/btcwire/blockheader.go readBlockHeader 80.00% (8/10) +github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 80.00% (8/10) github.com/conformal/btcwire/shahash.go NewShaHash 80.00% (4/5) github.com/conformal/btcwire/netaddress.go writeNetAddress 78.57% (11/14) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 77.78% (7/9) @@ -121,33 +121,33 @@ github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 76.92% (10/13) github.com/conformal/btcwire/msgtx.go readTxIn 76.47% (13/17) github.com/conformal/btcwire/msgtx.go MsgTx.BtcDecode 76.00% (19/25) github.com/conformal/btcwire/msgtx.go readTxOut 75.00% (9/12) +github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 75.00% (6/8) github.com/conformal/btcwire/blockheader.go writeBlockHeader 75.00% (6/8) github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 75.00% (6/8) github.com/conformal/btcwire/msgalert.go MsgAlert.BtcDecode 75.00% (6/8) -github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 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/invvect.go writeInvVect 75.00% (3/4) github.com/conformal/btcwire/invvect.go readInvVect 75.00% (3/4) -github.com/conformal/btcwire/msgtx.go writeOutPoint 75.00% (3/4) github.com/conformal/btcwire/msgtx.go MsgTx.BtcEncode 73.91% (17/23) 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/msgnotfound.go MsgNotFound.BtcDecode 69.23% (9/13) github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 69.23% (9/13) +github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 69.23% (9/13) github.com/conformal/btcwire/msginv.go MsgInv.BtcDecode 69.23% (9/13) 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/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/msgnotfound.go MsgNotFound.BtcEncode 66.67% (8/12) +github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcEncode 66.67% (12/18) github.com/conformal/btcwire/msginv.go MsgInv.BtcEncode 66.67% (8/12) +github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcEncode 66.67% (8/12) github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 66.67% (8/12) 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/msgaddr.go MsgAddr.BtcEncode 60.00% (9/15) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecodeTxLoc 0.00% (0/16) -github.com/conformal/btcwire --------------------------------- 82.30% (767/932) +github.com/conformal/btcwire --------------------------------- 82.42% (769/933)