1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 18:45:23 +00:00
v2fly/proxy/vmess/validator_test.go

111 lines
2.3 KiB
Go
Raw Normal View History

2018-02-09 10:32:12 +00:00
package vmess_test
import (
"testing"
"time"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/common/uuid"
. "github.com/v2fly/v2ray-core/v4/proxy/vmess"
2018-02-09 10:32:12 +00:00
)
2019-02-20 21:56:17 +00:00
func toAccount(a *Account) protocol.Account {
account, err := a.AsAccount()
common.Must(err)
return account
}
2018-02-09 10:32:12 +00:00
func TestUserValidator(t *testing.T) {
hasher := protocol.DefaultIDHash
v := NewTimedUserValidator(hasher)
defer common.Close(v)
id := uuid.New()
2018-08-26 22:33:27 +00:00
user := &protocol.MemoryUser{
2018-02-09 10:32:12 +00:00
Email: "test",
2018-08-26 22:33:27 +00:00
Account: toAccount(&Account{
2018-02-09 10:32:12 +00: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 12:03:02 +00:00
common.Must2(serial.WriteUint64(idHash, uint64(ts)))
userHash := idHash.Sum(nil)
2020-06-06 09:11:30 +00:00
euser, ets, found, _ := v.Get(userHash)
2019-02-02 21:19:30 +00: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 10:32:12 +00:00
}
{
testBigLag := func(lag time.Duration) {
ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix())
idHash := hasher(id.Bytes())
2018-11-03 12:03:02 +00:00
common.Must2(serial.WriteUint64(idHash, uint64(ts)))
userHash := idHash.Sum(nil)
2020-06-06 09:11:30 +00:00
euser, _, found, _ := v.Get(userHash)
2019-02-02 21:19:30 +00:00
if found || euser != nil {
t.Error("unexpected user")
}
}
testBigLag(121)
testBigLag(-121)
testBigLag(310)
testBigLag(-310)
testBigLag(500)
testBigLag(-500)
2018-02-09 10:32:12 +00:00
}
2019-02-02 21:19:30 +00: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 10:32:12 +00:00
}
2019-02-20 21:56:17 +00:00
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)
}
}