From 156146036b51e65b825d8d3dce9a7852f2097135 Mon Sep 17 00:00:00 2001 From: longXboy Date: Mon, 31 May 2021 17:17:33 +0800 Subject: [PATCH] fix test --- internal/host/host.go | 9 +-------- internal/host/host_test.go | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/internal/host/host.go b/internal/host/host.go index ce092c07e..0014c0a2e 100644 --- a/internal/host/host.go +++ b/internal/host/host.go @@ -7,14 +7,7 @@ import ( func isValidIP(addr string) bool { ip := net.ParseIP(addr) - if ip4 := ip.To4(); ip4 != nil { - return !ip4.IsLoopback() - } - // Following RFC 4193, Section 3. Private Address Space which says: - // The Internet Assigned Numbers Authority (IANA) has reserved the - // following block of the IPv6 address space for local internets: - // FC00:: - FDFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF (FC00::/7 prefix) - return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc + return ip.IsGlobalUnicast() && !ip.IsInterfaceLocalMulticast() } // Port return a real port. diff --git a/internal/host/host_test.go b/internal/host/host_test.go index e3b6436e1..36d6548b3 100644 --- a/internal/host/host_test.go +++ b/internal/host/host_test.go @@ -5,36 +5,40 @@ import ( "testing" ) -func TestPrivateIP(t *testing.T) { +func TestValidIP(t *testing.T) { tests := []struct { addr string expect bool }{ + {"127.0.0.1", false}, + {"255.255.255.255", false}, + {"0.0.0.0", false}, + {"localhost", false}, {"10.1.0.1", true}, {"172.16.0.1", true}, {"192.168.1.1", true}, - {"8.8.8.8", false}, - {"1.1.1.1", false}, - {"9.255.255.255", false}, + {"8.8.8.8", true}, + {"1.1.1.1", true}, + {"9.255.255.255", true}, {"10.0.0.0", true}, {"10.255.255.255", true}, - {"11.0.0.0", false}, - {"172.15.255.255", false}, + {"11.0.0.0", true}, + {"172.15.255.255", true}, {"172.16.0.0", true}, {"172.16.255.255", true}, {"172.23.18.255", true}, {"172.31.255.255", true}, {"172.31.0.0", true}, - {"172.32.0.0", false}, - {"192.167.255.255", false}, + {"172.32.0.0", true}, + {"192.167.255.255", true}, {"192.168.0.0", true}, {"192.168.255.255", true}, - {"192.169.0.0", false}, - {"fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false}, + {"192.169.0.0", true}, + {"fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true}, {"fc00::", true}, {"fcff:1200:0:44::", true}, {"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true}, - {"fe00::", false}, + {"fe00::", true}, } for _, test := range tests { t.Run(test.addr, func(t *testing.T) {