zebra_test/
network_addr.rs

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//! Contains test vectors for network protocol address messages:
//! * addr (v1): [addr Bitcoin Reference](https://developer.bitcoin.org/reference/p2p_networking.html#addr)
//! * addrv2: [ZIP-155](https://zips.z.cash/zip-0155#specification)
//!
//! These formats are deserialized into the
//! `zebra_network::protocol::external::Message::Addr` variant.

use hex::FromHex;
use lazy_static::lazy_static;

lazy_static! {
    /// Array of `addr` (v1) test vectors containing IP addresses.
    ///
    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addr`].
    /// They should produce successful results containing IP addresses.
    // From https://github.com/zingolabs/zcash/blob/9ee66e423a3fbf4829ffeec354e82f4fbceff864/src/test/netbase_tests.cpp#L397
    pub static ref ADDR_V1_IP_VECTORS: Vec<Vec<u8>> = vec![
        // stream_addrv1_hex
        <Vec<u8>>::from_hex(
            concat!(
                "02", // number of entries

                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
                "0000000000000000",                 // service flags, NODE_NONE
                "00000000000000000000000000000001", // address, fixed 16 bytes (IPv4-mapped IPv6), ::1
                "0000",                             // port, 0

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "0100000000000000",                 // service flags, NODE_NETWORK
                "00000000000000000000000000000001", // address, fixed 16 bytes (IPv4-mapped IPv6), ::1
                "00f1",                             // port, 241
            )
        ).expect("Message bytes are in valid hex representation"),

        // stream_torv3_incompatibly_serialized_to_v1
        <Vec<u8>>::from_hex(
            concat!(
                "01", // number of entries

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "0100000000000000",                 // service flags, NODE_NETWORK
                "00000000000000000000000000000000", // address, fixed 16 bytes (IPv4-mapped IPv6), ::
                "235a",                             // port, 9050
            )
        ).expect("Message bytes are in valid hex representation"),

        // Extra test cases:
        //
        // all services flags set
        <Vec<u8>>::from_hex(
            concat!(
                "01", // number of entries

                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
                "ffffffffffffffff",                 // service flags, all set
                "00000000000000000000000000000001", // address, fixed 16 bytes (IPv4-mapped IPv6), ::1
                "0000",                             // port, 0
            )
        ).expect("Message bytes are in valid hex representation"),

        // IPv4
        <Vec<u8>>::from_hex(
            concat!(
                "01", // number of entries

                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
                "0100000000000000",                 // service flags, NODE_NETWORK
                                                    // address, fixed 16 bytes (IPv4-mapped IPv6),
                "00000000000000000000ffff",         // IPv4-mapped IPv6 prefix, ::ffff...
                "7f000001",                         // IPv4, 127.0.0.1
                "0000",                             // port, 0
            )
        ).expect("Message bytes are in valid hex representation"),

    ];

    /// Array of empty or unsupported `addr` (v1) test vectors.
    ///
    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addr`].
    /// They should produce successful but empty results.
    pub static ref ADDR_V1_EMPTY_VECTORS: Vec<Vec<u8>> = vec![
        // Empty list
        <Vec<u8>>::from_hex(
            "00" // number of entries
        ).expect("Message bytes are in valid hex representation"),
    ];

    /// Array of ZIP-155 test vectors containing IP addresses.
    ///
    /// Some test vectors also contain some unsupported addresses.
    ///
    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addrv2`],
    /// They should produce successful results containing IP addresses.
    // From https://github.com/zingolabs/zcash/blob/9ee66e423a3fbf4829ffeec354e82f4fbceff864/src/test/netbase_tests.cpp#L421
    pub static ref ADDR_V2_IP_VECTORS: Vec<Vec<u8>> = vec![
        // stream_addrv2_hex
        <Vec<u8>>::from_hex(
            concat!(
                "03", // number of entries

                "61bc6649",                         // time, Fri Jan  9 02:54:25 UTC 2009
                "00",                               // service flags, COMPACTSIZE(NODE_NONE)
                "02",                               // network id, IPv6
                "10",                               // address length, COMPACTSIZE(16)
                "00000000000000000000000000000001", // address, ::1
                "0000",                             // port, 0

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "02",                               // network id, IPv6
                "10",                               // address length, COMPACTSIZE(16)
                "00000000000000000000000000000001", // address, ::1
                "00f1",                             // port, 241

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "04",                               // network id, TorV3
                "20",                               // address length, COMPACTSIZE(32)
                "53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88",
                                                    // address, (32 byte Tor v3 onion service public key)
                "235a",                             // port, 9050
            )
        ).expect("Message bytes are in valid hex representation"),

        // Extra test cases:
        //
        // IPv4
        <Vec<u8>>::from_hex(
            concat!(
                "02", // number of entries

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "01",                               // network id, IPv4
                "04",                               // address length, COMPACTSIZE(4)
                "7f000001",                         // address, 127.0.0.1
                "0001",                             // port, 1

                // check that variable-length encoding works
                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "02",                               // network id, IPv6
                "10",                               // address length, COMPACTSIZE(16)
                "00000000000000000000000000000001", // address, ::1
                "00f1",                             // port, 241
            )
        ).expect("Message bytes are in valid hex representation"),

        // all services flags set
        <Vec<u8>>::from_hex(
            concat!(
                "01", // number of entries

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "ffffffffffffffffff",               // service flags, COMPACTSIZE(all flags set)
                "02",                               // network id, IPv6
                "10",                               // address length, COMPACTSIZE(16)
                "00000000000000000000000000000001", // address, ::1
                "0000",                             // port, 0
            )
        ).expect("Message bytes are in valid hex representation"),

        // Unknown Network ID: address within typical size range
        <Vec<u8>>::from_hex(
            concat!(
                "02", // number of entries

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "fb",                               // network id, (unknown)
                "08",                               // address length, COMPACTSIZE(8)
                "0000000000000000",                 // address, (8 zero bytes)
                "0001",                             // port, 1

                // check that variable-length encoding works
                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "02",                               // network id, IPv6
                "10",                               // address length, COMPACTSIZE(16)
                "00000000000000000000000000000001", // address, ::1
                "00f1",                             // port, 241
            )
        ).expect("Message bytes are in valid hex representation"),

        // Unknown Network ID: zero-sized address
        <Vec<u8>>::from_hex(
            concat!(
                "02", // number of entries

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "fc",                               // network id, (unknown)
                "00",                               // address length, COMPACTSIZE(0)
                "",                                 // address, (no bytes)
                "0001",                             // port, 1

                // check that variable-length encoding works
                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "02",                               // network id, IPv6
                "10",                               // address length, COMPACTSIZE(16)
                "00000000000000000000000000000001", // address, ::1
                "00f1",                             // port, 241
            )
        ).expect("Message bytes are in valid hex representation"),

        // Unknown Network ID: maximum-sized address
        <Vec<u8>>::from_hex(
            format!("{}{}{}",
                    concat!(
                        "02", // number of entries

                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                        "fd",                               // network id, (unknown)
                        "fd0002",                           // address length, COMPACTSIZE(512)
                    ),
                    "00".repeat(512),                       // address, (512 zero bytes)
                    concat!(
                        "0001",                             // port, 1

                        // check that variable-length encoding works
                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                        "02",                               // network id, IPv6
                        "10",                               // address length, COMPACTSIZE(16)
                        "00000000000000000000000000000001", // address, ::1
                        "00f1",                             // port, 241
                    )
            )
        ).expect("Message bytes are in valid hex representation"),
    ];

    /// Array of empty or unsupported ZIP-155 test vectors.
    ///
    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addrv2`].
    /// They should produce successful but empty results.
    // From https://github.com/zingolabs/zcash/blob/9ee66e423a3fbf4829ffeec354e82f4fbceff864/src/test/netbase_tests.cpp#L421
    pub static ref ADDR_V2_EMPTY_VECTORS: Vec<Vec<u8>> = vec![
        // torv3_hex
        <Vec<u8>>::from_hex(
            concat!(
                "01", // number of entries

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "04",                               // network id, TorV3
                "20",                               // address length, COMPACTSIZE(32)
                "53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88",
                                                    // address, (32 byte Tor v3 onion service public key)
                "235a",                             // port, 9050
            )
        ).expect("Message bytes are in valid hex representation"),

        // Extra test cases:
        //
        // Empty list
        <Vec<u8>>::from_hex(
            "00" // number of entries
        ).expect("Message bytes are in valid hex representation"),
    ];

    /// Array of invalid ZIP-155 test vectors.
    ///
    /// These test vectors can be read by [`zebra_network::protocol::external::Codec::read_addrv2`].
    /// They should fail deserialization.
    pub static ref ADDR_V2_INVALID_VECTORS: Vec<Vec<u8>> = vec![
        // Invalid address size: too large, but under CompactSizeMessage limit
        <Vec<u8>>::from_hex(
            format!("{}{}{}",
                    concat!(
                        "02", // number of entries

                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                        "fe",                               // network id, (unknown)
                        "fd0102",                           // invalid address length, COMPACTSIZE(513)
                    ),
                    "00".repeat(513),                       // address, (513 zero bytes)
                    concat!(
                        "0001",                             // port, 1

                        // check that the entire message is ignored
                        "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                        "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                        "02",                               // network id, IPv6
                        "10",                               // address length, COMPACTSIZE(16)
                        "00000000000000000000000000000001", // address, ::1
                        "00f1",                             // port, 241
                    )
            )
        ).expect("Message bytes are in valid hex representation"),

        // Invalid address size: too large, over CompactSizeMessage limit
        <Vec<u8>>::from_hex(
            concat!(
                "01", // number of entries

                "79627683",                         // time, Tue Nov 22 11:22:33 UTC 2039
                "01",                               // service flags, COMPACTSIZE(NODE_NETWORK)
                "ff",                               // network id, (unknown)
                "feffffff7f",                       // invalid address length, COMPACTSIZE(2^31 - 1)
                // no address, generated bytes wouldn't fit in memory
            ),
        ).expect("Message bytes are in valid hex representation"),
    ];
}