server_spec_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package protocol_test
  2. import (
  3. "testing"
  4. "time"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. . "github.com/v2ray/v2ray-core/common/protocol"
  7. "github.com/v2ray/v2ray-core/testing/assert"
  8. )
  9. type TestAccount struct {
  10. id int
  11. }
  12. func (this *TestAccount) Equals(account Account) bool {
  13. return account.(*TestAccount).id == this.id
  14. }
  15. func TestServerSpecUser(t *testing.T) {
  16. assert := assert.On(t)
  17. account := &TestAccount{
  18. id: 0,
  19. }
  20. user := NewUser(UserLevel(0), "")
  21. user.Account = account
  22. rec := NewServerSpec(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), AlwaysValid(), user)
  23. assert.Bool(rec.HasUser(user)).IsTrue()
  24. account2 := &TestAccount{
  25. id: 1,
  26. }
  27. user2 := NewUser(UserLevel(0), "")
  28. user2.Account = account2
  29. assert.Bool(rec.HasUser(user2)).IsFalse()
  30. rec.AddUser(user2)
  31. assert.Bool(rec.HasUser(user2)).IsTrue()
  32. }
  33. func TestAlwaysValidStrategy(t *testing.T) {
  34. assert := assert.On(t)
  35. strategy := AlwaysValid()
  36. assert.Bool(strategy.IsValid()).IsTrue()
  37. strategy.Invalidate()
  38. assert.Bool(strategy.IsValid()).IsTrue()
  39. }
  40. func TestTimeoutValidStrategy(t *testing.T) {
  41. assert := assert.On(t)
  42. strategy := BeforeTime(time.Now().Add(2 * time.Second))
  43. assert.Bool(strategy.IsValid()).IsTrue()
  44. time.Sleep(3 * time.Second)
  45. assert.Bool(strategy.IsValid()).IsFalse()
  46. strategy = BeforeTime(time.Now().Add(2 * time.Second))
  47. strategy.Invalidate()
  48. assert.Bool(strategy.IsValid()).IsFalse()
  49. }