platform_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package platform_test
  2. import (
  3. "errors"
  4. "io/fs"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "testing"
  9. "github.com/v2fly/v2ray-core/v4/common"
  10. "github.com/v2fly/v2ray-core/v4/common/platform"
  11. "github.com/v2fly/v2ray-core/v4/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. if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
  23. common.Must(os.MkdirAll(tempPath, 0755))
  24. geoipBytes, err := common.FetchHTTPContent(geoipURL)
  25. common.Must(err)
  26. common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
  27. }
  28. }
  29. func TestNormalizeEnvName(t *testing.T) {
  30. cases := []struct {
  31. input string
  32. output string
  33. }{
  34. {
  35. input: "a",
  36. output: "A",
  37. },
  38. {
  39. input: "a.a",
  40. output: "A_A",
  41. },
  42. {
  43. input: "A.A.B",
  44. output: "A_A_B",
  45. },
  46. }
  47. for _, test := range cases {
  48. if v := platform.NormalizeEnvName(test.input); v != test.output {
  49. t.Error("unexpected output: ", v, " want ", test.output)
  50. }
  51. }
  52. }
  53. func TestEnvFlag(t *testing.T) {
  54. if v := (platform.EnvFlag{
  55. Name: "xxxxx.y",
  56. }.GetValueAsInt(10)); v != 10 {
  57. t.Error("env value: ", v)
  58. }
  59. }
  60. // TestWrongErrorCheckOnOSStat is a test to detect the misuse of error handling
  61. // in os.Stat, which will lead to failure to find & read geoip & geosite files.
  62. func TestWrongErrorCheckOnOSStat(t *testing.T) {
  63. theExpectedDir := filepath.Join("usr", "local", "share", "v2ray")
  64. getAssetLocation := func(file string) string {
  65. for _, p := range []string{
  66. filepath.Join(theExpectedDir, file),
  67. } {
  68. // errors.Is(fs.ErrNotExist, err) is a mistake supposed Not to
  69. // be discovered by the Go runtime, which will lead to failure to
  70. // find & read geoip & geosite files.
  71. // The correct code is `errors.Is(err, fs.ErrNotExist)`
  72. if _, err := os.Stat(p); err != nil && errors.Is(fs.ErrNotExist, err) {
  73. continue
  74. }
  75. // asset found
  76. return p
  77. }
  78. return filepath.Join("the", "wrong", "path", "not-exist.txt")
  79. }
  80. notExist := getAssetLocation("not-exist.txt")
  81. if filepath.Dir(notExist) != theExpectedDir {
  82. t.Error("asset dir:", notExist, "not in", theExpectedDir)
  83. }
  84. }
  85. func TestGetAssetLocation(t *testing.T) {
  86. // Test for external geo files
  87. wd, err := os.Getwd()
  88. common.Must(err)
  89. tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
  90. geoipPath := filepath.Join(tempPath, "geoip.dat")
  91. asset := platform.GetAssetLocation(geoipPath)
  92. if _, err := os.Stat(asset); err != nil && errors.Is(err, fs.ErrNotExist) {
  93. t.Error("cannot find external geo file:", asset)
  94. }
  95. exec, err := os.Executable()
  96. common.Must(err)
  97. loc := platform.GetAssetLocation("t")
  98. if filepath.Dir(loc) != filepath.Dir(exec) {
  99. t.Error("asset dir: ", loc, " not in ", exec)
  100. }
  101. os.Setenv("v2ray.location.asset", "/v2ray")
  102. if runtime.GOOS == "windows" {
  103. if v := platform.GetAssetLocation("t"); v != "\\v2ray\\t" {
  104. t.Error("asset loc: ", v)
  105. }
  106. } else {
  107. if v := platform.GetAssetLocation("t"); v != "/v2ray/t" {
  108. t.Error("asset loc: ", v)
  109. }
  110. }
  111. }