-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmsg_send_cmpct_test.go
More file actions
102 lines (84 loc) · 2.65 KB
/
msg_send_cmpct_test.go
File metadata and controls
102 lines (84 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package wire
import (
"bytes"
"errors"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestNewMsgSendcmpctSetsFields verifies the constructor initializes
// the message with the given value and default version.
func TestNewMsgSendcmpctSetsFields(t *testing.T) {
msg := NewMsgSendcmpct(true)
assert.True(t, msg.SendCmpct)
assert.Equal(t, uint64(1), msg.Version)
}
// TestMsgSendcmpct_Command ensures the command string is correct.
func TestMsgSendcmpctCommand(t *testing.T) {
msg := NewMsgSendcmpct(false)
assert.Equal(t, CmdSendcmpct, msg.Command())
}
// TestMsgSendcmpctMaxPayloadLength checks the fixed payload size.
func TestMsgSendcmpctMaxPayloadLength(t *testing.T) {
msg := NewMsgSendcmpct(true)
assert.Equal(t, uint64(9), msg.MaxPayloadLength(ProtocolVersion))
}
// TestMsgSendcmpctEncodeDecode exercises encode/decode round trips.
func TestMsgSendcmpctEncodeDecode(t *testing.T) {
cases := []struct {
name string
sendCmpct bool
}{
{name: "sendcmpct true", sendCmpct: true},
{name: "sendcmpct false", sendCmpct: false},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
msg := NewMsgSendcmpct(tt.sendCmpct)
var b bytes.Buffer
require.NoError(t, msg.BsvEncode(&b, ProtocolVersion, BaseEncoding))
expected := make([]byte, 1, 9)
if tt.sendCmpct {
expected[0] = 0x01
}
expected = append(expected, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
assert.Equal(t, expected, b.Bytes())
var decoded MsgSendcmpct
require.NoError(t, decoded.Bsvdecode(&b, ProtocolVersion, BaseEncoding))
assert.Equal(t, msg, &decoded)
})
}
}
// TestMsgSendcmpctWireErrors covers error paths during encoding and decoding.
func TestMsgSendcmpctWireErrors(t *testing.T) {
base := NewMsgSendcmpct(true)
var buf bytes.Buffer
require.NoError(t, base.BsvEncode(&buf, ProtocolVersion, BaseEncoding))
encoded := buf.Bytes()
wireErr := &MessageError{}
tests := []struct {
name string
max int
writeErr error
readErr error
}{
{name: "short write bool", max: 0, writeErr: io.ErrShortWrite, readErr: io.EOF},
{name: "partial version", max: 5, writeErr: io.ErrShortWrite, readErr: io.ErrUnexpectedEOF},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := newFixedWriter(tt.max)
err := base.BsvEncode(w, ProtocolVersion, BaseEncoding)
require.ErrorIs(t, err, tt.writeErr)
r := newFixedReader(tt.max, encoded)
var msg MsgSendcmpct
err = msg.Bsvdecode(r, ProtocolVersion, BaseEncoding)
if errors.As(tt.readErr, &wireErr) {
assert.ErrorAs(t, err, &wireErr)
} else {
require.ErrorIs(t, err, tt.readErr)
}
})
}
}