Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package vmess_test
 
import (
    "testing"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/protocol"
    "github.com/v2fly/v2ray-core/v5/common/serial"
    "github.com/v2fly/v2ray-core/v5/common/uuid"
    . "github.com/v2fly/v2ray-core/v5/proxy/vmess"
)
 
func toAccount(a *Account) protocol.Account {
    account, err := a.AsAccount()
    common.Must(err)
    return account
}
 
func TestUserValidator(t *testing.T) {
    hasher := protocol.DefaultIDHash
    v := NewTimedUserValidator(hasher)
    defer common.Close(v)
 
    id := uuid.New()
    user := &protocol.MemoryUser{
        Email: "test",
        Account: toAccount(&Account{
            Id:      id.String(),
            AlterId: 8,
        }),
    }
    common.Must(v.Add(user))
 
    {
        testSmallLag := func(lag int64) {
            ts := int64(v.GetBaseTime()) + lag + 240
            idHash := hasher(id.Bytes())
            common.Must2(serial.WriteUint64(idHash, uint64(ts)))
            userHash := idHash.Sum(nil)
 
            euser, ets, found, _ := v.Get(userHash)
            if !found {
                t.Fatal("user not found")
            }
            if euser.Email != user.Email {
                t.Error("unexpected user email: ", euser.Email, " want ", user.Email)
            }
            if int64(ets) != ts {
                t.Error("unexpected timestamp: ", ets, " want ", ts)
            }
        }
 
        testSmallLag(0)
        testSmallLag(40)
        testSmallLag(-40)
        testSmallLag(80)
        testSmallLag(-80)
        testSmallLag(120)
        testSmallLag(-120)
    }
 
    {
        testBigLag := func(lag int64) {
            ts := int64(v.GetBaseTime()) + lag + 240
            idHash := hasher(id.Bytes())
            common.Must2(serial.WriteUint64(idHash, uint64(ts)))
            userHash := idHash.Sum(nil)
 
            euser, _, found, _ := v.Get(userHash)
            if found || euser != nil {
                t.Error("unexpected user")
            }
        }
 
        testBigLag(121)
        testBigLag(-121)
        testBigLag(310)
        testBigLag(-310)
        testBigLag(500)
        testBigLag(-500)
    }
 
    if v := v.Remove(user.Email); !v {
        t.Error("unable to remove user")
    }
    if v := v.Remove(user.Email); v {
        t.Error("remove user twice")
    }
}
 
func BenchmarkUserValidator(b *testing.B) {
    for i := 0; i < b.N; i++ {
        hasher := protocol.DefaultIDHash
        v := NewTimedUserValidator(hasher)
 
        for j := 0; j < 1500; j++ {
            id := uuid.New()
            v.Add(&protocol.MemoryUser{
                Email: "test",
                Account: toAccount(&Account{
                    Id:      id.String(),
                    AlterId: 16,
                }),
            })
        }
 
        common.Close(v)
    }
}