2018-02-09 05:32:12 -05:00
|
|
|
package vmess_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/uuid"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
. "v2ray.com/core/proxy/vmess"
|
|
|
|
. "v2ray.com/ext/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserValidator(t *testing.T) {
|
|
|
|
assert := With(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))
|
|
|
|
|
|
|
|
{
|
2018-11-02 04:09:17 -04:00
|
|
|
testSmallLag := func(lag time.Duration) {
|
|
|
|
ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix())
|
|
|
|
idHash := hasher(id.Bytes())
|
|
|
|
idHash.Write(ts.Bytes(nil))
|
|
|
|
userHash := idHash.Sum(nil)
|
|
|
|
|
|
|
|
euser, ets, found := v.Get(userHash)
|
|
|
|
assert(found, IsTrue)
|
|
|
|
assert(euser.Email, Equals, user.Email)
|
|
|
|
assert(int64(ets), Equals, int64(ts))
|
|
|
|
}
|
|
|
|
|
|
|
|
testSmallLag(0)
|
|
|
|
testSmallLag(40)
|
|
|
|
testSmallLag(-40)
|
|
|
|
testSmallLag(80)
|
|
|
|
testSmallLag(-80)
|
|
|
|
testSmallLag(120)
|
|
|
|
testSmallLag(-120)
|
2018-02-09 05:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-11-02 04:09:17 -04:00
|
|
|
testBigLag := func(lag time.Duration) {
|
|
|
|
ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix())
|
|
|
|
idHash := hasher(id.Bytes())
|
|
|
|
idHash.Write(ts.Bytes(nil))
|
|
|
|
userHash := idHash.Sum(nil)
|
|
|
|
|
|
|
|
euser, _, found := v.Get(userHash)
|
|
|
|
assert(found, IsFalse)
|
|
|
|
assert(euser, IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
testBigLag(121)
|
|
|
|
testBigLag(-121)
|
|
|
|
testBigLag(310)
|
|
|
|
testBigLag(-310)
|
|
|
|
testBigLag(500)
|
|
|
|
testBigLag(-500)
|
2018-02-09 05:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(v.Remove(user.Email), IsTrue)
|
|
|
|
assert(v.Remove(user.Email), IsFalse)
|
|
|
|
}
|