Add negative tests for MsgGetData.

This commit adds tests for the error paths when encoded and decoding
MsgGetData.
This commit is contained in:
Dave Collins 2013-05-10 13:14:19 -05:00
parent 7c3129bf89
commit 2665b4358e
2 changed files with 125 additions and 25 deletions

View File

@ -8,6 +8,7 @@ import (
"bytes" "bytes"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"io"
"reflect" "reflect"
"testing" "testing"
) )
@ -220,3 +221,102 @@ func TestGetDataWire(t *testing.T) {
} }
} }
} }
// TestGetDataWireErrors performs negative tests against wire encode and decode
// of MsgGetData to confirm error paths work correctly.
func TestGetDataWireErrors(t *testing.T) {
pver := btcwire.ProtocolVersion
btcwireErr := &btcwire.MessageError{}
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
blockHash, err := btcwire.NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
iv := btcwire.NewInvVect(btcwire.InvVect_Block, blockHash)
// Base message used to induce errors.
baseGetData := btcwire.NewMsgGetData()
baseGetData.AddInvVect(iv)
baseGetDataEncoded := []byte{
0x02, // Varint for number of inv vectors
0x02, 0x00, 0x00, 0x00, // InvVect_Block
0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash
}
// Message that forces an error by having more than the max allowed inv
// vectors.
maxGetData := btcwire.NewMsgGetData()
for i := 0; i < btcwire.MaxInvPerMsg; i++ {
maxGetData.AddInvVect(iv)
}
maxGetData.InvList = append(maxGetData.InvList, iv)
maxGetDataEncoded := []byte{
0xfd, 0x51, 0xc3, // Varint for number of inv vectors (50001)
}
tests := []struct {
in *btcwire.MsgGetData // 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 inventory vector count
{baseGetData, baseGetDataEncoded, pver, 0, io.ErrShortWrite, io.EOF},
//// Force error in inventory list.
{baseGetData, baseGetDataEncoded, pver, 1, io.ErrShortWrite, io.EOF},
//// Force error with greater than max inventory vectors.
{maxGetData, maxGetDataEncoded, pver, 3, 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.MsgGetData
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
}
}
}
}

View File

@ -7,23 +7,25 @@ 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/common.go writeVarInt 100.00% (16/16)
github.com/conformal/btcwire/shahash.go NewShaHashFromStr 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/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/msginv.go MsgInv.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13) github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msginv.go MsgInv.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/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/blockheader.go readBlockHeader 100.00% (10/10) github.com/conformal/btcwire/blockheader.go readBlockHeader 100.00% (10/10)
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 100.00% (10/10) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 100.00% (10/10)
github.com/conformal/btcwire/message.go discardInput 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/msgblock.go MsgBlock.BtcEncode 100.00% (9/9)
github.com/conformal/btcwire/common.go readVarString 100.00% (8/8)
github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8) 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/message.go readMessageHeader 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/msgversion.go NewMsgVersionFromConn 100.00% (7/7)
github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7)
github.com/conformal/btcwire/common.go DoubleSha256 100.00% (7/7) github.com/conformal/btcwire/common.go DoubleSha256 100.00% (7/7)
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/common.go writeVarString 100.00% (7/7)
github.com/conformal/btcwire/common.go readElements 100.00% (5/5)
github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5) github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5) github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5)
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5) github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5)
@ -35,23 +37,23 @@ 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/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5) github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5)
github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5) github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 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/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5)
github.com/conformal/btcwire/common.go readElements 100.00% (5/5)
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/invvect.go writeInvVect 100.00% (4/4)
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.BtcEncode 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/netaddress.go maxNetAddressPayload 100.00% (4/4) github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4)
github.com/conformal/btcwire/msgblock.go MsgBlock.AddTransaction 100.00% (3/3) github.com/conformal/btcwire/msgpong.go MsgPong.MaxPayloadLength 100.00% (4/4)
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4)
github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4)
github.com/conformal/btcwire/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4)
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/error.go MessageError.Error 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/msgversion.go MsgVersion.HasService 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/msgaddr.go MsgAddr.MaxPayloadLength 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/invvect.go InvType.String 100.00% (3/3)
github.com/conformal/btcwire/shahash.go ShaHash.Bytes 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/msgblock.go MsgBlock.ClearTransactions 100.00% (2/2) 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/netaddress.go NetAddress.SetAddress 100.00% (2/2)
github.com/conformal/btcwire/msgalert.go MsgAlert.Command 100.00% (1/1) github.com/conformal/btcwire/msgalert.go MsgAlert.Command 100.00% (1/1)
@ -120,34 +122,32 @@ 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/netaddress.go NetAddress.AddService 100.00% (1/1)
github.com/conformal/btcwire/shahash.go ShaHash.IsEqual 100.00% (1/1) github.com/conformal/btcwire/shahash.go ShaHash.IsEqual 100.00% (1/1)
github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 85.71% (6/7) 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/msgblock.go MsgBlock.TxShas 85.71% (6/7)
github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 85.71% (6/7) github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 85.71% (6/7)
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 85.71% (6/7)
github.com/conformal/btcwire/msgtx.go MsgTx.BtcDecode 80.00% (20/25) github.com/conformal/btcwire/msgtx.go MsgTx.BtcDecode 80.00% (20/25)
github.com/conformal/btcwire/shahash.go NewShaHash 80.00% (4/5) 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 MsgTx.BtcEncode 78.26% (18/23)
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 76.92% (10/13) 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 readTxIn 76.47% (13/17)
github.com/conformal/btcwire/msgtx.go readTxOut 75.00% (9/12) github.com/conformal/btcwire/msgtx.go readTxOut 75.00% (9/12)
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/blockheader.go BlockHeader.BlockSha 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/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 readOutPoint 75.00% (3/4)
github.com/conformal/btcwire/msgtx.go writeOutPoint 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 writeTxIn 73.33% (11/15)
github.com/conformal/btcwire/msgtx.go writeTxOut 72.73% (8/11) 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/message.go WriteMessage 70.00% (21/30)
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 69.23% (9/13)
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcDecode 68.42% (13/19) 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/msggetblocks.go MsgGetBlocks.BtcDecode 68.42% (13/19)
github.com/conformal/btcwire/msgversion.go MsgVersion.BtcDecode 68.00% (17/25) 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/msggetblocks.go MsgGetBlocks.BtcEncode 66.67% (12/18)
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 66.67% (8/12) 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/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.BtcDecode 62.50% (10/16)
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcEncode 60.00% (9/15)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 60.00% (9/15) 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/msgblock.go MsgBlock.BtcDecodeTxLoc 0.00% (0/16)
github.com/conformal/btcwire --------------------------------- 85.96% (802/933) github.com/conformal/btcwire --------------------------------- 86.82% (810/933)