]> git.lizzy.rs Git - rust.git/blob - library/std/src/net/addr/ip/tests.rs
7c3430b2b217c5b838e22a63bf3511e846bc0f56
[rust.git] / library / std / src / net / addr / ip / tests.rs
1 use crate::net::test::{sa4, sa6, tsa};
2 use crate::net::*;
3 use crate::str::FromStr;
4
5 #[test]
6 fn test_from_str_ipv4() {
7     assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
8     assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
9     assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
10
11     // out of range
12     let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
13     assert_eq!(None, none);
14     // too short
15     let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
16     assert_eq!(None, none);
17     // too long
18     let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok();
19     assert_eq!(None, none);
20     // no number between dots
21     let none: Option<Ipv4Addr> = "255.0..1".parse().ok();
22     assert_eq!(None, none);
23     // octal
24     let none: Option<Ipv4Addr> = "255.0.0.01".parse().ok();
25     assert_eq!(None, none);
26     // octal zero
27     let none: Option<Ipv4Addr> = "255.0.0.00".parse().ok();
28     assert_eq!(None, none);
29     let none: Option<Ipv4Addr> = "255.0.00.0".parse().ok();
30     assert_eq!(None, none);
31 }
32
33 #[test]
34 fn test_from_str_ipv6() {
35     assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
36     assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
37
38     assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
39     assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
40
41     assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)), "2a02:6b8::11:11".parse());
42
43     // too long group
44     let none: Option<Ipv6Addr> = "::00000".parse().ok();
45     assert_eq!(None, none);
46     // too short
47     let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
48     assert_eq!(None, none);
49     // too long
50     let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
51     assert_eq!(None, none);
52     // triple colon
53     let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
54     assert_eq!(None, none);
55     // two double colons
56     let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok();
57     assert_eq!(None, none);
58     // `::` indicating zero groups of zeros
59     let none: Option<Ipv6Addr> = "1:2:3:4::5:6:7:8".parse().ok();
60     assert_eq!(None, none);
61 }
62
63 #[test]
64 fn test_from_str_ipv4_in_ipv6() {
65     assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)), "::192.0.2.33".parse());
66     assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)), "::FFFF:192.0.2.33".parse());
67     assert_eq!(
68         Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
69         "64:ff9b::192.0.2.33".parse()
70     );
71     assert_eq!(
72         Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
73         "2001:db8:122:c000:2:2100:192.0.2.33".parse()
74     );
75
76     // colon after v4
77     let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
78     assert_eq!(None, none);
79     // not enough groups
80     let none: Option<Ipv6Addr> = "1:2:3:4:5:127.0.0.1".parse().ok();
81     assert_eq!(None, none);
82     // too many groups
83     let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:127.0.0.1".parse().ok();
84     assert_eq!(None, none);
85 }
86
87 #[test]
88 fn test_from_str_socket_addr() {
89     assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)), "77.88.21.11:80".parse());
90     assert_eq!(Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)), "77.88.21.11:80".parse());
91     assert_eq!(
92         Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
93         "[2a02:6b8:0:1::1]:53".parse()
94     );
95     assert_eq!(
96         Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53, 0, 0)),
97         "[2a02:6b8:0:1::1]:53".parse()
98     );
99     assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)), "[::127.0.0.1]:22".parse());
100     assert_eq!(
101         Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22, 0, 0)),
102         "[::127.0.0.1]:22".parse()
103     );
104
105     // without port
106     let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
107     assert_eq!(None, none);
108     // without port
109     let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
110     assert_eq!(None, none);
111     // wrong brackets around v4
112     let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
113     assert_eq!(None, none);
114     // port out of range
115     let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
116     assert_eq!(None, none);
117 }
118
119 #[test]
120 fn ipv4_addr_to_string() {
121     assert_eq!(Ipv4Addr::new(127, 0, 0, 1).to_string(), "127.0.0.1");
122     // Short address
123     assert_eq!(Ipv4Addr::new(1, 1, 1, 1).to_string(), "1.1.1.1");
124     // Long address
125     assert_eq!(Ipv4Addr::new(127, 127, 127, 127).to_string(), "127.127.127.127");
126
127     // Test padding
128     assert_eq!(&format!("{:16}", Ipv4Addr::new(1, 1, 1, 1)), "1.1.1.1         ");
129     assert_eq!(&format!("{:>16}", Ipv4Addr::new(1, 1, 1, 1)), "         1.1.1.1");
130 }
131
132 #[test]
133 fn ipv6_addr_to_string() {
134     // ipv4-mapped address
135     let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
136     assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
137
138     // ipv4-compatible address
139     let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
140     assert_eq!(a1.to_string(), "::192.0.2.128");
141
142     // v6 address with no zero segments
143     assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(), "8:9:a:b:c:d:e:f");
144
145     // longest possible IPv6 length
146     assert_eq!(
147         Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888).to_string(),
148         "1111:2222:3333:4444:5555:6666:7777:8888"
149     );
150     // padding
151     assert_eq!(&format!("{:20}", Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8)), "1:2:3:4:5:6:7:8     ");
152     assert_eq!(&format!("{:>20}", Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8)), "     1:2:3:4:5:6:7:8");
153
154     // reduce a single run of zeros
155     assert_eq!(
156         "ae::ffff:102:304",
157         Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string()
158     );
159
160     // don't reduce just a single zero segment
161     assert_eq!("1:2:3:4:5:6:0:8", Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
162
163     // 'any' address
164     assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
165
166     // loopback address
167     assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
168
169     // ends in zeros
170     assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
171
172     // two runs of zeros, second one is longer
173     assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
174
175     // two runs of zeros, equal length
176     assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
177
178     // don't prefix `0x` to each segment in `dbg!`.
179     assert_eq!("1::4:5:0:0:8", &format!("{:#?}", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8)));
180 }
181
182 #[test]
183 fn ipv4_to_ipv6() {
184     assert_eq!(
185         Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
186         Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped()
187     );
188     assert_eq!(
189         Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
190         Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible()
191     );
192 }
193
194 #[test]
195 fn ipv6_to_ipv4_mapped() {
196     assert_eq!(
197         Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4_mapped(),
198         Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
199     );
200     assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4_mapped(), None);
201 }
202
203 #[test]
204 fn ipv6_to_ipv4() {
205     assert_eq!(
206         Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
207         Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
208     );
209     assert_eq!(
210         Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
211         Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
212     );
213     assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(), None);
214 }
215
216 #[test]
217 fn ip_properties() {
218     macro_rules! ip {
219         ($s:expr) => {
220             IpAddr::from_str($s).unwrap()
221         };
222     }
223
224     macro_rules! check {
225         ($s:expr) => {
226             check!($s, 0);
227         };
228
229         ($s:expr, $mask:expr) => {{
230             let unspec: u8 = 1 << 0;
231             let loopback: u8 = 1 << 1;
232             let global: u8 = 1 << 2;
233             let multicast: u8 = 1 << 3;
234             let doc: u8 = 1 << 4;
235             let benchmarking: u8 = 1 << 5;
236
237             if ($mask & unspec) == unspec {
238                 assert!(ip!($s).is_unspecified());
239             } else {
240                 assert!(!ip!($s).is_unspecified());
241             }
242
243             if ($mask & loopback) == loopback {
244                 assert!(ip!($s).is_loopback());
245             } else {
246                 assert!(!ip!($s).is_loopback());
247             }
248
249             if ($mask & global) == global {
250                 assert!(ip!($s).is_global());
251             } else {
252                 assert!(!ip!($s).is_global());
253             }
254
255             if ($mask & multicast) == multicast {
256                 assert!(ip!($s).is_multicast());
257             } else {
258                 assert!(!ip!($s).is_multicast());
259             }
260
261             if ($mask & doc) == doc {
262                 assert!(ip!($s).is_documentation());
263             } else {
264                 assert!(!ip!($s).is_documentation());
265             }
266
267             if ($mask & benchmarking) == benchmarking {
268                 assert!(ip!($s).is_benchmarking());
269             } else {
270                 assert!(!ip!($s).is_benchmarking());
271             }
272         }};
273     }
274
275     let unspec: u8 = 1 << 0;
276     let loopback: u8 = 1 << 1;
277     let global: u8 = 1 << 2;
278     let multicast: u8 = 1 << 3;
279     let doc: u8 = 1 << 4;
280     let benchmarking: u8 = 1 << 5;
281
282     check!("0.0.0.0", unspec);
283     check!("0.0.0.1");
284     check!("0.1.0.0");
285     check!("10.9.8.7");
286     check!("127.1.2.3", loopback);
287     check!("172.31.254.253");
288     check!("169.254.253.242");
289     check!("192.0.2.183", doc);
290     check!("192.1.2.183", global);
291     check!("192.168.254.253");
292     check!("198.51.100.0", doc);
293     check!("203.0.113.0", doc);
294     check!("203.2.113.0", global);
295     check!("224.0.0.0", global | multicast);
296     check!("239.255.255.255", global | multicast);
297     check!("255.255.255.255");
298     // make sure benchmarking addresses are not global
299     check!("198.18.0.0", benchmarking);
300     check!("198.18.54.2", benchmarking);
301     check!("198.19.255.255", benchmarking);
302     // make sure addresses reserved for protocol assignment are not global
303     check!("192.0.0.0");
304     check!("192.0.0.255");
305     check!("192.0.0.100");
306     // make sure reserved addresses are not global
307     check!("240.0.0.0");
308     check!("251.54.1.76");
309     check!("254.255.255.255");
310     // make sure shared addresses are not global
311     check!("100.64.0.0");
312     check!("100.127.255.255");
313     check!("100.100.100.0");
314
315     check!("::", unspec);
316     check!("::1", loopback);
317     check!("::0.0.0.2", global);
318     check!("1::", global);
319     check!("fc00::");
320     check!("fdff:ffff::");
321     check!("fe80:ffff::");
322     check!("febf:ffff::");
323     check!("fec0::", global);
324     check!("ff01::", global | multicast);
325     check!("ff02::", global | multicast);
326     check!("ff03::", global | multicast);
327     check!("ff04::", global | multicast);
328     check!("ff05::", global | multicast);
329     check!("ff08::", global | multicast);
330     check!("ff0e::", global | multicast);
331     check!("2001:db8:85a3::8a2e:370:7334", doc);
332     check!("2001:2::ac32:23ff:21", benchmarking);
333     check!("102:304:506:708:90a:b0c:d0e:f10", global);
334 }
335
336 #[test]
337 fn ipv4_properties() {
338     macro_rules! ip {
339         ($s:expr) => {
340             Ipv4Addr::from_str($s).unwrap()
341         };
342     }
343
344     macro_rules! check {
345         ($s:expr) => {
346             check!($s, 0);
347         };
348
349         ($s:expr, $mask:expr) => {{
350             let unspec: u16 = 1 << 0;
351             let loopback: u16 = 1 << 1;
352             let private: u16 = 1 << 2;
353             let link_local: u16 = 1 << 3;
354             let global: u16 = 1 << 4;
355             let multicast: u16 = 1 << 5;
356             let broadcast: u16 = 1 << 6;
357             let documentation: u16 = 1 << 7;
358             let benchmarking: u16 = 1 << 8;
359             let reserved: u16 = 1 << 10;
360             let shared: u16 = 1 << 11;
361
362             if ($mask & unspec) == unspec {
363                 assert!(ip!($s).is_unspecified());
364             } else {
365                 assert!(!ip!($s).is_unspecified());
366             }
367
368             if ($mask & loopback) == loopback {
369                 assert!(ip!($s).is_loopback());
370             } else {
371                 assert!(!ip!($s).is_loopback());
372             }
373
374             if ($mask & private) == private {
375                 assert!(ip!($s).is_private());
376             } else {
377                 assert!(!ip!($s).is_private());
378             }
379
380             if ($mask & link_local) == link_local {
381                 assert!(ip!($s).is_link_local());
382             } else {
383                 assert!(!ip!($s).is_link_local());
384             }
385
386             if ($mask & global) == global {
387                 assert!(ip!($s).is_global());
388             } else {
389                 assert!(!ip!($s).is_global());
390             }
391
392             if ($mask & multicast) == multicast {
393                 assert!(ip!($s).is_multicast());
394             } else {
395                 assert!(!ip!($s).is_multicast());
396             }
397
398             if ($mask & broadcast) == broadcast {
399                 assert!(ip!($s).is_broadcast());
400             } else {
401                 assert!(!ip!($s).is_broadcast());
402             }
403
404             if ($mask & documentation) == documentation {
405                 assert!(ip!($s).is_documentation());
406             } else {
407                 assert!(!ip!($s).is_documentation());
408             }
409
410             if ($mask & benchmarking) == benchmarking {
411                 assert!(ip!($s).is_benchmarking());
412             } else {
413                 assert!(!ip!($s).is_benchmarking());
414             }
415
416             if ($mask & reserved) == reserved {
417                 assert!(ip!($s).is_reserved());
418             } else {
419                 assert!(!ip!($s).is_reserved());
420             }
421
422             if ($mask & shared) == shared {
423                 assert!(ip!($s).is_shared());
424             } else {
425                 assert!(!ip!($s).is_shared());
426             }
427         }};
428     }
429
430     let unspec: u16 = 1 << 0;
431     let loopback: u16 = 1 << 1;
432     let private: u16 = 1 << 2;
433     let link_local: u16 = 1 << 3;
434     let global: u16 = 1 << 4;
435     let multicast: u16 = 1 << 5;
436     let broadcast: u16 = 1 << 6;
437     let documentation: u16 = 1 << 7;
438     let benchmarking: u16 = 1 << 8;
439     let reserved: u16 = 1 << 10;
440     let shared: u16 = 1 << 11;
441
442     check!("0.0.0.0", unspec);
443     check!("0.0.0.1");
444     check!("0.1.0.0");
445     check!("10.9.8.7", private);
446     check!("127.1.2.3", loopback);
447     check!("172.31.254.253", private);
448     check!("169.254.253.242", link_local);
449     check!("192.0.2.183", documentation);
450     check!("192.1.2.183", global);
451     check!("192.168.254.253", private);
452     check!("198.51.100.0", documentation);
453     check!("203.0.113.0", documentation);
454     check!("203.2.113.0", global);
455     check!("224.0.0.0", global | multicast);
456     check!("239.255.255.255", global | multicast);
457     check!("255.255.255.255", broadcast);
458     check!("198.18.0.0", benchmarking);
459     check!("198.18.54.2", benchmarking);
460     check!("198.19.255.255", benchmarking);
461     check!("192.0.0.0");
462     check!("192.0.0.255");
463     check!("192.0.0.100");
464     check!("240.0.0.0", reserved);
465     check!("251.54.1.76", reserved);
466     check!("254.255.255.255", reserved);
467     check!("100.64.0.0", shared);
468     check!("100.127.255.255", shared);
469     check!("100.100.100.0", shared);
470 }
471
472 #[test]
473 fn ipv6_properties() {
474     macro_rules! ip {
475         ($s:expr) => {
476             Ipv6Addr::from_str($s).unwrap()
477         };
478     }
479
480     macro_rules! check {
481         ($s:expr, &[$($octet:expr),*], $mask:expr) => {
482             assert_eq!($s, ip!($s).to_string());
483             let octets = &[$($octet),*];
484             assert_eq!(&ip!($s).octets(), octets);
485             assert_eq!(Ipv6Addr::from(*octets), ip!($s));
486
487             let unspecified: u32 = 1 << 0;
488             let loopback: u32 = 1 << 1;
489             let unique_local: u32 = 1 << 2;
490             let global: u32 = 1 << 3;
491             let unicast_link_local: u32 = 1 << 4;
492             let unicast_global: u32 = 1 << 7;
493             let documentation: u32 = 1 << 8;
494             let benchmarking: u32 = 1 << 16;
495             let multicast_interface_local: u32 = 1 << 9;
496             let multicast_link_local: u32 = 1 << 10;
497             let multicast_realm_local: u32 = 1 << 11;
498             let multicast_admin_local: u32 = 1 << 12;
499             let multicast_site_local: u32 = 1 << 13;
500             let multicast_organization_local: u32 = 1 << 14;
501             let multicast_global: u32 = 1 << 15;
502             let multicast: u32 = multicast_interface_local
503                 | multicast_admin_local
504                 | multicast_global
505                 | multicast_link_local
506                 | multicast_realm_local
507                 | multicast_site_local
508                 | multicast_organization_local;
509
510             if ($mask & unspecified) == unspecified {
511                 assert!(ip!($s).is_unspecified());
512             } else {
513                 assert!(!ip!($s).is_unspecified());
514             }
515             if ($mask & loopback) == loopback {
516                 assert!(ip!($s).is_loopback());
517             } else {
518                 assert!(!ip!($s).is_loopback());
519             }
520             if ($mask & unique_local) == unique_local {
521                 assert!(ip!($s).is_unique_local());
522             } else {
523                 assert!(!ip!($s).is_unique_local());
524             }
525             if ($mask & global) == global {
526                 assert!(ip!($s).is_global());
527             } else {
528                 assert!(!ip!($s).is_global());
529             }
530             if ($mask & unicast_link_local) == unicast_link_local {
531                 assert!(ip!($s).is_unicast_link_local());
532             } else {
533                 assert!(!ip!($s).is_unicast_link_local());
534             }
535             if ($mask & unicast_global) == unicast_global {
536                 assert!(ip!($s).is_unicast_global());
537             } else {
538                 assert!(!ip!($s).is_unicast_global());
539             }
540             if ($mask & documentation) == documentation {
541                 assert!(ip!($s).is_documentation());
542             } else {
543                 assert!(!ip!($s).is_documentation());
544             }
545             if ($mask & benchmarking) == benchmarking {
546                 assert!(ip!($s).is_benchmarking());
547             } else {
548                 assert!(!ip!($s).is_benchmarking());
549             }
550             if ($mask & multicast) != 0 {
551                 assert!(ip!($s).multicast_scope().is_some());
552                 assert!(ip!($s).is_multicast());
553             } else {
554                 assert!(ip!($s).multicast_scope().is_none());
555                 assert!(!ip!($s).is_multicast());
556             }
557             if ($mask & multicast_interface_local) == multicast_interface_local {
558                 assert_eq!(ip!($s).multicast_scope().unwrap(),
559                            Ipv6MulticastScope::InterfaceLocal);
560             }
561             if ($mask & multicast_link_local) == multicast_link_local {
562                 assert_eq!(ip!($s).multicast_scope().unwrap(),
563                            Ipv6MulticastScope::LinkLocal);
564             }
565             if ($mask & multicast_realm_local) == multicast_realm_local {
566                 assert_eq!(ip!($s).multicast_scope().unwrap(),
567                            Ipv6MulticastScope::RealmLocal);
568             }
569             if ($mask & multicast_admin_local) == multicast_admin_local {
570                 assert_eq!(ip!($s).multicast_scope().unwrap(),
571                            Ipv6MulticastScope::AdminLocal);
572             }
573             if ($mask & multicast_site_local) == multicast_site_local {
574                 assert_eq!(ip!($s).multicast_scope().unwrap(),
575                            Ipv6MulticastScope::SiteLocal);
576             }
577             if ($mask & multicast_organization_local) == multicast_organization_local {
578                 assert_eq!(ip!($s).multicast_scope().unwrap(),
579                            Ipv6MulticastScope::OrganizationLocal);
580             }
581             if ($mask & multicast_global) == multicast_global {
582                 assert_eq!(ip!($s).multicast_scope().unwrap(),
583                            Ipv6MulticastScope::Global);
584             }
585         }
586     }
587
588     let unspecified: u32 = 1 << 0;
589     let loopback: u32 = 1 << 1;
590     let unique_local: u32 = 1 << 2;
591     let global: u32 = 1 << 3;
592     let unicast_link_local: u32 = 1 << 4;
593     let unicast_global: u32 = 1 << 7;
594     let documentation: u32 = 1 << 8;
595     let benchmarking: u32 = 1 << 16;
596     let multicast_interface_local: u32 = 1 << 9;
597     let multicast_link_local: u32 = 1 << 10;
598     let multicast_realm_local: u32 = 1 << 11;
599     let multicast_admin_local: u32 = 1 << 12;
600     let multicast_site_local: u32 = 1 << 13;
601     let multicast_organization_local: u32 = 1 << 14;
602     let multicast_global: u32 = 1 << 15;
603
604     check!("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unspecified);
605
606     check!("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], loopback);
607
608     check!("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], global | unicast_global);
609
610     check!("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);
611
612     check!(
613         "::ffff:127.0.0.1",
614         &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1],
615         unicast_global
616     );
617
618     check!(
619         "64:ff9b:1::",
620         &[0, 0x64, 0xff, 0x9b, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
621         unicast_global
622     );
623
624     check!("100::", &[0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
625
626     check!("2001::", &[0x20, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
627
628     check!(
629         "2001:1::1",
630         &[0x20, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
631         global | unicast_global
632     );
633
634     check!(
635         "2001:1::2",
636         &[0x20, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
637         global | unicast_global
638     );
639
640     check!(
641         "2001:3::",
642         &[0x20, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
643         global | unicast_global
644     );
645
646     check!(
647         "2001:4:112::",
648         &[0x20, 1, 0, 4, 1, 0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
649         global | unicast_global
650     );
651
652     check!(
653         "2001:20::",
654         &[0x20, 1, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
655         global | unicast_global
656     );
657
658     check!("2001:30::", &[0x20, 1, 0, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
659
660     check!(
661         "2001:200::",
662         &[0x20, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
663         global | unicast_global
664     );
665
666     check!("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unique_local);
667
668     check!(
669         "fdff:ffff::",
670         &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
671         unique_local
672     );
673
674     check!(
675         "fe80:ffff::",
676         &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
677         unicast_link_local
678     );
679
680     check!("fe80::", &[0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_link_local);
681
682     check!(
683         "febf:ffff::",
684         &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
685         unicast_link_local
686     );
687
688     check!("febf::", &[0xfe, 0xbf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_link_local);
689
690     check!(
691         "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
692         &[
693             0xfe, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
694             0xff, 0xff
695         ],
696         unicast_link_local
697     );
698
699     check!(
700         "fe80::ffff:ffff:ffff:ffff",
701         &[
702             0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
703             0xff, 0xff
704         ],
705         unicast_link_local
706     );
707
708     check!(
709         "fe80:0:0:1::",
710         &[0xfe, 0x80, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
711         unicast_link_local
712     );
713
714     check!(
715         "fec0::",
716         &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
717         unicast_global | global
718     );
719
720     check!(
721         "ff01::",
722         &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
723         multicast_interface_local | global
724     );
725
726     check!(
727         "ff02::",
728         &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
729         multicast_link_local | global
730     );
731
732     check!(
733         "ff03::",
734         &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
735         multicast_realm_local | global
736     );
737
738     check!(
739         "ff04::",
740         &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
741         multicast_admin_local | global
742     );
743
744     check!(
745         "ff05::",
746         &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
747         multicast_site_local | global
748     );
749
750     check!(
751         "ff08::",
752         &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
753         multicast_organization_local | global
754     );
755
756     check!(
757         "ff0e::",
758         &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
759         multicast_global | global
760     );
761
762     check!(
763         "2001:db8:85a3::8a2e:370:7334",
764         &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
765         documentation
766     );
767
768     check!(
769         "2001:2::ac32:23ff:21",
770         &[0x20, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0xac, 0x32, 0x23, 0xff, 0, 0x21],
771         benchmarking
772     );
773
774     check!(
775         "102:304:506:708:90a:b0c:d0e:f10",
776         &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
777         global | unicast_global
778     );
779 }
780
781 #[test]
782 fn to_socket_addr_socketaddr() {
783     let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345);
784     assert_eq!(Ok(vec![a]), tsa(a));
785 }
786
787 #[test]
788 fn test_ipv4_to_int() {
789     let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
790     assert_eq!(u32::from(a), 0x11223344);
791 }
792
793 #[test]
794 fn test_int_to_ipv4() {
795     let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
796     assert_eq!(Ipv4Addr::from(0x11223344), a);
797 }
798
799 #[test]
800 fn test_ipv6_to_int() {
801     let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
802     assert_eq!(u128::from(a), 0x112233445566778899aabbccddeeff11u128);
803 }
804
805 #[test]
806 fn test_int_to_ipv6() {
807     let a = Ipv6Addr::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
808     assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a);
809 }
810
811 #[test]
812 fn ipv4_from_constructors() {
813     assert_eq!(Ipv4Addr::LOCALHOST, Ipv4Addr::new(127, 0, 0, 1));
814     assert!(Ipv4Addr::LOCALHOST.is_loopback());
815     assert_eq!(Ipv4Addr::UNSPECIFIED, Ipv4Addr::new(0, 0, 0, 0));
816     assert!(Ipv4Addr::UNSPECIFIED.is_unspecified());
817     assert_eq!(Ipv4Addr::BROADCAST, Ipv4Addr::new(255, 255, 255, 255));
818     assert!(Ipv4Addr::BROADCAST.is_broadcast());
819 }
820
821 #[test]
822 fn ipv6_from_constructors() {
823     assert_eq!(Ipv6Addr::LOCALHOST, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
824     assert!(Ipv6Addr::LOCALHOST.is_loopback());
825     assert_eq!(Ipv6Addr::UNSPECIFIED, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
826     assert!(Ipv6Addr::UNSPECIFIED.is_unspecified());
827 }
828
829 #[test]
830 fn ipv4_from_octets() {
831     assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
832 }
833
834 #[test]
835 fn ipv6_from_segments() {
836     let from_u16s =
837         Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
838     let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff);
839     assert_eq!(new, from_u16s);
840 }
841
842 #[test]
843 fn ipv6_from_octets() {
844     let from_u16s =
845         Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
846     let from_u8s = Ipv6Addr::from([
847         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
848         0xff,
849     ]);
850     assert_eq!(from_u16s, from_u8s);
851 }
852
853 #[test]
854 fn cmp() {
855     let v41 = Ipv4Addr::new(100, 64, 3, 3);
856     let v42 = Ipv4Addr::new(192, 0, 2, 2);
857     let v61 = "2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap();
858     let v62 = "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap();
859     assert!(v41 < v42);
860     assert!(v61 < v62);
861
862     assert_eq!(v41, IpAddr::V4(v41));
863     assert_eq!(v61, IpAddr::V6(v61));
864     assert!(v41 != IpAddr::V4(v42));
865     assert!(v61 != IpAddr::V6(v62));
866
867     assert!(v41 < IpAddr::V4(v42));
868     assert!(v61 < IpAddr::V6(v62));
869     assert!(IpAddr::V4(v41) < v42);
870     assert!(IpAddr::V6(v61) < v62);
871
872     assert!(v41 < IpAddr::V6(v61));
873     assert!(IpAddr::V4(v41) < v61);
874 }
875
876 #[test]
877 fn is_v4() {
878     let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
879     assert!(ip.is_ipv4());
880     assert!(!ip.is_ipv6());
881 }
882
883 #[test]
884 fn is_v6() {
885     let ip = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
886     assert!(!ip.is_ipv4());
887     assert!(ip.is_ipv6());
888 }
889
890 #[test]
891 fn ipv4_const() {
892     // test that the methods of `Ipv4Addr` are usable in a const context
893
894     const IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
895     assert_eq!(IP_ADDRESS, Ipv4Addr::LOCALHOST);
896
897     const OCTETS: [u8; 4] = IP_ADDRESS.octets();
898     assert_eq!(OCTETS, [127, 0, 0, 1]);
899
900     const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
901     assert!(!IS_UNSPECIFIED);
902
903     const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
904     assert!(IS_LOOPBACK);
905
906     const IS_PRIVATE: bool = IP_ADDRESS.is_private();
907     assert!(!IS_PRIVATE);
908
909     const IS_LINK_LOCAL: bool = IP_ADDRESS.is_link_local();
910     assert!(!IS_LINK_LOCAL);
911
912     const IS_GLOBAL: bool = IP_ADDRESS.is_global();
913     assert!(!IS_GLOBAL);
914
915     const IS_SHARED: bool = IP_ADDRESS.is_shared();
916     assert!(!IS_SHARED);
917
918     const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
919     assert!(!IS_BENCHMARKING);
920
921     const IS_RESERVED: bool = IP_ADDRESS.is_reserved();
922     assert!(!IS_RESERVED);
923
924     const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
925     assert!(!IS_MULTICAST);
926
927     const IS_BROADCAST: bool = IP_ADDRESS.is_broadcast();
928     assert!(!IS_BROADCAST);
929
930     const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
931     assert!(!IS_DOCUMENTATION);
932
933     const IP_V6_COMPATIBLE: Ipv6Addr = IP_ADDRESS.to_ipv6_compatible();
934     assert_eq!(
935         IP_V6_COMPATIBLE,
936         Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1])
937     );
938
939     const IP_V6_MAPPED: Ipv6Addr = IP_ADDRESS.to_ipv6_mapped();
940     assert_eq!(
941         IP_V6_MAPPED,
942         Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1])
943     );
944 }
945
946 #[test]
947 fn ipv6_const() {
948     // test that the methods of `Ipv6Addr` are usable in a const context
949
950     const IP_ADDRESS: Ipv6Addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
951     assert_eq!(IP_ADDRESS, Ipv6Addr::LOCALHOST);
952
953     const SEGMENTS: [u16; 8] = IP_ADDRESS.segments();
954     assert_eq!(SEGMENTS, [0, 0, 0, 0, 0, 0, 0, 1]);
955
956     const OCTETS: [u8; 16] = IP_ADDRESS.octets();
957     assert_eq!(OCTETS, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
958
959     const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
960     assert!(!IS_UNSPECIFIED);
961
962     const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
963     assert!(IS_LOOPBACK);
964
965     const IS_GLOBAL: bool = IP_ADDRESS.is_global();
966     assert!(!IS_GLOBAL);
967
968     const IS_UNIQUE_LOCAL: bool = IP_ADDRESS.is_unique_local();
969     assert!(!IS_UNIQUE_LOCAL);
970
971     const IS_UNICAST_LINK_LOCAL: bool = IP_ADDRESS.is_unicast_link_local();
972     assert!(!IS_UNICAST_LINK_LOCAL);
973
974     const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
975     assert!(!IS_DOCUMENTATION);
976
977     const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
978     assert!(!IS_BENCHMARKING);
979
980     const IS_UNICAST_GLOBAL: bool = IP_ADDRESS.is_unicast_global();
981     assert!(!IS_UNICAST_GLOBAL);
982
983     const MULTICAST_SCOPE: Option<Ipv6MulticastScope> = IP_ADDRESS.multicast_scope();
984     assert_eq!(MULTICAST_SCOPE, None);
985
986     const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
987     assert!(!IS_MULTICAST);
988
989     const IP_V4: Option<Ipv4Addr> = IP_ADDRESS.to_ipv4();
990     assert_eq!(IP_V4.unwrap(), Ipv4Addr::new(0, 0, 0, 1));
991 }
992
993 #[test]
994 fn ip_const() {
995     // test that the methods of `IpAddr` are usable in a const context
996
997     const IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
998
999     const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
1000     assert!(!IS_UNSPECIFIED);
1001
1002     const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
1003     assert!(IS_LOOPBACK);
1004
1005     const IS_GLOBAL: bool = IP_ADDRESS.is_global();
1006     assert!(!IS_GLOBAL);
1007
1008     const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
1009     assert!(!IS_MULTICAST);
1010
1011     const IS_IP_V4: bool = IP_ADDRESS.is_ipv4();
1012     assert!(IS_IP_V4);
1013
1014     const IS_IP_V6: bool = IP_ADDRESS.is_ipv6();
1015     assert!(!IS_IP_V6);
1016 }
1017
1018 #[test]
1019 fn structural_match() {
1020     // test that all IP types can be structurally matched upon
1021
1022     const IPV4: Ipv4Addr = Ipv4Addr::LOCALHOST;
1023     match IPV4 {
1024         Ipv4Addr::LOCALHOST => {}
1025         _ => unreachable!(),
1026     }
1027
1028     const IPV6: Ipv6Addr = Ipv6Addr::LOCALHOST;
1029     match IPV6 {
1030         Ipv6Addr::LOCALHOST => {}
1031         _ => unreachable!(),
1032     }
1033
1034     const IP: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
1035     match IP {
1036         IpAddr::V4(Ipv4Addr::LOCALHOST) => {}
1037         _ => unreachable!(),
1038     }
1039 }