mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.8 KiB
74 lines
1.8 KiB
9 years ago
|
// +build json
|
||
|
|
||
|
package net_test
|
||
9 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"testing"
|
||
|
|
||
9 years ago
|
. "github.com/v2ray/v2ray-core/common/net"
|
||
9 years ago
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||
9 years ago
|
)
|
||
|
|
||
|
func TestIntPort(t *testing.T) {
|
||
9 years ago
|
v2testing.Current(t)
|
||
9 years ago
|
|
||
|
var portRange PortRange
|
||
|
err := json.Unmarshal([]byte("1234"), &portRange)
|
||
|
assert.Error(err).IsNil()
|
||
|
|
||
9 years ago
|
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
|
||
|
assert.Uint16(portRange.To.Value()).Equals(uint16(1234))
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestOverRangeIntPort(t *testing.T) {
|
||
9 years ago
|
v2testing.Current(t)
|
||
9 years ago
|
|
||
|
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) {
|
||
9 years ago
|
v2testing.Current(t)
|
||
9 years ago
|
|
||
|
var portRange PortRange
|
||
|
err := json.Unmarshal([]byte("\"1234\""), &portRange)
|
||
|
assert.Error(err).IsNil()
|
||
|
|
||
9 years ago
|
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
|
||
|
assert.Uint16(portRange.To.Value()).Equals(uint16(1234))
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestStringPairPort(t *testing.T) {
|
||
9 years ago
|
v2testing.Current(t)
|
||
9 years ago
|
|
||
|
var portRange PortRange
|
||
|
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
|
||
|
assert.Error(err).IsNil()
|
||
|
|
||
9 years ago
|
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
|
||
|
assert.Uint16(portRange.To.Value()).Equals(uint16(5678))
|
||
9 years ago
|
}
|
||
|
|
||
|
func TestOverRangeStringPort(t *testing.T) {
|
||
9 years ago
|
v2testing.Current(t)
|
||
9 years ago
|
|
||
|
var portRange PortRange
|
||
9 years ago
|
err := json.Unmarshal([]byte("\"65536\""), &portRange)
|
||
9 years ago
|
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)
|
||
|
}
|