simplify testing

This commit is contained in:
v2ray committed 2016-05-24 21:55:46 +02:00
1 parent 3582b9d869
commit fc63f0432c
99 files changed
+726 -817

No files matched your search

+2 -3
View File
@@ -6,18 +6,17 @@ import (
"testing"
. "github.com/v2ray/v2ray-core/app/router/rules"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestChinaIPJson(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
rule := ParseRule([]byte(`{
"type": "chinaip",
"outboundTag": "x"
}`))
assert.StringLiteral(rule.Tag).Equals("x")
assert.String(rule.Tag).Equals("x")
assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn
assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com
assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com
+1 -2
View File
@@ -6,7 +6,6 @@ import (
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
@@ -15,7 +14,7 @@ func makeDestination(ip string) v2net.Destination {
}
func TestChinaIP(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
rule := NewChinaIPRule("tag")
assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn
+2 -3
View File
@@ -6,18 +6,17 @@ import (
"testing"
. "github.com/v2ray/v2ray-core/app/router/rules"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestChinaSitesJson(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
rule := ParseRule([]byte(`{
"type": "chinasites",
"outboundTag": "y"
}`))
assert.StringLiteral(rule.Tag).Equals("y")
assert.String(rule.Tag).Equals("y")
assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue()
assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue()
assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue()
+1 -2
View File
@@ -5,7 +5,6 @@ import (
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
@@ -14,7 +13,7 @@ func makeDomainDestination(domain string) v2net.Destination {
}
func TestChinaSites(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
rule := NewChinaSitesRule("tag")
assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue()
+7 -7
View File
@@ -3,9 +3,9 @@ package rules
import (
"net"
"regexp"
"strings"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/serial"
)
type Condition interface {
@@ -63,12 +63,12 @@ func (this *AnyCondition) Len() int {
}
type PlainDomainMatcher struct {
pattern serial.StringT
pattern string
}
func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
return &PlainDomainMatcher{
pattern: serial.StringT(pattern),
pattern: pattern,
}
}
@@ -76,8 +76,8 @@ func (this *PlainDomainMatcher) Apply(dest v2net.Destination) bool {
if !dest.Address().IsDomain() {
return false
}
domain := serial.StringT(dest.Address().Domain())
return domain.Contains(this.pattern)
domain := dest.Address().Domain()
return strings.Contains(domain, this.pattern)
}
type RegexpDomainMatcher struct {
@@ -98,8 +98,8 @@ func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool {
if !dest.Address().IsDomain() {
return false
}
domain := serial.StringT(dest.Address().Domain())
return this.pattern.MatchString(domain.ToLower().String())
domain := dest.Address().Domain()
return this.pattern.MatchString(strings.ToLower(domain))
}
type CIDRMatcher struct {
+2 -3
View File
@@ -7,12 +7,11 @@ import (
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestDomainRule(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
rule := ParseRule([]byte(`{
"type": "field",
@@ -33,7 +32,7 @@ func TestDomainRule(t *testing.T) {
}
func TestIPRule(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
rule := ParseRule([]byte(`{
"type": "field",
+3 -2
View File
@@ -108,10 +108,11 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro
}
func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
rawEntry := this.cache.Get(dest)
destStr := dest.String()
rawEntry := this.cache.Get(destStr)
if rawEntry == nil {
tag, err := this.takeDetourWithoutCache(dest)
this.cache.Set(dest, newCacheEntry(tag, err))
this.cache.Set(destStr, newCacheEntry(tag, err))
return tag, err
}
entry := rawEntry.(*cacheEntry)
+2 -3
View File
@@ -11,12 +11,11 @@ import (
"github.com/v2ray/v2ray-core/app/router"
. "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestSimpleRouter(t *testing.T) {
v2testing.Current(t)
assert := assert.On(t)
config := &RouterRuleConfig{
Rules: []*Rule{
@@ -37,5 +36,5 @@ func TestSimpleRouter(t *testing.T) {
tag, err := r.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80))
assert.Error(err).IsNil()
assert.StringLiteral(tag).Equals("test")
assert.String(tag).Equals("test")
}