Debian Codegraph @

src/ip/tests.rs

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