1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-06 18:26:47 -05:00
v2fly/proxy/vmess/validator_test.go

91 lines
1.9 KiB
Go
Raw Normal View History

2018-02-09 05:32:12 -05:00
package vmess_test
import (
"testing"
"time"
"v2ray.com/core/common"
"v2ray.com/core/common/protocol"
2018-11-03 08:03:02 -04:00
"v2ray.com/core/common/serial"
2018-11-02 13:20:02 -04:00
"v2ray.com/core/common/uuid"
2018-02-09 05:32:12 -05:00
. "v2ray.com/core/proxy/vmess"
)
func TestUserValidator(t *testing.T) {
hasher := protocol.DefaultIDHash
v := NewTimedUserValidator(hasher)
defer common.Close(v)
2018-08-26 18:33:27 -04:00
toAccount := func(a *Account) protocol.Account {
account, err := a.AsAccount()
common.Must(err)
return account
}
2018-02-09 05:32:12 -05:00
id := uuid.New()
2018-08-26 18:33:27 -04:00
user := &protocol.MemoryUser{
2018-02-09 05:32:12 -05:00
Email: "test",
2018-08-26 18:33:27 -04:00
Account: toAccount(&Account{
2018-02-09 05:32:12 -05:00
Id: id.String(),
AlterId: 8,
}),
}
common.Must(v.Add(user))
{
testSmallLag := func(lag time.Duration) {
ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix())
idHash := hasher(id.Bytes())
2018-11-03 08:03:02 -04:00
common.Must2(serial.WriteUint64(idHash, uint64(ts)))
userHash := idHash.Sum(nil)
euser, ets, found := v.Get(userHash)
2019-02-02 16:19:30 -05:00
if !found {
t.Fatal("user not found")
}
if euser.Email != user.Email {
t.Error("unexpected user email: ", euser.Email, " want ", user.Email)
}
if ets != ts {
t.Error("unexpected timestamp: ", ets, " want ", ts)
}
}
testSmallLag(0)
testSmallLag(40)
testSmallLag(-40)
testSmallLag(80)
testSmallLag(-80)
testSmallLag(120)
testSmallLag(-120)
2018-02-09 05:32:12 -05:00
}
{
testBigLag := func(lag time.Duration) {
ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix())
idHash := hasher(id.Bytes())
2018-11-03 08:03:02 -04:00
common.Must2(serial.WriteUint64(idHash, uint64(ts)))
userHash := idHash.Sum(nil)
euser, _, found := v.Get(userHash)
2019-02-02 16:19:30 -05:00
if found || euser != nil {
t.Error("unexpected user")
}
}
testBigLag(121)
testBigLag(-121)
testBigLag(310)
testBigLag(-310)
testBigLag(500)
testBigLag(-500)
2018-02-09 05:32:12 -05:00
}
2019-02-02 16:19:30 -05:00
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")
}
2018-02-09 05:32:12 -05:00
}