Add negative tests for MsgPing.

This commit add tests the error paths when encoding and decoding MsgPing.
This commit is contained in:
Dave Collins 2013-05-09 10:41:21 -05:00
parent bf5c0b58d8
commit 5e416389b1
2 changed files with 83 additions and 35 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"
) )
@ -192,3 +193,50 @@ func TestPingWire(t *testing.T) {
} }
} }
} }
// TestPingWireErrors performs negative tests against wire encode and decode
// of MsgPing to confirm error paths work correctly.
func TestPingWireErrors(t *testing.T) {
pver := btcwire.ProtocolVersion
tests := []struct {
in *btcwire.MsgPing // 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.
{
&btcwire.MsgPing{Nonce: 123123}, // 0x1e0f3
[]byte{0xf3, 0xe0, 0x01, 0x00},
pver,
2,
io.ErrShortWrite,
io.ErrUnexpectedEOF,
},
}
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 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.MsgPing
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, want :%v",
i, err, test.readErr)
continue
}
}
}

View file

@ -1,39 +1,41 @@
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/msgtx.go MsgTx.Copy 100.00% (24/24)
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/protocol.go ServiceFlag.String 100.00% (12/12) github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12)
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/common.go writeVarString 100.00% (7/7)
github.com/conformal/btcwire/common.go DoubleSha256 100.00% (7/7)
github.com/conformal/btcwire/common.go randomUint64 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/msgversion.go NewMsgVersionFromConn 100.00% (7/7) github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5) github.com/conformal/btcwire/common.go writeVarString 100.00% (7/7)
github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5)
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 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/common.go writeElements 100.00% (5/5)
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 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/msgaddr.go MsgAddr.AddAddress 100.00% (5/5)
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5) github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 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/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5) github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/common.go writeElements 100.00% (5/5)
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5)
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5)
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5)
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5) github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5)
github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5) github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 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/msgmempool.go MsgMemPool.BtcEncode 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/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.Bytes 100.00% (3/3) 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/shahash.go ShaHash.String 100.00% (4/4)
github.com/conformal/btcwire/msgversion.go MsgVersion.HasService 100.00% (3/3) github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcEncode 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/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4)
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/invvect.go InvType.String 100.00% (3/3)
github.com/conformal/btcwire/netaddress.go NetAddress.HasService 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.AddTransaction 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.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/msgverack.go MsgVerAck.BtcEncode 100.00% (1/1) github.com/conformal/btcwire/msgverack.go MsgVerAck.BtcEncode 100.00% (1/1)
github.com/conformal/btcwire/msgverack.go MsgVerAck.BtcDecode 100.00% (1/1) github.com/conformal/btcwire/msgverack.go MsgVerAck.BtcDecode 100.00% (1/1)
github.com/conformal/btcwire/common.go readElement 100.00% (1/1) github.com/conformal/btcwire/common.go readElement 100.00% (1/1)
@ -64,8 +66,12 @@ github.com/conformal/btcwire/msggetblocks.go NewMsgGetBlocks 100.00% (1/1)
github.com/conformal/btcwire/msggetdata.go MsgGetData.Command 100.00% (1/1) github.com/conformal/btcwire/msggetdata.go MsgGetData.Command 100.00% (1/1)
github.com/conformal/btcwire/msggetdata.go MsgGetData.MaxPayloadLength 100.00% (1/1) github.com/conformal/btcwire/msggetdata.go MsgGetData.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msggetdata.go NewMsgGetData 100.00% (1/1) github.com/conformal/btcwire/msggetdata.go NewMsgGetData 100.00% (1/1)
github.com/conformal/btcwire/msgverack.go MsgVerAck.MaxPayloadLength 100.00% (1/1) github.com/conformal/btcwire/msgversion.go NewMsgVersion 100.00% (1/1)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.Command 100.00% (1/1) github.com/conformal/btcwire/msgversion.go MsgVersion.MaxPayloadLength 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/msgversion.go MsgVersion.Command 100.00% (1/1)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.MaxPayloadLength 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/msgnotfound.go NewMsgNotFound 100.00% (1/1)
github.com/conformal/btcwire/msgping.go MsgPing.Command 100.00% (1/1) github.com/conformal/btcwire/msgping.go MsgPing.Command 100.00% (1/1)
@ -81,12 +87,6 @@ github.com/conformal/btcwire/msgverack.go MsgVerAck.Command 100.00% (1/1)
github.com/conformal/btcwire/msgtx.go MsgTx.Command 100.00% (1/1) github.com/conformal/btcwire/msgtx.go MsgTx.Command 100.00% (1/1)
github.com/conformal/btcwire/msgtx.go MsgTx.MaxPayloadLength 100.00% (1/1) github.com/conformal/btcwire/msgtx.go MsgTx.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgtx.go NewMsgTx 100.00% (1/1) github.com/conformal/btcwire/msgtx.go NewMsgTx 100.00% (1/1)
github.com/conformal/btcwire/msgversion.go NewMsgVersion 100.00% (1/1)
github.com/conformal/btcwire/msgversion.go MsgVersion.MaxPayloadLength 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/msgversion.go MsgVersion.Command 100.00% (1/1)
github.com/conformal/btcwire/msgheaders.go MsgHeaders.Command 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 MsgHeaders.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgheaders.go NewMsgHeaders 100.00% (1/1) github.com/conformal/btcwire/msgheaders.go NewMsgHeaders 100.00% (1/1)
@ -98,18 +98,18 @@ github.com/conformal/btcwire/msgmempool.go MsgMemPool.Command 100.00% (1/1)
github.com/conformal/btcwire/msgmempool.go MsgMemPool.MaxPayloadLength 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/msgmempool.go NewMsgMemPool 100.00% (1/1)
github.com/conformal/btcwire/msgverack.go NewMsgVerAck 100.00% (1/1) github.com/conformal/btcwire/msgverack.go NewMsgVerAck 100.00% (1/1)
github.com/conformal/btcwire/message.go makeEmptyMessage 94.44% (17/18) github.com/conformal/btcwire/msgverack.go MsgVerAck.MaxPayloadLength 100.00% (1/1)
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.Command 100.00% (1/1)
github.com/conformal/btcwire/message.go makeEmptyMessage 95.00% (19/20)
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 85.71% (6/7)
github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 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/msgpong.go MsgPong.BtcDecode 85.71% (6/7)
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 85.71% (6/7)
github.com/conformal/btcwire/netaddress.go readNetAddress 85.00% (17/20) github.com/conformal/btcwire/netaddress.go readNetAddress 85.00% (17/20)
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/blockheader.go readBlockHeader 80.00% (8/10)
github.com/conformal/btcwire/shahash.go NewShaHash 80.00% (4/5) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 80.00% (8/10)
github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 80.00% (4/5)
github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 80.00% (4/5)
github.com/conformal/btcwire/common.go readElements 80.00% (4/5) github.com/conformal/btcwire/common.go readElements 80.00% (4/5)
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/netaddress.go writeNetAddress 78.57% (11/14)
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 77.78% (7/9) github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 77.78% (7/9)
github.com/conformal/btcwire/msgtx.go readTxIn 76.47% (13/17) github.com/conformal/btcwire/msgtx.go readTxIn 76.47% (13/17)
@ -148,5 +148,5 @@ github.com/conformal/btcwire/message.go ReadMessage 57.14% (4/7)
github.com/conformal/btcwire/message.go readMessage 53.12% (17/32) github.com/conformal/btcwire/message.go readMessage 53.12% (17/32)
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/message.go discardInput 0.00% (0/10) github.com/conformal/btcwire/message.go discardInput 0.00% (0/10)
github.com/conformal/btcwire --------------------------------- 78.43% (731/932) github.com/conformal/btcwire --------------------------------- 78.69% (735/934)