1 // Copyright (c) 2013-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 "github.com/davecgh/go-spew/spew"
17 // TestFeeFilterLatest tests the MsgFeeFilter API against the latest protocol version.
18 func TestFeeFilterLatest(t *testing.T) {
19 pver := ProtocolVersion
21 minfee := rand.Int63()
22 msg := NewMsgFeeFilter(minfee)
23 if msg.MinFee != minfee {
24 t.Errorf("NewMsgFeeFilter: wrong minfee - got %v, want %v",
28 // Ensure the command is expected value.
29 wantCmd := "feefilter"
30 if cmd := msg.Command(); cmd != wantCmd {
31 t.Errorf("NewMsgFeeFilter: wrong command - got %v want %v",
35 // Ensure max payload is expected value for latest protocol version.
36 wantPayload := uint32(8)
37 maxPayload := msg.MaxPayloadLength(pver)
38 if maxPayload != wantPayload {
39 t.Errorf("MaxPayloadLength: wrong max payload length for "+
40 "protocol version %d - got %v, want %v", pver,
41 maxPayload, wantPayload)
44 // Test encode with latest protocol version.
46 err := msg.BtcEncode(&buf, pver, BaseEncoding)
48 t.Errorf("encode of MsgFeeFilter failed %v err <%v>", msg, err)
51 // Test decode with latest protocol version.
52 readmsg := NewMsgFeeFilter(0)
53 err = readmsg.BtcDecode(&buf, pver, BaseEncoding)
55 t.Errorf("decode of MsgFeeFilter failed [%v] err <%v>", buf, err)
58 // Ensure minfee is the same.
59 if msg.MinFee != readmsg.MinFee {
60 t.Errorf("Should get same minfee for protocol version %d", pver)
64 // TestFeeFilterWire tests the MsgFeeFilter wire encode and decode for various protocol
66 func TestFeeFilterWire(t *testing.T) {
68 in MsgFeeFilter // Message to encode
69 out MsgFeeFilter // Expected decoded message
70 buf []byte // Wire encoding
71 pver uint32 // Protocol version for wire encoding
73 // Latest protocol version.
75 MsgFeeFilter{MinFee: 123123}, // 0x1e0f3
76 MsgFeeFilter{MinFee: 123123}, // 0x1e0f3
77 []byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
81 // Protocol version FeeFilterVersion
83 MsgFeeFilter{MinFee: 456456}, // 0x6f708
84 MsgFeeFilter{MinFee: 456456}, // 0x6f708
85 []byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
90 t.Logf("Running %d tests", len(tests))
91 for i, test := range tests {
92 // Encode the message to wire format.
94 err := test.in.BtcEncode(&buf, test.pver, BaseEncoding)
96 t.Errorf("BtcEncode #%d error %v", i, err)
99 if !bytes.Equal(buf.Bytes(), test.buf) {
100 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
101 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
105 // Decode the message from wire format.
107 rbuf := bytes.NewReader(test.buf)
108 err = msg.BtcDecode(rbuf, test.pver, BaseEncoding)
110 t.Errorf("BtcDecode #%d error %v", i, err)
113 if !reflect.DeepEqual(msg, test.out) {
114 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
115 spew.Sdump(msg), spew.Sdump(test.out))
121 // TestFeeFilterWireErrors performs negative tests against wire encode and decode
122 // of MsgFeeFilter to confirm error paths work correctly.
123 func TestFeeFilterWireErrors(t *testing.T) {
124 pver := ProtocolVersion
125 pverNoFeeFilter := FeeFilterVersion - 1
126 wireErr := &MessageError{}
128 baseFeeFilter := NewMsgFeeFilter(123123) // 0x1e0f3
129 baseFeeFilterEncoded := []byte{
130 0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
134 in *MsgFeeFilter // Value to encode
135 buf []byte // Wire encoding
136 pver uint32 // Protocol version for wire encoding
137 max int // Max size of fixed buffer to induce errors
138 writeErr error // Expected write error
139 readErr error // Expected read error
141 // Latest protocol version with intentional read/write errors.
142 // Force error in minfee.
143 {baseFeeFilter, baseFeeFilterEncoded, pver, 0, io.ErrShortWrite, io.EOF},
144 // Force error due to unsupported protocol version.
145 {baseFeeFilter, baseFeeFilterEncoded, pverNoFeeFilter, 4, wireErr, wireErr},
148 t.Logf("Running %d tests", len(tests))
149 for i, test := range tests {
150 // Encode to wire format.
151 w := newFixedWriter(test.max)
152 err := test.in.BtcEncode(w, test.pver, BaseEncoding)
153 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
154 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
155 i, err, test.writeErr)
159 // For errors which are not of type MessageError, check them for
161 if _, ok := err.(*MessageError); !ok {
162 if err != test.writeErr {
163 t.Errorf("BtcEncode #%d wrong error got: %v, "+
164 "want: %v", i, err, test.writeErr)
169 // Decode from wire format.
171 r := newFixedReader(test.max, test.buf)
172 err = msg.BtcDecode(r, test.pver, BaseEncoding)
173 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
174 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
175 i, err, test.readErr)
179 // For errors which are not of type MessageError, check them for
181 if _, ok := err.(*MessageError); !ok {
182 if err != test.readErr {
183 t.Errorf("BtcDecode #%d wrong error got: %v, "+
184 "want: %v", i, err, test.readErr)