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
);
26 fn test_from_str_ipv6() {
27 assert_eq
!(Ok(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
28 assert_eq
!(Ok(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
30 assert_eq
!(Ok(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
31 assert_eq
!(Ok(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
33 assert_eq
!(Ok(Ipv6Addr
::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)), "2a02:6b8::11:11".parse());
36 let none
: Option
<Ipv6Addr
> = "::00000".parse().ok();
37 assert_eq
!(None
, none
);
39 let none
: Option
<Ipv6Addr
> = "1:2:3:4:5:6:7".parse().ok();
40 assert_eq
!(None
, none
);
42 let none
: Option
<Ipv6Addr
> = "1:2:3:4:5:6:7:8:9".parse().ok();
43 assert_eq
!(None
, none
);
45 let none
: Option
<Ipv6Addr
> = "1:2:::6:7:8".parse().ok();
46 assert_eq
!(None
, none
);
48 let none
: Option
<Ipv6Addr
> = "1:2::6::8".parse().ok();
49 assert_eq
!(None
, none
);
50 // `::` indicating zero groups of zeros
51 let none
: Option
<Ipv6Addr
> = "1:2:3:4::5:6:7:8".parse().ok();
52 assert_eq
!(None
, none
);
56 fn test_from_str_ipv4_in_ipv6() {
57 assert_eq
!(Ok(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 49152, 545)), "::192.0.2.33".parse());
58 assert_eq
!(Ok(Ipv6Addr
::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)), "::FFFF:192.0.2.33".parse());
60 Ok(Ipv6Addr
::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
61 "64:ff9b::192.0.2.33".parse()
64 Ok(Ipv6Addr
::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
65 "2001:db8:122:c000:2:2100:192.0.2.33".parse()
69 let none
: Option
<Ipv4Addr
> = "::127.0.0.1:".parse().ok();
70 assert_eq
!(None
, none
);
72 let none
: Option
<Ipv6Addr
> = "1.2.3.4.5:127.0.0.1".parse().ok();
73 assert_eq
!(None
, none
);
75 let none
: Option
<Ipv6Addr
> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
76 assert_eq
!(None
, none
);
80 fn test_from_str_socket_addr() {
81 assert_eq
!(Ok(sa4(Ipv4Addr
::new(77, 88, 21, 11), 80)), "77.88.21.11:80".parse());
82 assert_eq
!(Ok(SocketAddrV4
::new(Ipv4Addr
::new(77, 88, 21, 11), 80)), "77.88.21.11:80".parse());
84 Ok(sa6(Ipv6Addr
::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
85 "[2a02:6b8:0:1::1]:53".parse()
88 Ok(SocketAddrV6
::new(Ipv6Addr
::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53, 0, 0)),
89 "[2a02:6b8:0:1::1]:53".parse()
91 assert_eq
!(Ok(sa6(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)), "[::127.0.0.1]:22".parse());
93 Ok(SocketAddrV6
::new(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22, 0, 0)),
94 "[::127.0.0.1]:22".parse()
98 let none
: Option
<SocketAddr
> = "127.0.0.1".parse().ok();
99 assert_eq
!(None
, none
);
101 let none
: Option
<SocketAddr
> = "127.0.0.1:".parse().ok();
102 assert_eq
!(None
, none
);
103 // wrong brackets around v4
104 let none
: Option
<SocketAddr
> = "[127.0.0.1]:22".parse().ok();
105 assert_eq
!(None
, none
);
107 let none
: Option
<SocketAddr
> = "127.0.0.1:123456".parse().ok();
108 assert_eq
!(None
, none
);
112 fn ipv4_addr_to_string() {
113 assert_eq
!(Ipv4Addr
::new(127, 0, 0, 1).to_string(), "127.0.0.1");
115 assert_eq
!(Ipv4Addr
::new(1, 1, 1, 1).to_string(), "1.1.1.1");
117 assert_eq
!(Ipv4Addr
::new(127, 127, 127, 127).to_string(), "127.127.127.127");
120 assert_eq
!(&format
!("{:16}", Ipv4Addr
::new(1, 1, 1, 1)), "1.1.1.1 ");
121 assert_eq
!(&format
!("{:>16}", Ipv4Addr
::new(1, 1, 1, 1)), " 1.1.1.1");
125 fn ipv6_addr_to_string() {
126 // ipv4-mapped address
127 let a1
= Ipv6Addr
::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
128 assert_eq
!(a1
.to_string(), "::ffff:192.0.2.128");
130 // ipv4-compatible address
131 let a1
= Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
132 assert_eq
!(a1
.to_string(), "::192.0.2.128");
134 // v6 address with no zero segments
135 assert_eq
!(Ipv6Addr
::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(), "8:9:a:b:c:d:e:f");
137 // longest possible IPv6 length
139 Ipv6Addr
::new(0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888).to_string(),
140 "1111:2222:3333:4444:5555:6666:7777:8888"
143 assert_eq
!(&format
!("{:20}", Ipv6Addr
::new(1, 2, 3, 4, 5, 6, 7, 8)), "1:2:3:4:5:6:7:8 ");
144 assert_eq
!(&format
!("{:>20}", Ipv6Addr
::new(1, 2, 3, 4, 5, 6, 7, 8)), " 1:2:3:4:5:6:7:8");
146 // reduce a single run of zeros
149 Ipv6Addr
::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string()
152 // don't reduce just a single zero segment
153 assert_eq
!("1:2:3:4:5:6:0:8", Ipv6Addr
::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
156 assert_eq
!("::", Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
159 assert_eq
!("::1", Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
162 assert_eq
!("1::", Ipv6Addr
::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
164 // two runs of zeros, second one is longer
165 assert_eq
!("1:0:0:4::8", Ipv6Addr
::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
167 // two runs of zeros, equal length
168 assert_eq
!("1::4:5:0:0:8", Ipv6Addr
::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
174 Ipv6Addr
::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
175 Ipv4Addr
::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped()
178 Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
179 Ipv4Addr
::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible()
184 fn ipv6_to_ipv4_mapped() {
186 Ipv6Addr
::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4_mapped(),
187 Some(Ipv4Addr
::new(0x12, 0x34, 0x56, 0x78))
189 assert_eq
!(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4_mapped(), None
);
195 Ipv6Addr
::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
196 Some(Ipv4Addr
::new(0x12, 0x34, 0x56, 0x78))
199 Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
200 Some(Ipv4Addr
::new(0x12, 0x34, 0x56, 0x78))
202 assert_eq
!(Ipv6Addr
::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(), None
);
209 IpAddr
::from_str($s
).unwrap()
218 ($s
:expr
, $mask
:expr
) => {{
219 let unspec
: u8 = 1 << 0;
220 let loopback
: u8 = 1 << 1;
221 let global
: u8 = 1 << 2;
222 let multicast
: u8 = 1 << 3;
223 let doc
: u8 = 1 << 4;
225 if ($mask
& unspec
) == unspec
{
226 assert
!(ip
!($s
).is_unspecified());
228 assert
!(!ip
!($s
).is_unspecified());
231 if ($mask
& loopback
) == loopback
{
232 assert
!(ip
!($s
).is_loopback());
234 assert
!(!ip
!($s
).is_loopback());
237 if ($mask
& global
) == global
{
238 assert
!(ip
!($s
).is_global());
240 assert
!(!ip
!($s
).is_global());
243 if ($mask
& multicast
) == multicast
{
244 assert
!(ip
!($s
).is_multicast());
246 assert
!(!ip
!($s
).is_multicast());
249 if ($mask
& doc
) == doc
{
250 assert
!(ip
!($s
).is_documentation());
252 assert
!(!ip
!($s
).is_documentation());
257 let unspec
: u8 = 1 << 0;
258 let loopback
: u8 = 1 << 1;
259 let global
: u8 = 1 << 2;
260 let multicast
: u8 = 1 << 3;
261 let doc
: u8 = 1 << 4;
263 check
!("0.0.0.0", unspec
);
267 check
!("127.1.2.3", loopback
);
268 check
!("172.31.254.253");
269 check
!("169.254.253.242");
270 check
!("192.0.2.183", doc
);
271 check
!("192.1.2.183", global
);
272 check
!("192.168.254.253");
273 check
!("198.51.100.0", doc
);
274 check
!("203.0.113.0", doc
);
275 check
!("203.2.113.0", global
);
276 check
!("224.0.0.0", global
| multicast
);
277 check
!("239.255.255.255", global
| multicast
);
278 check
!("255.255.255.255");
279 // make sure benchmarking addresses are not global
280 check
!("198.18.0.0");
281 check
!("198.18.54.2");
282 check
!("198.19.255.255");
283 // make sure addresses reserved for protocol assignment are not global
285 check
!("192.0.0.255");
286 check
!("192.0.0.100");
287 // make sure reserved addresses are not global
289 check
!("251.54.1.76");
290 check
!("254.255.255.255");
291 // make sure shared addresses are not global
292 check
!("100.64.0.0");
293 check
!("100.127.255.255");
294 check
!("100.100.100.0");
296 check
!("::", unspec
);
297 check
!("::1", loopback
);
298 check
!("::0.0.0.2", global
);
299 check
!("1::", global
);
301 check
!("fdff:ffff::");
302 check
!("fe80:ffff::");
303 check
!("febf:ffff::");
304 check
!("fec0::", global
);
305 check
!("ff01::", multicast
);
306 check
!("ff02::", multicast
);
307 check
!("ff03::", multicast
);
308 check
!("ff04::", multicast
);
309 check
!("ff05::", multicast
);
310 check
!("ff08::", multicast
);
311 check
!("ff0e::", global
| multicast
);
312 check
!("2001:db8:85a3::8a2e:370:7334", doc
);
313 check
!("102:304:506:708:90a:b0c:d0e:f10", global
);
317 fn ipv4_properties() {
320 Ipv4Addr
::from_str($s
).unwrap()
329 ($s
:expr
, $mask
:expr
) => {{
330 let unspec
: u16 = 1 << 0;
331 let loopback
: u16 = 1 << 1;
332 let private
: u16 = 1 << 2;
333 let link_local
: u16 = 1 << 3;
334 let global
: u16 = 1 << 4;
335 let multicast
: u16 = 1 << 5;
336 let broadcast
: u16 = 1 << 6;
337 let documentation
: u16 = 1 << 7;
338 let benchmarking
: u16 = 1 << 8;
339 let ietf_protocol_assignment
: u16 = 1 << 9;
340 let reserved
: u16 = 1 << 10;
341 let shared
: u16 = 1 << 11;
343 if ($mask
& unspec
) == unspec
{
344 assert
!(ip
!($s
).is_unspecified());
346 assert
!(!ip
!($s
).is_unspecified());
349 if ($mask
& loopback
) == loopback
{
350 assert
!(ip
!($s
).is_loopback());
352 assert
!(!ip
!($s
).is_loopback());
355 if ($mask
& private
) == private
{
356 assert
!(ip
!($s
).is_private());
358 assert
!(!ip
!($s
).is_private());
361 if ($mask
& link_local
) == link_local
{
362 assert
!(ip
!($s
).is_link_local());
364 assert
!(!ip
!($s
).is_link_local());
367 if ($mask
& global
) == global
{
368 assert
!(ip
!($s
).is_global());
370 assert
!(!ip
!($s
).is_global());
373 if ($mask
& multicast
) == multicast
{
374 assert
!(ip
!($s
).is_multicast());
376 assert
!(!ip
!($s
).is_multicast());
379 if ($mask
& broadcast
) == broadcast
{
380 assert
!(ip
!($s
).is_broadcast());
382 assert
!(!ip
!($s
).is_broadcast());
385 if ($mask
& documentation
) == documentation
{
386 assert
!(ip
!($s
).is_documentation());
388 assert
!(!ip
!($s
).is_documentation());
391 if ($mask
& benchmarking
) == benchmarking
{
392 assert
!(ip
!($s
).is_benchmarking());
394 assert
!(!ip
!($s
).is_benchmarking());
397 if ($mask
& ietf_protocol_assignment
) == ietf_protocol_assignment
{
398 assert
!(ip
!($s
).is_ietf_protocol_assignment());
400 assert
!(!ip
!($s
).is_ietf_protocol_assignment());
403 if ($mask
& reserved
) == reserved
{
404 assert
!(ip
!($s
).is_reserved());
406 assert
!(!ip
!($s
).is_reserved());
409 if ($mask
& shared
) == shared
{
410 assert
!(ip
!($s
).is_shared());
412 assert
!(!ip
!($s
).is_shared());
417 let unspec
: u16 = 1 << 0;
418 let loopback
: u16 = 1 << 1;
419 let private
: u16 = 1 << 2;
420 let link_local
: u16 = 1 << 3;
421 let global
: u16 = 1 << 4;
422 let multicast
: u16 = 1 << 5;
423 let broadcast
: u16 = 1 << 6;
424 let documentation
: u16 = 1 << 7;
425 let benchmarking
: u16 = 1 << 8;
426 let ietf_protocol_assignment
: u16 = 1 << 9;
427 let reserved
: u16 = 1 << 10;
428 let shared
: u16 = 1 << 11;
430 check
!("0.0.0.0", unspec
);
433 check
!("10.9.8.7", private
);
434 check
!("127.1.2.3", loopback
);
435 check
!("172.31.254.253", private
);
436 check
!("169.254.253.242", link_local
);
437 check
!("192.0.2.183", documentation
);
438 check
!("192.1.2.183", global
);
439 check
!("192.168.254.253", private
);
440 check
!("198.51.100.0", documentation
);
441 check
!("203.0.113.0", documentation
);
442 check
!("203.2.113.0", global
);
443 check
!("224.0.0.0", global
| multicast
);
444 check
!("239.255.255.255", global
| multicast
);
445 check
!("255.255.255.255", broadcast
);
446 check
!("198.18.0.0", benchmarking
);
447 check
!("198.18.54.2", benchmarking
);
448 check
!("198.19.255.255", benchmarking
);
449 check
!("192.0.0.0", ietf_protocol_assignment
);
450 check
!("192.0.0.255", ietf_protocol_assignment
);
451 check
!("192.0.0.100", ietf_protocol_assignment
);
452 check
!("240.0.0.0", reserved
);
453 check
!("251.54.1.76", reserved
);
454 check
!("254.255.255.255", reserved
);
455 check
!("100.64.0.0", shared
);
456 check
!("100.127.255.255", shared
);
457 check
!("100.100.100.0", shared
);
461 fn ipv6_properties() {
464 Ipv6Addr
::from_str($s
).unwrap()
469 ($s
:expr
, &[$
($octet
:expr
),*], $mask
:expr
) => {
470 assert_eq
!($s
, ip
!($s
).to_string());
471 let octets
= &[$
($octet
),*];
472 assert_eq
!(&ip
!($s
).octets(), octets
);
473 assert_eq
!(Ipv6Addr
::from(*octets
), ip
!($s
));
475 let unspecified
: u16 = 1 << 0;
476 let loopback
: u16 = 1 << 1;
477 let unique_local
: u16 = 1 << 2;
478 let global
: u16 = 1 << 3;
479 let unicast_link_local
: u16 = 1 << 4;
480 let unicast_link_local_strict
: u16 = 1 << 5;
481 let unicast_site_local
: u16 = 1 << 6;
482 let unicast_global
: u16 = 1 << 7;
483 let documentation
: u16 = 1 << 8;
484 let multicast_interface_local
: u16 = 1 << 9;
485 let multicast_link_local
: u16 = 1 << 10;
486 let multicast_realm_local
: u16 = 1 << 11;
487 let multicast_admin_local
: u16 = 1 << 12;
488 let multicast_site_local
: u16 = 1 << 13;
489 let multicast_organization_local
: u16 = 1 << 14;
490 let multicast_global
: u16 = 1 << 15;
491 let multicast
: u16 = multicast_interface_local
492 | multicast_admin_local
494 | multicast_link_local
495 | multicast_realm_local
496 | multicast_site_local
497 | multicast_organization_local
;
499 if ($mask
& unspecified
) == unspecified
{
500 assert
!(ip
!($s
).is_unspecified());
502 assert
!(!ip
!($s
).is_unspecified());
504 if ($mask
& loopback
) == loopback
{
505 assert
!(ip
!($s
).is_loopback());
507 assert
!(!ip
!($s
).is_loopback());
509 if ($mask
& unique_local
) == unique_local
{
510 assert
!(ip
!($s
).is_unique_local());
512 assert
!(!ip
!($s
).is_unique_local());
514 if ($mask
& global
) == global
{
515 assert
!(ip
!($s
).is_global());
517 assert
!(!ip
!($s
).is_global());
519 if ($mask
& unicast_link_local
) == unicast_link_local
{
520 assert
!(ip
!($s
).is_unicast_link_local());
522 assert
!(!ip
!($s
).is_unicast_link_local());
524 if ($mask
& unicast_link_local_strict
) == unicast_link_local_strict
{
525 assert
!(ip
!($s
).is_unicast_link_local_strict());
527 assert
!(!ip
!($s
).is_unicast_link_local_strict());
529 if ($mask
& unicast_site_local
) == unicast_site_local
{
530 assert
!(ip
!($s
).is_unicast_site_local());
532 assert
!(!ip
!($s
).is_unicast_site_local());
534 if ($mask
& unicast_global
) == unicast_global
{
535 assert
!(ip
!($s
).is_unicast_global());
537 assert
!(!ip
!($s
).is_unicast_global());
539 if ($mask
& documentation
) == documentation
{
540 assert
!(ip
!($s
).is_documentation());
542 assert
!(!ip
!($s
).is_documentation());
544 if ($mask
& multicast
) != 0 {
545 assert
!(ip
!($s
).multicast_scope().is_some());
546 assert
!(ip
!($s
).is_multicast());
548 assert
!(ip
!($s
).multicast_scope().is_none());
549 assert
!(!ip
!($s
).is_multicast());
551 if ($mask
& multicast_interface_local
) == multicast_interface_local
{
552 assert_eq
!(ip
!($s
).multicast_scope().unwrap(),
553 Ipv6MulticastScope
::InterfaceLocal
);
555 if ($mask
& multicast_link_local
) == multicast_link_local
{
556 assert_eq
!(ip
!($s
).multicast_scope().unwrap(),
557 Ipv6MulticastScope
::LinkLocal
);
559 if ($mask
& multicast_realm_local
) == multicast_realm_local
{
560 assert_eq
!(ip
!($s
).multicast_scope().unwrap(),
561 Ipv6MulticastScope
::RealmLocal
);
563 if ($mask
& multicast_admin_local
) == multicast_admin_local
{
564 assert_eq
!(ip
!($s
).multicast_scope().unwrap(),
565 Ipv6MulticastScope
::AdminLocal
);
567 if ($mask
& multicast_site_local
) == multicast_site_local
{
568 assert_eq
!(ip
!($s
).multicast_scope().unwrap(),
569 Ipv6MulticastScope
::SiteLocal
);
571 if ($mask
& multicast_organization_local
) == multicast_organization_local
{
572 assert_eq
!(ip
!($s
).multicast_scope().unwrap(),
573 Ipv6MulticastScope
::OrganizationLocal
);
575 if ($mask
& multicast_global
) == multicast_global
{
576 assert_eq
!(ip
!($s
).multicast_scope().unwrap(),
577 Ipv6MulticastScope
::Global
);
582 let unspecified
: u16 = 1 << 0;
583 let loopback
: u16 = 1 << 1;
584 let unique_local
: u16 = 1 << 2;
585 let global
: u16 = 1 << 3;
586 let unicast_link_local
: u16 = 1 << 4;
587 let unicast_link_local_strict
: u16 = 1 << 5;
588 let unicast_site_local
: u16 = 1 << 6;
589 let unicast_global
: u16 = 1 << 7;
590 let documentation
: u16 = 1 << 8;
591 let multicast_interface_local
: u16 = 1 << 9;
592 let multicast_link_local
: u16 = 1 << 10;
593 let multicast_realm_local
: u16 = 1 << 11;
594 let multicast_admin_local
: u16 = 1 << 12;
595 let multicast_site_local
: u16 = 1 << 13;
596 let multicast_organization_local
: u16 = 1 << 14;
597 let multicast_global
: u16 = 1 << 15;
599 check
!("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unspecified
);
601 check
!("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], loopback
);
603 check
!("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], global
| unicast_global
);
605 check
!("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global
| unicast_global
);
607 check
!("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unique_local
);
611 &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
617 &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
623 &[0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
624 unicast_link_local
| unicast_link_local_strict
629 &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
633 check
!("febf::", &[0xfe, 0xbf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_link_local
);
636 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
638 0xfe, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
645 "fe80::ffff:ffff:ffff:ffff",
647 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
650 unicast_link_local
| unicast_link_local_strict
655 &[0xfe, 0x80, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
661 &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
662 unicast_site_local
| unicast_global
| global
667 &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
668 multicast_interface_local
671 check
!("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_link_local
);
673 check
!("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_realm_local
);
675 check
!("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_admin_local
);
677 check
!("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_site_local
);
681 &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
682 multicast_organization_local
687 &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
688 multicast_global
| global
692 "2001:db8:85a3::8a2e:370:7334",
693 &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
698 "102:304:506:708:90a:b0c:d0e:f10",
699 &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
700 global
| unicast_global
705 fn to_socket_addr_socketaddr() {
706 let a
= sa4(Ipv4Addr
::new(77, 88, 21, 11), 12345);
707 assert_eq
!(Ok(vec
![a
]), tsa(a
));
711 fn test_ipv4_to_int() {
712 let a
= Ipv4Addr
::new(0x11, 0x22, 0x33, 0x44);
713 assert_eq
!(u32::from(a
), 0x11223344);
717 fn test_int_to_ipv4() {
718 let a
= Ipv4Addr
::new(0x11, 0x22, 0x33, 0x44);
719 assert_eq
!(Ipv4Addr
::from(0x11223344), a
);
723 fn test_ipv6_to_int() {
724 let a
= Ipv6Addr
::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
725 assert_eq
!(u128
::from(a
), 0x112233445566778899aabbccddeeff11u128);
729 fn test_int_to_ipv6() {
730 let a
= Ipv6Addr
::new(0x1122, 0x3344, 0x5566, 0x7788, 0x99aa, 0xbbcc, 0xddee, 0xff11);
731 assert_eq
!(Ipv6Addr
::from(0x112233445566778899aabbccddeeff11u128), a
);
735 fn ipv4_from_constructors() {
736 assert_eq
!(Ipv4Addr
::LOCALHOST
, Ipv4Addr
::new(127, 0, 0, 1));
737 assert
!(Ipv4Addr
::LOCALHOST
.is_loopback());
738 assert_eq
!(Ipv4Addr
::UNSPECIFIED
, Ipv4Addr
::new(0, 0, 0, 0));
739 assert
!(Ipv4Addr
::UNSPECIFIED
.is_unspecified());
740 assert_eq
!(Ipv4Addr
::BROADCAST
, Ipv4Addr
::new(255, 255, 255, 255));
741 assert
!(Ipv4Addr
::BROADCAST
.is_broadcast());
745 fn ipv6_from_contructors() {
746 assert_eq
!(Ipv6Addr
::LOCALHOST
, Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 1));
747 assert
!(Ipv6Addr
::LOCALHOST
.is_loopback());
748 assert_eq
!(Ipv6Addr
::UNSPECIFIED
, Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 0));
749 assert
!(Ipv6Addr
::UNSPECIFIED
.is_unspecified());
753 fn ipv4_from_octets() {
754 assert_eq
!(Ipv4Addr
::from([127, 0, 0, 1]), Ipv4Addr
::new(127, 0, 0, 1))
758 fn ipv6_from_segments() {
760 Ipv6Addr
::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
761 let new
= Ipv6Addr
::new(0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff);
762 assert_eq
!(new
, from_u16s
);
766 fn ipv6_from_octets() {
768 Ipv6Addr
::from([0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff]);
769 let from_u8s
= Ipv6Addr
::from([
770 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
773 assert_eq
!(from_u16s
, from_u8s
);
778 let v41
= Ipv4Addr
::new(100, 64, 3, 3);
779 let v42
= Ipv4Addr
::new(192, 0, 2, 2);
780 let v61
= "2001:db8:f00::1002".parse
::<Ipv6Addr
>().unwrap();
781 let v62
= "2001:db8:f00::2001".parse
::<Ipv6Addr
>().unwrap();
785 assert_eq
!(v41
, IpAddr
::V4(v41
));
786 assert_eq
!(v61
, IpAddr
::V6(v61
));
787 assert
!(v41
!= IpAddr
::V4(v42
));
788 assert
!(v61
!= IpAddr
::V6(v62
));
790 assert
!(v41
< IpAddr
::V4(v42
));
791 assert
!(v61
< IpAddr
::V6(v62
));
792 assert
!(IpAddr
::V4(v41
) < v42
);
793 assert
!(IpAddr
::V6(v61
) < v62
);
795 assert
!(v41
< IpAddr
::V6(v61
));
796 assert
!(IpAddr
::V4(v41
) < v61
);
801 let ip
= IpAddr
::V4(Ipv4Addr
::new(100, 64, 3, 3));
802 assert
!(ip
.is_ipv4());
803 assert
!(!ip
.is_ipv6());
808 let ip
= IpAddr
::V6(Ipv6Addr
::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
809 assert
!(!ip
.is_ipv4());
810 assert
!(ip
.is_ipv6());
815 // test that the methods of `Ipv4Addr` are usable in a const context
817 const IP_ADDRESS
: Ipv4Addr
= Ipv4Addr
::new(127, 0, 0, 1);
818 assert_eq
!(IP_ADDRESS
, Ipv4Addr
::LOCALHOST
);
820 const OCTETS
: [u8; 4] = IP_ADDRESS
.octets();
821 assert_eq
!(OCTETS
, [127, 0, 0, 1]);
823 const IS_UNSPECIFIED
: bool
= IP_ADDRESS
.is_unspecified();
824 assert
!(!IS_UNSPECIFIED
);
826 const IS_LOOPBACK
: bool
= IP_ADDRESS
.is_loopback();
827 assert
!(IS_LOOPBACK
);
829 const IS_PRIVATE
: bool
= IP_ADDRESS
.is_private();
830 assert
!(!IS_PRIVATE
);
832 const IS_LINK_LOCAL
: bool
= IP_ADDRESS
.is_link_local();
833 assert
!(!IS_LINK_LOCAL
);
835 const IS_GLOBAL
: bool
= IP_ADDRESS
.is_global();
838 const IS_SHARED
: bool
= IP_ADDRESS
.is_shared();
841 const IS_IETF_PROTOCOL_ASSIGNMENT
: bool
= IP_ADDRESS
.is_ietf_protocol_assignment();
842 assert
!(!IS_IETF_PROTOCOL_ASSIGNMENT
);
844 const IS_BENCHMARKING
: bool
= IP_ADDRESS
.is_benchmarking();
845 assert
!(!IS_BENCHMARKING
);
847 const IS_RESERVED
: bool
= IP_ADDRESS
.is_reserved();
848 assert
!(!IS_RESERVED
);
850 const IS_MULTICAST
: bool
= IP_ADDRESS
.is_multicast();
851 assert
!(!IS_MULTICAST
);
853 const IS_BROADCAST
: bool
= IP_ADDRESS
.is_broadcast();
854 assert
!(!IS_BROADCAST
);
856 const IS_DOCUMENTATION
: bool
= IP_ADDRESS
.is_documentation();
857 assert
!(!IS_DOCUMENTATION
);
859 const IP_V6_COMPATIBLE
: Ipv6Addr
= IP_ADDRESS
.to_ipv6_compatible();
862 Ipv6Addr
::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1])
865 const IP_V6_MAPPED
: Ipv6Addr
= IP_ADDRESS
.to_ipv6_mapped();
868 Ipv6Addr
::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1])
874 // test that the methods of `Ipv6Addr` are usable in a const context
876 const IP_ADDRESS
: Ipv6Addr
= Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 1);
877 assert_eq
!(IP_ADDRESS
, Ipv6Addr
::LOCALHOST
);
879 const SEGMENTS
: [u16; 8] = IP_ADDRESS
.segments();
880 assert_eq
!(SEGMENTS
, [0, 0, 0, 0, 0, 0, 0, 1]);
882 const OCTETS
: [u8; 16] = IP_ADDRESS
.octets();
883 assert_eq
!(OCTETS
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
885 const IS_UNSPECIFIED
: bool
= IP_ADDRESS
.is_unspecified();
886 assert
!(!IS_UNSPECIFIED
);
888 const IS_LOOPBACK
: bool
= IP_ADDRESS
.is_loopback();
889 assert
!(IS_LOOPBACK
);
891 const IS_GLOBAL
: bool
= IP_ADDRESS
.is_global();
894 const IS_UNIQUE_LOCAL
: bool
= IP_ADDRESS
.is_unique_local();
895 assert
!(!IS_UNIQUE_LOCAL
);
897 const IS_UNICAST_LINK_LOCAL_STRICT
: bool
= IP_ADDRESS
.is_unicast_link_local_strict();
898 assert
!(!IS_UNICAST_LINK_LOCAL_STRICT
);
900 const IS_UNICAST_LINK_LOCAL
: bool
= IP_ADDRESS
.is_unicast_link_local();
901 assert
!(!IS_UNICAST_LINK_LOCAL
);
903 const IS_UNICAST_SITE_LOCAL
: bool
= IP_ADDRESS
.is_unicast_site_local();
904 assert
!(!IS_UNICAST_SITE_LOCAL
);
906 const IS_DOCUMENTATION
: bool
= IP_ADDRESS
.is_documentation();
907 assert
!(!IS_DOCUMENTATION
);
909 const IS_UNICAST_GLOBAL
: bool
= IP_ADDRESS
.is_unicast_global();
910 assert
!(!IS_UNICAST_GLOBAL
);
912 const MULTICAST_SCOPE
: Option
<Ipv6MulticastScope
> = IP_ADDRESS
.multicast_scope();
913 assert_eq
!(MULTICAST_SCOPE
, None
);
915 const IS_MULTICAST
: bool
= IP_ADDRESS
.is_multicast();
916 assert
!(!IS_MULTICAST
);
918 const IP_V4
: Option
<Ipv4Addr
> = IP_ADDRESS
.to_ipv4();
919 assert_eq
!(IP_V4
.unwrap(), Ipv4Addr
::new(0, 0, 0, 1));
924 // test that the methods of `IpAddr` are usable in a const context
926 const IP_ADDRESS
: IpAddr
= IpAddr
::V4(Ipv4Addr
::LOCALHOST
);
928 const IS_UNSPECIFIED
: bool
= IP_ADDRESS
.is_unspecified();
929 assert
!(!IS_UNSPECIFIED
);
931 const IS_LOOPBACK
: bool
= IP_ADDRESS
.is_loopback();
932 assert
!(IS_LOOPBACK
);
934 const IS_GLOBAL
: bool
= IP_ADDRESS
.is_global();
937 const IS_MULTICAST
: bool
= IP_ADDRESS
.is_multicast();
938 assert
!(!IS_MULTICAST
);