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.
12 "github.com/btcsuite/btcd/chaincfg/chainhash"
13 "github.com/davecgh/go-spew/spew"
16 // TestInvVectStringer tests the stringized output for inventory vector types.
17 func TestInvTypeStringer(t *testing.T) {
22 {InvTypeError, "ERROR"},
23 {InvTypeTx, "MSG_TX"},
24 {InvTypeBlock, "MSG_BLOCK"},
25 {0xffffffff, "Unknown InvType (4294967295)"},
28 t.Logf("Running %d tests", len(tests))
29 for i, test := range tests {
30 result := test.in.String()
31 if result != test.want {
32 t.Errorf("String #%d\n got: %s want: %s", i, result,
40 // TestInvVect tests the InvVect API.
41 func TestInvVect(t *testing.T) {
42 ivType := InvTypeBlock
43 hash := chainhash.Hash{}
45 // Ensure we get the same payload and signature back out.
46 iv := NewInvVect(ivType, &hash)
47 if iv.Type != ivType {
48 t.Errorf("NewInvVect: wrong type - got %v, want %v",
51 if !iv.Hash.IsEqual(&hash) {
52 t.Errorf("NewInvVect: wrong hash - got %v, want %v",
53 spew.Sdump(iv.Hash), spew.Sdump(hash))
58 // TestInvVectWire tests the InvVect wire encode and decode for various
59 // protocol versions and supported inventory vector types.
60 func TestInvVectWire(t *testing.T) {
62 hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
63 baseHash, err := chainhash.NewHashFromStr(hashStr)
65 t.Errorf("NewHashFromStr: %v", err)
68 // errInvVect is an inventory vector with an error.
69 errInvVect := InvVect{
71 Hash: chainhash.Hash{},
74 // errInvVectEncoded is the wire encoded bytes of errInvVect.
75 errInvVectEncoded := []byte{
76 0x00, 0x00, 0x00, 0x00, // InvTypeError
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // No hash
83 // txInvVect is an inventory vector representing a transaction.
89 // txInvVectEncoded is the wire encoded bytes of txInvVect.
90 txInvVectEncoded := []byte{
91 0x01, 0x00, 0x00, 0x00, // InvTypeTx
92 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
93 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
94 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
95 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash
98 // blockInvVect is an inventory vector representing a block.
99 blockInvVect := InvVect{
104 // blockInvVectEncoded is the wire encoded bytes of blockInvVect.
105 blockInvVectEncoded := []byte{
106 0x02, 0x00, 0x00, 0x00, // InvTypeBlock
107 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
108 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
109 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
110 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash
114 in InvVect // NetAddress to encode
115 out InvVect // Expected decoded NetAddress
116 buf []byte // Wire encoding
117 pver uint32 // Protocol version for wire encoding
119 // Latest protocol version error inventory vector.
127 // Latest protocol version tx inventory vector.
135 // Latest protocol version block inventory vector.
143 // Protocol version BIP0035Version error inventory vector.
151 // Protocol version BIP0035Version tx inventory vector.
159 // Protocol version BIP0035Version block inventory vector.
167 // Protocol version BIP0031Version error inventory vector.
175 // Protocol version BIP0031Version tx inventory vector.
183 // Protocol version BIP0031Version block inventory vector.
191 // Protocol version NetAddressTimeVersion error inventory vector.
196 NetAddressTimeVersion,
199 // Protocol version NetAddressTimeVersion tx inventory vector.
204 NetAddressTimeVersion,
207 // Protocol version NetAddressTimeVersion block inventory vector.
212 NetAddressTimeVersion,
215 // Protocol version MultipleAddressVersion error inventory vector.
220 MultipleAddressVersion,
223 // Protocol version MultipleAddressVersion tx inventory vector.
228 MultipleAddressVersion,
231 // Protocol version MultipleAddressVersion block inventory vector.
236 MultipleAddressVersion,
240 t.Logf("Running %d tests", len(tests))
241 for i, test := range tests {
242 // Encode to wire format.
244 err := writeInvVect(&buf, test.pver, &test.in)
246 t.Errorf("writeInvVect #%d error %v", i, err)
249 if !bytes.Equal(buf.Bytes(), test.buf) {
250 t.Errorf("writeInvVect #%d\n got: %s want: %s", i,
251 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
255 // Decode the message from wire format.
257 rbuf := bytes.NewReader(test.buf)
258 err = readInvVect(rbuf, test.pver, &iv)
260 t.Errorf("readInvVect #%d error %v", i, err)
263 if !reflect.DeepEqual(iv, test.out) {
264 t.Errorf("readInvVect #%d\n got: %s want: %s", i,
265 spew.Sdump(iv), spew.Sdump(test.out))