1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-03 12:35:24 +00:00
v2fly/common/platform/platform_test.go

94 lines
2.2 KiB
Go
Raw Normal View History

2017-05-25 23:11:38 +00:00
package platform_test
import (
"errors"
"io/fs"
2017-11-06 11:23:21 +00:00
"os"
"path/filepath"
2018-04-14 03:57:26 +00:00
"runtime"
2017-05-25 23:11:38 +00:00
"testing"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/platform"
2017-05-25 23:11:38 +00:00
)
func TestNormalizeEnvName(t *testing.T) {
cases := []struct {
input string
output string
}{
{
input: "a",
output: "A",
},
{
input: "a.a",
output: "A_A",
},
{
input: "A.A.B",
output: "A_A_B",
},
}
for _, test := range cases {
if v := platform.NormalizeEnvName(test.input); v != test.output {
2019-02-09 14:46:48 +00:00
t.Error("unexpected output: ", v, " want ", test.output)
}
2017-05-25 23:11:38 +00:00
}
}
func TestEnvFlag(t *testing.T) {
if v := (platform.EnvFlag{
2017-05-25 23:11:38 +00:00
Name: "xxxxx.y",
2019-02-10 14:26:43 +00:00
}.GetValueAsInt(10)); v != 10 {
2019-02-09 14:46:48 +00:00
t.Error("env value: ", v)
}
2017-05-25 23:11:38 +00:00
}
2017-11-06 11:23:21 +00:00
// TestWrongErrorCheckOnOSStat is a test to detect the misuse of error handling
// in os.Stat, which will lead to failure to find & read geoip & geosite files.
func TestWrongErrorCheckOnOSStat(t *testing.T) {
theExpectedDir := filepath.Join("usr", "local", "share", "v2ray")
getAssetLocation := func(file string) string {
for _, p := range []string{
filepath.Join(theExpectedDir, file),
} {
// errors.Is(fs.ErrNotExist, err) is a mistake supposed Not to
// be discovered by the Go runtime, which will lead to failure to
// find & read geoip & geosite files.
// The correct code is `errors.Is(err, fs.ErrNotExist)`
if _, err := os.Stat(p); err != nil && errors.Is(fs.ErrNotExist, err) {
continue
}
// asset found
return p
}
return filepath.Join("the", "wrong", "path", "not-exist.txt")
}
notExist := getAssetLocation("not-exist.txt")
if filepath.Dir(notExist) != theExpectedDir {
t.Error("asset dir:", notExist, "not in", theExpectedDir)
}
}
2017-11-06 11:23:21 +00:00
func TestGetAssetLocation(t *testing.T) {
exec, err := os.Executable()
common.Must(err)
loc := platform.GetAssetLocation("t")
2019-02-09 14:46:48 +00:00
if filepath.Dir(loc) != filepath.Dir(exec) {
t.Error("asset dir: ", loc, " not in ", exec)
}
2017-11-06 11:23:21 +00:00
os.Setenv("v2ray.location.asset", "/v2ray")
2018-04-14 03:57:26 +00:00
if runtime.GOOS == "windows" {
if v := platform.GetAssetLocation("t"); v != "\\v2ray\\t" {
2019-02-09 14:46:48 +00:00
t.Error("asset loc: ", v)
}
2018-04-14 03:57:26 +00:00
} else {
if v := platform.GetAssetLocation("t"); v != "/v2ray/t" {
2019-02-09 14:46:48 +00:00
t.Error("asset loc: ", v)
}
2018-04-14 03:57:26 +00:00
}
2017-11-06 11:23:21 +00:00
}