github.com/palcoin-project/palcd@v1.0.0/wire/msgfilterclear_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 "reflect" 10 "testing" 11 12 "github.com/davecgh/go-spew/spew" 13 ) 14 15 // TestFilterCLearLatest tests the MsgFilterClear API against the latest 16 // protocol version. 17 func TestFilterClearLatest(t *testing.T) { 18 pver := ProtocolVersion 19 20 msg := NewMsgFilterClear() 21 22 // Ensure the command is expected value. 23 wantCmd := "filterclear" 24 if cmd := msg.Command(); cmd != wantCmd { 25 t.Errorf("NewMsgFilterClear: wrong command - got %v want %v", 26 cmd, wantCmd) 27 } 28 29 // Ensure max payload is expected value for latest protocol version. 30 wantPayload := uint32(0) 31 maxPayload := msg.MaxPayloadLength(pver) 32 if maxPayload != wantPayload { 33 t.Errorf("MaxPayloadLength: wrong max payload length for "+ 34 "protocol version %d - got %v, want %v", pver, 35 maxPayload, wantPayload) 36 } 37 } 38 39 // TestFilterClearCrossProtocol tests the MsgFilterClear API when encoding with 40 // the latest protocol version and decoding with BIP0031Version. 41 func TestFilterClearCrossProtocol(t *testing.T) { 42 msg := NewMsgFilterClear() 43 44 // Encode with latest protocol version. 45 var buf bytes.Buffer 46 err := msg.BtcEncode(&buf, ProtocolVersion, LatestEncoding) 47 if err != nil { 48 t.Errorf("encode of MsgFilterClear failed %v err <%v>", msg, err) 49 } 50 51 // Decode with old protocol version. 52 var readmsg MsgFilterClear 53 err = readmsg.BtcDecode(&buf, BIP0031Version, LatestEncoding) 54 if err == nil { 55 t.Errorf("decode of MsgFilterClear succeeded when it "+ 56 "shouldn't have %v", msg) 57 } 58 } 59 60 // TestFilterClearWire tests the MsgFilterClear wire encode and decode for 61 // various protocol versions. 62 func TestFilterClearWire(t *testing.T) { 63 msgFilterClear := NewMsgFilterClear() 64 msgFilterClearEncoded := []byte{} 65 66 tests := []struct { 67 in *MsgFilterClear // Message to encode 68 out *MsgFilterClear // Expected decoded message 69 buf []byte // Wire encoding 70 pver uint32 // Protocol version for wire encoding 71 enc MessageEncoding // Message encoding format 72 }{ 73 // Latest protocol version. 74 { 75 msgFilterClear, 76 msgFilterClear, 77 msgFilterClearEncoded, 78 ProtocolVersion, 79 BaseEncoding, 80 }, 81 82 // Protocol version BIP0037Version + 1. 83 { 84 msgFilterClear, 85 msgFilterClear, 86 msgFilterClearEncoded, 87 BIP0037Version + 1, 88 BaseEncoding, 89 }, 90 91 // Protocol version BIP0037Version. 92 { 93 msgFilterClear, 94 msgFilterClear, 95 msgFilterClearEncoded, 96 BIP0037Version, 97 BaseEncoding, 98 }, 99 } 100 101 t.Logf("Running %d tests", len(tests)) 102 for i, test := range tests { 103 // Encode the message to wire format. 104 var buf bytes.Buffer 105 err := test.in.BtcEncode(&buf, test.pver, test.enc) 106 if err != nil { 107 t.Errorf("BtcEncode #%d error %v", i, err) 108 continue 109 } 110 if !bytes.Equal(buf.Bytes(), test.buf) { 111 t.Errorf("BtcEncode #%d\n got: %s want: %s", i, 112 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) 113 continue 114 } 115 116 // Decode the message from wire format. 117 var msg MsgFilterClear 118 rbuf := bytes.NewReader(test.buf) 119 err = msg.BtcDecode(rbuf, test.pver, test.enc) 120 if err != nil { 121 t.Errorf("BtcDecode #%d error %v", i, err) 122 continue 123 } 124 if !reflect.DeepEqual(&msg, test.out) { 125 t.Errorf("BtcDecode #%d\n got: %s want: %s", i, 126 spew.Sdump(msg), spew.Sdump(test.out)) 127 continue 128 } 129 } 130 } 131 132 // TestFilterClearWireErrors performs negative tests against wire encode and 133 // decode of MsgFilterClear to confirm error paths work correctly. 134 func TestFilterClearWireErrors(t *testing.T) { 135 pverNoFilterClear := BIP0037Version - 1 136 wireErr := &MessageError{} 137 138 baseFilterClear := NewMsgFilterClear() 139 baseFilterClearEncoded := []byte{} 140 141 tests := []struct { 142 in *MsgFilterClear // Value to encode 143 buf []byte // Wire encoding 144 pver uint32 // Protocol version for wire encoding 145 enc MessageEncoding // Message encoding format 146 max int // Max size of fixed buffer to induce errors 147 writeErr error // Expected write error 148 readErr error // Expected read error 149 }{ 150 // Force error due to unsupported protocol version. 151 { 152 baseFilterClear, baseFilterClearEncoded, 153 pverNoFilterClear, BaseEncoding, 4, wireErr, wireErr, 154 }, 155 } 156 157 t.Logf("Running %d tests", len(tests)) 158 for i, test := range tests { 159 // Encode to wire format. 160 w := newFixedWriter(test.max) 161 err := test.in.BtcEncode(w, test.pver, test.enc) 162 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { 163 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", 164 i, err, test.writeErr) 165 continue 166 } 167 168 // For errors which are not of type MessageError, check them for 169 // equality. 170 if _, ok := err.(*MessageError); !ok { 171 if err != test.writeErr { 172 t.Errorf("BtcEncode #%d wrong error got: %v, "+ 173 "want: %v", i, err, test.writeErr) 174 continue 175 } 176 } 177 178 // Decode from wire format. 179 var msg MsgFilterClear 180 r := newFixedReader(test.max, test.buf) 181 err = msg.BtcDecode(r, test.pver, test.enc) 182 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { 183 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", 184 i, err, test.readErr) 185 continue 186 } 187 188 // For errors which are not of type MessageError, check them for 189 // equality. 190 if _, ok := err.(*MessageError); !ok { 191 if err != test.readErr { 192 t.Errorf("BtcDecode #%d wrong error got: %v, "+ 193 "want: %v", i, err, test.readErr) 194 continue 195 } 196 } 197 198 } 199 }