You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/protocol/server_spec_test.go

72 lines
1.7 KiB

package protocol_test
import (
"testing"
"time"
6 years ago
"v2ray.com/core/common"
7 years ago
"v2ray.com/core/common/net"
. "v2ray.com/core/common/protocol"
7 years ago
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/vmess"
. "v2ray.com/ext/assert"
)
func TestAlwaysValidStrategy(t *testing.T) {
assert := With(t)
strategy := AlwaysValid()
assert(strategy.IsValid(), IsTrue)
strategy.Invalidate()
assert(strategy.IsValid(), IsTrue)
}
func TestTimeoutValidStrategy(t *testing.T) {
assert := With(t)
strategy := BeforeTime(time.Now().Add(2 * time.Second))
assert(strategy.IsValid(), IsTrue)
time.Sleep(3 * time.Second)
assert(strategy.IsValid(), IsFalse)
strategy = BeforeTime(time.Now().Add(2 * time.Second))
strategy.Invalidate()
assert(strategy.IsValid(), IsFalse)
}
7 years ago
func TestUserInServerSpec(t *testing.T) {
assert := With(t)
uuid1 := uuid.New()
uuid2 := uuid.New()
6 years ago
toAccount := func(a *vmess.Account) Account {
account, err := a.AsAccount()
common.Must(err)
return account
}
spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{
7 years ago
Email: "test1@v2ray.com",
6 years ago
Account: toAccount(&vmess.Account{Id: uuid1.String()}),
7 years ago
})
assert(spec.HasUser(&MemoryUser{
7 years ago
Email: "test1@v2ray.com",
6 years ago
Account: toAccount(&vmess.Account{Id: uuid2.String()}),
7 years ago
}), IsFalse)
spec.AddUser(&MemoryUser{Email: "test2@v2ray.com"})
assert(spec.HasUser(&MemoryUser{
7 years ago
Email: "test1@v2ray.com",
6 years ago
Account: toAccount(&vmess.Account{Id: uuid1.String()}),
7 years ago
}), IsTrue)
}
func TestPickUser(t *testing.T) {
assert := With(t)
spec := NewServerSpec(net.Destination{}, AlwaysValid(), &MemoryUser{Email: "test1@v2ray.com"}, &MemoryUser{Email: "test2@v2ray.com"}, &MemoryUser{Email: "test3@v2ray.com"})
7 years ago
user := spec.PickUser()
assert(user.Email, HasSuffix, "@v2ray.com")
}