2015-11-21 15:43:40 -05:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2015-11-26 06:22:34 -05:00
|
|
|
"encoding/json"
|
2015-11-21 15:43:40 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2015-11-21 15:43:40 -05:00
|
|
|
)
|
|
|
|
|
2015-11-26 06:22:34 -05:00
|
|
|
func TestStringListParsingList(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-26 06:22:34 -05:00
|
|
|
|
|
|
|
rawJson := `["a", "b", "c", "d"]`
|
|
|
|
var strList StringList
|
|
|
|
err := json.Unmarshal([]byte(rawJson), &strList)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(strList.Len()).Equals(4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringListParsingString(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-26 06:22:34 -05:00
|
|
|
|
|
|
|
rawJson := `"abcd"`
|
|
|
|
var strList StringList
|
|
|
|
err := json.Unmarshal([]byte(rawJson), &strList)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
assert.Int(strList.Len()).Equals(1)
|
|
|
|
}
|
|
|
|
|
2015-11-21 15:43:40 -05:00
|
|
|
func TestDomainMatching(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-21 15:43:40 -05:00
|
|
|
|
2015-12-07 05:58:43 -05:00
|
|
|
rawJson := `{
|
|
|
|
"type": "field",
|
|
|
|
"domain": ["google.com", "regexp:v2ray.com$"],
|
|
|
|
"tag": "test"
|
|
|
|
}`
|
|
|
|
rule := parseRule([]byte(rawJson))
|
2015-11-21 15:43:40 -05:00
|
|
|
dest := v2net.NewTCPDestination(v2net.DomainAddress("www.v2ray.com", 80))
|
|
|
|
assert.Bool(rule.Apply(dest)).IsTrue()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPortMatching(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-21 15:43:40 -05:00
|
|
|
|
|
|
|
rule := &FieldRule{
|
|
|
|
Port: &v2nettesting.PortRange{
|
|
|
|
FromValue: 0,
|
|
|
|
ToValue: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dest := v2net.NewTCPDestination(v2net.DomainAddress("www.v2ray.com", 80))
|
|
|
|
assert.Bool(rule.Apply(dest)).IsTrue()
|
|
|
|
}
|
2015-11-22 15:16:28 -05:00
|
|
|
|
|
|
|
func TestIPMatching(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-22 15:16:28 -05:00
|
|
|
|
|
|
|
rawJson := `{
|
|
|
|
"type": "field",
|
|
|
|
"ip": "10.0.0.0/8",
|
|
|
|
"tag": "test"
|
|
|
|
}`
|
|
|
|
rule := parseRule([]byte(rawJson))
|
|
|
|
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 80))
|
|
|
|
assert.Bool(rule.Apply(dest)).IsTrue()
|
|
|
|
}
|
2015-11-22 16:14:27 -05:00
|
|
|
|
2015-11-30 07:46:09 -05:00
|
|
|
func TestIPListMatching(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-30 07:46:09 -05:00
|
|
|
|
|
|
|
rawJson := `{
|
|
|
|
"type": "field",
|
|
|
|
"ip": ["10.0.0.0/8", "192.168.0.0/16"],
|
|
|
|
"tag": "test"
|
|
|
|
}`
|
|
|
|
rule := parseRule([]byte(rawJson))
|
|
|
|
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{192, 168, 1, 1}, 80))
|
|
|
|
assert.Bool(rule.Apply(dest)).IsTrue()
|
|
|
|
}
|
|
|
|
|
2015-11-22 16:14:27 -05:00
|
|
|
func TestPortNotMatching(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-22 16:14:27 -05:00
|
|
|
|
|
|
|
rawJson := `{
|
|
|
|
"type": "field",
|
|
|
|
"port": "80-100",
|
|
|
|
"tag": "test"
|
|
|
|
}`
|
|
|
|
rule := parseRule([]byte(rawJson))
|
|
|
|
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 79))
|
|
|
|
assert.Bool(rule.Apply(dest)).IsFalse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDomainNotMatching(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-22 16:14:27 -05:00
|
|
|
|
|
|
|
rawJson := `{
|
|
|
|
"type": "field",
|
2015-11-26 06:22:34 -05:00
|
|
|
"domain": ["google.com", "v2ray.com"],
|
2015-11-22 16:14:27 -05:00
|
|
|
"tag": "test"
|
|
|
|
}`
|
|
|
|
rule := parseRule([]byte(rawJson))
|
2015-11-26 06:22:34 -05:00
|
|
|
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 80))
|
|
|
|
assert.Bool(rule.Apply(dest)).IsFalse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDomainNotMatchingDomain(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-26 06:22:34 -05:00
|
|
|
|
|
|
|
rawJson := `{
|
|
|
|
"type": "field",
|
|
|
|
"domain": ["google.com", "v2ray.com"],
|
|
|
|
"tag": "test"
|
|
|
|
}`
|
|
|
|
rule := parseRule([]byte(rawJson))
|
|
|
|
dest := v2net.NewTCPDestination(v2net.DomainAddress("baidu.com", 80))
|
2015-11-22 16:14:27 -05:00
|
|
|
assert.Bool(rule.Apply(dest)).IsFalse()
|
|
|
|
}
|