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

74 lines
1.8 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)
assert.Error(err).Equals(InvalidPortRange)
err = json.Unmarshal([]byte("-1"), &portRange)
assert.Error(err).Equals(InvalidPortRange)
}
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)
2015-10-31 16:43:26 -04:00
assert.Error(err).Equals(InvalidPortRange)
err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
assert.Error(err).Equals(InvalidPortRange)
err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
assert.Error(err).Equals(InvalidPortRange)
err = json.Unmarshal([]byte("\"700-600\""), &portRange)
assert.Error(err).Equals(InvalidPortRange)
}