mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-03 00:22:05 +00:00
Test case for inbound detour network
This commit is contained in:
1 parent
1c6b66eacf
commit
1cec5ac643
5 files changed
+112
-9
No files matched your search
@@ -2,6 +2,7 @@ package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
@@ -9,17 +10,30 @@ import (
|
||||
|
||||
type NetworkList []string
|
||||
|
||||
func NewNetworkList(networks []string) NetworkList {
|
||||
list := NetworkList(make([]string, len(networks)))
|
||||
for idx, network := range networks {
|
||||
list[idx] = strings.ToLower(strings.TrimSpace(network))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (this *NetworkList) UnmarshalJSON(data []byte) error {
|
||||
var strList []string
|
||||
err := json.Unmarshal(data, &strList)
|
||||
if err != nil {
|
||||
return err
|
||||
if err == nil {
|
||||
*this = NewNetworkList(strList)
|
||||
return nil
|
||||
}
|
||||
*this = make([]string, len(strList))
|
||||
for idx, str := range strList {
|
||||
(*this)[idx] = strings.ToLower(str)
|
||||
|
||||
var str string
|
||||
err = json.Unmarshal(data, &str)
|
||||
if err == nil {
|
||||
strList := strings.Split(str, ",")
|
||||
*this = NewNetworkList(strList)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return errors.New("Unknown format of network list: " + string(data))
|
||||
}
|
||||
|
||||
func (this *NetworkList) HasNetwork(network v2net.Network) bool {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
)
|
||||
|
||||
func TestArrayNetworkList(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
var list NetworkList
|
||||
err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
|
||||
assert.Error(err).IsNil()
|
||||
assert.Bool(list.HasNetwork("tcp")).IsTrue()
|
||||
assert.Bool(list.HasNetwork("udp")).IsFalse()
|
||||
}
|
||||
|
||||
func TestStringNetworkList(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
var list NetworkList
|
||||
err := json.Unmarshal([]byte("\"TCP, ip\""), &list)
|
||||
assert.Error(err).IsNil()
|
||||
assert.Bool(list.HasNetwork("tcp")).IsTrue()
|
||||
assert.Bool(list.HasNetwork("udp")).IsFalse()
|
||||
}
|
||||
Reference in new issue
Block a user