From f9b6375d5be12b0eabbd21c061edc824affc3d20 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 12 May 2013 21:47:36 -0500 Subject: [PATCH] Add negative tests for MsgVersion. This commit adds tests for the error paths when encoding and decoding MsgVersion. --- msgversion_test.go | 108 +++++++++++++++++++++++++++++++++++++++++++++ test_coverage.txt | 62 +++++++++++++------------- 2 files changed, 139 insertions(+), 31 deletions(-) diff --git a/msgversion_test.go b/msgversion_test.go index 7cc2f493..cbb3bce4 100644 --- a/msgversion_test.go +++ b/msgversion_test.go @@ -8,8 +8,10 @@ import ( "bytes" "github.com/conformal/btcwire" "github.com/davecgh/go-spew/spew" + "io" "net" "reflect" + "strings" "testing" "time" ) @@ -225,6 +227,112 @@ func TestVersionWire(t *testing.T) { } } +// TestVersionWireErrors performs negative tests against wire encode and +// decode of MsgGetHeaders to confirm error paths work correctly. +func TestVersionWireErrors(t *testing.T) { + // Use protocol version 60002 specifically here instead of the latest + // because the test data is using bytes encoded with that protocol + // version. + pver := uint32(60002) + btcwireErr := &btcwire.MessageError{} + + // Copy the base version and change the user agent to exceed max limits. + bvc := *baseVersion + exceedUAVer := &bvc + newUA := "/" + strings.Repeat("t", btcwire.MaxUserAgentLen-8+1) + ":0.0.1/" + exceedUAVer.UserAgent = newUA + + // Encode the new UA length as a varint. + var newUAVarIntBuf bytes.Buffer + err := btcwire.TstWriteVarInt(&newUAVarIntBuf, pver, uint64(len(newUA))) + if err != nil { + t.Errorf("writeVarInt: error %v", err) + } + + // Make a new buffer big enough to hold the base version plus the new + // bytes for the bigger varint to hold the new size of the user agent + // and the new user agent string. Then stich it all together. + newLen := len(baseVersionEncoded) - len(baseVersion.UserAgent) + newLen = newLen + len(newUAVarIntBuf.Bytes()) - 1 + len(newUA) + exceedUAVerEncoded := make([]byte, newLen) + copy(exceedUAVerEncoded, baseVersionEncoded[0:80]) + copy(exceedUAVerEncoded[80:], newUAVarIntBuf.Bytes()) + copy(exceedUAVerEncoded[83:], []byte(newUA)) + copy(exceedUAVerEncoded[83+len(newUA):], baseVersionEncoded[97:100]) + + tests := []struct { + in *btcwire.MsgVersion // 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 + }{ + // Force error in protocol version. + {baseVersion, baseVersionEncoded, pver, 0, io.ErrShortWrite, io.EOF}, + // Force error in services. + {baseVersion, baseVersionEncoded, pver, 4, io.ErrShortWrite, io.EOF}, + // Force error in timestamp. + {baseVersion, baseVersionEncoded, pver, 12, io.ErrShortWrite, io.EOF}, + // Force error in remote address. + {baseVersion, baseVersionEncoded, pver, 20, io.ErrShortWrite, io.EOF}, + // Force error in local address. + {baseVersion, baseVersionEncoded, pver, 46, io.ErrShortWrite, io.EOF}, + // Force error in nonce. + {baseVersion, baseVersionEncoded, pver, 72, io.ErrShortWrite, io.EOF}, + // Force error in user agent length. + {baseVersion, baseVersionEncoded, pver, 80, io.ErrShortWrite, io.EOF}, + // Force error in user agent. + {baseVersion, baseVersionEncoded, pver, 81, io.ErrShortWrite, io.EOF}, + // Force error in last block. + {baseVersion, baseVersionEncoded, pver, 97, io.ErrShortWrite, io.EOF}, + // Force error due to user agent too big. + {exceedUAVer, exceedUAVerEncoded, pver, newLen, 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.MsgVersion + 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("BtcDecode #%d wrong error got: %v, "+ + "want: %v", i, err, test.readErr) + continue + } + } + } +} + // baseVersion is used in the various tests as a baseline MsgVersion. var baseVersion *btcwire.MsgVersion = &btcwire.MsgVersion{ ProtocolVersion: 60002, diff --git a/test_coverage.txt b/test_coverage.txt index 87b8b131..77e4f0a0 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,10 +1,12 @@ github.com/conformal/btcwire/message.go ReadMessage 100.00% (37/37) github.com/conformal/btcwire/message.go WriteMessage 100.00% (27/27) +github.com/conformal/btcwire/msgversion.go MsgVersion.BtcDecode 100.00% (25/25) 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/msgversion.go MsgVersion.BtcEncode 100.00% (22/22) 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/msggetblocks.go MsgGetBlocks.BtcDecode 100.00% (19/19) github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcDecode 100.00% (19/19) github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcEncode 100.00% (18/18) @@ -12,35 +14,34 @@ github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcEncode 100.00% github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecodeTxLoc 100.00% (16/16) github.com/conformal/btcwire/common.go writeVarInt 100.00% (16/16) github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 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/msginv.go MsgInv.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/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13) github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (13/13) github.com/conformal/btcwire/msginv.go MsgInv.BtcEncode 100.00% (12/12) 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/protocol.go ServiceFlag.String 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/message.go discardInput 100.00% (10/10) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 100.00% (9/9) -github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 100.00% (8/8) -github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8) github.com/conformal/btcwire/msgalert.go MsgAlert.BtcDecode 100.00% (8/8) github.com/conformal/btcwire/common.go readVarString 100.00% (8/8) -github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7) -github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7) +github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 100.00% (8/8) +github.com/conformal/btcwire/blockheader.go writeBlockHeader 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/msgpong.go MsgPong.BtcDecode 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/shahash.go NewShaHash 100.00% (5/5) -github.com/conformal/btcwire/common.go writeElements 100.00% (5/5) +github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7) +github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7) +github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7) +github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 100.00% (7/7) +github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 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/msgping.go MsgPing.BtcDecode 100.00% (5/5) @@ -48,33 +49,34 @@ github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5) github.com/conformal/btcwire/msgblock.go MsgBlock.TxShas 100.00% (5/5) github.com/conformal/btcwire/common.go readElements 100.00% (5/5) github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 100.00% (5/5) +github.com/conformal/btcwire/shahash.go NewShaHash 100.00% (5/5) github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5) -github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 100.00% (5/5) github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5) github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5) -github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 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/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5) github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5) github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5) -github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4) -github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4) +github.com/conformal/btcwire/common.go writeElements 100.00% (5/5) github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4) github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4) -github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 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/msgmempool.go MsgMemPool.BtcEncode 100.00% (4/4) +github.com/conformal/btcwire/msgpong.go MsgPong.MaxPayloadLength 100.00% (4/4) +github.com/conformal/btcwire/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4) +github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4) +github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4) +github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4) github.com/conformal/btcwire/msgblock.go MsgBlock.AddTransaction 100.00% (3/3) -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/shahash.go ShaHash.Bytes 100.00% (3/3) -github.com/conformal/btcwire/msgaddr.go MsgAddr.MaxPayloadLength 100.00% (3/3) github.com/conformal/btcwire/netaddress.go NetAddress.HasService 100.00% (3/3) github.com/conformal/btcwire/msgversion.go MsgVersion.HasService 100.00% (3/3) -github.com/conformal/btcwire/netaddress.go NetAddress.SetAddress 100.00% (2/2) +github.com/conformal/btcwire/shahash.go ShaHash.Bytes 100.00% (3/3) +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/msgblock.go MsgBlock.ClearTransactions 100.00% (2/2) -github.com/conformal/btcwire/invvect.go NewInvVect 100.00% (1/1) +github.com/conformal/btcwire/netaddress.go NetAddress.SetAddress 100.00% (2/2) +github.com/conformal/btcwire/msgheaders.go MsgHeaders.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) github.com/conformal/btcwire/common.go writeElement 100.00% (1/1) @@ -104,7 +106,6 @@ github.com/conformal/btcwire/msggetdata.go NewMsgGetData 100.00% (1/1) github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.Command 100.00% (1/1) github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.MaxPayloadLength 100.00% (1/1) github.com/conformal/btcwire/msggetheaders.go NewMsgGetHeaders 100.00% (1/1) -github.com/conformal/btcwire/msgheaders.go MsgHeaders.Command 100.00% (1/1) github.com/conformal/btcwire/msgheaders.go MsgHeaders.MaxPayloadLength 100.00% (1/1) github.com/conformal/btcwire/msgheaders.go NewMsgHeaders 100.00% (1/1) github.com/conformal/btcwire/msginv.go MsgInv.Command 100.00% (1/1) @@ -139,15 +140,14 @@ github.com/conformal/btcwire/msgversion.go MsgVersion.MaxPayloadLength 100.00 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/invvect.go NewInvVect 100.00% (1/1) github.com/conformal/btcwire/msgtx.go MsgTx.BtcDecode 80.00% (20/25) 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/msgtx.go writeOutPoint 75.00% (3/4) 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/msgversion.go MsgVersion.BtcDecode 68.00% (17/25) -github.com/conformal/btcwire/msgversion.go MsgVersion.BtcEncode 63.64% (14/22) -github.com/conformal/btcwire --------------------------------- 95.45% (881/923) +github.com/conformal/btcwire --------------------------------- 97.18% (897/923)