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:
parent
baeef07abb
commit
39cfc982b5
@ -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 {
|
||||||
|
@ -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
|
||||||
|
37
app/policy/manager_test.go
Normal file
37
app/policy/manager_test.go
Normal 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)
|
||||||
|
}
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user