Add negative tests for MsgGetBlocks.

This commit adds tests for the error paths when encoded and decoding
MsgGetBlocks.
This commit is contained in:
Dave Collins 2013-05-11 11:06:52 -05:00
parent 2a2745d0d3
commit 61b86b1bb5
2 changed files with 163 additions and 34 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"
) )
@ -258,3 +259,131 @@ func TestGetBlocksWire(t *testing.T) {
} }
} }
} }
// TestGetBlocksWireErrors performs negative tests against wire encode and
// decode of MsgGetBlocks to confirm error paths work correctly.
func TestGetBlocksWireErrors(t *testing.T) {
// Set protocol inside getheaders message. 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{}
// Block 99499 hash.
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
hashLocator, err := btcwire.NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 99500 hash.
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
hashLocator2, err := btcwire.NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 100000 hash.
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hashStop, err := btcwire.NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// MsgGetBlocks message with multiple block locators and a stop hash.
baseGetBlocks := btcwire.NewMsgGetBlocks(hashStop)
baseGetBlocks.ProtocolVersion = pver
baseGetBlocks.AddBlockLocatorHash(hashLocator2)
baseGetBlocks.AddBlockLocatorHash(hashLocator)
baseGetBlocksEncoded := []byte{
0x62, 0xea, 0x00, 0x00, // Protocol version 60002
0x02, // Varint for number of block locator hashes
0xe0, 0xde, 0x06, 0x44, 0x68, 0x13, 0x2c, 0x63,
0xd2, 0x20, 0xcc, 0x69, 0x12, 0x83, 0xcb, 0x65,
0xbc, 0xaa, 0xe4, 0x79, 0x94, 0xef, 0x9e, 0x7b,
0xad, 0xe7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 99500 hash
0x35, 0x75, 0x95, 0xb7, 0xf6, 0x8c, 0xb1, 0x60,
0xcc, 0xba, 0x2c, 0x9a, 0xc5, 0x42, 0x5f, 0xd9,
0x6f, 0x0a, 0x01, 0x3d, 0xc9, 0x7e, 0xc8, 0x40,
0x0f, 0x71, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 99499 hash
0x06, 0xe5, 0x33, 0xfd, 0x1a, 0xda, 0x86, 0x39,
0x1f, 0x3f, 0x6c, 0x34, 0x32, 0x04, 0xb0, 0xd2,
0x78, 0xd4, 0xaa, 0xec, 0x1c, 0x0b, 0x20, 0xaa,
0x27, 0xba, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // Hash stop
}
// Message that forces an error by having more than the max allowed
// block locator hashes.
maxGetBlocks := btcwire.NewMsgGetBlocks(hashStop)
for i := 0; i < btcwire.MaxBlockLocatorsPerMsg; i++ {
maxGetBlocks.AddBlockLocatorHash(&btcwire.GenesisHash)
}
maxGetBlocks.BlockLocatorHashes = append(maxGetBlocks.BlockLocatorHashes,
&btcwire.GenesisHash)
maxGetBlocksEncoded := []byte{
0x62, 0xea, 0x00, 0x00, // Protocol version 60002
0xfd, 0xf5, 0x01, // Varint for number of block loc hashes (501)
}
tests := []struct {
in *btcwire.MsgGetBlocks // 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.
{baseGetBlocks, baseGetBlocksEncoded, pver, 0, io.ErrShortWrite, io.EOF},
// Force error in block locator hash count.
{baseGetBlocks, baseGetBlocksEncoded, pver, 4, io.ErrShortWrite, io.EOF},
// Force error in block locator hashes.
{baseGetBlocks, baseGetBlocksEncoded, pver, 5, io.ErrShortWrite, io.EOF},
// Force error in stop hash.
{baseGetBlocks, baseGetBlocksEncoded, pver, 69, io.ErrShortWrite, io.EOF},
// Force error with greater than max block locator hashes.
{maxGetBlocks, maxGetBlocksEncoded, pver, 7, 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.MsgGetBlocks
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

@ -4,62 +4,66 @@ 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/common.go readVarInt 100.00% (24/24)
github.com/conformal/btcwire/netaddress.go readNetAddress 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/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/msggetheaders.go MsgGetHeaders.BtcEncode 100.00% (18/18)
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcEncode 100.00% (18/18)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 100.00% (16/16) 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/common.go writeVarInt 100.00% (16/16)
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcEncode 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/shahash.go NewShaHashFromStr 100.00% (15/15)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 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/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/msginv.go MsgInv.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13)
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/msginv.go MsgInv.BtcEncode 100.00% (12/12) 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/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.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/blockheader.go readBlockHeader 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/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/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 randomUint64 100.00% (7/7)
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/message.go readMessageHeader 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/msgpong.go MsgPong.BtcDecode 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/common.go writeVarString 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/msgpong.go MsgPong.BtcEncode 100.00% (7/7)
github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5) 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/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5)
github.com/conformal/btcwire/common.go writeElements 100.00% (5/5)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5)
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 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/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/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/msgping.go MsgPing.BtcDecode 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/msggetdata.go MsgGetData.AddInvVect 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/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5)
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/msgping.go MsgPing.MaxPayloadLength 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/msgmempool.go MsgMemPool.BtcDecode 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/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 readInvVect 100.00% (4/4)
github.com/conformal/btcwire/invvect.go writeInvVect 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/invvect.go InvType.String 100.00% (3/3)
github.com/conformal/btcwire/netaddress.go NetAddress.HasService 100.00% (3/3)
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/error.go MessageError.Error 100.00% (3/3)
github.com/conformal/btcwire/msgversion.go MsgVersion.HasService 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/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.SetAddress 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/msgblock.go MsgBlock.ClearTransactions 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)
@ -134,20 +138,16 @@ 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/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.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/blockheader.go BlockHeader.BlockSha 75.00% (6/8)
github.com/conformal/btcwire/msgtx.go readOutPoint 75.00% (3/4) 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/msgtx.go writeOutPoint 75.00% (3/4) 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 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/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/msgversion.go MsgVersion.BtcDecode 68.00% (17/25)
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/msgversion.go MsgVersion.BtcEncode 63.64% (14/22)
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 --------------------------------- 89.28% (833/933) github.com/conformal/btcwire --------------------------------- 91.85% (857/933)