1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/proxy/vmess/validator_test.go

111 lines
2.3 KiB
Go
Raw Normal View History

2018-02-09 05:32:12 -05:00
package vmess_test
import (
"testing"
"time"
2021-02-16 15:31:50 -05: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 05:32:12 -05:00
)
2019-02-20 16:56:17 -05:00
func toAccount(a *Account) protocol.Account {
account, err := a.AsAccount()
common.Must(err)
return account
}
2018-02-09 05:32:12 -05:00
func TestUserValidator(t *testing.T) {
hasher := protocol.DefaultIDHash
v := NewTimedUserValidator(hasher)
defer common.Close(v)
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)
2020-06-06 05:11:30 -04:00
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)
2020-06-06 05:11:30 -04:00
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
}
2019-02-20 16:56:17 -05: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)
}
}