vmess_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package vmess_test
  2. import (
  3. "testing"
  4. "time"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/serial"
  7. "v2ray.com/core/common/uuid"
  8. "v2ray.com/core/common/protocol"
  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. id := uuid.New()
  18. user := &protocol.User{
  19. Email: "test",
  20. Account: serial.ToTypedMessage(&Account{
  21. Id: id.String(),
  22. AlterId: 8,
  23. }),
  24. }
  25. common.Must(v.Add(user))
  26. {
  27. ts := protocol.Timestamp(time.Now().Unix())
  28. idHash := hasher(id.Bytes())
  29. idHash.Write(ts.Bytes(nil))
  30. userHash := idHash.Sum(nil)
  31. euser, ets, found := v.Get(userHash)
  32. assert(found, IsTrue)
  33. assert(euser.Email, Equals, user.Email)
  34. assert(int64(ets), Equals, int64(ts))
  35. }
  36. {
  37. ts := protocol.Timestamp(time.Now().Add(time.Second * 500).Unix())
  38. idHash := hasher(id.Bytes())
  39. idHash.Write(ts.Bytes(nil))
  40. userHash := idHash.Sum(nil)
  41. euser, _, found := v.Get(userHash)
  42. assert(found, IsFalse)
  43. assert(euser, IsNil)
  44. }
  45. assert(v.Remove(user.Email), IsTrue)
  46. assert(v.Remove(user.Email), IsFalse)
  47. }