1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/protocol/server_spec_test.go

66 lines
1.6 KiB
Go
Raw Normal View History

2016-07-25 10:48:09 -04:00
package protocol_test
import (
"testing"
2016-07-25 15:31:16 -04:00
"time"
2016-07-25 10:48:09 -04:00
2017-12-17 18:07:50 -05:00
"v2ray.com/core/common/net"
2016-08-20 14:55:45 -04:00
. "v2ray.com/core/common/protocol"
2017-12-17 18:07:50 -05:00
"v2ray.com/core/common/serial"
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/vmess"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2016-07-25 10:48:09 -04:00
)
2016-07-25 15:31:16 -04:00
func TestAlwaysValidStrategy(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-07-25 15:31:16 -04:00
strategy := AlwaysValid()
2017-10-24 10:15:35 -04:00
assert(strategy.IsValid(), IsTrue)
2016-07-25 15:31:16 -04:00
strategy.Invalidate()
2017-10-24 10:15:35 -04:00
assert(strategy.IsValid(), IsTrue)
2016-07-25 15:31:16 -04:00
}
func TestTimeoutValidStrategy(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-07-25 15:31:16 -04:00
strategy := BeforeTime(time.Now().Add(2 * time.Second))
2017-10-24 10:15:35 -04:00
assert(strategy.IsValid(), IsTrue)
2016-07-25 15:31:16 -04:00
time.Sleep(3 * time.Second)
2017-10-24 10:15:35 -04:00
assert(strategy.IsValid(), IsFalse)
2016-07-25 15:31:16 -04:00
strategy = BeforeTime(time.Now().Add(2 * time.Second))
strategy.Invalidate()
2017-10-24 10:15:35 -04:00
assert(strategy.IsValid(), IsFalse)
2016-07-25 15:31:16 -04:00
}
2017-12-17 18:07:50 -05:00
func TestUserInServerSpec(t *testing.T) {
assert := With(t)
uuid1 := uuid.New()
uuid2 := uuid.New()
spec := NewServerSpec(net.Destination{}, AlwaysValid(), &User{
Email: "test1@v2ray.com",
Account: serial.ToTypedMessage(&vmess.Account{Id: uuid1.String()}),
})
assert(spec.HasUser(&User{
Email: "test1@v2ray.com",
Account: serial.ToTypedMessage(&vmess.Account{Id: uuid2.String()}),
}), IsFalse)
spec.AddUser(&User{Email: "test2@v2ray.com"})
assert(spec.HasUser(&User{
Email: "test1@v2ray.com",
Account: serial.ToTypedMessage(&vmess.Account{Id: uuid1.String()}),
}), IsTrue)
}
func TestPickUser(t *testing.T) {
assert := With(t)
spec := NewServerSpec(net.Destination{}, AlwaysValid(), &User{Email: "test1@v2ray.com"}, &User{Email: "test2@v2ray.com"}, &User{Email: "test3@v2ray.com"})
user := spec.PickUser()
assert(user.Email, HasSuffix, "@v2ray.com")
}