server_spec_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package protocol_test
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/net"
  8. . "github.com/xtls/xray-core/common/protocol"
  9. "github.com/xtls/xray-core/common/uuid"
  10. "github.com/xtls/xray-core/proxy/vmess"
  11. )
  12. func TestAlwaysValidStrategy(t *testing.T) {
  13. strategy := AlwaysValid()
  14. if !strategy.IsValid() {
  15. t.Error("strategy not valid")
  16. }
  17. strategy.Invalidate()
  18. if !strategy.IsValid() {
  19. t.Error("strategy not valid")
  20. }
  21. }
  22. func TestTimeoutValidStrategy(t *testing.T) {
  23. strategy := BeforeTime(time.Now().Add(2 * time.Second))
  24. if !strategy.IsValid() {
  25. t.Error("strategy not valid")
  26. }
  27. time.Sleep(3 * time.Second)
  28. if strategy.IsValid() {
  29. t.Error("strategy is valid")
  30. }
  31. strategy = BeforeTime(time.Now().Add(2 * time.Second))
  32. strategy.Invalidate()
  33. if strategy.IsValid() {
  34. t.Error("strategy is valid")
  35. }
  36. }
  37. func TestUserInServerSpec(t *testing.T) {
  38. uuid1 := uuid.New()
  39. uuid2 := uuid.New()
  40. toAccount := func(a *vmess.Account) Account {
  41. account, err := a.AsAccount()
  42. common.Must(err)
  43. return account
  44. }
  45. spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{
  46. Email: "[email protected]",
  47. Account: toAccount(&vmess.Account{Id: uuid1.String()}),
  48. })
  49. if spec.HasUser(&MemoryUser{
  50. Email: "[email protected]",
  51. Account: toAccount(&vmess.Account{Id: uuid2.String()}),
  52. }) {
  53. t.Error("has user: ", uuid2)
  54. }
  55. spec.AddUser(&MemoryUser{Email: "[email protected]"})
  56. if !spec.HasUser(&MemoryUser{
  57. Email: "[email protected]",
  58. Account: toAccount(&vmess.Account{Id: uuid1.String()}),
  59. }) {
  60. t.Error("not having user: ", uuid1)
  61. }
  62. }
  63. func TestPickUser(t *testing.T) {
  64. spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{Email: "[email protected]"}, &MemoryUser{Email: "[email protected]"}, &MemoryUser{Email: "[email protected]"})
  65. user := spec.PickUser()
  66. if !strings.HasSuffix(user.Email, "@example.com") {
  67. t.Error("user: ", user.Email)
  68. }
  69. }