-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmsg_get_cfilters_test.go
More file actions
78 lines (62 loc) · 2.22 KB
/
msg_get_cfilters_test.go
File metadata and controls
78 lines (62 loc) · 2.22 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
package wire
import (
"bytes"
"io"
"testing"
"github.com/bsv-blockchain/go-bt/v2/chainhash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestNewMsgGetCFiltersDefaultValues tests the creation of a MsgGetCFilters
func TestNewMsgGetCFiltersDefaultValues(t *testing.T) {
pver := ProtocolVersion
stopHash, err := chainhash.NewHashFromStr("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
require.NoError(t, err)
msg := NewMsgGetCFilters(GCSFilterRegular, 10, stopHash)
require.Equal(t, GCSFilterRegular, msg.FilterType)
require.Equal(t, uint32(10), msg.StartHeight)
require.True(t, msg.StopHash.IsEqual(stopHash))
assert.Equal(t, CmdGetCFilters, msg.Command())
assert.Equal(t, uint64(1+4+chainhash.HashSize), msg.MaxPayloadLength(pver))
}
// TestMsgGetCFiltersEncodeDecode tests the encoding and decoding of MsgGetCFilters
func TestMsgGetCFiltersEncodeDecode(t *testing.T) {
pver := ProtocolVersion
stopHash := chainhash.Hash{}
msg := NewMsgGetCFilters(GCSFilterRegular, 5, &stopHash)
var buf bytes.Buffer
require.NoError(t, msg.BsvEncode(&buf, pver, BaseEncoding))
var decoded MsgGetCFilters
require.NoError(t, decoded.Bsvdecode(bytes.NewReader(buf.Bytes()), pver, BaseEncoding))
assert.Equal(t, msg, &decoded)
}
// TestMsgGetCFiltersEncodeDecodeErrors tests the error handling during encoding and decoding
func TestMsgGetCFiltersEncodeDecodeErrors(t *testing.T) {
pver := ProtocolVersion
stopHash := chainhash.Hash{}
msg := NewMsgGetCFilters(GCSFilterRegular, 1, &stopHash)
var good bytes.Buffer
require.NoError(t, msg.BsvEncode(&good, pver, BaseEncoding))
encoded := good.Bytes()
tests := []struct {
name string
max int
writeErr error
readErr error
}{
{"short filter type", 0, io.ErrShortWrite, io.EOF},
{"short start height", 1, io.ErrShortWrite, io.EOF},
{"short stop hash", 5, io.ErrShortWrite, io.EOF},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := newFixedWriter(tt.max)
err := msg.BsvEncode(w, pver, BaseEncoding)
require.ErrorIs(t, err, tt.writeErr)
r := newFixedReader(tt.max, encoded)
var dec MsgGetCFilters
err = dec.Bsvdecode(r, pver, BaseEncoding)
require.ErrorIs(t, err, tt.readErr)
})
}
}