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 // TestPing tests the MsgPing API against the latest protocol version.
17 func TestPing(t *testing.T) {
18 pver := ProtocolVersion
20 // Ensure we get the same nonce back out.
21 nonce, err := RandomUint64()
23 t.Errorf("RandomUint64: Error generating nonce: %v", err)
25 msg := NewMsgPing(nonce)
26 if msg.Nonce != nonce {
27 t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
31 // Ensure the command is expected value.
33 if cmd := msg.Command(); cmd != wantCmd {
34 t.Errorf("NewMsgPing: 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)
48 // TestPingBIP0031 tests the MsgPing API against the protocol version
50 func TestPingBIP0031(t *testing.T) {
51 // Use the protocol version just prior to BIP0031Version changes.
52 pver := BIP0031Version
55 nonce, err := RandomUint64()
57 t.Errorf("RandomUint64: Error generating nonce: %v", err)
59 msg := NewMsgPing(nonce)
60 if msg.Nonce != nonce {
61 t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
65 // Ensure max payload is expected value for old protocol version.
66 wantPayload := uint32(0)
67 maxPayload := msg.MaxPayloadLength(pver)
68 if maxPayload != wantPayload {
69 t.Errorf("MaxPayloadLength: wrong max payload length for "+
70 "protocol version %d - got %v, want %v", pver,
71 maxPayload, wantPayload)
74 // Test encode with old protocol version.
76 err = msg.BtcEncode(&buf, pver, enc)
78 t.Errorf("encode of MsgPing failed %v err <%v>", msg, err)
81 // Test decode with old protocol version.
82 readmsg := NewMsgPing(0)
83 err = readmsg.BtcDecode(&buf, pver, enc)
85 t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
88 // Since this protocol version doesn't support the nonce, make sure
89 // it didn't get encoded and decoded back out.
90 if msg.Nonce == readmsg.Nonce {
91 t.Errorf("Should not get same nonce for protocol version %d", pver)
95 // TestPingCrossProtocol tests the MsgPing API when encoding with the latest
96 // protocol version and decoding with BIP0031Version.
97 func TestPingCrossProtocol(t *testing.T) {
98 nonce, err := RandomUint64()
100 t.Errorf("RandomUint64: Error generating nonce: %v", err)
102 msg := NewMsgPing(nonce)
103 if msg.Nonce != nonce {
104 t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
108 // Encode with latest protocol version.
110 err = msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
112 t.Errorf("encode of MsgPing failed %v err <%v>", msg, err)
115 // Decode with old protocol version.
116 readmsg := NewMsgPing(0)
117 err = readmsg.BtcDecode(&buf, BIP0031Version, BaseEncoding)
119 t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
122 // Since one of the protocol versions doesn't support the nonce, make
123 // sure it didn't get encoded and decoded back out.
124 if msg.Nonce == readmsg.Nonce {
125 t.Error("Should not get same nonce for cross protocol")
129 // TestPingWire tests the MsgPing wire encode and decode for various protocol
131 func TestPingWire(t *testing.T) {
133 in MsgPing // Message to encode
134 out MsgPing // Expected decoded message
135 buf []byte // Wire encoding
136 pver uint32 // Protocol version for wire encoding
137 enc MessageEncoding // Message encoding format
139 // Latest protocol version.
141 MsgPing{Nonce: 123123}, // 0x1e0f3
142 MsgPing{Nonce: 123123}, // 0x1e0f3
143 []byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
148 // Protocol version BIP0031Version+1
150 MsgPing{Nonce: 456456}, // 0x6f708
151 MsgPing{Nonce: 456456}, // 0x6f708
152 []byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
157 // Protocol version BIP0031Version
159 MsgPing{Nonce: 789789}, // 0xc0d1d
160 MsgPing{Nonce: 0}, // No nonce for pver
161 []byte{}, // No nonce for pver
167 t.Logf("Running %d tests", len(tests))
168 for i, test := range tests {
169 // Encode the message to wire format.
171 err := test.in.BtcEncode(&buf, test.pver, test.enc)
173 t.Errorf("BtcEncode #%d error %v", i, err)
176 if !bytes.Equal(buf.Bytes(), test.buf) {
177 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
178 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
182 // Decode the message from wire format.
184 rbuf := bytes.NewReader(test.buf)
185 err = msg.BtcDecode(rbuf, test.pver, test.enc)
187 t.Errorf("BtcDecode #%d error %v", i, err)
190 if !reflect.DeepEqual(msg, test.out) {
191 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
192 spew.Sdump(msg), spew.Sdump(test.out))
198 // TestPingWireErrors performs negative tests against wire encode and decode
199 // of MsgPing to confirm error paths work correctly.
200 func TestPingWireErrors(t *testing.T) {
201 pver := ProtocolVersion
204 in *MsgPing // Value to encode
205 buf []byte // Wire encoding
206 pver uint32 // Protocol version for wire encoding
207 enc MessageEncoding // Message encoding format
208 max int // Max size of fixed buffer to induce errors
209 writeErr error // Expected write error
210 readErr error // Expected read error
212 // Latest protocol version with intentional read/write errors.
214 &MsgPing{Nonce: 123123}, // 0x1e0f3
215 []byte{0xf3, 0xe0, 0x01, 0x00},
224 t.Logf("Running %d tests", len(tests))
225 for i, test := range tests {
226 // Encode to wire format.
227 w := newFixedWriter(test.max)
228 err := test.in.BtcEncode(w, test.pver, test.enc)
229 if err != test.writeErr {
230 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
231 i, err, test.writeErr)
235 // Decode from wire format.
237 r := newFixedReader(test.max, test.buf)
238 err = msg.BtcDecode(r, test.pver, test.enc)
239 if err != test.readErr {
240 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
241 i, err, test.readErr)