2015-12-02 20:53:24 +01:00
|
|
|
// Copyright (c) 2016 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-08-08 18:42:54 +02:00
|
|
|
package wire
|
2015-12-02 20:53:24 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestSendHeaders tests the MsgSendHeaders API against the latest protocol
|
|
|
|
// version.
|
|
|
|
func TestSendHeaders(t *testing.T) {
|
2016-08-08 18:42:54 +02:00
|
|
|
pver := ProtocolVersion
|
2015-12-02 20:53:24 +01:00
|
|
|
|
|
|
|
// Ensure the command is expected value.
|
|
|
|
wantCmd := "sendheaders"
|
2016-08-08 18:42:54 +02:00
|
|
|
msg := NewMsgSendHeaders()
|
2015-12-02 20:53:24 +01:00
|
|
|
if cmd := msg.Command(); cmd != wantCmd {
|
|
|
|
t.Errorf("NewMsgSendHeaders: wrong command - got %v want %v",
|
|
|
|
cmd, wantCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure max payload is expected value.
|
|
|
|
wantPayload := uint32(0)
|
|
|
|
maxPayload := msg.MaxPayloadLength(pver)
|
|
|
|
if maxPayload != wantPayload {
|
|
|
|
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
|
|
|
"protocol version %d - got %v, want %v", pver,
|
|
|
|
maxPayload, wantPayload)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test encode with latest protocol version.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := msg.BtcEncode(&buf, pver)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("encode of MsgSendHeaders failed %v err <%v>", msg,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Older protocol versions should fail encode since message didn't
|
|
|
|
// exist yet.
|
2016-08-08 18:42:54 +02:00
|
|
|
oldPver := SendHeadersVersion - 1
|
2015-12-02 20:53:24 +01:00
|
|
|
err = msg.BtcEncode(&buf, oldPver)
|
|
|
|
if err == nil {
|
|
|
|
s := "encode of MsgSendHeaders passed for old protocol " +
|
|
|
|
"version %v err <%v>"
|
|
|
|
t.Errorf(s, msg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test decode with latest protocol version.
|
2016-08-08 18:42:54 +02:00
|
|
|
readmsg := NewMsgSendHeaders()
|
2015-12-02 20:53:24 +01:00
|
|
|
err = readmsg.BtcDecode(&buf, pver)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("decode of MsgSendHeaders failed [%v] err <%v>", buf,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Older protocol versions should fail decode since message didn't
|
|
|
|
// exist yet.
|
|
|
|
err = readmsg.BtcDecode(&buf, oldPver)
|
|
|
|
if err == nil {
|
|
|
|
s := "decode of MsgSendHeaders passed for old protocol " +
|
|
|
|
"version %v err <%v>"
|
|
|
|
t.Errorf(s, msg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendHeadersBIP0130 tests the MsgSendHeaders API against the protocol
|
|
|
|
// prior to version SendHeadersVersion.
|
|
|
|
func TestSendHeadersBIP0130(t *testing.T) {
|
|
|
|
// Use the protocol version just prior to SendHeadersVersion changes.
|
2016-08-08 18:42:54 +02:00
|
|
|
pver := SendHeadersVersion - 1
|
2015-12-02 20:53:24 +01:00
|
|
|
|
2016-08-08 18:42:54 +02:00
|
|
|
msg := NewMsgSendHeaders()
|
2015-12-02 20:53:24 +01:00
|
|
|
|
|
|
|
// Test encode with old protocol version.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := msg.BtcEncode(&buf, pver)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("encode of MsgSendHeaders succeeded when it should " +
|
|
|
|
"have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test decode with old protocol version.
|
2016-08-08 18:42:54 +02:00
|
|
|
readmsg := NewMsgSendHeaders()
|
2015-12-02 20:53:24 +01:00
|
|
|
err = readmsg.BtcDecode(&buf, pver)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("decode of MsgSendHeaders succeeded when it should " +
|
|
|
|
"have failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendHeadersCrossProtocol tests the MsgSendHeaders API when encoding with
|
|
|
|
// the latest protocol version and decoding with SendHeadersVersion.
|
|
|
|
func TestSendHeadersCrossProtocol(t *testing.T) {
|
2016-08-08 18:42:54 +02:00
|
|
|
msg := NewMsgSendHeaders()
|
2015-12-02 20:53:24 +01:00
|
|
|
|
|
|
|
// Encode with latest protocol version.
|
|
|
|
var buf bytes.Buffer
|
2016-08-08 18:42:54 +02:00
|
|
|
err := msg.BtcEncode(&buf, ProtocolVersion)
|
2015-12-02 20:53:24 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("encode of MsgSendHeaders failed %v err <%v>", msg,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode with old protocol version.
|
2016-08-08 18:42:54 +02:00
|
|
|
readmsg := NewMsgSendHeaders()
|
|
|
|
err = readmsg.BtcDecode(&buf, SendHeadersVersion)
|
2015-12-02 20:53:24 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("decode of MsgSendHeaders failed [%v] err <%v>", buf,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendHeadersWire tests the MsgSendHeaders wire encode and decode for
|
|
|
|
// various protocol versions.
|
|
|
|
func TestSendHeadersWire(t *testing.T) {
|
2016-08-08 18:42:54 +02:00
|
|
|
msgSendHeaders := NewMsgSendHeaders()
|
2015-12-02 20:53:24 +01:00
|
|
|
msgSendHeadersEncoded := []byte{}
|
|
|
|
|
|
|
|
tests := []struct {
|
2016-08-08 18:42:54 +02:00
|
|
|
in *MsgSendHeaders // Message to encode
|
|
|
|
out *MsgSendHeaders // Expected decoded message
|
|
|
|
buf []byte // Wire encoding
|
|
|
|
pver uint32 // Protocol version for wire encoding
|
2015-12-02 20:53:24 +01:00
|
|
|
}{
|
|
|
|
// Latest protocol version.
|
|
|
|
{
|
|
|
|
msgSendHeaders,
|
|
|
|
msgSendHeaders,
|
|
|
|
msgSendHeadersEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
ProtocolVersion,
|
2015-12-02 20:53:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version SendHeadersVersion+1
|
|
|
|
{
|
|
|
|
msgSendHeaders,
|
|
|
|
msgSendHeaders,
|
|
|
|
msgSendHeadersEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
SendHeadersVersion + 1,
|
2015-12-02 20:53:24 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version SendHeadersVersion
|
|
|
|
{
|
|
|
|
msgSendHeaders,
|
|
|
|
msgSendHeaders,
|
|
|
|
msgSendHeadersEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
SendHeadersVersion,
|
2015-12-02 20:53:24 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
// Encode the message to wire format.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := test.in.BtcEncode(&buf, test.pver)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("BtcEncode #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
|
|
t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the message from wire format.
|
2016-08-08 18:42:54 +02:00
|
|
|
var msg MsgSendHeaders
|
2015-12-02 20:53:24 +01:00
|
|
|
rbuf := bytes.NewReader(test.buf)
|
|
|
|
err = msg.BtcDecode(rbuf, test.pver)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("BtcDecode #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&msg, test.out) {
|
|
|
|
t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(msg), spew.Sdump(test.out))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|