github.com/metacubex/mihomo@v1.18.5/component/trie/trie_test.go (about) 1 package trie 2 3 import ( 4 "net" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestIpv4AddSuccess(t *testing.T) { 11 trie := NewIpCidrTrie() 12 err := trie.AddIpCidrForString("10.0.0.2/16") 13 assert.Equal(t, nil, err) 14 } 15 16 func TestIpv4AddFail(t *testing.T) { 17 trie := NewIpCidrTrie() 18 err := trie.AddIpCidrForString("333.00.23.2/23") 19 assert.IsType(t, new(net.ParseError), err) 20 21 err = trie.AddIpCidrForString("22.3.34.2/222") 22 assert.IsType(t, new(net.ParseError), err) 23 24 err = trie.AddIpCidrForString("2.2.2.2") 25 assert.IsType(t, new(net.ParseError), err) 26 } 27 28 func TestIpv4Search(t *testing.T) { 29 trie := NewIpCidrTrie() 30 // Boundary testing 31 assert.NoError(t, trie.AddIpCidrForString("149.154.160.0/20")) 32 assert.Equal(t, true, trie.IsContainForString("149.154.160.0")) 33 assert.Equal(t, true, trie.IsContainForString("149.154.175.255")) 34 assert.Equal(t, false, trie.IsContainForString("149.154.176.0")) 35 assert.Equal(t, false, trie.IsContainForString("149.154.159.255")) 36 37 assert.NoError(t, trie.AddIpCidrForString("129.2.36.0/16")) 38 assert.NoError(t, trie.AddIpCidrForString("10.2.36.0/18")) 39 assert.NoError(t, trie.AddIpCidrForString("16.2.23.0/24")) 40 assert.NoError(t, trie.AddIpCidrForString("11.2.13.2/26")) 41 assert.NoError(t, trie.AddIpCidrForString("55.5.6.3/8")) 42 assert.NoError(t, trie.AddIpCidrForString("66.23.25.4/6")) 43 44 assert.Equal(t, true, trie.IsContainForString("129.2.3.65")) 45 assert.Equal(t, false, trie.IsContainForString("15.2.3.1")) 46 assert.Equal(t, true, trie.IsContainForString("11.2.13.1")) 47 assert.Equal(t, true, trie.IsContainForString("55.0.0.0")) 48 assert.Equal(t, true, trie.IsContainForString("64.0.0.0")) 49 assert.Equal(t, false, trie.IsContainForString("128.0.0.0")) 50 51 assert.Equal(t, false, trie.IsContain(net.ParseIP("22"))) 52 assert.Equal(t, false, trie.IsContain(net.ParseIP(""))) 53 54 } 55 56 func TestIpv6AddSuccess(t *testing.T) { 57 trie := NewIpCidrTrie() 58 err := trie.AddIpCidrForString("2001:0db8:02de:0000:0000:0000:0000:0e13/32") 59 assert.Equal(t, nil, err) 60 61 err = trie.AddIpCidrForString("2001:1db8:f2de::0e13/18") 62 assert.Equal(t, nil, err) 63 } 64 65 func TestIpv6AddFail(t *testing.T) { 66 trie := NewIpCidrTrie() 67 err := trie.AddIpCidrForString("2001::25de::cade/23") 68 assert.IsType(t, new(net.ParseError), err) 69 70 err = trie.AddIpCidrForString("2001:0fa3:25de::cade/222") 71 assert.IsType(t, new(net.ParseError), err) 72 73 err = trie.AddIpCidrForString("2001:0fa3:25de::cade") 74 assert.IsType(t, new(net.ParseError), err) 75 } 76 77 func TestIpv6SearchSub(t *testing.T) { 78 trie := NewIpCidrTrie() 79 assert.NoError(t, trie.AddIpCidrForString("240e::/18")) 80 81 assert.Equal(t, true, trie.IsContainForString("240e:964:ea02:100:1800::71")) 82 83 } 84 85 func TestIpv6Search(t *testing.T) { 86 trie := NewIpCidrTrie() 87 88 // Boundary testing 89 assert.NoError(t, trie.AddIpCidrForString("2a0a:f280::/32")) 90 assert.Equal(t, true, trie.IsContainForString("2a0a:f280:0000:0000:0000:0000:0000:0000")) 91 assert.Equal(t, true, trie.IsContainForString("2a0a:f280:ffff:ffff:ffff:ffff:ffff:ffff")) 92 assert.Equal(t, false, trie.IsContainForString("2a0a:f279:ffff:ffff:ffff:ffff:ffff:ffff")) 93 assert.Equal(t, false, trie.IsContainForString("2a0a:f281:0000:0000:0000:0000:0000:0000")) 94 95 assert.NoError(t, trie.AddIpCidrForString("2001:b28:f23d:f001::e/128")) 96 assert.NoError(t, trie.AddIpCidrForString("2001:67c:4e8:f002::e/12")) 97 assert.NoError(t, trie.AddIpCidrForString("2001:b28:f23d:f003::e/96")) 98 assert.NoError(t, trie.AddIpCidrForString("2001:67c:4e8:f002::a/32")) 99 assert.NoError(t, trie.AddIpCidrForString("2001:67c:4e8:f004::a/60")) 100 assert.NoError(t, trie.AddIpCidrForString("2001:b28:f23f:f005::a/64")) 101 assert.Equal(t, true, trie.IsContainForString("2001:b28:f23d:f001::e")) 102 assert.Equal(t, false, trie.IsContainForString("2222::fff2")) 103 assert.Equal(t, true, trie.IsContainForString("2000::ffa0")) 104 assert.Equal(t, true, trie.IsContainForString("2001:b28:f23f:f005:5662::")) 105 assert.Equal(t, true, trie.IsContainForString("2001:67c:4e8:9666::1213")) 106 107 assert.Equal(t, false, trie.IsContain(net.ParseIP("22233:22"))) 108 } 109 110 func TestIpv4InIpv6(t *testing.T) { 111 trie := NewIpCidrTrie() 112 113 // Boundary testing 114 assert.NoError(t, trie.AddIpCidrForString("::ffff:198.18.5.138/128")) 115 }