decode_test.go 2.0 KB

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