github.com/xraypb/Xray-core@v1.8.1/infra/conf/common_test.go (about)

     1  package conf_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/google/go-cmp/cmp/cmpopts"
    10  	"github.com/xraypb/Xray-core/common"
    11  	"github.com/xraypb/Xray-core/common/net"
    12  	"github.com/xraypb/Xray-core/common/protocol"
    13  	. "github.com/xraypb/Xray-core/infra/conf"
    14  )
    15  
    16  func TestStringListUnmarshalError(t *testing.T) {
    17  	rawJSON := `1234`
    18  	list := new(StringList)
    19  	err := json.Unmarshal([]byte(rawJSON), list)
    20  	if err == nil {
    21  		t.Error("expected error, but got nil")
    22  	}
    23  }
    24  
    25  func TestStringListLen(t *testing.T) {
    26  	rawJSON := `"a, b, c, d"`
    27  	var list StringList
    28  	err := json.Unmarshal([]byte(rawJSON), &list)
    29  	common.Must(err)
    30  	if r := cmp.Diff([]string(list), []string{"a", " b", " c", " d"}); r != "" {
    31  		t.Error(r)
    32  	}
    33  }
    34  
    35  func TestIPParsing(t *testing.T) {
    36  	rawJSON := "\"8.8.8.8\""
    37  	var address Address
    38  	err := json.Unmarshal([]byte(rawJSON), &address)
    39  	common.Must(err)
    40  	if r := cmp.Diff(address.IP(), net.IP{8, 8, 8, 8}); r != "" {
    41  		t.Error(r)
    42  	}
    43  }
    44  
    45  func TestDomainParsing(t *testing.T) {
    46  	rawJSON := "\"example.com\""
    47  	var address Address
    48  	common.Must(json.Unmarshal([]byte(rawJSON), &address))
    49  	if address.Domain() != "example.com" {
    50  		t.Error("domain: ", address.Domain())
    51  	}
    52  }
    53  
    54  func TestURLParsing(t *testing.T) {
    55  	{
    56  		rawJSON := "\"https://dns.google/dns-query\""
    57  		var address Address
    58  		common.Must(json.Unmarshal([]byte(rawJSON), &address))
    59  		if address.Domain() != "https://dns.google/dns-query" {
    60  			t.Error("URL: ", address.Domain())
    61  		}
    62  	}
    63  	{
    64  		rawJSON := "\"https+local://dns.google/dns-query\""
    65  		var address Address
    66  		common.Must(json.Unmarshal([]byte(rawJSON), &address))
    67  		if address.Domain() != "https+local://dns.google/dns-query" {
    68  			t.Error("URL: ", address.Domain())
    69  		}
    70  	}
    71  }
    72  
    73  func TestInvalidAddressJson(t *testing.T) {
    74  	rawJSON := "1234"
    75  	var address Address
    76  	err := json.Unmarshal([]byte(rawJSON), &address)
    77  	if err == nil {
    78  		t.Error("nil error")
    79  	}
    80  }
    81  
    82  func TestStringNetwork(t *testing.T) {
    83  	var network Network
    84  	common.Must(json.Unmarshal([]byte(`"tcp"`), &network))
    85  	if v := network.Build(); v != net.Network_TCP {
    86  		t.Error("network: ", v)
    87  	}
    88  }
    89  
    90  func TestArrayNetworkList(t *testing.T) {
    91  	var list NetworkList
    92  	common.Must(json.Unmarshal([]byte("[\"Tcp\"]"), &list))
    93  
    94  	nlist := list.Build()
    95  	if !net.HasNetwork(nlist, net.Network_TCP) {
    96  		t.Error("no tcp network")
    97  	}
    98  	if net.HasNetwork(nlist, net.Network_UDP) {
    99  		t.Error("has udp network")
   100  	}
   101  }
   102  
   103  func TestStringNetworkList(t *testing.T) {
   104  	var list NetworkList
   105  	common.Must(json.Unmarshal([]byte("\"TCP, ip\""), &list))
   106  
   107  	nlist := list.Build()
   108  	if !net.HasNetwork(nlist, net.Network_TCP) {
   109  		t.Error("no tcp network")
   110  	}
   111  	if net.HasNetwork(nlist, net.Network_UDP) {
   112  		t.Error("has udp network")
   113  	}
   114  }
   115  
   116  func TestInvalidNetworkJson(t *testing.T) {
   117  	var list NetworkList
   118  	err := json.Unmarshal([]byte("0"), &list)
   119  	if err == nil {
   120  		t.Error("nil error")
   121  	}
   122  }
   123  
   124  func TestIntPort(t *testing.T) {
   125  	var portRange PortRange
   126  	common.Must(json.Unmarshal([]byte("1234"), &portRange))
   127  
   128  	if r := cmp.Diff(portRange, PortRange{
   129  		From: 1234, To: 1234,
   130  	}); r != "" {
   131  		t.Error(r)
   132  	}
   133  }
   134  
   135  func TestOverRangeIntPort(t *testing.T) {
   136  	var portRange PortRange
   137  	err := json.Unmarshal([]byte("70000"), &portRange)
   138  	if err == nil {
   139  		t.Error("nil error")
   140  	}
   141  
   142  	err = json.Unmarshal([]byte("-1"), &portRange)
   143  	if err == nil {
   144  		t.Error("nil error")
   145  	}
   146  }
   147  
   148  func TestEnvPort(t *testing.T) {
   149  	common.Must(os.Setenv("PORT", "1234"))
   150  
   151  	var portRange PortRange
   152  	common.Must(json.Unmarshal([]byte("\"env:PORT\""), &portRange))
   153  
   154  	if r := cmp.Diff(portRange, PortRange{
   155  		From: 1234, To: 1234,
   156  	}); r != "" {
   157  		t.Error(r)
   158  	}
   159  }
   160  
   161  func TestSingleStringPort(t *testing.T) {
   162  	var portRange PortRange
   163  	common.Must(json.Unmarshal([]byte("\"1234\""), &portRange))
   164  
   165  	if r := cmp.Diff(portRange, PortRange{
   166  		From: 1234, To: 1234,
   167  	}); r != "" {
   168  		t.Error(r)
   169  	}
   170  }
   171  
   172  func TestStringPairPort(t *testing.T) {
   173  	var portRange PortRange
   174  	common.Must(json.Unmarshal([]byte("\"1234-5678\""), &portRange))
   175  
   176  	if r := cmp.Diff(portRange, PortRange{
   177  		From: 1234, To: 5678,
   178  	}); r != "" {
   179  		t.Error(r)
   180  	}
   181  }
   182  
   183  func TestOverRangeStringPort(t *testing.T) {
   184  	var portRange PortRange
   185  	err := json.Unmarshal([]byte("\"65536\""), &portRange)
   186  	if err == nil {
   187  		t.Error("nil error")
   188  	}
   189  
   190  	err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
   191  	if err == nil {
   192  		t.Error("nil error")
   193  	}
   194  
   195  	err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
   196  	if err == nil {
   197  		t.Error("nil error")
   198  	}
   199  
   200  	err = json.Unmarshal([]byte("\"700-600\""), &portRange)
   201  	if err == nil {
   202  		t.Error("nil error")
   203  	}
   204  }
   205  
   206  func TestUserParsing(t *testing.T) {
   207  	user := new(User)
   208  	common.Must(json.Unmarshal([]byte(`{
   209      "id": "96edb838-6d68-42ef-a933-25f7ac3a9d09",
   210      "email": "love@example.com",
   211      "level": 1
   212    }`), user))
   213  
   214  	nUser := user.Build()
   215  	if r := cmp.Diff(nUser, &protocol.User{
   216  		Level: 1,
   217  		Email: "love@example.com",
   218  	}, cmpopts.IgnoreUnexported(protocol.User{})); r != "" {
   219  		t.Error(r)
   220  	}
   221  }
   222  
   223  func TestInvalidUserJson(t *testing.T) {
   224  	user := new(User)
   225  	err := json.Unmarshal([]byte(`{"email": 1234}`), user)
   226  	if err == nil {
   227  		t.Error("nil error")
   228  	}
   229  }