platform_test.go 3.1 KB

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