// SPDX-License-Identifier: GPL-3.0-or-later package netutil import "testing" func TestPublicIPMatchesPythonCases(t *testing.T) { for _, address := range []string{"8.8.8.8", "1.1.1.1"} { if !IsPublicIP(address) { t.Fatalf("expected public: %s", address) } } for _, address := range []string{ "10.0.0.5", "192.168.1.1", "172.16.0.1", "127.0.0.1", "169.254.1.1", "100.64.0.1", "::1", "not-an-ip", "", } { if IsPublicIP(address) { t.Fatalf("expected non-public: %s", address) } } } func TestCIDRAndEndpointHelpers(t *testing.T) { if !IPInCIDRs("203.0.113.55", []string{"203.0.113.0/24"}) || IPInCIDRs("8.8.8.8", []string{"203.0.113.0/24"}) { t.Fatal("CIDR matching failed") } host, port := SplitHostPort("[2001:db8::1]:22") if host != "2001:db8::1" || port != "22" { t.Fatalf("unexpected IPv6 split: %q %q", host, port) } host, port = SplitHostPort("10.2.0.2%proton0:61877") if host != "10.2.0.2%proton0" || port != "61877" { t.Fatalf("unexpected zone split: %q %q", host, port) } }