From c74bcc9c505218882827e2a88721cce7ddc2cc9a Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 4 Feb 2016 10:43:04 +0000 Subject: [PATCH] more json test cases --- common/net/address_json_test.go | 9 +++++++ common/net/network_json_test.go | 8 ++++++ common/protocol/user_json_test.go | 43 +++++++++++++++++++++++++++++++ shell/point/config_json.go | 16 ++++++------ 4 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 common/protocol/user_json_test.go diff --git a/common/net/address_json_test.go b/common/net/address_json_test.go index a3234ea50..d665a00dc 100644 --- a/common/net/address_json_test.go +++ b/common/net/address_json_test.go @@ -35,3 +35,12 @@ func TestDomainParsing(t *testing.T) { assert.Bool(address.Address.IsDomain()).IsTrue() assert.StringLiteral(address.Address.Domain()).Equals("v2ray.com") } + +func TestInvalidJson(t *testing.T) { + v2testing.Current(t) + + rawJson := "1234" + var address AddressJson + err := json.Unmarshal([]byte(rawJson), &address) + assert.Error(err).IsNotNil() +} diff --git a/common/net/network_json_test.go b/common/net/network_json_test.go index b0d0f81d3..3181db018 100644 --- a/common/net/network_json_test.go +++ b/common/net/network_json_test.go @@ -30,3 +30,11 @@ func TestStringNetworkList(t *testing.T) { assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue() assert.Bool(list.HasNetwork(Network("udp"))).IsFalse() } + +func TestInvalidJson(t *testing.T) { + v2testing.Current(t) + + var list NetworkList + err := json.Unmarshal([]byte("0"), &list) + assert.Error(err).IsNotNil() +} diff --git a/common/protocol/user_json_test.go b/common/protocol/user_json_test.go new file mode 100644 index 000000000..0d3dd0558 --- /dev/null +++ b/common/protocol/user_json_test.go @@ -0,0 +1,43 @@ +// +build json + +package protocol_test + +import ( + "encoding/json" + "testing" + + . "github.com/v2ray/v2ray-core/common/protocol" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestUserParsing(t *testing.T) { + v2testing.Current(t) + + user := new(User) + err := json.Unmarshal([]byte(`{ + "id": "96edb838-6d68-42ef-a933-25f7ac3a9d09", + "email": "love@v2ray.com", + "level": 1, + "alterId": 100 + }`), user) + assert.Error(err).IsNil() + assert.String(user.ID).Equals("96edb838-6d68-42ef-a933-25f7ac3a9d09") + assert.Byte(byte(user.Level)).Equals(1) +} + +func TestInvalidUserJson(t *testing.T) { + v2testing.Current(t) + + user := new(User) + err := json.Unmarshal([]byte(`{"id": 1234}`), user) + assert.Error(err).IsNotNil() +} + +func TestInvalidIdJson(t *testing.T) { + v2testing.Current(t) + + user := new(User) + err := json.Unmarshal([]byte(`{"id": "1234"}`), user) + assert.Error(err).IsNotNil() +} diff --git a/shell/point/config_json.go b/shell/point/config_json.go index 26e8da599..85dc3b688 100644 --- a/shell/point/config_json.go +++ b/shell/point/config_json.go @@ -95,6 +95,14 @@ func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error { this.Strategy = jsonConfig.Strategy this.Concurrency = jsonConfig.Concurrency this.Refresh = jsonConfig.RefreshMin + if this.Strategy == AllocationStrategyRandom { + if this.Refresh == 0 { + this.Refresh = 5 + } + if this.Concurrency == 0 { + this.Concurrency = 3 + } + } if this.Refresh == 0 { this.Refresh = DefaultRefreshMinute } @@ -128,14 +136,6 @@ func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error { Refresh: DefaultRefreshMinute, } } - if this.Allocation.Strategy == AllocationStrategyRandom { - if this.Allocation.Refresh == DefaultRefreshMinute { - this.Allocation.Refresh = 5 - } - if this.Allocation.Concurrency == 0 { - this.Allocation.Concurrency = 3 - } - } return nil }