github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgfilteradd_test.go (about) 1 // Copyright (c) 2014-2015 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 // TestFilterAddLatest tests the MsgFilterAdd API against the latest protocol 18 // version. 19 func TestFilterAddLatest(t *testing.T) { 20 pver := wire.ProtocolVersion 21 22 data := []byte{0x01, 0x02} 23 msg := wire.NewMsgFilterAdd(data) 24 25 // Ensure the command is expected value. 26 wantCmd := "filteradd" 27 if cmd := msg.Command(); cmd != wantCmd { 28 t.Errorf("NewMsgFilterAdd: 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(523) 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 MsgFilterAdd failed %v err <%v>", msg, err) 46 } 47 48 // Test decode with latest protocol version. 49 var readmsg wire.MsgFilterAdd 50 err = readmsg.BtcDecode(&buf, pver) 51 if err != nil { 52 t.Errorf("decode of MsgFilterAdd failed [%v] err <%v>", buf, err) 53 } 54 55 return 56 } 57 58 // TestFilterAddCrossProtocol tests the MsgFilterAdd API when encoding with the 59 // latest protocol version and decoding with BIP0031Version. 60 func TestFilterAddCrossProtocol(t *testing.T) { 61 data := []byte{0x01, 0x02} 62 msg := wire.NewMsgFilterAdd(data) 63 if !bytes.Equal(msg.Data, data) { 64 t.Errorf("should get same data back out") 65 } 66 67 // Encode with latest protocol version. 68 var buf bytes.Buffer 69 err := msg.BtcEncode(&buf, wire.ProtocolVersion) 70 if err != nil { 71 t.Errorf("encode of MsgFilterAdd failed %v err <%v>", msg, err) 72 } 73 74 // Decode with old protocol version. 75 var readmsg wire.MsgFilterAdd 76 err = readmsg.BtcDecode(&buf, wire.BIP0031Version) 77 if err == nil { 78 t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+ 79 "have %v", msg) 80 } 81 82 // Since one of the protocol versions doesn't support the filteradd 83 // message, make sure the data didn't get encoded and decoded back out. 84 if bytes.Equal(msg.Data, readmsg.Data) { 85 t.Error("should not get same data for cross protocol") 86 } 87 88 } 89 90 // TestFilterAddMaxDataSize tests the MsgFilterAdd API maximum data size. 91 func TestFilterAddMaxDataSize(t *testing.T) { 92 data := bytes.Repeat([]byte{0xff}, 521) 93 msg := wire.NewMsgFilterAdd(data) 94 95 // Encode with latest protocol version. 96 var buf bytes.Buffer 97 err := msg.BtcEncode(&buf, wire.ProtocolVersion) 98 if err == nil { 99 t.Errorf("encode of MsgFilterAdd succeeded when it shouldn't "+ 100 "have %v", msg) 101 } 102 103 // Decode with latest protocol version. 104 readbuf := bytes.NewReader(data) 105 err = msg.BtcDecode(readbuf, wire.ProtocolVersion) 106 if err == nil { 107 t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+ 108 "have %v", msg) 109 } 110 } 111 112 // TestFilterAddWireErrors performs negative tests against wire encode and decode 113 // of MsgFilterAdd to confirm error paths work correctly. 114 func TestFilterAddWireErrors(t *testing.T) { 115 pver := wire.ProtocolVersion 116 pverNoFilterAdd := wire.BIP0037Version - 1 117 wireErr := &wire.MessageError{} 118 119 baseData := []byte{0x01, 0x02, 0x03, 0x04} 120 baseFilterAdd := wire.NewMsgFilterAdd(baseData) 121 baseFilterAddEncoded := append([]byte{0x04}, baseData...) 122 123 tests := []struct { 124 in *wire.MsgFilterAdd // Value to encode 125 buf []byte // Wire encoding 126 pver uint32 // Protocol version for wire encoding 127 max int // Max size of fixed buffer to induce errors 128 writeErr error // Expected write error 129 readErr error // Expected read error 130 }{ 131 // Latest protocol version with intentional read/write errors. 132 // Force error in data size. 133 { 134 baseFilterAdd, baseFilterAddEncoded, pver, 0, 135 io.ErrShortWrite, io.EOF, 136 }, 137 // Force error in data. 138 { 139 baseFilterAdd, baseFilterAddEncoded, pver, 1, 140 io.ErrShortWrite, io.EOF, 141 }, 142 // Force error due to unsupported protocol version. 143 { 144 baseFilterAdd, baseFilterAddEncoded, pverNoFilterAdd, 5, 145 wireErr, wireErr, 146 }, 147 } 148 149 t.Logf("Running %d tests", len(tests)) 150 for i, test := range tests { 151 // Encode to wire format. 152 w := newFixedWriter(test.max) 153 err := test.in.BtcEncode(w, test.pver) 154 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { 155 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", 156 i, err, test.writeErr) 157 continue 158 } 159 160 // For errors which are not of type wire.MessageError, check 161 // them for equality. 162 if _, ok := err.(*wire.MessageError); !ok { 163 if err != test.writeErr { 164 t.Errorf("BtcEncode #%d wrong error got: %v, "+ 165 "want: %v", i, err, test.writeErr) 166 continue 167 } 168 } 169 170 // Decode from wire format. 171 var msg wire.MsgFilterAdd 172 r := newFixedReader(test.max, test.buf) 173 err = msg.BtcDecode(r, test.pver) 174 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { 175 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", 176 i, err, test.readErr) 177 continue 178 } 179 180 // For errors which are not of type wire.MessageError, check 181 // them for equality. 182 if _, ok := err.(*wire.MessageError); !ok { 183 if err != test.readErr { 184 t.Errorf("BtcDecode #%d wrong error got: %v, "+ 185 "want: %v", i, err, test.readErr) 186 continue 187 } 188 } 189 } 190 }