github.com/database64128/shadowsocks-go@v1.7.0/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 ` 29 30 func testContains(t *testing.T, s *netipx.IPSet, addr netip.Addr, expectedResult bool) { 31 if s.Contains(addr) != expectedResult { 32 t.Errorf("%s should return %v", addr, expectedResult) 33 } 34 } 35 36 func testPrefixSet(t *testing.T, s *netipx.IPSet) { 37 testContains(t, s, netip.AddrFrom4([4]byte{}), true) 38 testContains(t, s, netip.AddrFrom4([4]byte{10, 0, 0, 1}), true) 39 testContains(t, s, netip.AddrFrom4([4]byte{100, 64, 0, 1}), true) 40 testContains(t, s, netip.AddrFrom4([4]byte{127, 0, 0, 1}), true) 41 testContains(t, s, netip.AddrFrom4([4]byte{169, 254, 0, 1}), true) 42 testContains(t, s, netip.AddrFrom4([4]byte{172, 16, 0, 1}), true) 43 testContains(t, s, netip.AddrFrom4([4]byte{192, 0, 0, 1}), true) 44 testContains(t, s, netip.AddrFrom4([4]byte{192, 0, 2, 1}), true) 45 testContains(t, s, netip.AddrFrom4([4]byte{192, 88, 99, 1}), true) 46 testContains(t, s, netip.AddrFrom4([4]byte{192, 168, 0, 1}), true) 47 testContains(t, s, netip.AddrFrom4([4]byte{198, 18, 0, 1}), true) 48 testContains(t, s, netip.AddrFrom4([4]byte{198, 51, 100, 1}), true) 49 testContains(t, s, netip.AddrFrom4([4]byte{203, 0, 113, 1}), true) 50 testContains(t, s, netip.AddrFrom4([4]byte{224, 0, 0, 1}), true) 51 testContains(t, s, netip.AddrFrom4([4]byte{1, 1, 1, 1}), false) 52 testContains(t, s, netip.AddrFrom4([4]byte{8, 8, 8, 8}), false) 53 testContains(t, s, netip.AddrFrom16([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), true) 54 testContains(t, s, netip.AddrFrom16([16]byte{0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), true) 55 testContains(t, s, netip.AddrFrom16([16]byte{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), true) 56 testContains(t, s, netip.AddrFrom16([16]byte{0x20, 0x01, 0x0d, 0xb8, 0xfa, 0xd6, 0x05, 0x72, 0xac, 0xbe, 0x71, 0x43, 0x14, 0xe5, 0x7a, 0x6e}), false) 57 testContains(t, s, netip.IPv6Unspecified(), false) 58 } 59 60 func TestPrefixSet(t *testing.T) { 61 s, err := IPSetFromText(testPrefixSetText) 62 if err != nil { 63 t.Fatal(err) 64 } 65 66 testPrefixSet(t, s) 67 68 text := IPSetToText(s) 69 if string(text) != testPrefixSetText[20:] { 70 t.Errorf("IPSetToText(s) returned %s", text) 71 } 72 }