1 use crate::net::test::{sa4, sa6, tsa};
3 use crate::str::FromStr;
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());
12 let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok();
13 assert_eq!(None, none);
15 let none: Option<Ipv4Addr> = "255.0.0".parse().ok();
16 assert_eq!(None, none);
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);
24 let none: Option<Ipv4Addr> = "255.0.0.01".parse().ok();
25 assert_eq!(None, none);
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);
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());
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());
41 assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)), "2a02:6b8::11:11".parse());
44 let none: Option<Ipv6Addr> = "::00000".parse().ok();
45 assert_eq!(None, none);
47 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
48 assert_eq!(None, none);
50 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
51 assert_eq!(None, none);
53 let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
54 assert_eq!(None, none);
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);
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());
68 Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
69 "64:ff9b::192.0.2.33".parse()
72 Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
73 "2001:db8:122:c000:2:2100:192.0.2.33".parse()
77 let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
78 assert_eq!(None, none);
80 let none: Option<Ipv6Addr> = "1:2:3:4:5:127.0.0.1".parse().ok();
81 assert_eq!(None, none);
83 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:127.0.0.1".parse().ok();
84 assert_eq!(None, none);
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());
92 Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
93 "[2a02:6b8:0:1::1]:53".parse()
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()
99 assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)), "[::127.0.0.1]:22".parse());
101 Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22, 0, 0)),
102 "[::127.0.0.1]:22".parse()
106 let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
107 assert_eq!(None, none);
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);
115 let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
116 assert_eq!(None, none);
120 fn ipv4_addr_to_string() {
121 assert_eq!(Ipv4Addr::new(127, 0, 0, 1).to_string(), "127.0.0.1");
123 assert_eq!(Ipv4Addr::new(1, 1, 1, 1).to_string(), "1.1.1.1");
125 assert_eq!(Ipv4Addr::new(127, 127, 127, 127).to_string(), "127.127.127.127");
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");
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");
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");
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");
145 // longest possible IPv6 length
147 Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888).to_string(),
148 "1111:2222:3333:4444:5555:6666:7777:8888"
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");
154 // reduce a single run of zeros
157 Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string()
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());
164 assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
167 assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
170 assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
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());
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());
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)));
185 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
186 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped()
189 Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
190 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible()
195 fn ipv6_to_ipv4_mapped() {
197 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4_mapped(),
198 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
200 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4_mapped(), None);
206 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
207 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
210 Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
211 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
213 assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(), None);
220 IpAddr::from_str($s).unwrap()
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;
237 if ($mask & unspec) == unspec {
238 assert!(ip!($s).is_unspecified());
240 assert!(!ip!($s).is_unspecified());
243 if ($mask & loopback) == loopback {
244 assert!(ip!($s).is_loopback());
246 assert!(!ip!($s).is_loopback());
249 if ($mask & global) == global {
250 assert!(ip!($s).is_global());
252 assert!(!ip!($s).is_global());
255 if ($mask & multicast) == multicast {
256 assert!(ip!($s).is_multicast());
258 assert!(!ip!($s).is_multicast());
261 if ($mask & doc) == doc {
262 assert!(ip!($s).is_documentation());
264 assert!(!ip!($s).is_documentation());
267 if ($mask & benchmarking) == benchmarking {
268 assert!(ip!($s).is_benchmarking());
270 assert!(!ip!($s).is_benchmarking());
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;
282 check!("0.0.0.0", unspec);
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
304 check!("192.0.0.255");
305 check!("192.0.0.100");
306 // make sure reserved addresses are not global
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");
315 check!("::", unspec);
316 check!("::1", loopback);
317 check!("::0.0.0.2", global);
318 check!("1::", global);
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);
337 fn ipv4_properties() {
340 Ipv4Addr::from_str($s).unwrap()
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;
362 if ($mask & unspec) == unspec {
363 assert!(ip!($s).is_unspecified());
365 assert!(!ip!($s).is_unspecified());
368 if ($mask & loopback) == loopback {
369 assert!(ip!($s).is_loopback());
371 assert!(!ip!($s).is_loopback());
374 if ($mask & private) == private {
375 assert!(ip!($s).is_private());
377 assert!(!ip!($s).is_private());
380 if ($mask & link_local) == link_local {
381 assert!(ip!($s).is_link_local());
383 assert!(!ip!($s).is_link_local());
386 if ($mask & global) == global {
387 assert!(ip!($s).is_global());
389 assert!(!ip!($s).is_global());
392 if ($mask & multicast) == multicast {
393 assert!(ip!($s).is_multicast());
395 assert!(!ip!($s).is_multicast());
398 if ($mask & broadcast) == broadcast {
399 assert!(ip!($s).is_broadcast());
401 assert!(!ip!($s).is_broadcast());
404 if ($mask & documentation) == documentation {
405 assert!(ip!($s).is_documentation());
407 assert!(!ip!($s).is_documentation());
410 if ($mask & benchmarking) == benchmarking {
411 assert!(ip!($s).is_benchmarking());
413 assert!(!ip!($s).is_benchmarking());
416 if ($mask & reserved) == reserved {
417 assert!(ip!($s).is_reserved());
419 assert!(!ip!($s).is_reserved());
422 if ($mask & shared) == shared {
423 assert!(ip!($s).is_shared());
425 assert!(!ip!($s).is_shared());
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;
442 check!("0.0.0.0", unspec);
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);
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);
473 fn ipv6_properties() {
476 Ipv6Addr::from_str($s).unwrap()
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));
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
505 | multicast_link_local
506 | multicast_realm_local
507 | multicast_site_local
508 | multicast_organization_local;
510 if ($mask & unspecified) == unspecified {
511 assert!(ip!($s).is_unspecified());
513 assert!(!ip!($s).is_unspecified());
515 if ($mask & loopback) == loopback {
516 assert!(ip!($s).is_loopback());
518 assert!(!ip!($s).is_loopback());
520 if ($mask & unique_local) == unique_local {
521 assert!(ip!($s).is_unique_local());
523 assert!(!ip!($s).is_unique_local());
525 if ($mask & global) == global {
526 assert!(ip!($s).is_global());
528 assert!(!ip!($s).is_global());
530 if ($mask & unicast_link_local) == unicast_link_local {
531 assert!(ip!($s).is_unicast_link_local());
533 assert!(!ip!($s).is_unicast_link_local());
535 if ($mask & unicast_global) == unicast_global {
536 assert!(ip!($s).is_unicast_global());
538 assert!(!ip!($s).is_unicast_global());
540 if ($mask & documentation) == documentation {
541 assert!(ip!($s).is_documentation());
543 assert!(!ip!($s).is_documentation());
545 if ($mask & benchmarking) == benchmarking {
546 assert!(ip!($s).is_benchmarking());
548 assert!(!ip!($s).is_benchmarking());
550 if ($mask & multicast) != 0 {
551 assert!(ip!($s).multicast_scope().is_some());
552 assert!(ip!($s).is_multicast());
554 assert!(ip!($s).multicast_scope().is_none());
555 assert!(!ip!($s).is_multicast());
557 if ($mask & multicast_interface_local) == multicast_interface_local {
558 assert_eq!(ip!($s).multicast_scope().unwrap(),
559 Ipv6MulticastScope::InterfaceLocal);
561 if ($mask & multicast_link_local) == multicast_link_local {
562 assert_eq!(ip!($s).multicast_scope().unwrap(),
563 Ipv6MulticastScope::LinkLocal);
565 if ($mask & multicast_realm_local) == multicast_realm_local {
566 assert_eq!(ip!($s).multicast_scope().unwrap(),
567 Ipv6MulticastScope::RealmLocal);
569 if ($mask & multicast_admin_local) == multicast_admin_local {
570 assert_eq!(ip!($s).multicast_scope().unwrap(),
571 Ipv6MulticastScope::AdminLocal);
573 if ($mask & multicast_site_local) == multicast_site_local {
574 assert_eq!(ip!($s).multicast_scope().unwrap(),
575 Ipv6MulticastScope::SiteLocal);
577 if ($mask & multicast_organization_local) == multicast_organization_local {
578 assert_eq!(ip!($s).multicast_scope().unwrap(),
579 Ipv6MulticastScope::OrganizationLocal);
581 if ($mask & multicast_global) == multicast_global {
582 assert_eq!(ip!($s).multicast_scope().unwrap(),
583 Ipv6MulticastScope::Global);
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;
604 check!("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unspecified);
606 check!("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], loopback);
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);
610 check!("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);
614 &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 1],
620 &[0, 0x64, 0xff, 0x9b, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
624 check!("100::", &[0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
626 check!("2001::", &[0x20, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
630 &[0x20, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
631 global | unicast_global
636 &[0x20, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
637 global | unicast_global
642 &[0x20, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
643 global | unicast_global
648 &[0x20, 1, 0, 4, 1, 0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
649 global | unicast_global
654 &[0x20, 1, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
655 global | unicast_global
658 check!("2001:30::", &[0x20, 1, 0, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
662 &[0x20, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
663 global | unicast_global
666 check!("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unique_local);
670 &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
676 &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
680 check!("fe80::", &[0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_link_local);
684 &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
688 check!("febf::", &[0xfe, 0xbf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_link_local);
691 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
693 0xfe, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
700 "fe80::ffff:ffff:ffff:ffff",
702 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
710 &[0xfe, 0x80, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
716 &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
717 unicast_global | global
722 &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
723 multicast_interface_local | global
728 &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
729 multicast_link_local | global
734 &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
735 multicast_realm_local | global
740 &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
741 multicast_admin_local | global
746 &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
747 multicast_site_local | global
752 &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
753 multicast_organization_local | global
758 &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
759 multicast_global | global
763 "2001:db8:85a3::8a2e:370:7334",
764 &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
769 "2001:2::ac32:23ff:21",
770 &[0x20, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0xac, 0x32, 0x23, 0xff, 0, 0x21],
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
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));
788 fn test_ipv4_to_int() {
789 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
790 assert_eq!(u32::from(a), 0x11223344);
794 fn test_int_to_ipv4() {
795 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
796 assert_eq!(Ipv4Addr::from(0x11223344), a);
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);
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);
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());
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());
830 fn ipv4_from_octets() {
831 assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
835 fn ipv6_from_segments() {
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);
843 fn ipv6_from_octets() {
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,
850 assert_eq!(from_u16s, from_u8s);
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();
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));
867 assert!(v41 < IpAddr::V4(v42));
868 assert!(v61 < IpAddr::V6(v62));
869 assert!(IpAddr::V4(v41) < v42);
870 assert!(IpAddr::V6(v61) < v62);
872 assert!(v41 < IpAddr::V6(v61));
873 assert!(IpAddr::V4(v41) < v61);
878 let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
879 assert!(ip.is_ipv4());
880 assert!(!ip.is_ipv6());
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());
892 // test that the methods of `Ipv4Addr` are usable in a const context
894 const IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
895 assert_eq!(IP_ADDRESS, Ipv4Addr::LOCALHOST);
897 const OCTETS: [u8; 4] = IP_ADDRESS.octets();
898 assert_eq!(OCTETS, [127, 0, 0, 1]);
900 const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
901 assert!(!IS_UNSPECIFIED);
903 const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
904 assert!(IS_LOOPBACK);
906 const IS_PRIVATE: bool = IP_ADDRESS.is_private();
907 assert!(!IS_PRIVATE);
909 const IS_LINK_LOCAL: bool = IP_ADDRESS.is_link_local();
910 assert!(!IS_LINK_LOCAL);
912 const IS_GLOBAL: bool = IP_ADDRESS.is_global();
915 const IS_SHARED: bool = IP_ADDRESS.is_shared();
918 const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
919 assert!(!IS_BENCHMARKING);
921 const IS_RESERVED: bool = IP_ADDRESS.is_reserved();
922 assert!(!IS_RESERVED);
924 const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
925 assert!(!IS_MULTICAST);
927 const IS_BROADCAST: bool = IP_ADDRESS.is_broadcast();
928 assert!(!IS_BROADCAST);
930 const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
931 assert!(!IS_DOCUMENTATION);
933 const IP_V6_COMPATIBLE: Ipv6Addr = IP_ADDRESS.to_ipv6_compatible();
936 Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1])
939 const IP_V6_MAPPED: Ipv6Addr = IP_ADDRESS.to_ipv6_mapped();
942 Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1])
948 // test that the methods of `Ipv6Addr` are usable in a const context
950 const IP_ADDRESS: Ipv6Addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
951 assert_eq!(IP_ADDRESS, Ipv6Addr::LOCALHOST);
953 const SEGMENTS: [u16; 8] = IP_ADDRESS.segments();
954 assert_eq!(SEGMENTS, [0, 0, 0, 0, 0, 0, 0, 1]);
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]);
959 const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
960 assert!(!IS_UNSPECIFIED);
962 const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
963 assert!(IS_LOOPBACK);
965 const IS_GLOBAL: bool = IP_ADDRESS.is_global();
968 const IS_UNIQUE_LOCAL: bool = IP_ADDRESS.is_unique_local();
969 assert!(!IS_UNIQUE_LOCAL);
971 const IS_UNICAST_LINK_LOCAL: bool = IP_ADDRESS.is_unicast_link_local();
972 assert!(!IS_UNICAST_LINK_LOCAL);
974 const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
975 assert!(!IS_DOCUMENTATION);
977 const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
978 assert!(!IS_BENCHMARKING);
980 const IS_UNICAST_GLOBAL: bool = IP_ADDRESS.is_unicast_global();
981 assert!(!IS_UNICAST_GLOBAL);
983 const MULTICAST_SCOPE: Option<Ipv6MulticastScope> = IP_ADDRESS.multicast_scope();
984 assert_eq!(MULTICAST_SCOPE, None);
986 const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
987 assert!(!IS_MULTICAST);
989 const IP_V4: Option<Ipv4Addr> = IP_ADDRESS.to_ipv4();
990 assert_eq!(IP_V4.unwrap(), Ipv4Addr::new(0, 0, 0, 1));
995 // test that the methods of `IpAddr` are usable in a const context
997 const IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
999 const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
1000 assert!(!IS_UNSPECIFIED);
1002 const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
1003 assert!(IS_LOOPBACK);
1005 const IS_GLOBAL: bool = IP_ADDRESS.is_global();
1006 assert!(!IS_GLOBAL);
1008 const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
1009 assert!(!IS_MULTICAST);
1011 const IS_IP_V4: bool = IP_ADDRESS.is_ipv4();
1014 const IS_IP_V6: bool = IP_ADDRESS.is_ipv6();
1019 fn structural_match() {
1020 // test that all IP types can be structurally matched upon
1022 const IPV4: Ipv4Addr = Ipv4Addr::LOCALHOST;
1024 Ipv4Addr::LOCALHOST => {}
1025 _ => unreachable!(),
1028 const IPV6: Ipv6Addr = Ipv6Addr::LOCALHOST;
1030 Ipv6Addr::LOCALHOST => {}
1031 _ => unreachable!(),
1034 const IP: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
1036 IpAddr::V4(Ipv4Addr::LOCALHOST) => {}
1037 _ => unreachable!(),