validator_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package vmess_test
  2. import (
  3. "testing"
  4. "time"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/uuid"
  8. "v2ray.com/core/common/vio"
  9. . "v2ray.com/core/proxy/vmess"
  10. . "v2ray.com/ext/assert"
  11. )
  12. func TestUserValidator(t *testing.T) {
  13. assert := With(t)
  14. hasher := protocol.DefaultIDHash
  15. v := NewTimedUserValidator(hasher)
  16. defer common.Close(v)
  17. toAccount := func(a *Account) protocol.Account {
  18. account, err := a.AsAccount()
  19. common.Must(err)
  20. return account
  21. }
  22. id := uuid.New()
  23. user := &protocol.MemoryUser{
  24. Email: "test",
  25. Account: toAccount(&Account{
  26. Id: id.String(),
  27. AlterId: 8,
  28. }),
  29. }
  30. common.Must(v.Add(user))
  31. {
  32. testSmallLag := func(lag time.Duration) {
  33. ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix())
  34. idHash := hasher(id.Bytes())
  35. common.Must2(vio.WriteUint64(idHash, uint64(ts)))
  36. userHash := idHash.Sum(nil)
  37. euser, ets, found := v.Get(userHash)
  38. assert(found, IsTrue)
  39. assert(euser.Email, Equals, user.Email)
  40. assert(int64(ets), Equals, int64(ts))
  41. }
  42. testSmallLag(0)
  43. testSmallLag(40)
  44. testSmallLag(-40)
  45. testSmallLag(80)
  46. testSmallLag(-80)
  47. testSmallLag(120)
  48. testSmallLag(-120)
  49. }
  50. {
  51. testBigLag := func(lag time.Duration) {
  52. ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix())
  53. idHash := hasher(id.Bytes())
  54. common.Must2(vio.WriteUint64(idHash, uint64(ts)))
  55. userHash := idHash.Sum(nil)
  56. euser, _, found := v.Get(userHash)
  57. assert(found, IsFalse)
  58. assert(euser, IsNil)
  59. }
  60. testBigLag(121)
  61. testBigLag(-121)
  62. testBigLag(310)
  63. testBigLag(-310)
  64. testBigLag(500)
  65. testBigLag(-500)
  66. }
  67. assert(v.Remove(user.Email), IsTrue)
  68. assert(v.Remove(user.Email), IsFalse)
  69. }