msgfeefilter_test.go raw
1 package wire
2
3 import (
4 "bytes"
5 "io"
6 "math/rand"
7 "reflect"
8 "testing"
9
10 "github.com/davecgh/go-spew/spew"
11 )
12
13 // TestFeeFilterLatest tests the MsgFeeFilter API against the latest protocol version.
14 func TestFeeFilterLatest(t *testing.T) {
15 pver := ProtocolVersion
16 minfee := rand.Int63()
17 msg := NewMsgFeeFilter(minfee)
18 if msg.MinFee != minfee {
19 t.Errorf("NewMsgFeeFilter: wrong minfee - got %v, want %v",
20 msg.MinFee, minfee,
21 )
22 }
23 // Ensure the command is expected value.
24 wantCmd := "feefilter"
25 if cmd := msg.Command(); cmd != wantCmd {
26 t.Errorf("NewMsgFeeFilter: 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(8)
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 e := msg.BtcEncode(&buf, pver, BaseEncoding)
42 if e != nil {
43 t.Errorf("encode of MsgFeeFilter failed %v e <%v>", msg, e)
44 }
45 // Test decode with latest protocol version.
46 readmsg := NewMsgFeeFilter(0)
47 e = readmsg.BtcDecode(&buf, pver, BaseEncoding)
48 if e != nil {
49 t.Errorf("decode of MsgFeeFilter failed [%v] e <%v>", buf, e)
50 }
51 // Ensure minfee is the same.
52 if msg.MinFee != readmsg.MinFee {
53 t.Errorf("Should get same minfee for protocol version %d", pver)
54 }
55 }
56
57 // TestFeeFilterWire tests the MsgFeeFilter wire encode and decode for various protocol versions.
58 func TestFeeFilterWire(t *testing.T) {
59 tests := []struct {
60 in MsgFeeFilter // Message to encode
61 out MsgFeeFilter // Expected decoded message
62 buf []byte // Wire encoding
63 pver uint32 // Protocol version for wire encoding
64 }{
65 // Latest protocol version.
66 {
67 MsgFeeFilter{MinFee: 123123}, // 0x1e0f3
68 MsgFeeFilter{MinFee: 123123}, // 0x1e0f3
69 []byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
70 ProtocolVersion,
71 },
72 // Protocol version FeeFilterVersion
73 {
74 MsgFeeFilter{MinFee: 456456}, // 0x6f708
75 MsgFeeFilter{MinFee: 456456}, // 0x6f708
76 []byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
77 FeeFilterVersion,
78 },
79 }
80 t.Logf("Running %d tests", len(tests))
81 for i, test := range tests {
82 // Encode the message to wire format.
83 var buf bytes.Buffer
84 e := test.in.BtcEncode(&buf, test.pver, BaseEncoding)
85 if e != nil {
86 t.Errorf("BtcEncode #%d error %v", i, e)
87 continue
88 }
89 if !bytes.Equal(buf.Bytes(), test.buf) {
90 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
91 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf),
92 )
93 continue
94 }
95 // Decode the message from wire format.
96 var msg MsgFeeFilter
97 rbuf := bytes.NewReader(test.buf)
98 e = msg.BtcDecode(rbuf, test.pver, BaseEncoding)
99 if e != nil {
100 t.Errorf("BtcDecode #%d error %v", i, e)
101 continue
102 }
103 if !reflect.DeepEqual(msg, test.out) {
104 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
105 spew.Sdump(msg), spew.Sdump(test.out),
106 )
107 continue
108 }
109 }
110 }
111
112 // TestFeeFilterWireErrors performs negative tests against wire encode and decode of MsgFeeFilter to confirm error paths
113 // work correctly.
114 func TestFeeFilterWireErrors(t *testing.T) {
115 pver := ProtocolVersion
116 pverNoFeeFilter := FeeFilterVersion - 1
117 wireErr := &MessageError{}
118 baseFeeFilter := NewMsgFeeFilter(123123) // 0x1e0f3
119 baseFeeFilterEncoded := []byte{
120 0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
121 }
122 tests := []struct {
123 in *MsgFeeFilter // value to encode
124 buf []byte // Wire encoding
125 pver uint32 // Protocol version for wire encoding
126 max int // Max size of fixed buffer to induce errors
127 writeErr error // Expected write error
128 readErr error // Expected read error
129 }{
130 // Latest protocol version with intentional read/write errors. Force error in minfee.
131 {baseFeeFilter, baseFeeFilterEncoded, pver, 0, io.ErrShortWrite, io.EOF},
132 // Force error due to unsupported protocol version.
133 {baseFeeFilter, baseFeeFilterEncoded, pverNoFeeFilter, 4, wireErr, wireErr},
134 }
135 t.Logf("Running %d tests", len(tests))
136 for i, test := range tests {
137 // Encode to wire format.
138 w := newFixedWriter(test.max)
139 e := test.in.BtcEncode(w, test.pver, BaseEncoding)
140 if reflect.TypeOf(e) != reflect.TypeOf(test.writeErr) {
141 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
142 i, e, test.writeErr,
143 )
144 continue
145 }
146 // For errors which are not of type MessageError, check them for equality.
147 if _, ok := e.(*MessageError); !ok {
148 if e != test.writeErr {
149 t.Errorf("BtcEncode #%d wrong error got: %v, "+
150 "want: %v", i, e, test.writeErr,
151 )
152 continue
153 }
154 }
155 // Decode from wire format.
156 var msg MsgFeeFilter
157 r := newFixedReader(test.max, test.buf)
158 e = msg.BtcDecode(r, test.pver, BaseEncoding)
159 if reflect.TypeOf(e) != reflect.TypeOf(test.readErr) {
160 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
161 i, e, test.readErr,
162 )
163 continue
164 }
165 // For errors which are not of type MessageError, check them for equality.
166 if _, ok := e.(*MessageError); !ok {
167 if e != test.readErr {
168 t.Errorf("BtcDecode #%d wrong error got: %v, "+
169 "want: %v", i, e, test.readErr,
170 )
171 continue
172 }
173 }
174 }
175 }
176