1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

test case for policy

This commit is contained in:
Darien Raymond 2018-02-17 23:37:09 +01:00
parent baeef07abb
commit 39cfc982b5
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 43 additions and 28 deletions

View File

@ -14,24 +14,6 @@ func (s *Second) Duration() time.Duration {
return time.Second * time.Duration(s.Value) return time.Second * time.Duration(s.Value)
} }
// OverrideWith overrides current Policy with another one.
func (p *Policy) OverrideWith(another *Policy) {
if another.Timeout != nil {
if another.Timeout.Handshake != nil {
p.Timeout.Handshake = another.Timeout.Handshake
}
if another.Timeout.ConnectionIdle != nil {
p.Timeout.ConnectionIdle = another.Timeout.ConnectionIdle
}
if another.Timeout.UplinkOnly != nil {
p.Timeout.UplinkOnly = another.Timeout.UplinkOnly
}
if another.Timeout.DownlinkOnly != nil {
p.Timeout.DownlinkOnly = another.Timeout.DownlinkOnly
}
}
}
func (p *Policy) ToCorePolicy() core.Policy { func (p *Policy) ToCorePolicy() core.Policy {
var cp core.Policy var cp core.Policy
if p.Timeout != nil { if p.Timeout != nil {

View File

@ -19,19 +19,15 @@ func New(ctx context.Context, config *Config) (*Instance, error) {
} }
if len(config.Level) > 0 { if len(config.Level) > 0 {
for lv, p := range config.Level { for lv, p := range config.Level {
dp := core.DefaultPolicy() m.levels[lv] = p.ToCorePolicy().OverrideWith(core.DefaultPolicy())
dp.OverrideWith(p.ToCorePolicy())
m.levels[lv] = dp
} }
} }
v := core.FromContext(ctx) v := core.FromContext(ctx)
if v == nil { if v != nil {
return nil, newError("V is not in context.") if err := v.RegisterFeature((*core.PolicyManager)(nil), m); err != nil {
} return nil, newError("unable to register PolicyManager in core").Base(err).AtError()
}
if err := v.RegisterFeature((*core.PolicyManager)(nil), m); err != nil {
return nil, newError("unable to register PolicyManager in core").Base(err).AtError()
} }
return m, nil return m, nil

View File

@ -0,0 +1,37 @@
package policy_test
import (
"context"
"testing"
"time"
"v2ray.com/core"
. "v2ray.com/core/app/policy"
. "v2ray.com/ext/assert"
)
func TestPolicy(t *testing.T) {
assert := With(t)
manager, err := New(context.Background(), &Config{
Level: map[uint32]*Policy{
0: &Policy{
Timeout: &Policy_Timeout{
Handshake: &Second{
Value: 2,
},
},
},
},
})
assert(err, IsNil)
pDefault := core.DefaultPolicy()
p0 := manager.ForLevel(0)
assert(p0.Timeouts.Handshake, Equals, 2*time.Second)
assert(p0.Timeouts.ConnectionIdle, Equals, pDefault.Timeouts.ConnectionIdle)
p1 := manager.ForLevel(1)
assert(p1.Timeouts.Handshake, Equals, pDefault.Timeouts.Handshake)
}

View File

@ -43,7 +43,7 @@ type Policy struct {
// OverrideWith overrides the current Policy with another one. All values with default value will be overridden. // OverrideWith overrides the current Policy with another one. All values with default value will be overridden.
func (p Policy) OverrideWith(another Policy) Policy { func (p Policy) OverrideWith(another Policy) Policy {
p.Timeouts.OverrideWith(another.Timeouts) p.Timeouts = p.Timeouts.OverrideWith(another.Timeouts)
return p return p
} }