github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/prefixset/prefixset_test.go (about) 1 package prefixset 2 3 import ( 4 "net/netip" 5 "testing" 6 7 "go4.org/netipx" 8 ) 9 10 const testPrefixSetText = `# Private prefixes. 11 0.0.0.0/8 12 10.0.0.0/8 13 100.64.0.0/10 14 127.0.0.0/8 15 169.254.0.0/16 16 172.16.0.0/12 17 192.0.0.0/24 18 192.0.2.0/24 19 192.88.99.0/24 20 192.168.0.0/16 21 198.18.0.0/15 22 198.51.100.0/24 23 203.0.113.0/24 24 224.0.0.0/3 25 ::1/128 26 fc00::/7 27 fe80::/10 28 ff00::/8 29 ` 30 31 func testContains(t *testing.T, s *netipx.IPSet, addr netip.Addr, expectedResult bool) { 32 if s.Contains(addr) != expectedResult { 33 t.Errorf("%s should return %v", addr, expectedResult) 34 } 35 } 36 37 func testPrefixSet(t *testing.T, s *netipx.IPSet) { 38 testContains(t, s, netip.IPv4Unspecified(), true) 39 testContains(t, s, netip.AddrFrom4([4]byte{10, 0, 0, 1}), true) 40 testContains(t, s, netip.AddrFrom4([4]byte{100, 64, 0, 1}), true) 41 testContains(t, s, netip.AddrFrom4([4]byte{127, 0, 0, 1}), true) 42 testContains(t, s, netip.AddrFrom4([4]byte{169, 254, 0, 1}), true) 43 testContains(t, s, netip.AddrFrom4([4]byte{172, 16, 0, 1}), true) 44 testContains(t, s, netip.AddrFrom4([4]byte{192, 0, 0, 1}), true) 45 testContains(t, s, netip.AddrFrom4([4]byte{192, 0, 2, 1}), true) 46 testContains(t, s, netip.AddrFrom4([4]byte{192, 88, 99, 1}), true) 47 testContains(t, s, netip.AddrFrom4([4]byte{192, 168, 0, 1}), true) 48 testContains(t, s, netip.AddrFrom4([4]byte{198, 18, 0, 1}), true) 49 testContains(t, s, netip.AddrFrom4([4]byte{198, 51, 100, 1}), true) 50 testContains(t, s, netip.AddrFrom4([4]byte{203, 0, 113, 1}), true) 51 testContains(t, s, netip.AddrFrom4([4]byte{224, 0, 0, 1}), true) 52 testContains(t, s, netip.AddrFrom4([4]byte{1, 1, 1, 1}), false) 53 testContains(t, s, netip.AddrFrom4([4]byte{8, 8, 8, 8}), false) 54 testContains(t, s, netip.IPv6Loopback(), true) 55 testContains(t, s, netip.AddrFrom16([16]byte{0: 0xfc, 15: 1}), true) 56 testContains(t, s, netip.AddrFrom16([16]byte{0: 0xfe, 1: 0x80, 15: 1}), true) 57 testContains(t, s, netip.AddrFrom16([16]byte{0: 0xff, 15: 1}), true) 58 testContains(t, s, netip.AddrFrom16([16]byte{0x20, 0x01, 0x0d, 0xb8, 0xfa, 0xd6, 0x05, 0x72, 0xac, 0xbe, 0x71, 0x43, 0x14, 0xe5, 0x7a, 0x6e}), false) 59 testContains(t, s, netip.IPv6Unspecified(), false) 60 } 61 62 func TestPrefixSet(t *testing.T) { 63 s, err := IPSetFromText(testPrefixSetText) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 testPrefixSet(t, s) 69 70 text := IPSetToText(s) 71 if string(text) != testPrefixSetText[20:] { 72 t.Errorf("IPSetToText(s) returned %s", text) 73 } 74 }