From 98ee44befdac36a6e32269b3c01b8eea73d29555 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Tue, 7 Sep 2021 13:43:28 +0100 Subject: [PATCH] update test for router refactor --- app/dns/dns_test.go | 36 +++++++------- app/dns/hosts_test.go | 2 +- app/router/command/command_test.go | 5 +- app/router/condition_geoip_test.go | 11 +++-- app/router/condition_test.go | 31 ++++++------ app/router/router_test.go | 13 ++--- infra/conf/synthetic/dns/dns_test.go | 2 +- infra/conf/synthetic/router/router_test.go | 55 +++++++++++----------- infra/conf/v4/v2ray_test.go | 7 +-- testing/scenarios/dns_test.go | 5 +- testing/scenarios/reverse_test.go | 17 +++---- 11 files changed, 96 insertions(+), 88 deletions(-) diff --git a/app/dns/dns_test.go b/app/dns/dns_test.go index 91566cb9a..69d7139d7 100644 --- a/app/dns/dns_test.go +++ b/app/dns/dns_test.go @@ -1,6 +1,7 @@ package dns_test import ( + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "google.golang.org/protobuf/types/known/anypb" "testing" "time" @@ -14,7 +15,6 @@ import ( "github.com/v2fly/v2ray-core/v4/app/policy" "github.com/v2fly/v2ray-core/v4/app/proxyman" _ "github.com/v2fly/v2ray-core/v4/app/proxyman/outbound" - "github.com/v2fly/v2ray-core/v4/app/router" "github.com/v2fly/v2ray-core/v4/common" "github.com/v2fly/v2ray-core/v4/common/net" "github.com/v2fly/v2ray-core/v4/common/serial" @@ -430,7 +430,7 @@ func TestStaticHostDomain(t *testing.T) { Port: uint32(port), }, }, - StaticHosts: []*Config_HostMapping{ + StaticHosts: []*HostMapping{ { Type: DomainMatchingType_Full, Domain: "example.com", @@ -496,10 +496,10 @@ func TestIPMatch(t *testing.T) { }, Port: uint32(port), }, - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { CountryCode: "local", - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { // inner ip, will not match Ip: []byte{192, 168, 11, 1}, @@ -520,10 +520,10 @@ func TestIPMatch(t *testing.T) { }, Port: uint32(port), }, - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { CountryCode: "test", - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{8, 8, 8, 8}, Prefix: 32, @@ -532,7 +532,7 @@ func TestIPMatch(t *testing.T) { }, { CountryCode: "test", - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{8, 8, 8, 4}, Prefix: 32, @@ -620,10 +620,10 @@ func TestLocalDomain(t *testing.T) { // Equivalent of dotless:localhost {Type: DomainMatchingType_Regex, Domain: "^[^.]*localhost[^.]*$"}, }, - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { // Will match localhost, localhost-a and localhost-b, CountryCode: "local", - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ {Ip: []byte{127, 0, 0, 2}, Prefix: 32}, {Ip: []byte{127, 0, 0, 3}, Prefix: 32}, {Ip: []byte{127, 0, 0, 4}, Prefix: 32}, @@ -649,7 +649,7 @@ func TestLocalDomain(t *testing.T) { }, }, }, - StaticHosts: []*Config_HostMapping{ + StaticHosts: []*HostMapping{ { Type: DomainMatchingType_Full, Domain: "hostnamestatic", @@ -818,9 +818,9 @@ func TestMultiMatchPrioritizedDomain(t *testing.T) { Domain: "google.com", }, }, - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { // Will only match 8.8.8.8 and 8.8.4.4 - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ {Ip: []byte{8, 8, 8, 8}, Prefix: 32}, {Ip: []byte{8, 8, 4, 4}, Prefix: 32}, }, @@ -843,9 +843,9 @@ func TestMultiMatchPrioritizedDomain(t *testing.T) { Domain: "google.com", }, }, - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { // Will match 8.8.8.8 and 8.8.8.7, etc - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ {Ip: []byte{8, 8, 8, 7}, Prefix: 24}, }, }, @@ -867,9 +867,9 @@ func TestMultiMatchPrioritizedDomain(t *testing.T) { Domain: "api.google.com", }, }, - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { // Will only match 8.8.7.7 (api.google.com) - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ {Ip: []byte{8, 8, 7, 7}, Prefix: 32}, }, }, @@ -891,9 +891,9 @@ func TestMultiMatchPrioritizedDomain(t *testing.T) { Domain: "v2.api.google.com", }, }, - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { // Will only match 8.8.7.8 (v2.api.google.com) - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ {Ip: []byte{8, 8, 7, 8}, Prefix: 32}, }, }, diff --git a/app/dns/hosts_test.go b/app/dns/hosts_test.go index 1a36bbb4d..64790f7e4 100644 --- a/app/dns/hosts_test.go +++ b/app/dns/hosts_test.go @@ -12,7 +12,7 @@ import ( ) func TestStaticHosts(t *testing.T) { - pb := []*Config_HostMapping{ + pb := []*HostMapping{ { Type: DomainMatchingType_Full, Domain: "v2fly.org", diff --git a/app/router/command/command_test.go b/app/router/command/command_test.go index 5d8bfde4c..c9889bb62 100644 --- a/app/router/command/command_test.go +++ b/app/router/command/command_test.go @@ -2,6 +2,7 @@ package command_test import ( "context" + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "testing" "time" @@ -232,11 +233,11 @@ func TestSerivceTestRoute(t *testing.T) { TargetTag: &router.RoutingRule_Tag{Tag: "out"}, }, { - Domain: []*router.Domain{{Type: router.Domain_RootDomain, Value: "com"}}, + Domain: []*routercommon.Domain{{Type: routercommon.Domain_RootDomain, Value: "com"}}, TargetTag: &router.RoutingRule_Tag{Tag: "out"}, }, { - SourceGeoip: []*router.GeoIP{{CountryCode: "private", Cidr: []*router.CIDR{{Ip: []byte{127, 0, 0, 0}, Prefix: 8}}}}, + SourceGeoip: []*routercommon.GeoIP{{CountryCode: "private", Cidr: []*routercommon.CIDR{{Ip: []byte{127, 0, 0, 0}, Prefix: 8}}}}, TargetTag: &router.RoutingRule_Tag{Tag: "out"}, }, { diff --git a/app/router/condition_geoip_test.go b/app/router/condition_geoip_test.go index 9a2f957a2..a32d9f6c3 100644 --- a/app/router/condition_geoip_test.go +++ b/app/router/condition_geoip_test.go @@ -2,6 +2,7 @@ package router_test import ( "errors" + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "io/fs" "os" "path/filepath" @@ -38,17 +39,17 @@ func init() { func TestGeoIPMatcherContainer(t *testing.T) { container := &router.GeoIPMatcherContainer{} - m1, err := container.Add(&router.GeoIP{ + m1, err := container.Add(&routercommon.GeoIP{ CountryCode: "CN", }) common.Must(err) - m2, err := container.Add(&router.GeoIP{ + m2, err := container.Add(&routercommon.GeoIP{ CountryCode: "US", }) common.Must(err) - m3, err := container.Add(&router.GeoIP{ + m3, err := container.Add(&routercommon.GeoIP{ CountryCode: "CN", }) common.Must(err) @@ -194,12 +195,12 @@ func TestGeoIPMatcher6US(t *testing.T) { } } -func loadGeoIP(country string) ([]*router.CIDR, error) { +func loadGeoIP(country string) ([]*routercommon.CIDR, error) { geoipBytes, err := filesystem.ReadAsset("geoip.dat") if err != nil { return nil, err } - var geoipList router.GeoIPList + var geoipList routercommon.GeoIPList if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil { return nil, err } diff --git a/app/router/condition_test.go b/app/router/condition_test.go index 7f576abd9..08787de1c 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -2,6 +2,7 @@ package router_test import ( "errors" + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "io/fs" "os" "path/filepath" @@ -79,18 +80,18 @@ func TestRoutingRule(t *testing.T) { }{ { rule: &router.RoutingRule{ - Domain: []*router.Domain{ + Domain: []*routercommon.Domain{ { Value: "v2fly.org", - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, }, { Value: "google.com", - Type: router.Domain_RootDomain, + Type: routercommon.Domain_RootDomain, }, { Value: "^facebook\\.com$", - Type: router.Domain_Regex, + Type: routercommon.Domain_Regex, }, }, }, @@ -127,7 +128,7 @@ func TestRoutingRule(t *testing.T) { }, { rule: &router.RoutingRule{ - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{8, 8, 8, 8}, Prefix: 32, @@ -163,9 +164,9 @@ func TestRoutingRule(t *testing.T) { }, { rule: &router.RoutingRule{ - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{8, 8, 8, 8}, Prefix: 32, @@ -203,7 +204,7 @@ func TestRoutingRule(t *testing.T) { }, { rule: &router.RoutingRule{ - SourceCidr: []*router.CIDR{ + SourceCidr: []*routercommon.CIDR{ { Ip: []byte{192, 168, 0, 0}, Prefix: 16, @@ -351,12 +352,12 @@ func TestRoutingRule(t *testing.T) { } } -func loadGeoSite(country string) ([]*router.Domain, error) { +func loadGeoSite(country string) ([]*routercommon.Domain, error) { geositeBytes, err := filesystem.ReadAsset("geosite.dat") if err != nil { return nil, err } - var geositeList router.GeoSiteList + var geositeList routercommon.GeoSiteList if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil { return nil, err } @@ -502,12 +503,12 @@ func BenchmarkDomainMatcher(b *testing.B) { } func BenchmarkMultiGeoIPMatcher(b *testing.B) { - var geoips []*router.GeoIP + var geoips []*routercommon.GeoIP { ips, err := loadGeoIP("CN") common.Must(err) - geoips = append(geoips, &router.GeoIP{ + geoips = append(geoips, &routercommon.GeoIP{ CountryCode: "CN", Cidr: ips, }) @@ -516,7 +517,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) { { ips, err := loadGeoIP("JP") common.Must(err) - geoips = append(geoips, &router.GeoIP{ + geoips = append(geoips, &routercommon.GeoIP{ CountryCode: "JP", Cidr: ips, }) @@ -525,7 +526,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) { { ips, err := loadGeoIP("CA") common.Must(err) - geoips = append(geoips, &router.GeoIP{ + geoips = append(geoips, &routercommon.GeoIP{ CountryCode: "CA", Cidr: ips, }) @@ -534,7 +535,7 @@ func BenchmarkMultiGeoIPMatcher(b *testing.B) { { ips, err := loadGeoIP("US") common.Must(err) - geoips = append(geoips, &router.GeoIP{ + geoips = append(geoips, &routercommon.GeoIP{ CountryCode: "US", Cidr: ips, }) diff --git a/app/router/router_test.go b/app/router/router_test.go index f9041b1d3..afc7bc88f 100644 --- a/app/router/router_test.go +++ b/app/router/router_test.go @@ -2,6 +2,7 @@ package router_test import ( "context" + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "testing" "github.com/golang/mock/gomock" @@ -145,13 +146,13 @@ func TestLeastLoadBalancer(t *testing.T) { func TestIPOnDemand(t *testing.T) { config := &Config{ - DomainStrategy: Config_IpOnDemand, + DomainStrategy: DomainStrategy_IpOnDemand, Rule: []*RoutingRule{ { TargetTag: &RoutingRule_Tag{ Tag: "test", }, - Cidr: []*CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{192, 168, 0, 0}, Prefix: 16, @@ -180,13 +181,13 @@ func TestIPOnDemand(t *testing.T) { func TestIPIfNonMatchDomain(t *testing.T) { config := &Config{ - DomainStrategy: Config_IpIfNonMatch, + DomainStrategy: DomainStrategy_IpIfNonMatch, Rule: []*RoutingRule{ { TargetTag: &RoutingRule_Tag{ Tag: "test", }, - Cidr: []*CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{192, 168, 0, 0}, Prefix: 16, @@ -215,13 +216,13 @@ func TestIPIfNonMatchDomain(t *testing.T) { func TestIPIfNonMatchIP(t *testing.T) { config := &Config{ - DomainStrategy: Config_IpIfNonMatch, + DomainStrategy: DomainStrategy_IpIfNonMatch, Rule: []*RoutingRule{ { TargetTag: &RoutingRule_Tag{ Tag: "test", }, - Cidr: []*CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{127, 0, 0, 0}, Prefix: 8, diff --git a/infra/conf/synthetic/dns/dns_test.go b/infra/conf/synthetic/dns/dns_test.go index 2e62216c4..8616e7dd8 100644 --- a/infra/conf/synthetic/dns/dns_test.go +++ b/infra/conf/synthetic/dns/dns_test.go @@ -112,7 +112,7 @@ func TestDNSConfigParsing(t *testing.T) { }, }, }, - StaticHosts: []*dns.Config_HostMapping{ + StaticHosts: []*dns.HostMapping{ { Type: dns.DomainMatchingType_Subdomain, Domain: "example.com", diff --git a/infra/conf/synthetic/router/router_test.go b/infra/conf/synthetic/router/router_test.go index d717ee090..1c0fd6fd3 100644 --- a/infra/conf/synthetic/router/router_test.go +++ b/infra/conf/synthetic/router/router_test.go @@ -2,6 +2,7 @@ package router_test import ( "encoding/json" + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/testassist" router2 "github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/router" "testing" @@ -108,7 +109,7 @@ func TestRouterConfig(t *testing.T) { }`, Parser: createParser(), Output: &router.Config{ - DomainStrategy: router.Config_AsIs, + DomainStrategy: router.DomainStrategy_AsIs, BalancingRule: []*router.BalancingRule{ { Tag: "b1", @@ -140,13 +141,13 @@ func TestRouterConfig(t *testing.T) { }, Rule: []*router.RoutingRule{ { - Domain: []*router.Domain{ + Domain: []*routercommon.Domain{ { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "baidu.com", }, { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "qq.com", }, }, @@ -155,13 +156,13 @@ func TestRouterConfig(t *testing.T) { }, }, { - Domain: []*router.Domain{ + Domain: []*routercommon.Domain{ { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "v2fly.org", }, { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "github.com", }, }, @@ -170,9 +171,9 @@ func TestRouterConfig(t *testing.T) { }, }, { - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{10, 0, 0, 0}, Prefix: 8, @@ -248,16 +249,16 @@ func TestRouterConfig(t *testing.T) { }`, Parser: createParser(), Output: &router.Config{ - DomainStrategy: router.Config_IpIfNonMatch, + DomainStrategy: router.DomainStrategy_IpIfNonMatch, Rule: []*router.RoutingRule{ { - Domain: []*router.Domain{ + Domain: []*routercommon.Domain{ { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "baidu.com", }, { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "qq.com", }, }, @@ -266,13 +267,13 @@ func TestRouterConfig(t *testing.T) { }, }, { - Domain: []*router.Domain{ + Domain: []*routercommon.Domain{ { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "v2fly.org", }, { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "github.com", }, }, @@ -281,9 +282,9 @@ func TestRouterConfig(t *testing.T) { }, }, { - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{10, 0, 0, 0}, Prefix: 8, @@ -334,16 +335,16 @@ func TestRouterConfig(t *testing.T) { }`, Parser: createParser(), Output: &router.Config{ - DomainStrategy: router.Config_AsIs, + DomainStrategy: router.DomainStrategy_AsIs, Rule: []*router.RoutingRule{ { - Domain: []*router.Domain{ + Domain: []*routercommon.Domain{ { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "baidu.com", }, { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "qq.com", }, }, @@ -352,13 +353,13 @@ func TestRouterConfig(t *testing.T) { }, }, { - Domain: []*router.Domain{ + Domain: []*routercommon.Domain{ { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "v2fly.org", }, { - Type: router.Domain_Plain, + Type: routercommon.Domain_Plain, Value: "github.com", }, }, @@ -367,9 +368,9 @@ func TestRouterConfig(t *testing.T) { }, }, { - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{10, 0, 0, 0}, Prefix: 8, diff --git a/infra/conf/v4/v2ray_test.go b/infra/conf/v4/v2ray_test.go index b516302e6..de96d7e74 100644 --- a/infra/conf/v4/v2ray_test.go +++ b/infra/conf/v4/v2ray_test.go @@ -2,6 +2,7 @@ package v4_test import ( "encoding/json" + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/muxcfg" "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/testassist" "github.com/v2fly/v2ray-core/v4/infra/conf/v4" @@ -158,12 +159,12 @@ func TestV2RayConfig(t *testing.T) { serial.ToTypedMessage(&proxyman.InboundConfig{}), serial.ToTypedMessage(&proxyman.OutboundConfig{}), serial.ToTypedMessage(&router.Config{ - DomainStrategy: router.Config_AsIs, + DomainStrategy: router.DomainStrategy_AsIs, Rule: []*router.RoutingRule{ { - Geoip: []*router.GeoIP{ + Geoip: []*routercommon.GeoIP{ { - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{10, 0, 0, 0}, Prefix: 8, diff --git a/testing/scenarios/dns_test.go b/testing/scenarios/dns_test.go index edb813d7d..b23d0b13e 100644 --- a/testing/scenarios/dns_test.go +++ b/testing/scenarios/dns_test.go @@ -2,6 +2,7 @@ package scenarios import ( "fmt" + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "google.golang.org/protobuf/types/known/anypb" "testing" "time" @@ -38,10 +39,10 @@ func TestResolveIP(t *testing.T) { }, }), serial.ToTypedMessage(&router.Config{ - DomainStrategy: router.Config_IpIfNonMatch, + DomainStrategy: router.DomainStrategy_IpIfNonMatch, Rule: []*router.RoutingRule{ { - Cidr: []*router.CIDR{ + Cidr: []*routercommon.CIDR{ { Ip: []byte{127, 0, 0, 0}, Prefix: 8, diff --git a/testing/scenarios/reverse_test.go b/testing/scenarios/reverse_test.go index da034dce1..856519be8 100644 --- a/testing/scenarios/reverse_test.go +++ b/testing/scenarios/reverse_test.go @@ -1,6 +1,7 @@ package scenarios import ( + "github.com/v2fly/v2ray-core/v4/app/router/routercommon" "google.golang.org/protobuf/types/known/anypb" "testing" "time" @@ -54,8 +55,8 @@ func TestReverseProxy(t *testing.T) { serial.ToTypedMessage(&router.Config{ Rule: []*router.RoutingRule{ { - Domain: []*router.Domain{ - {Type: router.Domain_Full, Value: "test.v2fly.org"}, + Domain: []*routercommon.Domain{ + {Type: routercommon.Domain_Full, Value: "test.v2fly.org"}, }, TargetTag: &router.RoutingRule_Tag{ Tag: "portal", @@ -123,8 +124,8 @@ func TestReverseProxy(t *testing.T) { serial.ToTypedMessage(&router.Config{ Rule: []*router.RoutingRule{ { - Domain: []*router.Domain{ - {Type: router.Domain_Full, Value: "test.v2fly.org"}, + Domain: []*routercommon.Domain{ + {Type: routercommon.Domain_Full, Value: "test.v2fly.org"}, }, TargetTag: &router.RoutingRule_Tag{ Tag: "reverse", @@ -239,8 +240,8 @@ func TestReverseProxyLongRunning(t *testing.T) { serial.ToTypedMessage(&router.Config{ Rule: []*router.RoutingRule{ { - Domain: []*router.Domain{ - {Type: router.Domain_Full, Value: "test.v2fly.org"}, + Domain: []*routercommon.Domain{ + {Type: routercommon.Domain_Full, Value: "test.v2fly.org"}, }, TargetTag: &router.RoutingRule_Tag{ Tag: "portal", @@ -322,8 +323,8 @@ func TestReverseProxyLongRunning(t *testing.T) { serial.ToTypedMessage(&router.Config{ Rule: []*router.RoutingRule{ { - Domain: []*router.Domain{ - {Type: router.Domain_Full, Value: "test.v2fly.org"}, + Domain: []*routercommon.Domain{ + {Type: routercommon.Domain_Full, Value: "test.v2fly.org"}, }, TargetTag: &router.RoutingRule_Tag{ Tag: "reverse",