Skip to main content

zeph_tools/
net.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Network utilities for tool crates.
5
6// Re-export the canonical implementation from zeph-common.
7pub use zeph_common::net::is_private_ip;
8
9#[cfg(test)]
10mod tests {
11    use super::*;
12    use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
13
14    #[test]
15    fn loopback_v4() {
16        assert!(is_private_ip(IpAddr::V4(Ipv4Addr::LOCALHOST)));
17    }
18
19    #[test]
20    fn private_class_a() {
21        assert!(is_private_ip("10.0.0.1".parse().unwrap()));
22    }
23
24    #[test]
25    fn private_class_b() {
26        assert!(is_private_ip("172.16.0.1".parse().unwrap()));
27    }
28
29    #[test]
30    fn private_class_c() {
31        assert!(is_private_ip("192.168.1.1".parse().unwrap()));
32    }
33
34    #[test]
35    fn link_local_v4() {
36        assert!(is_private_ip("169.254.1.1".parse().unwrap()));
37    }
38
39    #[test]
40    fn unspecified_v4() {
41        assert!(is_private_ip(IpAddr::V4(Ipv4Addr::UNSPECIFIED)));
42    }
43
44    #[test]
45    fn broadcast_v4() {
46        assert!(is_private_ip("255.255.255.255".parse().unwrap()));
47    }
48
49    #[test]
50    fn cgnat_v4() {
51        assert!(is_private_ip("100.64.0.1".parse().unwrap()));
52        assert!(is_private_ip("100.127.255.255".parse().unwrap()));
53    }
54
55    #[test]
56    fn public_v4_not_blocked() {
57        assert!(!is_private_ip("8.8.8.8".parse().unwrap()));
58        assert!(!is_private_ip("1.1.1.1".parse().unwrap()));
59    }
60
61    #[test]
62    fn loopback_v6() {
63        assert!(is_private_ip(IpAddr::V6(Ipv6Addr::LOCALHOST)));
64    }
65
66    #[test]
67    fn unspecified_v6() {
68        assert!(is_private_ip(IpAddr::V6(Ipv6Addr::UNSPECIFIED)));
69    }
70
71    #[test]
72    fn ula_v6() {
73        assert!(is_private_ip("fc00::1".parse().unwrap()));
74        assert!(is_private_ip("fd12:3456:789a::1".parse().unwrap()));
75    }
76
77    #[test]
78    fn link_local_v6() {
79        assert!(is_private_ip("fe80::1".parse().unwrap()));
80    }
81
82    #[test]
83    fn ipv4_mapped_private() {
84        assert!(is_private_ip("::ffff:127.0.0.1".parse().unwrap()));
85        assert!(is_private_ip("::ffff:192.168.0.1".parse().unwrap()));
86        assert!(is_private_ip("::ffff:100.64.0.1".parse().unwrap()));
87    }
88
89    #[test]
90    fn public_v6_not_blocked() {
91        assert!(!is_private_ip("2001:4860:4860::8888".parse().unwrap()));
92    }
93
94    #[test]
95    fn cgnat_boundary_not_blocked() {
96        assert!(!is_private_ip("100.128.0.0".parse().unwrap()));
97    }
98
99    #[test]
100    fn ipv4_mapped_unspecified() {
101        assert!(is_private_ip("::ffff:0.0.0.0".parse().unwrap()));
102    }
103}