2021-05-04 06:19:03 -04:00
|
|
|
package memconservative
|
|
|
|
|
|
|
|
import (
|
2021-05-04 14:13:52 -04:00
|
|
|
"bytes"
|
2021-05-04 06:19:03 -04:00
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2021-05-04 06:21:49 -04:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
|
2021-05-04 06:19:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-05-04 09:51:38 -04:00
|
|
|
const (
|
|
|
|
geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
|
|
|
|
geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
|
|
|
|
)
|
|
|
|
|
2021-05-04 06:19:03 -04:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-05-04 09:51:38 -04:00
|
|
|
tempPath := filepath.Join(wd, "..", "..", "..", "..", "testing", "temp")
|
|
|
|
geoipPath := filepath.Join(tempPath, "geoip.dat")
|
|
|
|
geositePath := filepath.Join(tempPath, "geosite.dat")
|
2021-05-04 06:19:03 -04:00
|
|
|
|
2021-05-04 09:51:38 -04:00
|
|
|
os.Setenv("v2ray.location.asset", tempPath)
|
2021-05-04 06:19:03 -04:00
|
|
|
|
2021-05-04 14:13:52 -04:00
|
|
|
if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
|
|
|
common.Must(os.MkdirAll(tempPath, 0755))
|
|
|
|
geoipBytes, err := common.FetchHTTPContent(geoipURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
|
2021-05-04 06:19:03 -04:00
|
|
|
}
|
2021-05-04 14:13:52 -04:00
|
|
|
if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
|
|
|
common.Must(os.MkdirAll(tempPath, 0755))
|
|
|
|
geositeBytes, err := common.FetchHTTPContent(geositeURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geositePath, geositeBytes))
|
2021-05-04 06:19:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeGeoIP(t *testing.T) {
|
|
|
|
filename := platform.GetAssetLocation("geoip.dat")
|
|
|
|
result, err := Decode(filename, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []byte{10, 4, 84, 69, 83, 84, 18, 8, 10, 4, 127, 0, 0, 0, 16, 8}
|
2021-05-04 14:13:52 -04:00
|
|
|
if !bytes.Equal(result, expected) {
|
2021-05-04 06:19:03 -04:00
|
|
|
t.Errorf("failed to load geoip:test, expected: %v, got: %v", expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeGeoSite(t *testing.T) {
|
|
|
|
filename := platform.GetAssetLocation("geosite.dat")
|
|
|
|
result, err := Decode(filename, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []byte{10, 4, 84, 69, 83, 84, 18, 20, 8, 3, 18, 16, 116, 101, 115, 116, 46, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109}
|
2021-05-04 14:13:52 -04:00
|
|
|
if !bytes.Equal(result, expected) {
|
2021-05-04 06:19:03 -04:00
|
|
|
t.Errorf("failed to load geosite:test, expected: %v, got: %v", expected, result)
|
|
|
|
}
|
|
|
|
}
|