1 // Copyright (c) 2014-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
14 // TestFilterCLearLatest tests the MsgFilterLoad API against the latest protocol
16 func TestFilterLoadLatest(t *testing.T) {
17 pver := ProtocolVersion
20 data := []byte{0x01, 0x02}
21 msg := NewMsgFilterLoad(data, 10, 0, 0)
23 // Ensure the command is expected value.
24 wantCmd := "filterload"
25 if cmd := msg.Command(); cmd != wantCmd {
26 t.Errorf("NewMsgFilterLoad: wrong command - got %v want %v",
30 // Ensure max payload is expected value for latest protocol version.
31 wantPayload := uint32(36012)
32 maxPayload := msg.MaxPayloadLength(pver)
33 if maxPayload != wantPayload {
34 t.Errorf("MaxPayLoadLength: wrong max payload length for "+
35 "protocol version %d - got %v, want %v", pver,
36 maxPayload, wantPayload)
39 // Test encode with latest protocol version.
41 err := msg.BtcEncode(&buf, pver, enc)
43 t.Errorf("encode of MsgFilterLoad failed %v err <%v>", msg, err)
46 // Test decode with latest protocol version.
47 readmsg := MsgFilterLoad{}
48 err = readmsg.BtcDecode(&buf, pver, enc)
50 t.Errorf("decode of MsgFilterLoad failed [%v] err <%v>", buf, err)
54 // TestFilterLoadCrossProtocol tests the MsgFilterLoad API when encoding with
55 // the latest protocol version and decoding with BIP0031Version.
56 func TestFilterLoadCrossProtocol(t *testing.T) {
57 data := []byte{0x01, 0x02}
58 msg := NewMsgFilterLoad(data, 10, 0, 0)
60 // Encode with latest protocol version.
62 err := msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
64 t.Errorf("encode of NewMsgFilterLoad failed %v err <%v>", msg,
68 // Decode with old protocol version.
69 var readmsg MsgFilterLoad
70 err = readmsg.BtcDecode(&buf, BIP0031Version, BaseEncoding)
72 t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
77 // TestFilterLoadMaxFilterSize tests the MsgFilterLoad API maximum filter size.
78 func TestFilterLoadMaxFilterSize(t *testing.T) {
79 data := bytes.Repeat([]byte{0xff}, 36001)
80 msg := NewMsgFilterLoad(data, 10, 0, 0)
82 // Encode with latest protocol version.
84 err := msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
86 t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't "+
90 // Decode with latest protocol version.
91 readbuf := bytes.NewReader(data)
92 err = msg.BtcDecode(readbuf, ProtocolVersion, BaseEncoding)
94 t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't "+
99 // TestFilterLoadMaxHashFuncsSize tests the MsgFilterLoad API maximum hash functions.
100 func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
101 data := bytes.Repeat([]byte{0xff}, 10)
102 msg := NewMsgFilterLoad(data, 61, 0, 0)
104 // Encode with latest protocol version.
106 err := msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
108 t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't have %v",
114 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // filter
115 0x3d, 0x00, 0x00, 0x00, // max hash funcs
116 0x00, 0x00, 0x00, 0x00, // tweak
119 // Decode with latest protocol version.
120 readbuf := bytes.NewReader(newBuf)
121 err = msg.BtcDecode(readbuf, ProtocolVersion, BaseEncoding)
123 t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
128 // TestFilterLoadWireErrors performs negative tests against wire encode and decode
129 // of MsgFilterLoad to confirm error paths work correctly.
130 func TestFilterLoadWireErrors(t *testing.T) {
131 pver := ProtocolVersion
132 pverNoFilterLoad := BIP0037Version - 1
133 wireErr := &MessageError{}
135 baseFilter := []byte{0x01, 0x02, 0x03, 0x04}
136 baseFilterLoad := NewMsgFilterLoad(baseFilter, 10, 0, BloomUpdateNone)
137 baseFilterLoadEncoded := append([]byte{0x04}, baseFilter...)
138 baseFilterLoadEncoded = append(baseFilterLoadEncoded,
139 0x00, 0x00, 0x00, 0x0a, // HashFuncs
140 0x00, 0x00, 0x00, 0x00, // Tweak
144 in *MsgFilterLoad // Value to encode
145 buf []byte // Wire encoding
146 pver uint32 // Protocol version for wire encoding
147 enc MessageEncoding // Message encoding format
148 max int // Max size of fixed buffer to induce errors
149 writeErr error // Expected write error
150 readErr error // Expected read error
152 // Latest protocol version with intentional read/write errors.
153 // Force error in filter size.
155 baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 0,
156 io.ErrShortWrite, io.EOF,
158 // Force error in filter.
160 baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 1,
161 io.ErrShortWrite, io.EOF,
163 // Force error in hash funcs.
165 baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 5,
166 io.ErrShortWrite, io.EOF,
168 // Force error in tweak.
170 baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 9,
171 io.ErrShortWrite, io.EOF,
173 // Force error in flags.
175 baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 13,
176 io.ErrShortWrite, io.EOF,
178 // Force error due to unsupported protocol version.
180 baseFilterLoad, baseFilterLoadEncoded, pverNoFilterLoad, BaseEncoding,
181 10, wireErr, wireErr,
185 t.Logf("Running %d tests", len(tests))
186 for i, test := range tests {
187 // Encode to wire format.
188 w := newFixedWriter(test.max)
189 err := test.in.BtcEncode(w, test.pver, test.enc)
190 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
191 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
192 i, err, test.writeErr)
196 // For errors which are not of type MessageError, check them for
198 if _, ok := err.(*MessageError); !ok {
199 if err != test.writeErr {
200 t.Errorf("BtcEncode #%d wrong error got: %v, "+
201 "want: %v", i, err, test.writeErr)
206 // Decode from wire format.
207 var msg MsgFilterLoad
208 r := newFixedReader(test.max, test.buf)
209 err = msg.BtcDecode(r, test.pver, test.enc)
210 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
211 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
212 i, err, test.readErr)
216 // For errors which are not of type MessageError, check them for
218 if _, ok := err.(*MessageError); !ok {
219 if err != test.readErr {
220 t.Errorf("BtcDecode #%d wrong error got: %v, "+
221 "want: %v", i, err, test.readErr)