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"
|
2015-12-02 09:27:18 -05:00
|
|
|
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) {
|
2015-12-02 09:27:18 -05:00
|
|
|
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) {
|
2015-12-02 09:27:18 -05:00
|
|
|
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) {
|
2015-12-02 09:27:18 -05:00
|
|
|
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) {
|
2015-12-02 09:27:18 -05:00
|
|
|
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) {
|
2015-12-02 09:27:18 -05:00
|
|
|
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
|
|
|
}
|