-
Notifications
You must be signed in to change notification settings - Fork 127
/
pattern_test.go
69 lines (60 loc) · 2.54 KB
/
pattern_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// date : 2016-05-13
// author: xjdrew
//
package kone
import (
"net"
"testing"
"github.com/xjdrew/kone/tcpip"
"github.com/stretchr/testify/assert"
)
func TestDomainPattern(t *testing.T) {
pattern := NewDomainPattern("A", "Example.com")
assert.Equal(t, "A", pattern.Proxy())
assert.True(t, pattern.Match("example.com"))
assert.True(t, pattern.Match("Example.Com")) // case insensitive
assert.False(t, pattern.Match("api.example.com")) // suffix
assert.False(t, pattern.Match("1example.com"))
assert.False(t, pattern.Match("example.hk"))
assert.False(t, pattern.Match("example.com.hk"))
}
func TestDomainSuffixPattern(t *testing.T) {
pattern := NewDomainSuffixPattern("A", "Example.com")
assert.Equal(t, "A", pattern.Proxy())
assert.True(t, pattern.Match("example.com"))
assert.True(t, pattern.Match("Example.Com")) // case insensitive
assert.True(t, pattern.Match("api.example.com")) // suffix
assert.True(t, pattern.Match("1example.com"))
assert.False(t, pattern.Match("example.hk"))
assert.False(t, pattern.Match("example.com.hk"))
}
func TestDomainKeywordPattern(t *testing.T) {
pattern := NewDomainKeywordPattern("A", "Example.com")
assert.Equal(t, "A", pattern.Proxy())
assert.True(t, pattern.Match("example.com"))
assert.True(t, pattern.Match("Example.Com")) // case insensitive
assert.True(t, pattern.Match("api.example.com")) // suffix
assert.True(t, pattern.Match("1example.com"))
assert.False(t, pattern.Match("example.hk"))
assert.True(t, pattern.Match("example.com.hk"))
}
func TestIPCountryPattern(t *testing.T) {
pattern := NewGEOIPPattern("A", "US")
assert.Equal(t, "A", pattern.Proxy())
assert.True(t, pattern.Match(net.ParseIP("216.58.197.99"))) // google.hk
assert.False(t, pattern.Match(net.ParseIP("110.242.68.66"))) // baidu.com, china
}
func TestIPCIDRPattern(t *testing.T) {
_, ipNet, _ := net.ParseCIDR("192.168.100.1/16")
pattern := NewIPCIDRPattern("D", ipNet)
assert.Equal(t, "D", pattern.Proxy())
assert.False(t, pattern.Match(net.ParseIP("192.167.255.255")))
assert.True(t, pattern.Match(net.ParseIP("192.168.0.0")))
assert.True(t, pattern.Match(net.ParseIP("192.168.255.255")))
assert.True(t, pattern.Match(net.ParseIP("192.168.108.255")))
assert.False(t, pattern.Match(tcpip.ConvertIPv4ToUint32(net.ParseIP("192.167.255.255"))))
assert.True(t, pattern.Match(tcpip.ConvertIPv4ToUint32(net.ParseIP("192.168.0.0"))))
assert.True(t, pattern.Match(tcpip.ConvertIPv4ToUint32(net.ParseIP("192.168.255.255"))))
assert.True(t, pattern.Match(tcpip.ConvertIPv4ToUint32(net.ParseIP("192.168.108.255"))))
}