Add negative tests for Serialize/Deserialize funcs.

This commit adds tests for the error paths when serializing and
deserializing MsgBlock and MsgTx.
This commit is contained in:
Dave Collins 2013-08-05 11:50:41 -05:00
parent 29eb4d4250
commit dea1bac359
3 changed files with 169 additions and 48 deletions

View file

@ -358,6 +358,66 @@ func TestBlockSerialize(t *testing.T) {
}
}
// TestBlockSerializeErrors performs negative tests against wire encode and
// decode of MsgBlock to confirm error paths work correctly.
func TestBlockSerializeErrors(t *testing.T) {
tests := []struct {
in *btcwire.MsgBlock // Value to encode
buf []byte // Serialized data
max int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
}{
// Force error in version.
{&blockOne, blockOneBytes, 0, io.ErrShortWrite, io.EOF},
// Force error in prev block hash.
{&blockOne, blockOneBytes, 4, io.ErrShortWrite, io.EOF},
// Force error in merkle root.
{&blockOne, blockOneBytes, 36, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
{&blockOne, blockOneBytes, 68, io.ErrShortWrite, io.EOF},
// Force error in difficulty bits.
{&blockOne, blockOneBytes, 72, io.ErrShortWrite, io.EOF},
// Force error in header nonce.
{&blockOne, blockOneBytes, 76, io.ErrShortWrite, io.EOF},
// Force error in transaction count.
{&blockOne, blockOneBytes, 80, io.ErrShortWrite, io.EOF},
// Force error in transactions.
{&blockOne, blockOneBytes, 81, io.ErrShortWrite, io.EOF},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Serialize the block.
w := newFixedWriter(test.max)
err := test.in.Serialize(w)
if err != test.writeErr {
t.Errorf("Serialize #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
continue
}
// Deserialize the block.
var block btcwire.MsgBlock
r := newFixedReader(test.max, test.buf)
err = block.Deserialize(r)
if err != test.readErr {
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
i, err, test.readErr)
continue
}
var txLocBlock btcwire.MsgBlock
rbuf := bytes.NewBuffer(test.buf[0:test.max])
_, err = txLocBlock.DeserializeTxLoc(rbuf)
if err != test.readErr {
t.Errorf("DeserializeTxLoc #%d wrong error got: %v, want: %v",
i, err, test.readErr)
continue
}
}
}
var blockOne = btcwire.MsgBlock{
Header: btcwire.BlockHeader{
Version: 1,

View file

@ -432,6 +432,67 @@ func TestTxSerialize(t *testing.T) {
}
}
// TestTxSerializeErrors performs negative tests against wire encode and decode
// of MsgTx to confirm error paths work correctly.
func TestTxSerializeErrors(t *testing.T) {
tests := []struct {
in *btcwire.MsgTx // Value to encode
buf []byte // Serialized data
max int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
}{
// Force error in version.
{multiTx, multiTxEncoded, 0, io.ErrShortWrite, io.EOF},
// Force error in number of transaction inputs.
{multiTx, multiTxEncoded, 4, io.ErrShortWrite, io.EOF},
// Force error in transaction input previous block hash.
{multiTx, multiTxEncoded, 5, io.ErrShortWrite, io.EOF},
// Force error in transaction input previous block hash.
{multiTx, multiTxEncoded, 5, io.ErrShortWrite, io.EOF},
// Force error in transaction input previous block output index.
{multiTx, multiTxEncoded, 37, io.ErrShortWrite, io.EOF},
// Force error in transaction input signature script length.
{multiTx, multiTxEncoded, 41, io.ErrShortWrite, io.EOF},
// Force error in transaction input signature script.
{multiTx, multiTxEncoded, 42, io.ErrShortWrite, io.EOF},
// Force error in transaction input sequence.
{multiTx, multiTxEncoded, 49, io.ErrShortWrite, io.EOF},
// Force error in number of transaction outputs.
{multiTx, multiTxEncoded, 53, io.ErrShortWrite, io.EOF},
// Force error in transaction output value.
{multiTx, multiTxEncoded, 54, io.ErrShortWrite, io.EOF},
// Force error in transaction output pk script length.
{multiTx, multiTxEncoded, 62, io.ErrShortWrite, io.EOF},
// Force error in transaction output pk script.
{multiTx, multiTxEncoded, 63, io.ErrShortWrite, io.EOF},
// Force error in transaction output lock time.
{multiTx, multiTxEncoded, 130, io.ErrShortWrite, io.EOF},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Serialize the transaction.
w := newFixedWriter(test.max)
err := test.in.Serialize(w)
if err != test.writeErr {
t.Errorf("Serialize #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
continue
}
// Deserialize the transaction.
var tx btcwire.MsgTx
r := newFixedReader(test.max, test.buf)
err = tx.Deserialize(r)
if err != test.readErr {
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
i, err, test.readErr)
continue
}
}
}
// multiTx is a MsgTx with an input and output and used in various tests.
var multiTx = &btcwire.MsgTx{
Version: 1,

View file

@ -19,91 +19,91 @@ 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/msgblock.go MsgBlock.BtcDecodeTxLoc 100.00% (15/15)
github.com/conformal/btcwire/msgblock.go MsgBlock.DeserializeTxLoc 100.00% (15/15)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 100.00% (15/15)
github.com/conformal/btcwire/msgtx.go writeTxIn 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/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msgtx.go readTxOut 100.00% (12/12)
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/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13)
github.com/conformal/btcwire/msginv.go MsgInv.BtcEncode 100.00% (12/12)
github.com/conformal/btcwire/protocol.go ServiceFlag.String 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/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12)
github.com/conformal/btcwire/msgtx.go readTxOut 100.00% (12/12)
github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12)
github.com/conformal/btcwire/msgtx.go writeTxOut 100.00% (11/11)
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/blockheader.go readBlockHeader 100.00% (10/10)
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/msgalert.go MsgAlert.BtcDecode 100.00% (8/8)
github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8)
github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 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/message.go readMessageHeader 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/msgpong.go MsgPong.BtcDecode 100.00% (7/7)
github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7)
github.com/conformal/btcwire/message.go readMessageHeader 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/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5)
github.com/conformal/btcwire/shahash.go NewShaHash 100.00% (5/5)
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 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/blockheader.go BlockHeader.BlockSha 100.00% (5/5)
github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5)
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 100.00% (5/5)
github.com/conformal/btcwire/msgblock.go MsgBlock.TxShas 100.00% (5/5)
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5)
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5)
github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5)
github.com/conformal/btcwire/shahash.go NewShaHash 100.00% (5/5)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5)
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5)
github.com/conformal/btcwire/common.go readElements 100.00% (5/5)
github.com/conformal/btcwire/common.go writeElements 100.00% (5/5)
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 100.00% (5/5)
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 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/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 100.00% (5/5)
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5)
github.com/conformal/btcwire/msgtx.go writeOutPoint 100.00% (4/4)
github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4)
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcEncode 100.00% (4/4)
github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4)
github.com/conformal/btcwire/msgtx.go readOutPoint 100.00% (4/4)
github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4)
github.com/conformal/btcwire/invvect.go writeInvVect 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/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/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4)
github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4)
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/msgtx.go writeOutPoint 100.00% (4/4)
github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4)
github.com/conformal/btcwire/msgtx.go readOutPoint 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/msgaddr.go MsgAddr.MaxPayloadLength 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/shahash.go ShaHash.Bytes 100.00% (3/3)
github.com/conformal/btcwire/netaddress.go NetAddress.SetAddress 100.00% (2/2)
github.com/conformal/btcwire/invvect.go InvType.String 100.00% (3/3)
github.com/conformal/btcwire/msgblock.go MsgBlock.ClearTransactions 100.00% (2/2)
github.com/conformal/btcwire/msginv.go MsgInv.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/netaddress.go NetAddress.SetAddress 100.00% (2/2)
github.com/conformal/btcwire/msginv.go NewMsgInv 100.00% (1/1)
github.com/conformal/btcwire/msggetheaders.go NewMsgGetHeaders 100.00% (1/1)
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msggetdata.go NewMsgGetData 100.00% (1/1)
github.com/conformal/btcwire/msgmempool.go MsgMemPool.Command 100.00% (1/1)
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.Command 100.00% (1/1)
github.com/conformal/btcwire/msgmempool.go MsgMemPool.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgmempool.go NewMsgMemPool 100.00% (1/1)
github.com/conformal/btcwire/msggetdata.go NewMsgGetData 100.00% (1/1)
github.com/conformal/btcwire/msggetdata.go MsgGetData.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msggetdata.go MsgGetData.Command 100.00% (1/1)
github.com/conformal/btcwire/msggetblocks.go NewMsgGetBlocks 100.00% (1/1)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.Command 100.00% (1/1)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgnotfound.go NewMsgNotFound 100.00% (1/1)
github.com/conformal/btcwire/msggetblocks.go NewMsgGetBlocks 100.00% (1/1)
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.Command 100.00% (1/1)
github.com/conformal/btcwire/msgping.go MsgPing.Command 100.00% (1/1)
github.com/conformal/btcwire/common.go RandomUint64 100.00% (1/1)
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.Command 100.00% (1/1)
github.com/conformal/btcwire/msgping.go NewMsgPing 100.00% (1/1)
github.com/conformal/btcwire/common.go RandomUint64 100.00% (1/1)
github.com/conformal/btcwire/msggetaddr.go NewMsgGetAddr 100.00% (1/1)
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.Command 100.00% (1/1)
github.com/conformal/btcwire/msgpong.go MsgPong.Command 100.00% (1/1)
github.com/conformal/btcwire/msggetaddr.go MsgGetAddr.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgpong.go NewMsgPong 100.00% (1/1)
@ -132,27 +132,27 @@ github.com/conformal/btcwire/msgverack.go MsgVerAck.BtcEncode 100.00% (1/1)
github.com/conformal/btcwire/msgverack.go MsgVerAck.Command 100.00% (1/1)
github.com/conformal/btcwire/msgverack.go MsgVerAck.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgverack.go NewMsgVerAck 100.00% (1/1)
github.com/conformal/btcwire/msgaddr.go MsgAddr.ClearAddresses 100.00% (1/1)
github.com/conformal/btcwire/error.go messageError 100.00% (1/1)
github.com/conformal/btcwire/msgversion.go MsgVersion.AddService 100.00% (1/1)
github.com/conformal/btcwire/msgblock.go MsgBlock.Serialize 100.00% (1/1)
github.com/conformal/btcwire/shahash.go ShaHash.IsEqual 100.00% (1/1)
github.com/conformal/btcwire/msgversion.go MsgVersion.Command 100.00% (1/1)
github.com/conformal/btcwire/msgblock.go MsgBlock.Deserialize 100.00% (1/1)
github.com/conformal/btcwire/msgversion.go NewMsgVersion 100.00% (1/1)
github.com/conformal/btcwire/shahash.go ShaHash.IsEqual 100.00% (1/1)
github.com/conformal/btcwire/error.go messageError 100.00% (1/1)
github.com/conformal/btcwire/msgalert.go NewMsgAlert 100.00% (1/1)
github.com/conformal/btcwire/msgmempool.go MsgMemPool.Command 100.00% (1/1)
github.com/conformal/btcwire/netaddress.go NetAddress.AddService 100.00% (1/1)
github.com/conformal/btcwire/msggetheaders.go NewMsgGetHeaders 100.00% (1/1)
github.com/conformal/btcwire/msginv.go MsgInv.Command 100.00% (1/1)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.Command 100.00% (1/1)
github.com/conformal/btcwire/msgalert.go MsgAlert.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/blockheader.go NewBlockHeader 100.00% (1/1)
github.com/conformal/btcwire/msgalert.go MsgAlert.Command 100.00% (1/1)
github.com/conformal/btcwire/msgaddr.go NewMsgAddr 100.00% (1/1)
github.com/conformal/btcwire/msgaddr.go MsgAddr.Command 100.00% (1/1)
github.com/conformal/btcwire/msgalert.go NewMsgAlert 100.00% (1/1)
github.com/conformal/btcwire/invvect.go NewInvVect 100.00% (1/1)
github.com/conformal/btcwire/msgaddr.go MsgAddr.ClearAddresses 100.00% (1/1)
github.com/conformal/btcwire/msgheaders.go NewMsgHeaders 100.00% (1/1)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgblock.go MsgBlock.DeserializeTxLoc 86.67% (13/15)
github.com/conformal/btcwire --------------------------------- 99.79% (943/945)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.Command 100.00% (1/1)
github.com/conformal/btcwire/msginv.go MsgInv.Command 100.00% (1/1)
github.com/conformal/btcwire/msginv.go MsgInv.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire --------------------------------- 100.00% (945/945)