]> git.proxmox.com Git - rustc.git/blob - library/std/src/net/ip/tests.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / net / 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 }
24
25 #[test]
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());
29
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());
32
33 assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)), "2a02:6b8::11:11".parse());
34
35 // too long group
36 let none: Option<Ipv6Addr> = "::00000".parse().ok();
37 assert_eq!(None, none);
38 // too short
39 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok();
40 assert_eq!(None, none);
41 // too long
42 let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok();
43 assert_eq!(None, none);
44 // triple colon
45 let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok();
46 assert_eq!(None, none);
47 // two double colons
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);
53 }
54
55 #[test]
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());
59 assert_eq!(
60 Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
61 "64:ff9b::192.0.2.33".parse()
62 );
63 assert_eq!(
64 Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
65 "2001:db8:122:c000:2:2100:192.0.2.33".parse()
66 );
67
68 // colon after v4
69 let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok();
70 assert_eq!(None, none);
71 // not enough groups
72 let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok();
73 assert_eq!(None, none);
74 // too many groups
75 let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
76 assert_eq!(None, none);
77 }
78
79 #[test]
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());
83 assert_eq!(
84 Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
85 "[2a02:6b8:0:1::1]:53".parse()
86 );
87 assert_eq!(
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()
90 );
91 assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)), "[::127.0.0.1]:22".parse());
92 assert_eq!(
93 Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22, 0, 0)),
94 "[::127.0.0.1]:22".parse()
95 );
96
97 // without port
98 let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
99 assert_eq!(None, none);
100 // without port
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);
106 // port out of range
107 let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
108 assert_eq!(None, none);
109 }
110
111 #[test]
112 fn ipv4_addr_to_string() {
113 assert_eq!(Ipv4Addr::new(127, 0, 0, 1).to_string(), "127.0.0.1");
114 // Short address
115 assert_eq!(Ipv4Addr::new(1, 1, 1, 1).to_string(), "1.1.1.1");
116 // Long address
117 assert_eq!(Ipv4Addr::new(127, 127, 127, 127).to_string(), "127.127.127.127");
118
119 // Test padding
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");
122 }
123
124 #[test]
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");
129
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");
133
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");
136
137 // longest possible IPv6 length
138 assert_eq!(
139 Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888).to_string(),
140 "1111:2222:3333:4444:5555:6666:7777:8888"
141 );
142 // padding
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");
145
146 // reduce a single run of zeros
147 assert_eq!(
148 "ae::ffff:102:304",
149 Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string()
150 );
151
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());
154
155 // 'any' address
156 assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
157
158 // loopback address
159 assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
160
161 // ends in zeros
162 assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
163
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());
166
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());
169 }
170
171 #[test]
172 fn ipv4_to_ipv6() {
173 assert_eq!(
174 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
175 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped()
176 );
177 assert_eq!(
178 Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
179 Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible()
180 );
181 }
182
183 #[test]
184 fn ipv6_to_ipv4_mapped() {
185 assert_eq!(
186 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4_mapped(),
187 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
188 );
189 assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4_mapped(), None);
190 }
191
192 #[test]
193 fn ipv6_to_ipv4() {
194 assert_eq!(
195 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
196 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
197 );
198 assert_eq!(
199 Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
200 Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))
201 );
202 assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(), None);
203 }
204
205 #[test]
206 fn ip_properties() {
207 macro_rules! ip {
208 ($s:expr) => {
209 IpAddr::from_str($s).unwrap()
210 };
211 }
212
213 macro_rules! check {
214 ($s:expr) => {
215 check!($s, 0);
216 };
217
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;
224
225 if ($mask & unspec) == unspec {
226 assert!(ip!($s).is_unspecified());
227 } else {
228 assert!(!ip!($s).is_unspecified());
229 }
230
231 if ($mask & loopback) == loopback {
232 assert!(ip!($s).is_loopback());
233 } else {
234 assert!(!ip!($s).is_loopback());
235 }
236
237 if ($mask & global) == global {
238 assert!(ip!($s).is_global());
239 } else {
240 assert!(!ip!($s).is_global());
241 }
242
243 if ($mask & multicast) == multicast {
244 assert!(ip!($s).is_multicast());
245 } else {
246 assert!(!ip!($s).is_multicast());
247 }
248
249 if ($mask & doc) == doc {
250 assert!(ip!($s).is_documentation());
251 } else {
252 assert!(!ip!($s).is_documentation());
253 }
254 }};
255 }
256
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;
262
263 check!("0.0.0.0", unspec);
264 check!("0.0.0.1");
265 check!("0.1.0.0");
266 check!("10.9.8.7");
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
284 check!("192.0.0.0");
285 check!("192.0.0.255");
286 check!("192.0.0.100");
287 // make sure reserved addresses are not global
288 check!("240.0.0.0");
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");
295
296 check!("::", unspec);
297 check!("::1", loopback);
298 check!("::0.0.0.2", global);
299 check!("1::", global);
300 check!("fc00::");
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);
314 }
315
316 #[test]
317 fn ipv4_properties() {
318 macro_rules! ip {
319 ($s:expr) => {
320 Ipv4Addr::from_str($s).unwrap()
321 };
322 }
323
324 macro_rules! check {
325 ($s:expr) => {
326 check!($s, 0);
327 };
328
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;
342
343 if ($mask & unspec) == unspec {
344 assert!(ip!($s).is_unspecified());
345 } else {
346 assert!(!ip!($s).is_unspecified());
347 }
348
349 if ($mask & loopback) == loopback {
350 assert!(ip!($s).is_loopback());
351 } else {
352 assert!(!ip!($s).is_loopback());
353 }
354
355 if ($mask & private) == private {
356 assert!(ip!($s).is_private());
357 } else {
358 assert!(!ip!($s).is_private());
359 }
360
361 if ($mask & link_local) == link_local {
362 assert!(ip!($s).is_link_local());
363 } else {
364 assert!(!ip!($s).is_link_local());
365 }
366
367 if ($mask & global) == global {
368 assert!(ip!($s).is_global());
369 } else {
370 assert!(!ip!($s).is_global());
371 }
372
373 if ($mask & multicast) == multicast {
374 assert!(ip!($s).is_multicast());
375 } else {
376 assert!(!ip!($s).is_multicast());
377 }
378
379 if ($mask & broadcast) == broadcast {
380 assert!(ip!($s).is_broadcast());
381 } else {
382 assert!(!ip!($s).is_broadcast());
383 }
384
385 if ($mask & documentation) == documentation {
386 assert!(ip!($s).is_documentation());
387 } else {
388 assert!(!ip!($s).is_documentation());
389 }
390
391 if ($mask & benchmarking) == benchmarking {
392 assert!(ip!($s).is_benchmarking());
393 } else {
394 assert!(!ip!($s).is_benchmarking());
395 }
396
397 if ($mask & ietf_protocol_assignment) == ietf_protocol_assignment {
398 assert!(ip!($s).is_ietf_protocol_assignment());
399 } else {
400 assert!(!ip!($s).is_ietf_protocol_assignment());
401 }
402
403 if ($mask & reserved) == reserved {
404 assert!(ip!($s).is_reserved());
405 } else {
406 assert!(!ip!($s).is_reserved());
407 }
408
409 if ($mask & shared) == shared {
410 assert!(ip!($s).is_shared());
411 } else {
412 assert!(!ip!($s).is_shared());
413 }
414 }};
415 }
416
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;
429
430 check!("0.0.0.0", unspec);
431 check!("0.0.0.1");
432 check!("0.1.0.0");
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);
458 }
459
460 #[test]
461 fn ipv6_properties() {
462 macro_rules! ip {
463 ($s:expr) => {
464 Ipv6Addr::from_str($s).unwrap()
465 };
466 }
467
468 macro_rules! check {
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));
474
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
493 | multicast_global
494 | multicast_link_local
495 | multicast_realm_local
496 | multicast_site_local
497 | multicast_organization_local;
498
499 if ($mask & unspecified) == unspecified {
500 assert!(ip!($s).is_unspecified());
501 } else {
502 assert!(!ip!($s).is_unspecified());
503 }
504 if ($mask & loopback) == loopback {
505 assert!(ip!($s).is_loopback());
506 } else {
507 assert!(!ip!($s).is_loopback());
508 }
509 if ($mask & unique_local) == unique_local {
510 assert!(ip!($s).is_unique_local());
511 } else {
512 assert!(!ip!($s).is_unique_local());
513 }
514 if ($mask & global) == global {
515 assert!(ip!($s).is_global());
516 } else {
517 assert!(!ip!($s).is_global());
518 }
519 if ($mask & unicast_link_local) == unicast_link_local {
520 assert!(ip!($s).is_unicast_link_local());
521 } else {
522 assert!(!ip!($s).is_unicast_link_local());
523 }
524 if ($mask & unicast_link_local_strict) == unicast_link_local_strict {
525 assert!(ip!($s).is_unicast_link_local_strict());
526 } else {
527 assert!(!ip!($s).is_unicast_link_local_strict());
528 }
529 if ($mask & unicast_site_local) == unicast_site_local {
530 assert!(ip!($s).is_unicast_site_local());
531 } else {
532 assert!(!ip!($s).is_unicast_site_local());
533 }
534 if ($mask & unicast_global) == unicast_global {
535 assert!(ip!($s).is_unicast_global());
536 } else {
537 assert!(!ip!($s).is_unicast_global());
538 }
539 if ($mask & documentation) == documentation {
540 assert!(ip!($s).is_documentation());
541 } else {
542 assert!(!ip!($s).is_documentation());
543 }
544 if ($mask & multicast) != 0 {
545 assert!(ip!($s).multicast_scope().is_some());
546 assert!(ip!($s).is_multicast());
547 } else {
548 assert!(ip!($s).multicast_scope().is_none());
549 assert!(!ip!($s).is_multicast());
550 }
551 if ($mask & multicast_interface_local) == multicast_interface_local {
552 assert_eq!(ip!($s).multicast_scope().unwrap(),
553 Ipv6MulticastScope::InterfaceLocal);
554 }
555 if ($mask & multicast_link_local) == multicast_link_local {
556 assert_eq!(ip!($s).multicast_scope().unwrap(),
557 Ipv6MulticastScope::LinkLocal);
558 }
559 if ($mask & multicast_realm_local) == multicast_realm_local {
560 assert_eq!(ip!($s).multicast_scope().unwrap(),
561 Ipv6MulticastScope::RealmLocal);
562 }
563 if ($mask & multicast_admin_local) == multicast_admin_local {
564 assert_eq!(ip!($s).multicast_scope().unwrap(),
565 Ipv6MulticastScope::AdminLocal);
566 }
567 if ($mask & multicast_site_local) == multicast_site_local {
568 assert_eq!(ip!($s).multicast_scope().unwrap(),
569 Ipv6MulticastScope::SiteLocal);
570 }
571 if ($mask & multicast_organization_local) == multicast_organization_local {
572 assert_eq!(ip!($s).multicast_scope().unwrap(),
573 Ipv6MulticastScope::OrganizationLocal);
574 }
575 if ($mask & multicast_global) == multicast_global {
576 assert_eq!(ip!($s).multicast_scope().unwrap(),
577 Ipv6MulticastScope::Global);
578 }
579 }
580 }
581
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;
598
599 check!("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unspecified);
600
601 check!("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], loopback);
602
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);
604
605 check!("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);
606
607 check!("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unique_local);
608
609 check!(
610 "fdff:ffff::",
611 &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
612 unique_local
613 );
614
615 check!(
616 "fe80:ffff::",
617 &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
618 unicast_link_local
619 );
620
621 check!(
622 "fe80::",
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
625 );
626
627 check!(
628 "febf:ffff::",
629 &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
630 unicast_link_local
631 );
632
633 check!("febf::", &[0xfe, 0xbf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_link_local);
634
635 check!(
636 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
637 &[
638 0xfe, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
639 0xff, 0xff
640 ],
641 unicast_link_local
642 );
643
644 check!(
645 "fe80::ffff:ffff:ffff:ffff",
646 &[
647 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
648 0xff, 0xff
649 ],
650 unicast_link_local | unicast_link_local_strict
651 );
652
653 check!(
654 "fe80:0:0:1::",
655 &[0xfe, 0x80, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
656 unicast_link_local
657 );
658
659 check!(
660 "fec0::",
661 &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
662 unicast_site_local | unicast_global | global
663 );
664
665 check!(
666 "ff01::",
667 &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
668 multicast_interface_local
669 );
670
671 check!("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_link_local);
672
673 check!("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_realm_local);
674
675 check!("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_admin_local);
676
677 check!("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], multicast_site_local);
678
679 check!(
680 "ff08::",
681 &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
682 multicast_organization_local
683 );
684
685 check!(
686 "ff0e::",
687 &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
688 multicast_global | global
689 );
690
691 check!(
692 "2001:db8:85a3::8a2e:370:7334",
693 &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34],
694 documentation
695 );
696
697 check!(
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
701 );
702 }
703
704 #[test]
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));
708 }
709
710 #[test]
711 fn test_ipv4_to_int() {
712 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
713 assert_eq!(u32::from(a), 0x11223344);
714 }
715
716 #[test]
717 fn test_int_to_ipv4() {
718 let a = Ipv4Addr::new(0x11, 0x22, 0x33, 0x44);
719 assert_eq!(Ipv4Addr::from(0x11223344), a);
720 }
721
722 #[test]
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);
726 }
727
728 #[test]
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);
732 }
733
734 #[test]
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());
742 }
743
744 #[test]
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());
750 }
751
752 #[test]
753 fn ipv4_from_octets() {
754 assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
755 }
756
757 #[test]
758 fn ipv6_from_segments() {
759 let from_u16s =
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);
763 }
764
765 #[test]
766 fn ipv6_from_octets() {
767 let from_u16s =
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,
771 0xff,
772 ]);
773 assert_eq!(from_u16s, from_u8s);
774 }
775
776 #[test]
777 fn cmp() {
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();
782 assert!(v41 < v42);
783 assert!(v61 < v62);
784
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));
789
790 assert!(v41 < IpAddr::V4(v42));
791 assert!(v61 < IpAddr::V6(v62));
792 assert!(IpAddr::V4(v41) < v42);
793 assert!(IpAddr::V6(v61) < v62);
794
795 assert!(v41 < IpAddr::V6(v61));
796 assert!(IpAddr::V4(v41) < v61);
797 }
798
799 #[test]
800 fn is_v4() {
801 let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
802 assert!(ip.is_ipv4());
803 assert!(!ip.is_ipv6());
804 }
805
806 #[test]
807 fn is_v6() {
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());
811 }
812
813 #[test]
814 fn ipv4_const() {
815 // test that the methods of `Ipv4Addr` are usable in a const context
816
817 const IP_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
818 assert_eq!(IP_ADDRESS, Ipv4Addr::LOCALHOST);
819
820 const OCTETS: [u8; 4] = IP_ADDRESS.octets();
821 assert_eq!(OCTETS, [127, 0, 0, 1]);
822
823 const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
824 assert!(!IS_UNSPECIFIED);
825
826 const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
827 assert!(IS_LOOPBACK);
828
829 const IS_PRIVATE: bool = IP_ADDRESS.is_private();
830 assert!(!IS_PRIVATE);
831
832 const IS_LINK_LOCAL: bool = IP_ADDRESS.is_link_local();
833 assert!(!IS_LINK_LOCAL);
834
835 const IS_GLOBAL: bool = IP_ADDRESS.is_global();
836 assert!(!IS_GLOBAL);
837
838 const IS_SHARED: bool = IP_ADDRESS.is_shared();
839 assert!(!IS_SHARED);
840
841 const IS_IETF_PROTOCOL_ASSIGNMENT: bool = IP_ADDRESS.is_ietf_protocol_assignment();
842 assert!(!IS_IETF_PROTOCOL_ASSIGNMENT);
843
844 const IS_BENCHMARKING: bool = IP_ADDRESS.is_benchmarking();
845 assert!(!IS_BENCHMARKING);
846
847 const IS_RESERVED: bool = IP_ADDRESS.is_reserved();
848 assert!(!IS_RESERVED);
849
850 const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
851 assert!(!IS_MULTICAST);
852
853 const IS_BROADCAST: bool = IP_ADDRESS.is_broadcast();
854 assert!(!IS_BROADCAST);
855
856 const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
857 assert!(!IS_DOCUMENTATION);
858
859 const IP_V6_COMPATIBLE: Ipv6Addr = IP_ADDRESS.to_ipv6_compatible();
860 assert_eq!(
861 IP_V6_COMPATIBLE,
862 Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1])
863 );
864
865 const IP_V6_MAPPED: Ipv6Addr = IP_ADDRESS.to_ipv6_mapped();
866 assert_eq!(
867 IP_V6_MAPPED,
868 Ipv6Addr::from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1])
869 );
870 }
871
872 #[test]
873 fn ipv6_const() {
874 // test that the methods of `Ipv6Addr` are usable in a const context
875
876 const IP_ADDRESS: Ipv6Addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
877 assert_eq!(IP_ADDRESS, Ipv6Addr::LOCALHOST);
878
879 const SEGMENTS: [u16; 8] = IP_ADDRESS.segments();
880 assert_eq!(SEGMENTS, [0, 0, 0, 0, 0, 0, 0, 1]);
881
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]);
884
885 const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
886 assert!(!IS_UNSPECIFIED);
887
888 const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
889 assert!(IS_LOOPBACK);
890
891 const IS_GLOBAL: bool = IP_ADDRESS.is_global();
892 assert!(!IS_GLOBAL);
893
894 const IS_UNIQUE_LOCAL: bool = IP_ADDRESS.is_unique_local();
895 assert!(!IS_UNIQUE_LOCAL);
896
897 const IS_UNICAST_LINK_LOCAL_STRICT: bool = IP_ADDRESS.is_unicast_link_local_strict();
898 assert!(!IS_UNICAST_LINK_LOCAL_STRICT);
899
900 const IS_UNICAST_LINK_LOCAL: bool = IP_ADDRESS.is_unicast_link_local();
901 assert!(!IS_UNICAST_LINK_LOCAL);
902
903 const IS_UNICAST_SITE_LOCAL: bool = IP_ADDRESS.is_unicast_site_local();
904 assert!(!IS_UNICAST_SITE_LOCAL);
905
906 const IS_DOCUMENTATION: bool = IP_ADDRESS.is_documentation();
907 assert!(!IS_DOCUMENTATION);
908
909 const IS_UNICAST_GLOBAL: bool = IP_ADDRESS.is_unicast_global();
910 assert!(!IS_UNICAST_GLOBAL);
911
912 const MULTICAST_SCOPE: Option<Ipv6MulticastScope> = IP_ADDRESS.multicast_scope();
913 assert_eq!(MULTICAST_SCOPE, None);
914
915 const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
916 assert!(!IS_MULTICAST);
917
918 const IP_V4: Option<Ipv4Addr> = IP_ADDRESS.to_ipv4();
919 assert_eq!(IP_V4.unwrap(), Ipv4Addr::new(0, 0, 0, 1));
920 }
921
922 #[test]
923 fn ip_const() {
924 // test that the methods of `IpAddr` are usable in a const context
925
926 const IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
927
928 const IS_UNSPECIFIED: bool = IP_ADDRESS.is_unspecified();
929 assert!(!IS_UNSPECIFIED);
930
931 const IS_LOOPBACK: bool = IP_ADDRESS.is_loopback();
932 assert!(IS_LOOPBACK);
933
934 const IS_GLOBAL: bool = IP_ADDRESS.is_global();
935 assert!(!IS_GLOBAL);
936
937 const IS_MULTICAST: bool = IP_ADDRESS.is_multicast();
938 assert!(!IS_MULTICAST);
939 }