From ed2779e3337b6914a1ab804a2ed16c1dbcb60f2e Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 22 Oct 2018 20:59:01 +0200 Subject: [PATCH] more test case for router --- app/router/condition.go | 3 +-- app/router/condition_test.go | 46 ++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/app/router/condition.go b/app/router/condition.go index 120dd39b5..53d0f162d 100644 --- a/app/router/condition.go +++ b/app/router/condition.go @@ -4,10 +4,9 @@ import ( "context" "strings" - "v2ray.com/core/common/session" - "v2ray.com/core/app/dispatcher" "v2ray.com/core/common/net" + "v2ray.com/core/common/session" "v2ray.com/core/common/strmatcher" "v2ray.com/core/proxy" ) diff --git a/app/router/condition_test.go b/app/router/condition_test.go index d0053a558..d8feb12ab 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -31,8 +31,6 @@ func withInbound(inbound *session.Inbound) context.Context { } func TestRoutingRule(t *testing.T) { - assert := With(t) - type ruleTest struct { input context.Context output bool @@ -126,6 +124,26 @@ func TestRoutingRule(t *testing.T) { }, }, }, + { + rule: &RoutingRule{ + SourceCidr: []*CIDR{ + { + Ip: []byte{192, 168, 0, 0}, + Prefix: 16, + }, + }, + }, + test: []ruleTest{ + { + input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("192.168.0.1"), 80)}), + output: true, + }, + { + input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("10.0.0.1"), 80)}), + output: false, + }, + }, + }, { rule: &RoutingRule{ UserEmail: []string{ @@ -158,14 +176,32 @@ func TestRoutingRule(t *testing.T) { }, }, }, + { + rule: &RoutingRule{ + InboundTag: []string{"test", "test1"}, + }, + test: []ruleTest{ + { + input: withInbound(&session.Inbound{Tag: "test"}), + output: true, + }, + { + input: withInbound(&session.Inbound{Tag: "test2"}), + output: false, + }, + }, + }, } for _, test := range cases { cond, err := test.rule.BuildCondition() - assert(err, IsNil) + common.Must(err) - for _, t := range test.test { - assert(cond.Apply(t.input), Equals, t.output) + for _, subtest := range test.test { + actual := cond.Apply(subtest.input) + if actual != subtest.output { + t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual) + } } } }