validator_test.go 1.6 KB

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