invvect_test.go raw
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.
4
5 package wire
6
7 import (
8 "bytes"
9 "reflect"
10 "testing"
11
12 "github.com/btcsuite/btcd/chainhash/v2"
13 "github.com/davecgh/go-spew/spew"
14 )
15
16 // TestInvVectStringer tests the stringized output for inventory vector types.
17 func TestInvTypeStringer(t *testing.T) {
18 tests := []struct {
19 in InvType
20 want string
21 }{
22 {InvTypeError, "ERROR"},
23 {InvTypeTx, "MSG_TX"},
24 {InvTypeBlock, "MSG_BLOCK"},
25 {0xffffffff, "Unknown InvType (4294967295)"},
26 }
27
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,
33 test.want)
34 continue
35 }
36 }
37
38 }
39
40 // TestInvVect tests the InvVect API.
41 func TestInvVect(t *testing.T) {
42 ivType := InvTypeBlock
43 hash := chainhash.Hash{}
44
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",
49 iv.Type, ivType)
50 }
51 if !iv.Hash.IsEqual(&hash) {
52 t.Errorf("NewInvVect: wrong hash - got %v, want %v",
53 spew.Sdump(iv.Hash), spew.Sdump(hash))
54 }
55
56 }
57
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) {
61 // Block 203707 hash.
62 hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
63 baseHash, err := chainhash.NewHashFromStr(hashStr)
64 if err != nil {
65 t.Errorf("NewHashFromStr: %v", err)
66 }
67
68 // errInvVect is an inventory vector with an error.
69 errInvVect := InvVect{
70 Type: InvTypeError,
71 Hash: chainhash.Hash{},
72 }
73
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
81 }
82
83 // txInvVect is an inventory vector representing a transaction.
84 txInvVect := InvVect{
85 Type: InvTypeTx,
86 Hash: *baseHash,
87 }
88
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
96 }
97
98 // blockInvVect is an inventory vector representing a block.
99 blockInvVect := InvVect{
100 Type: InvTypeBlock,
101 Hash: *baseHash,
102 }
103
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
111 }
112
113 tests := []struct {
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
118 }{
119 // Latest protocol version error inventory vector.
120 {
121 errInvVect,
122 errInvVect,
123 errInvVectEncoded,
124 ProtocolVersion,
125 },
126
127 // Latest protocol version tx inventory vector.
128 {
129 txInvVect,
130 txInvVect,
131 txInvVectEncoded,
132 ProtocolVersion,
133 },
134
135 // Latest protocol version block inventory vector.
136 {
137 blockInvVect,
138 blockInvVect,
139 blockInvVectEncoded,
140 ProtocolVersion,
141 },
142
143 // Protocol version BIP0035Version error inventory vector.
144 {
145 errInvVect,
146 errInvVect,
147 errInvVectEncoded,
148 BIP0035Version,
149 },
150
151 // Protocol version BIP0035Version tx inventory vector.
152 {
153 txInvVect,
154 txInvVect,
155 txInvVectEncoded,
156 BIP0035Version,
157 },
158
159 // Protocol version BIP0035Version block inventory vector.
160 {
161 blockInvVect,
162 blockInvVect,
163 blockInvVectEncoded,
164 BIP0035Version,
165 },
166
167 // Protocol version BIP0031Version error inventory vector.
168 {
169 errInvVect,
170 errInvVect,
171 errInvVectEncoded,
172 BIP0031Version,
173 },
174
175 // Protocol version BIP0031Version tx inventory vector.
176 {
177 txInvVect,
178 txInvVect,
179 txInvVectEncoded,
180 BIP0031Version,
181 },
182
183 // Protocol version BIP0031Version block inventory vector.
184 {
185 blockInvVect,
186 blockInvVect,
187 blockInvVectEncoded,
188 BIP0031Version,
189 },
190
191 // Protocol version NetAddressTimeVersion error inventory vector.
192 {
193 errInvVect,
194 errInvVect,
195 errInvVectEncoded,
196 NetAddressTimeVersion,
197 },
198
199 // Protocol version NetAddressTimeVersion tx inventory vector.
200 {
201 txInvVect,
202 txInvVect,
203 txInvVectEncoded,
204 NetAddressTimeVersion,
205 },
206
207 // Protocol version NetAddressTimeVersion block inventory vector.
208 {
209 blockInvVect,
210 blockInvVect,
211 blockInvVectEncoded,
212 NetAddressTimeVersion,
213 },
214
215 // Protocol version MultipleAddressVersion error inventory vector.
216 {
217 errInvVect,
218 errInvVect,
219 errInvVectEncoded,
220 MultipleAddressVersion,
221 },
222
223 // Protocol version MultipleAddressVersion tx inventory vector.
224 {
225 txInvVect,
226 txInvVect,
227 txInvVectEncoded,
228 MultipleAddressVersion,
229 },
230
231 // Protocol version MultipleAddressVersion block inventory vector.
232 {
233 blockInvVect,
234 blockInvVect,
235 blockInvVectEncoded,
236 MultipleAddressVersion,
237 },
238 }
239
240 t.Logf("Running %d tests", len(tests))
241 var b [8]byte
242 for i, test := range tests {
243 // Encode to wire format.
244 var buf bytes.Buffer
245 err := writeInvVectBuf(&buf, test.pver, &test.in, b[:])
246 if err != nil {
247 t.Errorf("writeInvVect #%d error %v", i, err)
248 continue
249 }
250 if !bytes.Equal(buf.Bytes(), test.buf) {
251 t.Errorf("writeInvVect #%d\n got: %s want: %s", i,
252 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
253 continue
254 }
255
256 // Decode the message from wire format.
257 var iv InvVect
258 rbuf := bytes.NewReader(test.buf)
259 err = readInvVectBuf(rbuf, test.pver, &iv, b[:])
260 if err != nil {
261 t.Errorf("readInvVect #%d error %v", i, err)
262 continue
263 }
264 if !reflect.DeepEqual(iv, test.out) {
265 t.Errorf("readInvVect #%d\n got: %s want: %s", i,
266 spew.Sdump(iv), spew.Sdump(test.out))
267 continue
268 }
269 }
270 }
271