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