github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/geodata/memconservative/decode_test.go (about) 1 package memconservative 2 3 import ( 4 "bytes" 5 "errors" 6 "io/fs" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/v2fly/v2ray-core/v5/common" 12 "github.com/v2fly/v2ray-core/v5/common/platform" 13 "github.com/v2fly/v2ray-core/v5/common/platform/filesystem" 14 ) 15 16 func init() { 17 const ( 18 geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat" 19 geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat" 20 ) 21 22 wd, err := os.Getwd() 23 common.Must(err) 24 25 tempPath := filepath.Join(wd, "..", "..", "..", "..", "testing", "temp") 26 geoipPath := filepath.Join(tempPath, "geoip.dat") 27 geositePath := filepath.Join(tempPath, "geosite.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 if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) { 38 common.Must(os.MkdirAll(tempPath, 0o755)) 39 geositeBytes, err := common.FetchHTTPContent(geositeURL) 40 common.Must(err) 41 common.Must(filesystem.WriteFile(geositePath, geositeBytes)) 42 } 43 } 44 45 func TestDecodeGeoIP(t *testing.T) { 46 filename := platform.GetAssetLocation("geoip.dat") 47 result, err := Decode(filename, "test") 48 if err != nil { 49 t.Error(err) 50 } 51 52 expected := []byte{10, 4, 84, 69, 83, 84, 18, 8, 10, 4, 127, 0, 0, 0, 16, 8} 53 if !bytes.Equal(result, expected) { 54 t.Errorf("failed to load geoip:test, expected: %v, got: %v", expected, result) 55 } 56 } 57 58 func TestDecodeGeoSite(t *testing.T) { 59 filename := platform.GetAssetLocation("geosite.dat") 60 result, err := Decode(filename, "test") 61 if err != nil { 62 t.Error(err) 63 } 64 65 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} 66 if !bytes.Equal(result, expected) { 67 t.Errorf("failed to load geosite:test, expected: %v, got: %v", expected, result) 68 } 69 }