forked from gcash/bchd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgxverack_test.go
125 lines (111 loc) · 2.76 KB
/
msgxverack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2019 The bchd developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"bytes"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
// TestVerAck tests the MsgXVerAck API.
func TestXVerAck(t *testing.T) {
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "xverack"
msg := NewMsgXVerAck()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgXVerAck: wrong command - got %v want %v",
cmd, wantCmd)
}
// Ensure max payload is expected value.
wantPayload := uint32(0)
maxPayload := msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
t.Errorf("MaxPayloadLength: wrong max payload length for "+
"protocol version %d - got %v, want %v", pver,
maxPayload, wantPayload)
}
}
// TestXVerAckWire tests the MsgXVerAck wire encode and decode for various
// protocol versions.
func TestXVerAckWire(t *testing.T) {
msgXVerAck := NewMsgXVerAck()
msgXVerAckEncoded := []byte{}
tests := []struct {
in *MsgXVerAck // Message to encode
out *MsgXVerAck // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
enc MessageEncoding // Message encoding format
}{
// Latest protocol version.
{
msgXVerAck,
msgXVerAck,
msgXVerAckEncoded,
ProtocolVersion,
BaseEncoding,
},
// Protocol version BIP0035Version.
{
msgXVerAck,
msgXVerAck,
msgXVerAckEncoded,
BIP0035Version,
BaseEncoding,
},
// Protocol version BIP0031Version.
{
msgXVerAck,
msgXVerAck,
msgXVerAckEncoded,
BIP0031Version,
BaseEncoding,
},
// Protocol version NetAddressTimeVersion.
{
msgXVerAck,
msgXVerAck,
msgXVerAckEncoded,
NetAddressTimeVersion,
BaseEncoding,
},
// Protocol version MultipleAddressVersion.
{
msgXVerAck,
msgXVerAck,
msgXVerAckEncoded,
MultipleAddressVersion,
BaseEncoding,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Encode the message to wire format.
var buf bytes.Buffer
err := test.in.BchEncode(&buf, test.pver, test.enc)
if err != nil {
t.Errorf("BchEncode #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("BchEncode #%d\n got: %s want: %s", i,
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
continue
}
// Decode the message from wire format.
var msg MsgXVerAck
rbuf := bytes.NewReader(test.buf)
err = msg.BchDecode(rbuf, test.pver, test.enc)
if err != nil {
t.Errorf("BchDecode #%d error %v", i, err)
continue
}
if !reflect.DeepEqual(&msg, test.out) {
t.Errorf("BchDecode #%d\n got: %s want: %s", i,
spew.Sdump(msg), spew.Sdump(test.out))
continue
}
}
}