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
70
71
72
73
74
75
76
77
78
79
package protocol_test
 
import (
    "strings"
    "testing"
    "time"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/net"
    . "github.com/v2fly/v2ray-core/v5/common/protocol"
    "github.com/v2fly/v2ray-core/v5/common/uuid"
    "github.com/v2fly/v2ray-core/v5/proxy/vmess"
)
 
func TestAlwaysValidStrategy(t *testing.T) {
    strategy := AlwaysValid()
    if !strategy.IsValid() {
        t.Error("strategy not valid")
    }
    strategy.Invalidate()
    if !strategy.IsValid() {
        t.Error("strategy not valid")
    }
}
 
func TestTimeoutValidStrategy(t *testing.T) {
    strategy := BeforeTime(time.Now().Add(2 * time.Second))
    if !strategy.IsValid() {
        t.Error("strategy not valid")
    }
    time.Sleep(3 * time.Second)
    if strategy.IsValid() {
        t.Error("strategy is valid")
    }
 
    strategy = BeforeTime(time.Now().Add(2 * time.Second))
    strategy.Invalidate()
    if strategy.IsValid() {
        t.Error("strategy is valid")
    }
}
 
func TestUserInServerSpec(t *testing.T) {
    uuid1 := uuid.New()
    uuid2 := uuid.New()
 
    toAccount := func(a *vmess.Account) Account {
        account, err := a.AsAccount()
        common.Must(err)
        return account
    }
 
    spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{
        Email:   "test1@v2fly.org",
        Account: toAccount(&vmess.Account{Id: uuid1.String()}),
    })
    if spec.HasUser(&MemoryUser{
        Email:   "test1@v2fly.org",
        Account: toAccount(&vmess.Account{Id: uuid2.String()}),
    }) {
        t.Error("has user: ", uuid2)
    }
 
    spec.AddUser(&MemoryUser{Email: "test2@v2fly.org"})
    if !spec.HasUser(&MemoryUser{
        Email:   "test1@v2fly.org",
        Account: toAccount(&vmess.Account{Id: uuid1.String()}),
    }) {
        t.Error("not having user: ", uuid1)
    }
}
 
func TestPickUser(t *testing.T) {
    spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{Email: "test1@v2fly.org"}, &MemoryUser{Email: "test2@v2fly.org"}, &MemoryUser{Email: "test3@v2fly.org"})
    user := spec.PickUser()
    if !strings.HasSuffix(user.Email, "@v2fly.org") {
        t.Error("user: ", user.Email)
    }
}