Skip to content

Commit

Permalink
Chore: Refactor infra/conf.TestToCidrList() (#4017)
Browse files Browse the repository at this point in the history
  • Loading branch information
zonescape authored Nov 14, 2024
1 parent 17825b2 commit ec1fd00
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
20 changes: 0 additions & 20 deletions infra/conf/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,15 @@ package conf_test

import (
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/xtls/xray-core/app/dns"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/platform/filesystem"
. "github.com/xtls/xray-core/infra/conf"
"google.golang.org/protobuf/proto"
)

func init() {
wd, err := os.Getwd()
common.Must(err)

if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat")))
}

os.Setenv("xray.location.asset", wd)
}

func TestDNSConfigParsing(t *testing.T) {
defer func() {
os.Unsetenv("xray.location.asset")
}()

parserCreator := func() func(string) (proto.Message, error) {
return func(s string) (proto.Message, error) {
config := new(DNSConfig)
Expand Down
44 changes: 34 additions & 10 deletions infra/conf/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conf_test

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -18,21 +19,44 @@ import (
"google.golang.org/protobuf/proto"
)

func init() {
wd, err := os.Getwd()
common.Must(err)

if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat")))
func getAssetPath(file string) (string, error) {
path := platform.GetAssetLocation(file)
_, err := os.Stat(path)
if os.IsNotExist(err) {
path := filepath.Join("..", "..", "resources", file)
_, err := os.Stat(path)
if os.IsNotExist(err) {
return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file)
}
if err != nil {
return "", fmt.Errorf("can't stat %s: %v", path, err)
}
return path, nil
}
if err != nil {
return "", fmt.Errorf("can't stat %s: %v", path, err)
}

os.Setenv("xray.location.asset", wd)
return path, nil
}

func TestToCidrList(t *testing.T) {
t.Log(os.Getenv("xray.location.asset"))
tempDir, err := os.MkdirTemp("", "test-")
if err != nil {
t.Fatalf("can't create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)

geoipPath, err := getAssetPath("geoip.dat")
if err != nil {
t.Fatal(err)
}

common.Must(filesystem.CopyFile(filepath.Join(tempDir, "geoip.dat"), geoipPath))
common.Must(filesystem.CopyFile(filepath.Join(tempDir, "geoiptestrouter.dat"), geoipPath))

common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), "geoip.dat"))
os.Setenv("xray.location.asset", tempDir)
defer os.Unsetenv("xray.location.asset")

ips := StringList([]string{
"geoip:us",
Expand All @@ -44,7 +68,7 @@ func TestToCidrList(t *testing.T) {
"ext-ip:geoiptestrouter.dat:!ca",
})

_, err := ToCidrList(ips)
_, err = ToCidrList(ips)
if err != nil {
t.Fatalf("Failed to parse geoip list, got %s", err)
}
Expand Down

0 comments on commit ec1fd00

Please sign in to comment.