From 5d5beb2028b9848c3c1d208b1a058d6c6db6d866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E3=81=AE=E3=81=8B=E3=81=88=E3=81=A7?= Date: Thu, 8 Apr 2021 13:25:22 +0800 Subject: [PATCH] Add test to ParaseIPList --- common/matcher/geoip/geoip_test.go | 45 ++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/common/matcher/geoip/geoip_test.go b/common/matcher/geoip/geoip_test.go index 0e583ff3..60295782 100644 --- a/common/matcher/geoip/geoip_test.go +++ b/common/matcher/geoip/geoip_test.go @@ -25,6 +25,27 @@ func init() { } } +func TestParaseIPList(t *testing.T) { + t.Log(os.Getenv("xray.location.asset")) + + common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), platform.GetAssetLocation("geoip.dat"))) + + ips := []string{ + "geoip:us", + "geoip:cn", + "geoip:!cn", + "ext:geoiptestrouter.dat:!cn", + "ext:geoiptestrouter.dat:ca", + "ext-ip:geoiptestrouter.dat:!cn", + "ext-ip:geoiptestrouter.dat:!ca", + } + + _, err := ParaseIPList(ips) + if err != nil { + t.Fatalf("Failed to parse geoip list, got %s", err) + } +} + func TestGeoIPMatcherContainer(t *testing.T) { container := &GeoIPMatcherContainer{} @@ -123,18 +144,6 @@ func TestGeoIPMatcher(t *testing.T) { } } -func TestGeoIPMatcher4CN(t *testing.T) { - ips, err := loadGeoIP("CN") - common.Must(err) - - matcher := &GeoIPMatcher{} - common.Must(matcher.Init(ips)) - - if matcher.Match([]byte{8, 8, 8, 8}) { - t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does") - } -} - func TestGeoIPReverseMatcher(t *testing.T) { cidrList := CIDRList{ {Ip: []byte{8, 8, 8, 8}, Prefix: 32}, @@ -171,6 +180,18 @@ func TestGeoIPReverseMatcher(t *testing.T) { } } +func TestGeoIPMatcher4CN(t *testing.T) { + ips, err := loadGeoIP("CN") + common.Must(err) + + matcher := &GeoIPMatcher{} + common.Must(matcher.Init(ips)) + + if matcher.Match([]byte{8, 8, 8, 8}) { + t.Error("expect CN geoip doesn't contain 8.8.8.8, but actually does") + } +} + func TestGeoIPMatcher6US(t *testing.T) { ips, err := loadGeoIP("US") common.Must(err)