1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/infra/conf/cfgcommon/common_test.go

232 lines
5.2 KiB
Go
Raw Normal View History

package cfgcommon_test
2019-02-10 13:04:11 -05:00
import (
"encoding/json"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
2019-02-10 13:04:11 -05:00
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/protocol"
2021-05-04 09:52:35 -04:00
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
2019-02-10 13:04:11 -05:00
)
func TestStringListUnmarshalError(t *testing.T) {
rawJSON := `1234`
list := new(cfgcommon.StringList)
err := json.Unmarshal([]byte(rawJSON), list)
2019-02-10 13:04:11 -05:00
if err == nil {
t.Error("expected error, but got nil")
}
}
func TestStringListLen(t *testing.T) {
rawJSON := `"a, b, c, d"`
var list cfgcommon.StringList
err := json.Unmarshal([]byte(rawJSON), &list)
2019-02-10 13:04:11 -05:00
common.Must(err)
if r := cmp.Diff([]string(list), []string{"a", " b", " c", " d"}); r != "" {
t.Error(r)
}
}
func TestIPParsing(t *testing.T) {
rawJSON := "\"8.8.8.8\""
var address cfgcommon.Address
err := json.Unmarshal([]byte(rawJSON), &address)
2019-02-10 13:04:11 -05:00
common.Must(err)
if r := cmp.Diff(address.IP(), net.IP{8, 8, 8, 8}); r != "" {
t.Error(r)
}
}
func TestDomainParsing(t *testing.T) {
2021-02-16 15:31:50 -05:00
rawJSON := "\"v2fly.org\""
var address cfgcommon.Address
common.Must(json.Unmarshal([]byte(rawJSON), &address))
2021-02-16 15:31:50 -05:00
if address.Domain() != "v2fly.org" {
2019-02-10 13:04:11 -05:00
t.Error("domain: ", address.Domain())
}
}
2019-12-05 23:55:14 -05:00
func TestURLParsing(t *testing.T) {
{
rawJSON := "\"https://dns.google/dns-query\""
var address cfgcommon.Address
common.Must(json.Unmarshal([]byte(rawJSON), &address))
2019-12-05 23:55:14 -05:00
if address.Domain() != "https://dns.google/dns-query" {
t.Error("URL: ", address.Domain())
}
}
{
rawJSON := "\"https+local://dns.google/dns-query\""
var address cfgcommon.Address
common.Must(json.Unmarshal([]byte(rawJSON), &address))
2019-12-05 23:55:14 -05:00
if address.Domain() != "https+local://dns.google/dns-query" {
t.Error("URL: ", address.Domain())
}
}
}
2019-02-10 13:04:11 -05:00
func TestInvalidAddressJson(t *testing.T) {
rawJSON := "1234"
var address cfgcommon.Address
err := json.Unmarshal([]byte(rawJSON), &address)
2019-02-10 13:04:11 -05:00
if err == nil {
t.Error("nil error")
}
}
func TestStringNetwork(t *testing.T) {
var network cfgcommon.Network
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte(`"tcp"`), &network))
if v := network.Build(); v != net.Network_TCP {
t.Error("network: ", v)
}
}
func TestArrayNetworkList(t *testing.T) {
var list cfgcommon.NetworkList
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte("[\"Tcp\"]"), &list))
nlist := list.Build()
if !net.HasNetwork(nlist, net.Network_TCP) {
t.Error("no tcp network")
}
if net.HasNetwork(nlist, net.Network_UDP) {
t.Error("has udp network")
}
}
func TestStringNetworkList(t *testing.T) {
var list cfgcommon.NetworkList
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte("\"TCP, ip\""), &list))
nlist := list.Build()
if !net.HasNetwork(nlist, net.Network_TCP) {
t.Error("no tcp network")
}
if net.HasNetwork(nlist, net.Network_UDP) {
t.Error("has udp network")
}
}
func TestInvalidNetworkJson(t *testing.T) {
var list cfgcommon.NetworkList
2019-02-10 13:04:11 -05:00
err := json.Unmarshal([]byte("0"), &list)
if err == nil {
t.Error("nil error")
}
}
func TestIntPort(t *testing.T) {
var portRange cfgcommon.PortRange
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte("1234"), &portRange))
if r := cmp.Diff(portRange, cfgcommon.PortRange{
2019-02-10 13:04:11 -05:00
From: 1234, To: 1234,
}); r != "" {
t.Error(r)
}
}
func TestOverRangeIntPort(t *testing.T) {
var portRange cfgcommon.PortRange
2019-02-10 13:04:11 -05:00
err := json.Unmarshal([]byte("70000"), &portRange)
if err == nil {
t.Error("nil error")
}
err = json.Unmarshal([]byte("-1"), &portRange)
if err == nil {
t.Error("nil error")
}
}
func TestEnvPort(t *testing.T) {
common.Must(os.Setenv("PORT", "1234"))
var portRange cfgcommon.PortRange
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte("\"env:PORT\""), &portRange))
if r := cmp.Diff(portRange, cfgcommon.PortRange{
2019-02-10 13:04:11 -05:00
From: 1234, To: 1234,
}); r != "" {
t.Error(r)
}
}
func TestSingleStringPort(t *testing.T) {
var portRange cfgcommon.PortRange
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte("\"1234\""), &portRange))
if r := cmp.Diff(portRange, cfgcommon.PortRange{
2019-02-10 13:04:11 -05:00
From: 1234, To: 1234,
}); r != "" {
t.Error(r)
}
}
func TestStringPairPort(t *testing.T) {
var portRange cfgcommon.PortRange
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte("\"1234-5678\""), &portRange))
if r := cmp.Diff(portRange, cfgcommon.PortRange{
2019-02-10 13:04:11 -05:00
From: 1234, To: 5678,
}); r != "" {
t.Error(r)
}
}
func TestOverRangeStringPort(t *testing.T) {
var portRange cfgcommon.PortRange
2019-02-10 13:04:11 -05:00
err := json.Unmarshal([]byte("\"65536\""), &portRange)
if err == nil {
t.Error("nil error")
}
err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
if err == nil {
t.Error("nil error")
}
err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
if err == nil {
t.Error("nil error")
}
err = json.Unmarshal([]byte("\"700-600\""), &portRange)
if err == nil {
t.Error("nil error")
}
}
func TestUserParsing(t *testing.T) {
user := new(cfgcommon.User)
2019-02-10 13:04:11 -05:00
common.Must(json.Unmarshal([]byte(`{
"id": "96edb838-6d68-42ef-a933-25f7ac3a9d09",
2021-02-16 15:31:50 -05:00
"email": "love@v2fly.org",
2019-02-10 13:04:11 -05:00
"level": 1,
"alterId": 100
}`), user))
nUser := user.Build()
if r := cmp.Diff(nUser, &protocol.User{
Level: 1,
2021-02-16 15:31:50 -05:00
Email: "love@v2fly.org",
2020-06-08 04:16:50 -04:00
}, cmpopts.IgnoreUnexported(protocol.User{})); r != "" {
2019-02-10 13:04:11 -05:00
t.Error(r)
}
}
func TestInvalidUserJson(t *testing.T) {
user := new(cfgcommon.User)
2019-02-10 13:04:11 -05:00
err := json.Unmarshal([]byte(`{"email": 1234}`), user)
if err == nil {
t.Error("nil error")
}
}