1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/common/net/port_json_test.go

74 lines
1.9 KiB
Go
Raw Normal View History

2016-01-15 07:39:36 -05:00
// +build json
package net_test
2015-10-31 16:43:26 -04:00
import (
"encoding/json"
"testing"
2016-01-15 07:39:36 -05:00
. "github.com/v2ray/v2ray-core/common/net"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
2015-10-31 16:43:26 -04:00
)
func TestIntPort(t *testing.T) {
v2testing.Current(t)
2015-10-31 16:43:26 -04:00
var portRange PortRange
err := json.Unmarshal([]byte("1234"), &portRange)
assert.Error(err).IsNil()
2016-01-15 07:39:36 -05:00
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
assert.Uint16(portRange.To.Value()).Equals(uint16(1234))
2015-10-31 16:43:26 -04:00
}
func TestOverRangeIntPort(t *testing.T) {
v2testing.Current(t)
2015-10-31 16:43:26 -04:00
var portRange PortRange
err := json.Unmarshal([]byte("70000"), &portRange)
2016-01-30 06:23:56 -05:00
assert.Error(err).Equals(ErrorInvalidPortRange)
2015-10-31 16:43:26 -04:00
err = json.Unmarshal([]byte("-1"), &portRange)
2016-01-30 06:23:56 -05:00
assert.Error(err).Equals(ErrorInvalidPortRange)
2015-10-31 16:43:26 -04:00
}
func TestSingleStringPort(t *testing.T) {
v2testing.Current(t)
2015-10-31 16:43:26 -04:00
var portRange PortRange
err := json.Unmarshal([]byte("\"1234\""), &portRange)
assert.Error(err).IsNil()
2016-01-15 07:39:36 -05:00
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
assert.Uint16(portRange.To.Value()).Equals(uint16(1234))
2015-10-31 16:43:26 -04:00
}
func TestStringPairPort(t *testing.T) {
v2testing.Current(t)
2015-10-31 16:43:26 -04:00
var portRange PortRange
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
assert.Error(err).IsNil()
2016-01-15 07:39:36 -05:00
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
assert.Uint16(portRange.To.Value()).Equals(uint16(5678))
2015-10-31 16:43:26 -04:00
}
func TestOverRangeStringPort(t *testing.T) {
v2testing.Current(t)
2015-10-31 16:43:26 -04:00
var portRange PortRange
2015-11-03 14:07:46 -05:00
err := json.Unmarshal([]byte("\"65536\""), &portRange)
2016-01-30 06:23:56 -05:00
assert.Error(err).Equals(ErrorInvalidPortRange)
2015-10-31 16:43:26 -04:00
err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
2016-01-30 06:23:56 -05:00
assert.Error(err).Equals(ErrorInvalidPortRange)
2015-10-31 16:43:26 -04:00
err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
2016-01-30 06:23:56 -05:00
assert.Error(err).Equals(ErrorInvalidPortRange)
2015-10-31 16:43:26 -04:00
err = json.Unmarshal([]byte("\"700-600\""), &portRange)
2016-01-30 06:23:56 -05:00
assert.Error(err).Equals(ErrorInvalidPortRange)
2015-10-31 16:43:26 -04:00
}