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