vmess_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. ts := protocol.Timestamp(time.Now().Unix())
  32. idHash := hasher(id.Bytes())
  33. idHash.Write(ts.Bytes(nil))
  34. userHash := idHash.Sum(nil)
  35. euser, ets, found := v.Get(userHash)
  36. assert(found, IsTrue)
  37. assert(euser.Email, Equals, user.Email)
  38. assert(int64(ets), Equals, int64(ts))
  39. }
  40. {
  41. ts := protocol.Timestamp(time.Now().Add(time.Second * 500).Unix())
  42. idHash := hasher(id.Bytes())
  43. idHash.Write(ts.Bytes(nil))
  44. userHash := idHash.Sum(nil)
  45. euser, _, found := v.Get(userHash)
  46. assert(found, IsFalse)
  47. assert(euser, IsNil)
  48. }
  49. assert(v.Remove(user.Email), IsTrue)
  50. assert(v.Remove(user.Email), IsFalse)
  51. }