msgfilteradd_test.go raw
1 package wire
2
3 import (
4 "bytes"
5 "io"
6 "reflect"
7 "testing"
8 )
9
10 // TestFilterAddLatest tests the MsgFilterAdd API against the latest protocol version.
11 func TestFilterAddLatest(t *testing.T) {
12 enc := BaseEncoding
13 pver := ProtocolVersion
14 data := []byte{0x01, 0x02}
15 msg := NewMsgFilterAdd(data)
16 // Ensure the command is expected value.
17 wantCmd := "filteradd"
18 if cmd := msg.Command(); cmd != wantCmd {
19 t.Errorf("NewMsgFilterAdd: wrong command - got %v want %v",
20 cmd, wantCmd,
21 )
22 }
23 // Ensure max payload is expected value for latest protocol version.
24 wantPayload := uint32(523)
25 maxPayload := msg.MaxPayloadLength(pver)
26 if maxPayload != wantPayload {
27 t.Errorf("MaxPayloadLength: wrong max payload length for "+
28 "protocol version %d - got %v, want %v", pver,
29 maxPayload, wantPayload,
30 )
31 }
32 // Test encode with latest protocol version.
33 var buf bytes.Buffer
34 e := msg.BtcEncode(&buf, pver, enc)
35 if e != nil {
36 t.Errorf("encode of MsgFilterAdd failed %v e <%v>", msg, e)
37 }
38 // Test decode with latest protocol version.
39 var readmsg MsgFilterAdd
40 e = readmsg.BtcDecode(&buf, pver, enc)
41 if e != nil {
42 t.Errorf("decode of MsgFilterAdd failed [%v] e <%v>", buf, e)
43 }
44 }
45
46 // TestFilterAddCrossProtocol tests the MsgFilterAdd API when encoding with the latest protocol version and decoding
47 // with BIP0031Version.
48 func TestFilterAddCrossProtocol(t *testing.T) {
49 data := []byte{0x01, 0x02}
50 msg := NewMsgFilterAdd(data)
51 if !bytes.Equal(msg.Data, data) {
52 t.Errorf("should get same data back out")
53 }
54 // Encode with latest protocol version.
55 var buf bytes.Buffer
56 e := msg.BtcEncode(&buf, ProtocolVersion, LatestEncoding)
57 if e != nil {
58 t.Errorf("encode of MsgFilterAdd failed %v e <%v>", msg, e)
59 }
60 // Decode with old protocol version.
61 var readmsg MsgFilterAdd
62 e = readmsg.BtcDecode(&buf, BIP0031Version, LatestEncoding)
63 if e == nil {
64 t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+
65 "have %v", msg,
66 )
67 }
68 // Since one of the protocol versions doesn't support the filteradd message, make sure the data didn't get encoded
69 // and decoded back out.
70 if bytes.Equal(msg.Data, readmsg.Data) {
71 t.Error("should not get same data for cross protocol")
72 }
73 }
74
75 // TestFilterAddMaxDataSize tests the MsgFilterAdd API maximum data size.
76 func TestFilterAddMaxDataSize(t *testing.T) {
77 data := bytes.Repeat([]byte{0xff}, 521)
78 msg := NewMsgFilterAdd(data)
79 // Encode with latest protocol version.
80 var buf bytes.Buffer
81 e := msg.BtcEncode(&buf, ProtocolVersion, LatestEncoding)
82 if e == nil {
83 t.Errorf("encode of MsgFilterAdd succeeded when it shouldn't "+
84 "have %v", msg,
85 )
86 }
87 // Decode with latest protocol version.
88 readbuf := bytes.NewReader(data)
89 e = msg.BtcDecode(readbuf, ProtocolVersion, LatestEncoding)
90 if e == nil {
91 t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+
92 "have %v", msg,
93 )
94 }
95 }
96
97 // TestFilterAddWireErrors performs negative tests against wire encode and decode of MsgFilterAdd to confirm error paths
98 // work correctly.
99 func TestFilterAddWireErrors(t *testing.T) {
100 pver := ProtocolVersion
101 pverNoFilterAdd := BIP0037Version - 1
102 wireErr := &MessageError{}
103 baseData := []byte{0x01, 0x02, 0x03, 0x04}
104 baseFilterAdd := NewMsgFilterAdd(baseData)
105 baseFilterAddEncoded := append([]byte{0x04}, baseData...)
106 tests := []struct {
107 in *MsgFilterAdd // value to encode
108 buf []byte // Wire encoding
109 pver uint32 // Protocol version for wire encoding
110 enc MessageEncoding // Message encoding format
111 max int // Max size of fixed buffer to induce errors
112 writeErr error // Expected write error
113 readErr error // Expected read error
114 }{
115 // Latest protocol version with intentional read/write errors. Force error in data size.
116 {
117 baseFilterAdd, baseFilterAddEncoded, pver, BaseEncoding, 0,
118 io.ErrShortWrite, io.EOF,
119 },
120 // Force error in data.
121 {
122 baseFilterAdd, baseFilterAddEncoded, pver, BaseEncoding, 1,
123 io.ErrShortWrite, io.EOF,
124 },
125 // Force error due to unsupported protocol version.
126 {
127 baseFilterAdd, baseFilterAddEncoded, pverNoFilterAdd, BaseEncoding, 5,
128 wireErr, wireErr,
129 },
130 }
131 t.Logf("Running %d tests", len(tests))
132 for i, test := range tests {
133 // Encode to wire format.
134 w := newFixedWriter(test.max)
135 e := test.in.BtcEncode(w, test.pver, test.enc)
136 if reflect.TypeOf(e) != reflect.TypeOf(test.writeErr) {
137 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
138 i, e, test.writeErr,
139 )
140 continue
141 }
142 // For errors which are not of type MessageError, check them for equality.
143 if _, ok := e.(*MessageError); !ok {
144 if e != test.writeErr {
145 t.Errorf("BtcEncode #%d wrong error got: %v, "+
146 "want: %v", i, e, test.writeErr,
147 )
148 continue
149 }
150 }
151 // Decode from wire format.
152 var msg MsgFilterAdd
153 r := newFixedReader(test.max, test.buf)
154 e = msg.BtcDecode(r, test.pver, test.enc)
155 if reflect.TypeOf(e) != reflect.TypeOf(test.readErr) {
156 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
157 i, e, test.readErr,
158 )
159 continue
160 }
161 // For errors which are not of type MessageError, check them for equality.
162 if _, ok := e.(*MessageError); !ok {
163 if e != test.readErr {
164 t.Errorf("BtcDecode #%d wrong error got: %v, "+
165 "want: %v", i, e, test.readErr,
166 )
167 continue
168 }
169 }
170 }
171 }
172