1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 19:45:24 +00:00
v2fly/common/protocol/server_spec_test.go

80 lines
1.9 KiB
Go
Raw Normal View History

2016-07-25 14:48:09 +00:00
package protocol_test
import (
2019-02-10 14:02:28 +00:00
"strings"
2016-07-25 14:48:09 +00:00
"testing"
2016-07-25 19:31:16 +00:00
"time"
2016-07-25 14:48:09 +00:00
2018-08-26 22:33:27 +00:00
"v2ray.com/core/common"
2017-12-17 23:07:50 +00:00
"v2ray.com/core/common/net"
2016-08-20 18:55:45 +00:00
. "v2ray.com/core/common/protocol"
2017-12-17 23:07:50 +00:00
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/vmess"
2016-07-25 14:48:09 +00:00
)
2016-07-25 19:31:16 +00:00
func TestAlwaysValidStrategy(t *testing.T) {
strategy := AlwaysValid()
2019-02-10 14:02:28 +00:00
if !strategy.IsValid() {
t.Error("strategy not valid")
}
2016-07-25 19:31:16 +00:00
strategy.Invalidate()
2019-02-10 14:02:28 +00:00
if !strategy.IsValid() {
t.Error("strategy not valid")
}
2016-07-25 19:31:16 +00:00
}
func TestTimeoutValidStrategy(t *testing.T) {
strategy := BeforeTime(time.Now().Add(2 * time.Second))
2019-02-10 14:02:28 +00:00
if !strategy.IsValid() {
t.Error("strategy not valid")
}
2016-07-25 19:31:16 +00:00
time.Sleep(3 * time.Second)
2019-02-10 14:02:28 +00:00
if strategy.IsValid() {
t.Error("strategy is valid")
}
2016-07-25 19:31:16 +00:00
strategy = BeforeTime(time.Now().Add(2 * time.Second))
strategy.Invalidate()
2019-02-10 14:02:28 +00:00
if strategy.IsValid() {
t.Error("strategy is valid")
}
2016-07-25 19:31:16 +00:00
}
2017-12-17 23:07:50 +00:00
func TestUserInServerSpec(t *testing.T) {
uuid1 := uuid.New()
uuid2 := uuid.New()
2018-08-26 22:33:27 +00:00
toAccount := func(a *vmess.Account) Account {
account, err := a.AsAccount()
common.Must(err)
return account
}
2018-08-26 22:11:32 +00:00
spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{
2017-12-17 23:07:50 +00:00
Email: "test1@v2ray.com",
2018-08-26 22:33:27 +00:00
Account: toAccount(&vmess.Account{Id: uuid1.String()}),
2017-12-17 23:07:50 +00:00
})
2019-02-10 14:02:28 +00:00
if spec.HasUser(&MemoryUser{
2017-12-17 23:07:50 +00:00
Email: "test1@v2ray.com",
2018-08-26 22:33:27 +00:00
Account: toAccount(&vmess.Account{Id: uuid2.String()}),
2019-02-10 14:02:28 +00:00
}) {
t.Error("has user: ", uuid2)
}
2017-12-17 23:07:50 +00:00
2018-08-26 22:11:32 +00:00
spec.AddUser(&MemoryUser{Email: "test2@v2ray.com"})
2019-02-10 14:02:28 +00:00
if !spec.HasUser(&MemoryUser{
2017-12-17 23:07:50 +00:00
Email: "test1@v2ray.com",
2018-08-26 22:33:27 +00:00
Account: toAccount(&vmess.Account{Id: uuid1.String()}),
2019-02-10 14:02:28 +00:00
}) {
t.Error("not having user: ", uuid1)
}
2017-12-17 23:07:50 +00:00
}
func TestPickUser(t *testing.T) {
2018-08-26 22:11:32 +00:00
spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{Email: "test1@v2ray.com"}, &MemoryUser{Email: "test2@v2ray.com"}, &MemoryUser{Email: "test3@v2ray.com"})
2017-12-17 23:07:50 +00:00
user := spec.PickUser()
2019-02-10 14:02:28 +00:00
if !strings.HasSuffix(user.Email, "@v2ray.com") {
t.Error("user: ", user.Email)
}
2017-12-17 23:07:50 +00:00
}