github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgfilterload_test.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package wire_test 7 8 import ( 9 "bytes" 10 "io" 11 "reflect" 12 "testing" 13 14 "github.com/dashpay/godash/wire" 15 ) 16 17 // TestFilterCLearLatest tests the MsgFilterLoad API against the latest protocol 18 // version. 19 func TestFilterLoadLatest(t *testing.T) { 20 pver := wire.ProtocolVersion 21 22 data := []byte{0x01, 0x02} 23 msg := wire.NewMsgFilterLoad(data, 10, 0, 0) 24 25 // Ensure the command is expected value. 26 wantCmd := "filterload" 27 if cmd := msg.Command(); cmd != wantCmd { 28 t.Errorf("NewMsgFilterLoad: wrong command - got %v want %v", 29 cmd, wantCmd) 30 } 31 32 // Ensure max payload is expected value for latest protocol version. 33 wantPayload := uint32(36012) 34 maxPayload := msg.MaxPayloadLength(pver) 35 if maxPayload != wantPayload { 36 t.Errorf("MaxPayLoadLength: wrong max payload length for "+ 37 "protocol version %d - got %v, want %v", pver, 38 maxPayload, wantPayload) 39 } 40 41 // Test encode with latest protocol version. 42 var buf bytes.Buffer 43 err := msg.BtcEncode(&buf, pver) 44 if err != nil { 45 t.Errorf("encode of MsgFilterLoad failed %v err <%v>", msg, err) 46 } 47 48 // Test decode with latest protocol version. 49 readmsg := wire.MsgFilterLoad{} 50 err = readmsg.BtcDecode(&buf, pver) 51 if err != nil { 52 t.Errorf("decode of MsgFilterLoad failed [%v] err <%v>", buf, err) 53 } 54 55 return 56 } 57 58 // TestFilterLoadCrossProtocol tests the MsgFilterLoad API when encoding with 59 // the latest protocol version and decoding with BIP0031Version. 60 func TestFilterLoadCrossProtocol(t *testing.T) { 61 data := []byte{0x01, 0x02} 62 msg := wire.NewMsgFilterLoad(data, 10, 0, 0) 63 64 // Encode with latest protocol version. 65 var buf bytes.Buffer 66 err := msg.BtcEncode(&buf, wire.ProtocolVersion) 67 if err != nil { 68 t.Errorf("encode of NewMsgFilterLoad failed %v err <%v>", msg, 69 err) 70 } 71 72 // Decode with old protocol version. 73 var readmsg wire.MsgFilterLoad 74 err = readmsg.BtcDecode(&buf, wire.BIP0031Version) 75 if err == nil { 76 t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v", 77 msg) 78 } 79 } 80 81 // TestFilterLoadMaxFilterSize tests the MsgFilterLoad API maximum filter size. 82 func TestFilterLoadMaxFilterSize(t *testing.T) { 83 data := bytes.Repeat([]byte{0xff}, 36001) 84 msg := wire.NewMsgFilterLoad(data, 10, 0, 0) 85 86 // Encode with latest protocol version. 87 var buf bytes.Buffer 88 err := msg.BtcEncode(&buf, wire.ProtocolVersion) 89 if err == nil { 90 t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't "+ 91 "have %v", msg) 92 } 93 94 // Decode with latest protocol version. 95 readbuf := bytes.NewReader(data) 96 err = msg.BtcDecode(readbuf, wire.ProtocolVersion) 97 if err == nil { 98 t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't "+ 99 "have %v", msg) 100 } 101 } 102 103 // TestFilterLoadMaxHashFuncsSize tests the MsgFilterLoad API maximum hash functions. 104 func TestFilterLoadMaxHashFuncsSize(t *testing.T) { 105 data := bytes.Repeat([]byte{0xff}, 10) 106 msg := wire.NewMsgFilterLoad(data, 61, 0, 0) 107 108 // Encode with latest protocol version. 109 var buf bytes.Buffer 110 err := msg.BtcEncode(&buf, wire.ProtocolVersion) 111 if err == nil { 112 t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't have %v", 113 msg) 114 } 115 116 newBuf := []byte{ 117 0x0a, // filter size 118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // filter 119 0x3d, 0x00, 0x00, 0x00, // max hash funcs 120 0x00, 0x00, 0x00, 0x00, // tweak 121 0x00, // update Type 122 } 123 // Decode with latest protocol version. 124 readbuf := bytes.NewReader(newBuf) 125 err = msg.BtcDecode(readbuf, wire.ProtocolVersion) 126 if err == nil { 127 t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v", 128 msg) 129 } 130 } 131 132 // TestFilterLoadWireErrors performs negative tests against wire encode and decode 133 // of MsgFilterLoad to confirm error paths work correctly. 134 func TestFilterLoadWireErrors(t *testing.T) { 135 pver := wire.ProtocolVersion 136 pverNoFilterLoad := wire.BIP0037Version - 1 137 wireErr := &wire.MessageError{} 138 139 baseFilter := []byte{0x01, 0x02, 0x03, 0x04} 140 baseFilterLoad := wire.NewMsgFilterLoad(baseFilter, 10, 0, 141 wire.BloomUpdateNone) 142 baseFilterLoadEncoded := append([]byte{0x04}, baseFilter...) 143 baseFilterLoadEncoded = append(baseFilterLoadEncoded, 144 0x00, 0x00, 0x00, 0x0a, // HashFuncs 145 0x00, 0x00, 0x00, 0x00, // Tweak 146 0x00) // Flags 147 148 tests := []struct { 149 in *wire.MsgFilterLoad // Value to encode 150 buf []byte // Wire encoding 151 pver uint32 // Protocol version for wire encoding 152 max int // Max size of fixed buffer to induce errors 153 writeErr error // Expected write error 154 readErr error // Expected read error 155 }{ 156 // Latest protocol version with intentional read/write errors. 157 // Force error in filter size. 158 { 159 baseFilterLoad, baseFilterLoadEncoded, pver, 0, 160 io.ErrShortWrite, io.EOF, 161 }, 162 // Force error in filter. 163 { 164 baseFilterLoad, baseFilterLoadEncoded, pver, 1, 165 io.ErrShortWrite, io.EOF, 166 }, 167 // Force error in hash funcs. 168 { 169 baseFilterLoad, baseFilterLoadEncoded, pver, 5, 170 io.ErrShortWrite, io.EOF, 171 }, 172 // Force error in tweak. 173 { 174 baseFilterLoad, baseFilterLoadEncoded, pver, 9, 175 io.ErrShortWrite, io.EOF, 176 }, 177 // Force error in flags. 178 { 179 baseFilterLoad, baseFilterLoadEncoded, pver, 13, 180 io.ErrShortWrite, io.EOF, 181 }, 182 // Force error due to unsupported protocol version. 183 { 184 baseFilterLoad, baseFilterLoadEncoded, pverNoFilterLoad, 185 10, wireErr, wireErr, 186 }, 187 } 188 189 t.Logf("Running %d tests", len(tests)) 190 for i, test := range tests { 191 // Encode to wire format. 192 w := newFixedWriter(test.max) 193 err := test.in.BtcEncode(w, test.pver) 194 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { 195 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", 196 i, err, test.writeErr) 197 continue 198 } 199 200 // For errors which are not of type wire.MessageError, check 201 // them for equality. 202 if _, ok := err.(*wire.MessageError); !ok { 203 if err != test.writeErr { 204 t.Errorf("BtcEncode #%d wrong error got: %v, "+ 205 "want: %v", i, err, test.writeErr) 206 continue 207 } 208 } 209 210 // Decode from wire format. 211 var msg wire.MsgFilterLoad 212 r := newFixedReader(test.max, test.buf) 213 err = msg.BtcDecode(r, test.pver) 214 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { 215 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", 216 i, err, test.readErr) 217 continue 218 } 219 220 // For errors which are not of type wire.MessageError, check 221 // them for equality. 222 if _, ok := err.(*wire.MessageError); !ok { 223 if err != test.readErr { 224 t.Errorf("BtcDecode #%d wrong error got: %v, "+ 225 "want: %v", i, err, test.readErr) 226 continue 227 } 228 } 229 230 } 231 }