Skip to content

Commit

Permalink
update test for router refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokangwang committed Sep 7, 2021
1 parent 3c56a7f commit 98ee44b
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 88 deletions.
36 changes: 18 additions & 18 deletions app/dns/dns_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -430,7 +430,7 @@ func TestStaticHostDomain(t *testing.T) {
Port: uint32(port),
},
},
StaticHosts: []*Config_HostMapping{
StaticHosts: []*HostMapping{
{
Type: DomainMatchingType_Full,
Domain: "example.com",
Expand Down Expand Up @@ -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},
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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},
Expand All @@ -649,7 +649,7 @@ func TestLocalDomain(t *testing.T) {
},
},
},
StaticHosts: []*Config_HostMapping{
StaticHosts: []*HostMapping{
{
Type: DomainMatchingType_Full,
Domain: "hostnamestatic",
Expand Down Expand Up @@ -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},
},
Expand All @@ -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},
},
},
Expand All @@ -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},
},
},
Expand All @@ -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},
},
},
Expand Down
2 changes: 1 addition & 1 deletion app/dns/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestStaticHosts(t *testing.T) {
pb := []*Config_HostMapping{
pb := []*HostMapping{
{
Type: DomainMatchingType_Full,
Domain: "v2fly.org",
Expand Down
5 changes: 3 additions & 2 deletions app/router/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command_test

import (
"context"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"testing"
"time"

Expand Down Expand Up @@ -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"},
},
{
Expand Down
11 changes: 6 additions & 5 deletions app/router/condition_geoip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router_test

import (
"errors"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 16 additions & 15 deletions app/router/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router_test

import (
"errors"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
})
Expand All @@ -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,
})
Expand All @@ -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,
})
Expand All @@ -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,
})
Expand Down
13 changes: 7 additions & 6 deletions app/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router_test

import (
"context"
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion infra/conf/synthetic/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestDNSConfigParsing(t *testing.T) {
},
},
},
StaticHosts: []*dns.Config_HostMapping{
StaticHosts: []*dns.HostMapping{
{
Type: dns.DomainMatchingType_Subdomain,
Domain: "example.com",
Expand Down
Loading

0 comments on commit 98ee44b

Please sign in to comment.