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