github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/rule/rule_test.go (about) 1 package rule_test 2 3 import ( 4 "context" 5 "errors" 6 "io/fs" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/v2fly/v2ray-core/v4/common" 12 "github.com/v2fly/v2ray-core/v4/common/platform" 13 "github.com/v2fly/v2ray-core/v4/common/platform/filesystem" 14 "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon" 15 "github.com/v2fly/v2ray-core/v4/infra/conf/geodata" 16 _ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard" 17 "github.com/v2fly/v2ray-core/v4/infra/conf/rule" 18 ) 19 20 func init() { 21 const geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat" 22 23 wd, err := os.Getwd() 24 common.Must(err) 25 26 tempPath := filepath.Join(wd, "..", "..", "..", "testing", "temp") 27 geoipPath := filepath.Join(tempPath, "geoip.dat") 28 29 os.Setenv("v2ray.location.asset", tempPath) 30 31 if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { 32 common.Must(os.MkdirAll(tempPath, 0o755)) 33 geoipBytes, err := common.FetchHTTPContent(geoipURL) 34 common.Must(err) 35 common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) 36 } 37 } 38 39 func TestToCidrList(t *testing.T) { 40 t.Log(os.Getenv("v2ray.location.asset")) 41 42 common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), platform.GetAssetLocation("geoip.dat"))) 43 44 ips := cfgcommon.StringList([]string{ 45 "geoip:us", 46 "geoip:cn", 47 "geoip:!cn", 48 "ext:geoiptestrouter.dat:!cn", 49 "ext:geoiptestrouter.dat:ca", 50 "ext-ip:geoiptestrouter.dat:!cn", 51 "ext-ip:geoiptestrouter.dat:!ca", 52 }) 53 54 cfgctx := cfgcommon.NewConfigureLoadingContext(context.Background()) 55 56 if loader, err := geodata.GetGeoDataLoader("standard"); err == nil { 57 cfgcommon.SetGeoDataLoader(cfgctx, loader) 58 } else { 59 t.Fatal(err) 60 } 61 62 _, err := rule.ToCidrList(cfgctx, ips) 63 if err != nil { 64 t.Fatalf("Failed to parse geoip list, got %s", err) 65 } 66 }