github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/platform/platform_test.go (about)

     1  package platform_test
     2  
     3  import (
     4  	"errors"
     5  	"io/fs"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"testing"
    10  
    11  	"github.com/v2fly/v2ray-core/v5/common"
    12  	"github.com/v2fly/v2ray-core/v5/common/platform"
    13  )
    14  
    15  func TestNormalizeEnvName(t *testing.T) {
    16  	cases := []struct {
    17  		input  string
    18  		output string
    19  	}{
    20  		{
    21  			input:  "a",
    22  			output: "A",
    23  		},
    24  		{
    25  			input:  "a.a",
    26  			output: "A_A",
    27  		},
    28  		{
    29  			input:  "A.A.B",
    30  			output: "A_A_B",
    31  		},
    32  	}
    33  	for _, test := range cases {
    34  		if v := platform.NormalizeEnvName(test.input); v != test.output {
    35  			t.Error("unexpected output: ", v, " want ", test.output)
    36  		}
    37  	}
    38  }
    39  
    40  func TestEnvFlag(t *testing.T) {
    41  	if v := (platform.EnvFlag{
    42  		Name: "xxxxx.y",
    43  	}.GetValueAsInt(10)); v != 10 {
    44  		t.Error("env value: ", v)
    45  	}
    46  }
    47  
    48  // TestWrongErrorCheckOnOSStat is a test to detect the misuse of error handling
    49  // in os.Stat, which will lead to failure to find & read geoip & geosite files.
    50  func TestWrongErrorCheckOnOSStat(t *testing.T) {
    51  	theExpectedDir := filepath.Join("usr", "local", "share", "v2ray")
    52  	getAssetLocation := func(file string) string {
    53  		for _, p := range []string{
    54  			filepath.Join(theExpectedDir, file),
    55  		} {
    56  			// errors.Is(fs.ErrNotExist, err) is a mistake supposed Not to
    57  			// be discovered by the Go runtime, which will lead to failure to
    58  			// find & read geoip & geosite files.
    59  			// The correct code is `errors.Is(err, fs.ErrNotExist)`
    60  			if _, err := os.Stat(p); err != nil && errors.Is(fs.ErrNotExist, err) {
    61  				continue
    62  			}
    63  			// asset found
    64  			return p
    65  		}
    66  		return filepath.Join("the", "wrong", "path", "not-exist.txt")
    67  	}
    68  
    69  	notExist := getAssetLocation("not-exist.txt")
    70  	if filepath.Dir(notExist) != theExpectedDir {
    71  		t.Error("asset dir:", notExist, "not in", theExpectedDir)
    72  	}
    73  }
    74  
    75  func TestGetAssetLocation(t *testing.T) {
    76  	exec, err := os.Executable()
    77  	common.Must(err)
    78  	loc := platform.GetAssetLocation("t")
    79  	if filepath.Dir(loc) != filepath.Dir(exec) {
    80  		t.Error("asset dir: ", loc, " not in ", exec)
    81  	}
    82  
    83  	os.Setenv("v2ray.location.asset", "/v2ray")
    84  	if runtime.GOOS == "windows" {
    85  		if v := platform.GetAssetLocation("t"); v != "\\v2ray\\t" {
    86  			t.Error("asset loc: ", v)
    87  		}
    88  	} else {
    89  		if v := platform.GetAssetLocation("t"); v != "/v2ray/t" {
    90  			t.Error("asset loc: ", v)
    91  		}
    92  	}
    93  }