1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/net/port_json_test.go
2016-08-27 00:04:35 +02:00

73 lines
1.7 KiB
Go

// +build json
package net_test
import (
"encoding/json"
"testing"
. "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
)
func TestIntPort(t *testing.T) {
assert := assert.On(t)
var portRange PortRange
err := json.Unmarshal([]byte("1234"), &portRange)
assert.Error(err).IsNil()
assert.Uint32(portRange.From).Equals(1234)
assert.Uint32(portRange.To).Equals(1234)
}
func TestOverRangeIntPort(t *testing.T) {
assert := assert.On(t)
var portRange PortRange
err := json.Unmarshal([]byte("70000"), &portRange)
assert.Error(err).Equals(ErrInvalidPortRange)
err = json.Unmarshal([]byte("-1"), &portRange)
assert.Error(err).Equals(ErrInvalidPortRange)
}
func TestSingleStringPort(t *testing.T) {
assert := assert.On(t)
var portRange PortRange
err := json.Unmarshal([]byte("\"1234\""), &portRange)
assert.Error(err).IsNil()
assert.Uint32(portRange.From).Equals(1234)
assert.Uint32(portRange.To).Equals(1234)
}
func TestStringPairPort(t *testing.T) {
assert := assert.On(t)
var portRange PortRange
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
assert.Error(err).IsNil()
assert.Uint32(portRange.From).Equals(1234)
assert.Uint32(portRange.To).Equals(5678)
}
func TestOverRangeStringPort(t *testing.T) {
assert := assert.On(t)
var portRange PortRange
err := json.Unmarshal([]byte("\"65536\""), &portRange)
assert.Error(err).Equals(ErrInvalidPortRange)
err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
assert.Error(err).Equals(ErrInvalidPortRange)
err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
assert.Error(err).Equals(ErrInvalidPortRange)
err = json.Unmarshal([]byte("\"700-600\""), &portRange)
assert.Error(err).Equals(ErrInvalidPortRange)
}