mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-1128] Change message command to uint32 * [NOD-1128] Don't use iota * [NOD-1128] Remove redundant line
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package wire
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/kaspanet/kaspad/util/daghash"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
)
|
|
|
|
// TestSelectedTip tests the MsgSelectedTip API.
|
|
func TestSelectedTip(t *testing.T) {
|
|
pver := ProtocolVersion
|
|
|
|
// Ensure the command is expected value.
|
|
wantCmd := MessageCommand(20)
|
|
msg := NewMsgSelectedTip(&daghash.ZeroHash)
|
|
if cmd := msg.Command(); cmd != wantCmd {
|
|
t.Errorf("NewMsgSelectedTip: wrong command - got %v want %v",
|
|
cmd, wantCmd)
|
|
}
|
|
|
|
// Ensure max payload is expected value.
|
|
wantPayload := uint32(32)
|
|
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)
|
|
}
|
|
}
|
|
|
|
// TestSelectedTipWire tests the MsgSelectedTip wire encode and decode for various
|
|
// protocol versions.
|
|
func TestSelectedTipWire(t *testing.T) {
|
|
hash := &daghash.Hash{1, 2, 3}
|
|
msgSelectedTip := NewMsgSelectedTip(hash)
|
|
msgSelectedTipEncoded := []byte{
|
|
0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
}
|
|
|
|
tests := []struct {
|
|
in *MsgSelectedTip // Message to encode
|
|
out *MsgSelectedTip // Expected decoded message
|
|
buf []byte // Wire encoding
|
|
pver uint32 // Protocol version for wire encoding
|
|
}{
|
|
// Latest protocol version.
|
|
{
|
|
msgSelectedTip,
|
|
msgSelectedTip,
|
|
msgSelectedTipEncoded,
|
|
ProtocolVersion,
|
|
},
|
|
}
|
|
|
|
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.KaspaEncode(&buf, test.pver)
|
|
if err != nil {
|
|
t.Errorf("KaspaEncode #%d error %v", i, err)
|
|
continue
|
|
}
|
|
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
t.Errorf("KaspaEncode #%d\n got: %s want: %s", i,
|
|
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
continue
|
|
}
|
|
|
|
// Decode the message from wire format.
|
|
var msg MsgSelectedTip
|
|
rbuf := bytes.NewReader(test.buf)
|
|
err = msg.KaspaDecode(rbuf, test.pver)
|
|
if err != nil {
|
|
t.Errorf("KaspaDecode #%d error %v", i, err)
|
|
continue
|
|
}
|
|
if !reflect.DeepEqual(&msg, test.out) {
|
|
t.Errorf("KaspaDecode #%d\n got: %s want: %s", i,
|
|
spew.Sdump(msg), spew.Sdump(test.out))
|
|
continue
|
|
}
|
|
}
|
|
}
|