Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package memconservative
 
import (
    "bytes"
    "errors"
    "io/fs"
    "os"
    "path/filepath"
    "testing"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/platform"
    "github.com/v2fly/v2ray-core/v5/common/platform/filesystem"
)
 
func init() {
    const (
        geoipURL   = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
        geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
    )
 
    wd, err := os.Getwd()
    common.Must(err)
 
    tempPath := filepath.Join(wd, "..", "..", "..", "..", "testing", "temp")
    geoipPath := filepath.Join(tempPath, "geoip.dat")
    geositePath := filepath.Join(tempPath, "geosite.dat")
 
    os.Setenv("v2ray.location.asset", tempPath)
 
    if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
        common.Must(os.MkdirAll(tempPath, 0o755))
        geoipBytes, err := common.FetchHTTPContent(geoipURL)
        common.Must(err)
        common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
    }
    if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
        common.Must(os.MkdirAll(tempPath, 0o755))
        geositeBytes, err := common.FetchHTTPContent(geositeURL)
        common.Must(err)
        common.Must(filesystem.WriteFile(geositePath, geositeBytes))
    }
}
 
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}
    if !bytes.Equal(result, expected) {
        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}
    if !bytes.Equal(result, expected) {
        t.Errorf("failed to load geosite:test, expected: %v, got: %v", expected, result)
    }
}