From ace2c44c2b4ca9422d85e39cf995bf547978ca6d Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Tue, 4 May 2021 19:07:35 +0100 Subject: [PATCH] Revert "Refine geodata related tests (#967)" This reverts commit 7805ca2a --- app/router/condition_geoip_test.go | 16 +++++++------ app/router/condition_test.go | 31 ++++++++++++++----------- common/geodata/decode_test.go | 36 +++++++++++++++++------------- infra/conf/dns_test.go | 31 ++++++++++++++----------- infra/conf/router_test.go | 15 ++++++++----- 5 files changed, 75 insertions(+), 54 deletions(-) diff --git a/app/router/condition_geoip_test.go b/app/router/condition_geoip_test.go index bed5c7bb0..2a1259897 100644 --- a/app/router/condition_geoip_test.go +++ b/app/router/condition_geoip_test.go @@ -20,15 +20,17 @@ func init() { common.Must(err) tempPath := filepath.Join(wd, "..", "..", "testing", "temp") + geoipPath := filepath.Join(tempPath, "geoip.dat") + os.Setenv("v2ray.location.asset", tempPath) - geoipPath := platform.GetAssetLocation("geoip.dat") - - if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geoipBytes, err := common.FetchHTTPContent(geoipURL) - common.Must(err) - common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { + common.Must(os.MkdirAll(tempPath, 0755)) + geoipBytes, err := common.FetchHTTPContent(geoipURL) + common.Must(err) + common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + } } } diff --git a/app/router/condition_test.go b/app/router/condition_test.go index 05e891308..7cee49d7a 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -26,22 +26,27 @@ func init() { common.Must(err) tempPath := filepath.Join(wd, "..", "..", "testing", "temp") + geoipPath := filepath.Join(tempPath, "geoip.dat") + geositePath := filepath.Join(tempPath, "geosite.dat") + os.Setenv("v2ray.location.asset", tempPath) - geoipPath := platform.GetAssetLocation("geoip.dat") - geositePath := platform.GetAssetLocation("geosite.dat") - - if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geoipBytes, err := common.FetchHTTPContent(geoipURL) - common.Must(err) - common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { + common.Must(os.MkdirAll(tempPath, 0755)) + geoipBytes, err := common.FetchHTTPContent(geoipURL) + common.Must(err) + common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + } } - if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geositeBytes, err := common.FetchHTTPContent(geositeURL) - common.Must(err) - common.Must(filesystem.WriteFile(geositePath, geositeBytes)) + + if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { + common.Must(os.MkdirAll(tempPath, 0755)) + geositeBytes, err := common.FetchHTTPContent(geositeURL) + common.Must(err) + common.Must(filesystem.WriteFile(geositePath, geositeBytes)) + } } } diff --git a/common/geodata/decode_test.go b/common/geodata/decode_test.go index 00c6af88d..8c77d488e 100644 --- a/common/geodata/decode_test.go +++ b/common/geodata/decode_test.go @@ -1,13 +1,14 @@ package geodata_test import ( - "bytes" "errors" "io/fs" "os" "path/filepath" "testing" + "github.com/google/go-cmp/cmp" + "github.com/v2fly/v2ray-core/v4/common" "github.com/v2fly/v2ray-core/v4/common/geodata" "github.com/v2fly/v2ray-core/v4/common/platform" @@ -24,22 +25,27 @@ func init() { common.Must(err) tempPath := filepath.Join(wd, "..", "..", "testing", "temp") + geoipPath := filepath.Join(tempPath, "geoip.dat") + geositePath := filepath.Join(tempPath, "geosite.dat") + os.Setenv("v2ray.location.asset", tempPath) - geoipPath := platform.GetAssetLocation("geoip.dat") - geositePath := platform.GetAssetLocation("geosite.dat") + common.Must(os.MkdirAll(tempPath, 0755)) - if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geoipBytes, err := common.FetchHTTPContent(geoipURL) - common.Must(err) - common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { + geoipBytes, err := common.FetchHTTPContent(geoipURL) + common.Must(err) + common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + } } - if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geositeBytes, err := common.FetchHTTPContent(geositeURL) - common.Must(err) - common.Must(filesystem.WriteFile(geositePath, geositeBytes)) + + if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { + geositeBytes, err := common.FetchHTTPContent(geositeURL) + common.Must(err) + common.Must(filesystem.WriteFile(geositePath, geositeBytes)) + } } } @@ -51,7 +57,7 @@ func TestDecodeGeoIP(t *testing.T) { } expected := []byte{10, 4, 84, 69, 83, 84, 18, 8, 10, 4, 127, 0, 0, 0, 16, 8} - if !bytes.Equal(result, expected) { + if cmp.Diff(result, expected) != "" { t.Errorf("failed to load geoip:test, expected: %v, got: %v", expected, result) } } @@ -64,7 +70,7 @@ func TestDecodeGeoSite(t *testing.T) { } expected := []byte{10, 4, 84, 69, 83, 84, 18, 20, 8, 3, 18, 16, 116, 101, 115, 116, 46, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109} - if !bytes.Equal(result, expected) { + if cmp.Diff(result, expected) != "" { t.Errorf("failed to load geosite:test, expected: %v, got: %v", expected, result) } } diff --git a/infra/conf/dns_test.go b/infra/conf/dns_test.go index 9b6a49176..fdddbb4fa 100644 --- a/infra/conf/dns_test.go +++ b/infra/conf/dns_test.go @@ -23,22 +23,27 @@ func init() { common.Must(err) tempPath := filepath.Join(wd, "..", "..", "testing", "temp") + geoipPath := filepath.Join(tempPath, "geoip.dat") + geositePath := filepath.Join(tempPath, "geosite.dat") + os.Setenv("v2ray.location.asset", tempPath) - geoipPath := platform.GetAssetLocation("geoip.dat") - geositePath := platform.GetAssetLocation("geosite.dat") - - if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geoipBytes, err := common.FetchHTTPContent(geoipURL) - common.Must(err) - common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { + common.Must(os.MkdirAll(tempPath, 0755)) + geoipBytes, err := common.FetchHTTPContent(geoipURL) + common.Must(err) + common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + } } - if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geositeBytes, err := common.FetchHTTPContent(geositeURL) - common.Must(err) - common.Must(filesystem.WriteFile(geositePath, geositeBytes)) + + if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { + common.Must(os.MkdirAll(tempPath, 0755)) + geositeBytes, err := common.FetchHTTPContent(geositeURL) + common.Must(err) + common.Must(filesystem.WriteFile(geositePath, geositeBytes)) + } } } diff --git a/infra/conf/router_test.go b/infra/conf/router_test.go index 759680363..2bc55472c 100644 --- a/infra/conf/router_test.go +++ b/infra/conf/router_test.go @@ -24,15 +24,18 @@ func init() { common.Must(err) tempPath := filepath.Join(wd, "..", "..", "testing", "temp") + geoipPath := filepath.Join(tempPath, "geoip.dat") + os.Setenv("v2ray.location.asset", tempPath) - geoipPath := platform.GetAssetLocation("geoip.dat") + common.Must(os.MkdirAll(tempPath, 0755)) - if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { - common.Must(os.MkdirAll(tempPath, 0755)) - geoipBytes, err := common.FetchHTTPContent(geoipURL) - common.Must(err) - common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) { + geoipBytes, err := common.FetchHTTPContent(geoipURL) + common.Must(err) + common.Must(filesystem.WriteFile(geoipPath, geoipBytes)) + } } }