zebra_test/
network_addr.rs

1//! Contains test vectors for network protocol address messages:
2//! * addr (v1): [addr Bitcoin Reference](https://developer.bitcoin.org/reference/p2p_networking.html#addr)
3//! * addrv2: [ZIP-155](https://zips.z.cash/zip-0155#specification)
4//!
5//! These formats are deserialized into the
6//! `zebra_network::protocol::external::Message::Addr` variant.
7
8use hex::FromHex;
9use lazy_static::lazy_static;
10
11lazy_static! {
12    /// Array of `addr` (v1) test vectors containing IP addresses.
13    ///
14    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addr`].
15    /// They should produce successful results containing IP addresses.
16    // From https://github.com/zingolabs/zcash/blob/9ee66e423a3fbf4829ffeec354e82f4fbceff864/src/test/netbase_tests.cpp#L397
17    pub static ref ADDR_V1_IP_VECTORS: Vec<Vec<u8>> = vec![
18        // stream_addrv1_hex
19        <Vec<u8>>::from_hex(
20            concat!(
21                "02", // number of entries
22
23                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
24                "0000000000000000",                 // service flags, NODE_NONE
25                "00000000000000000000000000000001", // address, fixed 16 bytes (IPv4-mapped IPv6), ::1
26                "0000",                             // port, 0
27
28                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
29                "0100000000000000",                 // service flags, NODE_NETWORK
30                "00000000000000000000000000000001", // address, fixed 16 bytes (IPv4-mapped IPv6), ::1
31                "00f1",                             // port, 241
32            )
33        ).expect("Message bytes are in valid hex representation"),
34
35        // stream_torv3_incompatibly_serialized_to_v1
36        <Vec<u8>>::from_hex(
37            concat!(
38                "01", // number of entries
39
40                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
41                "0100000000000000",                 // service flags, NODE_NETWORK
42                "00000000000000000000000000000000", // address, fixed 16 bytes (IPv4-mapped IPv6), ::
43                "235a",                             // port, 9050
44            )
45        ).expect("Message bytes are in valid hex representation"),
46
47        // Extra test cases:
48        //
49        // all services flags set
50        <Vec<u8>>::from_hex(
51            concat!(
52                "01", // number of entries
53
54                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
55                "ffffffffffffffff",                 // service flags, all set
56                "00000000000000000000000000000001", // address, fixed 16 bytes (IPv4-mapped IPv6), ::1
57                "0000",                             // port, 0
58            )
59        ).expect("Message bytes are in valid hex representation"),
60
61        // IPv4
62        <Vec<u8>>::from_hex(
63            concat!(
64                "01", // number of entries
65
66                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
67                "0100000000000000",                 // service flags, NODE_NETWORK
68                                                    // address, fixed 16 bytes (IPv4-mapped IPv6),
69                "00000000000000000000ffff",         // IPv4-mapped IPv6 prefix, ::ffff...
70                "7f000001",                         // IPv4, 127.0.0.1
71                "0000",                             // port, 0
72            )
73        ).expect("Message bytes are in valid hex representation"),
74
75    ];
76
77    /// Array of empty or unsupported `addr` (v1) test vectors.
78    ///
79    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addr`].
80    /// They should produce successful but empty results.
81    pub static ref ADDR_V1_EMPTY_VECTORS: Vec<Vec<u8>> = vec![
82        // Empty list
83        <Vec<u8>>::from_hex(
84            "00" // number of entries
85        ).expect("Message bytes are in valid hex representation"),
86    ];
87
88    /// Array of ZIP-155 test vectors containing IP addresses.
89    ///
90    /// Some test vectors also contain some unsupported addresses.
91    ///
92    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addrv2`],
93    /// They should produce successful results containing IP addresses.
94    // From https://github.com/zingolabs/zcash/blob/9ee66e423a3fbf4829ffeec354e82f4fbceff864/src/test/netbase_tests.cpp#L421
95    pub static ref ADDR_V2_IP_VECTORS: Vec<Vec<u8>> = vec![
96        // stream_addrv2_hex
97        <Vec<u8>>::from_hex(
98            concat!(
99                "03", // number of entries
100
101                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
102                "00",                               // service flags, COMPACTSIZE(NODE_NONE)
103                "02",                               // network id, IPv6
104                "10",                               // address length, COMPACTSIZE(16)
105                "00000000000000000000000000000001", // address, ::1
106                "0000",                             // port, 0
107
108                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
109                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
110                "02",                               // network id, IPv6
111                "10",                               // address length, COMPACTSIZE(16)
112                "00000000000000000000000000000001", // address, ::1
113                "00f1",                             // port, 241
114
115                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
116                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
117                "04",                               // network id, TorV3
118                "20",                               // address length, COMPACTSIZE(32)
119                "53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88",
120                                                    // address, (32 byte Tor v3 onion service public key)
121                "235a",                             // port, 9050
122            )
123        ).expect("Message bytes are in valid hex representation"),
124
125        // Extra test cases:
126        //
127        // IPv4
128        <Vec<u8>>::from_hex(
129            concat!(
130                "02", // number of entries
131
132                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
133                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
134                "01",                               // network id, IPv4
135                "04",                               // address length, COMPACTSIZE(4)
136                "7f000001",                         // address, 127.0.0.1
137                "0001",                             // port, 1
138
139                // check that variable-length encoding works
140                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
141                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
142                "02",                               // network id, IPv6
143                "10",                               // address length, COMPACTSIZE(16)
144                "00000000000000000000000000000001", // address, ::1
145                "00f1",                             // port, 241
146            )
147        ).expect("Message bytes are in valid hex representation"),
148
149        // all services flags set
150        <Vec<u8>>::from_hex(
151            concat!(
152                "01", // number of entries
153
154                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
155                "ffffffffffffffffff",               // service flags, COMPACTSIZE(all flags set)
156                "02",                               // network id, IPv6
157                "10",                               // address length, COMPACTSIZE(16)
158                "00000000000000000000000000000001", // address, ::1
159                "0000",                             // port, 0
160            )
161        ).expect("Message bytes are in valid hex representation"),
162
163        // Unknown Network ID: address within typical size range
164        <Vec<u8>>::from_hex(
165            concat!(
166                "02", // number of entries
167
168                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
169                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
170                "fb",                               // network id, (unknown)
171                "08",                               // address length, COMPACTSIZE(8)
172                "0000000000000000",                 // address, (8 zero bytes)
173                "0001",                             // port, 1
174
175                // check that variable-length encoding works
176                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
177                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
178                "02",                               // network id, IPv6
179                "10",                               // address length, COMPACTSIZE(16)
180                "00000000000000000000000000000001", // address, ::1
181                "00f1",                             // port, 241
182            )
183        ).expect("Message bytes are in valid hex representation"),
184
185        // Unknown Network ID: zero-sized address
186        <Vec<u8>>::from_hex(
187            concat!(
188                "02", // number of entries
189
190                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
191                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
192                "fc",                               // network id, (unknown)
193                "00",                               // address length, COMPACTSIZE(0)
194                "",                                 // address, (no bytes)
195                "0001",                             // port, 1
196
197                // check that variable-length encoding works
198                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
199                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
200                "02",                               // network id, IPv6
201                "10",                               // address length, COMPACTSIZE(16)
202                "00000000000000000000000000000001", // address, ::1
203                "00f1",                             // port, 241
204            )
205        ).expect("Message bytes are in valid hex representation"),
206
207        // Unknown Network ID: maximum-sized address
208        <Vec<u8>>::from_hex(
209            format!("{}{}{}",
210                    concat!(
211                        "02", // number of entries
212
213                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
214                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
215                        "fd",                               // network id, (unknown)
216                        "fd0002",                           // address length, COMPACTSIZE(512)
217                    ),
218                    "00".repeat(512),                       // address, (512 zero bytes)
219                    concat!(
220                        "0001",                             // port, 1
221
222                        // check that variable-length encoding works
223                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
224                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
225                        "02",                               // network id, IPv6
226                        "10",                               // address length, COMPACTSIZE(16)
227                        "00000000000000000000000000000001", // address, ::1
228                        "00f1",                             // port, 241
229                    )
230            )
231        ).expect("Message bytes are in valid hex representation"),
232    ];
233
234    /// Array of empty or unsupported ZIP-155 test vectors.
235    ///
236    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addrv2`].
237    /// They should produce successful but empty results.
238    // From https://github.com/zingolabs/zcash/blob/9ee66e423a3fbf4829ffeec354e82f4fbceff864/src/test/netbase_tests.cpp#L421
239    pub static ref ADDR_V2_EMPTY_VECTORS: Vec<Vec<u8>> = vec![
240        // torv3_hex
241        <Vec<u8>>::from_hex(
242            concat!(
243                "01", // number of entries
244
245                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
246                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
247                "04",                               // network id, TorV3
248                "20",                               // address length, COMPACTSIZE(32)
249                "53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88",
250                                                    // address, (32 byte Tor v3 onion service public key)
251                "235a",                             // port, 9050
252            )
253        ).expect("Message bytes are in valid hex representation"),
254
255        // Extra test cases:
256        //
257        // Empty list
258        <Vec<u8>>::from_hex(
259            "00" // number of entries
260        ).expect("Message bytes are in valid hex representation"),
261    ];
262
263    /// Array of invalid ZIP-155 test vectors.
264    ///
265    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addrv2`].
266    /// They should fail deserialization.
267    pub static ref ADDR_V2_INVALID_VECTORS: Vec<Vec<u8>> = vec![
268        // Invalid address size: too large, but under CompactSizeMessage limit
269        <Vec<u8>>::from_hex(
270            format!("{}{}{}",
271                    concat!(
272                        "02", // number of entries
273
274                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
275                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
276                        "fe",                               // network id, (unknown)
277                        "fd0102",                           // invalid address length, COMPACTSIZE(513)
278                    ),
279                    "00".repeat(513),                       // address, (513 zero bytes)
280                    concat!(
281                        "0001",                             // port, 1
282
283                        // check that the entire message is ignored
284                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
285                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
286                        "02",                               // network id, IPv6
287                        "10",                               // address length, COMPACTSIZE(16)
288                        "00000000000000000000000000000001", // address, ::1
289                        "00f1",                             // port, 241
290                    )
291            )
292        ).expect("Message bytes are in valid hex representation"),
293
294        // Invalid address size: too large, over CompactSizeMessage limit
295        <Vec<u8>>::from_hex(
296            concat!(
297                "01", // number of entries
298
299                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
300                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
301                "ff",                               // network id, (unknown)
302                "feffffff7f",                       // invalid address length, COMPACTSIZE(2^31 - 1)
303                // no address, generated bytes wouldn't fit in memory
304            ),
305        ).expect("Message bytes are in valid hex representation"),
306    ];
307}