From 078a92128aba3b0caf3c781d825c1ad17cbaea85 Mon Sep 17 00:00:00 2001 From: comwrg Date: Fri, 2 Nov 2018 16:09:17 +0800 Subject: [PATCH] add more test cases for `proxy/vmess/validator.go` --- proxy/vmess/validator_test.go | 49 ++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/proxy/vmess/validator_test.go b/proxy/vmess/validator_test.go index 0aeed17ac..087014215 100644 --- a/proxy/vmess/validator_test.go +++ b/proxy/vmess/validator_test.go @@ -36,26 +36,45 @@ func TestUserValidator(t *testing.T) { common.Must(v.Add(user)) { - ts := protocol.Timestamp(time.Now().Unix()) - idHash := hasher(id.Bytes()) - idHash.Write(ts.Bytes(nil)) - userHash := idHash.Sum(nil) + 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)) + 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) } { - ts := protocol.Timestamp(time.Now().Add(time.Second * 500).Unix()) - idHash := hasher(id.Bytes()) - idHash.Write(ts.Bytes(nil)) - userHash := idHash.Sum(nil) + 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) + euser, _, found := v.Get(userHash) + assert(found, IsFalse) + assert(euser, IsNil) + } + + testBigLag(121) + testBigLag(-121) + testBigLag(310) + testBigLag(-310) + testBigLag(500) + testBigLag(-500) } assert(v.Remove(user.Email), IsTrue)