From 56a79a21906a179ea10d608c3c184fc485c9b0f4 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 30 Nov 2015 15:14:38 +0000 Subject: [PATCH] remove DetourTag and add more test cases --- app/router/router.go | 3 +-- app/router/rules/config/json/rules.go | 5 ++-- app/router/rules/config/rules.go | 3 +-- app/router/rules/config/testing/rule.go | 18 ++++++++++++++ app/router/rules/router.go | 7 ++---- app/router/rules/router_test.go | 29 ++++++++++++++++++++++ shell/point/config/config.go | 4 +-- shell/point/config/json/outbound_detour.go | 5 ++-- shell/point/config/testing/mocks/config.go | 4 +-- shell/point/point.go | 4 +-- 10 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 app/router/rules/config/testing/rule.go create mode 100644 app/router/rules/router_test.go diff --git a/app/router/router.go b/app/router/router.go index 137eaa91e..3852959fc 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -4,7 +4,6 @@ import ( "errors" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/shell/point/config" ) var ( @@ -12,7 +11,7 @@ var ( ) type Router interface { - TakeDetour(v2net.Destination) (config.DetourTag, error) + TakeDetour(v2net.Destination) (string, error) } type RouterFactory interface { diff --git a/app/router/rules/config/json/rules.go b/app/router/rules/config/json/rules.go index 3f936a765..8dbc15b49 100644 --- a/app/router/rules/config/json/rules.go +++ b/app/router/rules/config/json/rules.go @@ -2,7 +2,6 @@ package json import ( v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/shell/point/config" ) type Rule struct { @@ -10,8 +9,8 @@ type Rule struct { OutboundTag string `json:"outboundTag"` } -func (this *Rule) Tag() config.DetourTag { - return config.DetourTag(this.OutboundTag) +func (this *Rule) Tag() string { + return this.OutboundTag } func (this *Rule) Apply(dest v2net.Destination) bool { diff --git a/app/router/rules/config/rules.go b/app/router/rules/config/rules.go index 3ecb34b22..b18a30535 100644 --- a/app/router/rules/config/rules.go +++ b/app/router/rules/config/rules.go @@ -2,10 +2,9 @@ package config import ( v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/shell/point/config" ) type Rule interface { - Tag() config.DetourTag + Tag() string Apply(dest v2net.Destination) bool } diff --git a/app/router/rules/config/testing/rule.go b/app/router/rules/config/testing/rule.go new file mode 100644 index 000000000..775003337 --- /dev/null +++ b/app/router/rules/config/testing/rule.go @@ -0,0 +1,18 @@ +package testing + +import ( + v2net "github.com/v2ray/v2ray-core/common/net" +) + +type TestRule struct { + Function func(v2net.Destination) bool + TagValue string +} + +func (this *TestRule) Apply(dest v2net.Destination) bool { + return this.Function(dest) +} + +func (this *TestRule) Tag() string { + return this.TagValue +} diff --git a/app/router/rules/router.go b/app/router/rules/router.go index 432b24050..b8c4752a6 100644 --- a/app/router/rules/router.go +++ b/app/router/rules/router.go @@ -7,27 +7,24 @@ import ( "github.com/v2ray/v2ray-core/app/router/rules/config" "github.com/v2ray/v2ray-core/app/router/rules/config/json" v2net "github.com/v2ray/v2ray-core/common/net" - pointconfig "github.com/v2ray/v2ray-core/shell/point/config" ) var ( InvalidRule = errors.New("Invalid Rule") NoRuleApplicable = errors.New("No rule applicable") - - EmptyTag = pointconfig.DetourTag("") ) type Router struct { rules []config.Rule } -func (this *Router) TakeDetour(dest v2net.Destination) (pointconfig.DetourTag, error) { +func (this *Router) TakeDetour(dest v2net.Destination) (string, error) { for _, rule := range this.rules { if rule.Apply(dest) { return rule.Tag(), nil } } - return EmptyTag, NoRuleApplicable + return "", NoRuleApplicable } type RouterFactory struct { diff --git a/app/router/rules/router_test.go b/app/router/rules/router_test.go new file mode 100644 index 000000000..ee222c381 --- /dev/null +++ b/app/router/rules/router_test.go @@ -0,0 +1,29 @@ +package rules + +import ( + "testing" + + "github.com/v2ray/v2ray-core/app/router/rules/config" + testinconfig "github.com/v2ray/v2ray-core/app/router/rules/config/testing" + v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/testing/unit" +) + +func TestSimpleRouter(t *testing.T) { + assert := unit.Assert(t) + + router := &Router{ + rules: []config.Rule{ + &testinconfig.TestRule{ + TagValue: "test", + Function: func(dest v2net.Destination) bool { + return dest.IsTCP() + }, + }, + }, + } + + tag, err := router.TakeDetour(v2net.NewTCPDestination(v2net.DomainAddress("v2ray.com", 80))) + assert.Error(err).IsNil() + assert.String(tag).Equals("test") +} diff --git a/shell/point/config/config.go b/shell/point/config/config.go index 0fe3d2f51..2e524ecd8 100644 --- a/shell/point/config/config.go +++ b/shell/point/config/config.go @@ -5,8 +5,6 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" ) -type DetourTag string - type ConnectionConfig interface { Protocol() string Settings() interface{} @@ -24,7 +22,7 @@ type InboundDetourConfig interface { type OutboundDetourConfig interface { Protocol() string - Tag() DetourTag + Tag() string Settings() interface{} } diff --git a/shell/point/config/json/outbound_detour.go b/shell/point/config/json/outbound_detour.go index 189978ab7..a5cbe0080 100644 --- a/shell/point/config/json/outbound_detour.go +++ b/shell/point/config/json/outbound_detour.go @@ -4,7 +4,6 @@ import ( "encoding/json" proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" - "github.com/v2ray/v2ray-core/shell/point/config" ) type OutboundDetourConfig struct { @@ -17,8 +16,8 @@ func (this *OutboundDetourConfig) Protocol() string { return this.ProtocolValue } -func (this *OutboundDetourConfig) Tag() config.DetourTag { - return config.DetourTag(this.TagValue) +func (this *OutboundDetourConfig) Tag() string { + return this.TagValue } func (this *OutboundDetourConfig) Settings() interface{} { diff --git a/shell/point/config/testing/mocks/config.go b/shell/point/config/testing/mocks/config.go index d1ae533a5..ddb64d5a4 100644 --- a/shell/point/config/testing/mocks/config.go +++ b/shell/point/config/testing/mocks/config.go @@ -47,10 +47,10 @@ func (this *InboundDetourConfig) PortRange() v2net.PortRange { type OutboundDetourConfig struct { ConnectionConfig - TagValue config.DetourTag + TagValue string } -func (this *OutboundDetourConfig) Tag() config.DetourTag { +func (this *OutboundDetourConfig) Tag() string { return this.TagValue } diff --git a/shell/point/point.go b/shell/point/point.go index 95879fa14..41e24f9bb 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -16,7 +16,7 @@ type Point struct { ich connhandler.InboundConnectionHandler och connhandler.OutboundConnectionHandler idh []*InboundDetourHandler - odh map[config.DetourTag]connhandler.OutboundConnectionHandler + odh map[string]connhandler.OutboundConnectionHandler router router.Router } @@ -70,7 +70,7 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) { outboundDetours := pConfig.OutboundDetours() if len(outboundDetours) > 0 { - vpoint.odh = make(map[config.DetourTag]connhandler.OutboundConnectionHandler) + vpoint.odh = make(map[string]connhandler.OutboundConnectionHandler) for _, detourConfig := range outboundDetours { detourFactory := connhandler.GetOutboundConnectionHandlerFactory(detourConfig.Protocol()) if detourFactory == nil {