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-08-20 14:55:45 -04:00
|
|
|
. "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/testing/assert"
|
2015-10-31 16:43:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIntPort(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
var portRange PortRange
|
|
|
|
err := json.Unmarshal([]byte("1234"), &portRange)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2016-08-26 18:04:35 -04:00
|
|
|
assert.Uint32(portRange.From).Equals(1234)
|
|
|
|
assert.Uint32(portRange.To).Equals(1234)
|
2015-10-31 16:43:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverRangeIntPort(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
var portRange PortRange
|
|
|
|
err := json.Unmarshal([]byte("70000"), &portRange)
|
2016-06-27 02:53:35 -04:00
|
|
|
assert.Error(err).Equals(ErrInvalidPortRange)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte("-1"), &portRange)
|
2016-06-27 02:53:35 -04:00
|
|
|
assert.Error(err).Equals(ErrInvalidPortRange)
|
2015-10-31 16:43:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSingleStringPort(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
var portRange PortRange
|
|
|
|
err := json.Unmarshal([]byte("\"1234\""), &portRange)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2016-08-26 18:04:35 -04:00
|
|
|
assert.Uint32(portRange.From).Equals(1234)
|
|
|
|
assert.Uint32(portRange.To).Equals(1234)
|
2015-10-31 16:43:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringPairPort(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
var portRange PortRange
|
|
|
|
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
2016-08-26 18:04:35 -04:00
|
|
|
assert.Uint32(portRange.From).Equals(1234)
|
|
|
|
assert.Uint32(portRange.To).Equals(5678)
|
2015-10-31 16:43:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverRangeStringPort(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(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-06-27 02:53:35 -04:00
|
|
|
assert.Error(err).Equals(ErrInvalidPortRange)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
|
2016-06-27 02:53:35 -04:00
|
|
|
assert.Error(err).Equals(ErrInvalidPortRange)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
|
2016-06-27 02:53:35 -04:00
|
|
|
assert.Error(err).Equals(ErrInvalidPortRange)
|
2015-10-31 16:43:26 -04:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte("\"700-600\""), &portRange)
|
2016-06-27 02:53:35 -04:00
|
|
|
assert.Error(err).Equals(ErrInvalidPortRange)
|
2015-10-31 16:43:26 -04:00
|
|
|
}
|