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