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

63 lines
1.4 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
v2net "github.com/v2ray/v2ray-core/common/net"
. "github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/testing/assert"
)
2016-07-25 11:36:24 -04:00
type TestAccount struct {
id int
}
func (this *TestAccount) Equals(account Account) bool {
return account.(*TestAccount).id == this.id
}
2016-07-25 15:31:16 -04:00
func TestServerSpecUser(t *testing.T) {
2016-07-25 10:48:09 -04:00
assert := assert.On(t)
2016-07-25 11:36:24 -04:00
account := &TestAccount{
id: 0,
2016-07-25 10:48:09 -04:00
}
2016-07-25 11:36:24 -04:00
user := NewUser(UserLevel(0), "")
user.Account = account
2016-07-25 10:48:09 -04:00
rec := NewServerSpec(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), AlwaysValid(), user)
assert.Bool(rec.HasUser(user)).IsTrue()
2016-07-25 11:36:24 -04:00
account2 := &TestAccount{
id: 1,
2016-07-25 10:48:09 -04:00
}
2016-07-25 11:36:24 -04:00
user2 := NewUser(UserLevel(0), "")
user2.Account = account2
2016-07-25 10:48:09 -04:00
assert.Bool(rec.HasUser(user2)).IsFalse()
rec.AddUser(user2)
assert.Bool(rec.HasUser(user2)).IsTrue()
}
2016-07-25 15:31:16 -04:00
func TestAlwaysValidStrategy(t *testing.T) {
assert := assert.On(t)
strategy := AlwaysValid()
assert.Bool(strategy.IsValid()).IsTrue()
strategy.Invalidate()
assert.Bool(strategy.IsValid()).IsTrue()
}
func TestTimeoutValidStrategy(t *testing.T) {
assert := assert.On(t)
strategy := BeforeTime(time.Now().Add(2 * time.Second))
assert.Bool(strategy.IsValid()).IsTrue()
time.Sleep(3 * time.Second)
assert.Bool(strategy.IsValid()).IsFalse()
strategy = BeforeTime(time.Now().Add(2 * time.Second))
strategy.Invalidate()
assert.Bool(strategy.IsValid()).IsFalse()
}