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.
13 "github.com/davecgh/go-spew/spew"
16 // TestPongLatest tests the MsgPong API against the latest protocol version.
17 func TestPongLatest(t *testing.T) {
19 pver := ProtocolVersion
21 nonce, err := RandomUint64()
23 t.Errorf("RandomUint64: error generating nonce: %v", err)
25 msg := NewMsgPong(nonce)
26 if msg.Nonce != nonce {
27 t.Errorf("NewMsgPong: wrong nonce - got %v, want %v",
31 // Ensure the command is expected value.
33 if cmd := msg.Command(); cmd != wantCmd {
34 t.Errorf("NewMsgPong: wrong command - got %v want %v",
38 // Ensure max payload is expected value for latest protocol version.
39 wantPayload := uint32(8)
40 maxPayload := msg.MaxPayloadLength(pver)
41 if maxPayload != wantPayload {
42 t.Errorf("MaxPayloadLength: wrong max payload length for "+
43 "protocol version %d - got %v, want %v", pver,
44 maxPayload, wantPayload)
47 // Test encode with latest protocol version.
49 err = msg.BtcEncode(&buf, pver, enc)
51 t.Errorf("encode of MsgPong failed %v err <%v>", msg, err)
54 // Test decode with latest protocol version.
55 readmsg := NewMsgPong(0)
56 err = readmsg.BtcDecode(&buf, pver, enc)
58 t.Errorf("decode of MsgPong failed [%v] err <%v>", buf, err)
61 // Ensure nonce is the same.
62 if msg.Nonce != readmsg.Nonce {
63 t.Errorf("Should get same nonce for protocol version %d", pver)
67 // TestPongBIP0031 tests the MsgPong API against the protocol version
69 func TestPongBIP0031(t *testing.T) {
70 // Use the protocol version just prior to BIP0031Version changes.
71 pver := BIP0031Version
74 nonce, err := RandomUint64()
76 t.Errorf("Error generating nonce: %v", err)
78 msg := NewMsgPong(nonce)
79 if msg.Nonce != nonce {
80 t.Errorf("Should get same nonce back out.")
83 // Ensure max payload is expected value for old protocol version.
84 size := msg.MaxPayloadLength(pver)
86 t.Errorf("Max length should be 0 for pong protocol version %d.",
90 // Test encode with old protocol version.
92 err = msg.BtcEncode(&buf, pver, enc)
94 t.Errorf("encode of MsgPong succeeded when it shouldn't have %v",
98 // Test decode with old protocol version.
99 readmsg := NewMsgPong(0)
100 err = readmsg.BtcDecode(&buf, pver, enc)
102 t.Errorf("decode of MsgPong succeeded when it shouldn't have %v",
106 // Since this protocol version doesn't support pong, make sure the
107 // nonce didn't get encoded and decoded back out.
108 if msg.Nonce == readmsg.Nonce {
109 t.Errorf("Should not get same nonce for protocol version %d", pver)
113 // TestPongCrossProtocol tests the MsgPong API when encoding with the latest
114 // protocol version and decoding with BIP0031Version.
115 func TestPongCrossProtocol(t *testing.T) {
116 nonce, err := RandomUint64()
118 t.Errorf("Error generating nonce: %v", err)
120 msg := NewMsgPong(nonce)
121 if msg.Nonce != nonce {
122 t.Errorf("Should get same nonce back out.")
125 // Encode with latest protocol version.
127 err = msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
129 t.Errorf("encode of MsgPong failed %v err <%v>", msg, err)
132 // Decode with old protocol version.
133 readmsg := NewMsgPong(0)
134 err = readmsg.BtcDecode(&buf, BIP0031Version, BaseEncoding)
136 t.Errorf("encode of MsgPong succeeded when it shouldn't have %v",
140 // Since one of the protocol versions doesn't support the pong message,
141 // make sure the nonce didn't get encoded and decoded back out.
142 if msg.Nonce == readmsg.Nonce {
143 t.Error("Should not get same nonce for cross protocol")
147 // TestPongWire tests the MsgPong wire encode and decode for various protocol
149 func TestPongWire(t *testing.T) {
151 in MsgPong // Message to encode
152 out MsgPong // Expected decoded message
153 buf []byte // Wire encoding
154 pver uint32 // Protocol version for wire encoding
155 enc MessageEncoding // Message encoding format
157 // Latest protocol version.
159 MsgPong{Nonce: 123123}, // 0x1e0f3
160 MsgPong{Nonce: 123123}, // 0x1e0f3
161 []byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
166 // Protocol version BIP0031Version+1
168 MsgPong{Nonce: 456456}, // 0x6f708
169 MsgPong{Nonce: 456456}, // 0x6f708
170 []byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
176 t.Logf("Running %d tests", len(tests))
177 for i, test := range tests {
178 // Encode the message to wire format.
180 err := test.in.BtcEncode(&buf, test.pver, test.enc)
182 t.Errorf("BtcEncode #%d error %v", i, err)
185 if !bytes.Equal(buf.Bytes(), test.buf) {
186 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
187 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
191 // Decode the message from wire format.
193 rbuf := bytes.NewReader(test.buf)
194 err = msg.BtcDecode(rbuf, test.pver, test.enc)
196 t.Errorf("BtcDecode #%d error %v", i, err)
199 if !reflect.DeepEqual(msg, test.out) {
200 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
201 spew.Sdump(msg), spew.Sdump(test.out))
207 // TestPongWireErrors performs negative tests against wire encode and decode
208 // of MsgPong to confirm error paths work correctly.
209 func TestPongWireErrors(t *testing.T) {
210 pver := ProtocolVersion
211 pverNoPong := BIP0031Version
212 wireErr := &MessageError{}
214 basePong := NewMsgPong(123123) // 0x1e0f3
215 basePongEncoded := []byte{
216 0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
220 in *MsgPong // Value to encode
221 buf []byte // Wire encoding
222 pver uint32 // Protocol version for wire encoding
223 enc MessageEncoding // Message encoding format
224 max int // Max size of fixed buffer to induce errors
225 writeErr error // Expected write error
226 readErr error // Expected read error
228 // Latest protocol version with intentional read/write errors.
229 // Force error in nonce.
230 {basePong, basePongEncoded, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF},
231 // Force error due to unsupported protocol version.
232 {basePong, basePongEncoded, pverNoPong, BaseEncoding, 4, wireErr, wireErr},
235 t.Logf("Running %d tests", len(tests))
236 for i, test := range tests {
237 // Encode to wire format.
238 w := newFixedWriter(test.max)
239 err := test.in.BtcEncode(w, test.pver, test.enc)
240 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
241 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
242 i, err, test.writeErr)
246 // For errors which are not of type MessageError, check them for
248 if _, ok := err.(*MessageError); !ok {
249 if err != test.writeErr {
250 t.Errorf("BtcEncode #%d wrong error got: %v, "+
251 "want: %v", i, err, test.writeErr)
256 // Decode from wire format.
258 r := newFixedReader(test.max, test.buf)
259 err = msg.BtcDecode(r, test.pver, test.enc)
260 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
261 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
262 i, err, test.readErr)
266 // For errors which are not of type MessageError, check them for
268 if _, ok := err.(*MessageError); !ok {
269 if err != test.readErr {
270 t.Errorf("BtcDecode #%d wrong error got: %v, "+
271 "want: %v", i, err, test.readErr)