config_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package json
  2. import (
  3. "encoding/json"
  4. "net"
  5. "testing"
  6. "github.com/v2ray/v2ray-core/proxy/common/config"
  7. jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
  8. "github.com/v2ray/v2ray-core/testing/unit"
  9. )
  10. func TestAccountMapParsing(t *testing.T) {
  11. assert := unit.Assert(t)
  12. var accountMap SocksAccountMap
  13. err := json.Unmarshal([]byte("[{\"user\": \"a\", \"pass\":\"b\"}, {\"user\": \"c\", \"pass\":\"d\"}]"), &accountMap)
  14. assert.Error(err).IsNil()
  15. assert.Bool(accountMap.HasAccount("a", "b")).IsTrue()
  16. assert.Bool(accountMap.HasAccount("a", "c")).IsFalse()
  17. assert.Bool(accountMap.HasAccount("c", "d")).IsTrue()
  18. assert.Bool(accountMap.HasAccount("e", "d")).IsTrue()
  19. }
  20. func TestDefaultIPAddress(t *testing.T) {
  21. assert := unit.Assert(t)
  22. socksConfig := jsonconfig.CreateConfig("socks", config.TypeInbound).(*SocksConfig)
  23. assert.String(socksConfig.IP().String()).Equals("127.0.0.1")
  24. }
  25. func TestIPAddressParsing(t *testing.T) {
  26. assert := unit.Assert(t)
  27. var ipAddress IPAddress
  28. err := json.Unmarshal([]byte("\"1.2.3.4\""), &ipAddress)
  29. assert.Error(err).IsNil()
  30. assert.String(net.IP(ipAddress).String()).Equals("1.2.3.4")
  31. }
  32. func TestNoAuthConfig(t *testing.T) {
  33. assert := unit.Assert(t)
  34. var config SocksConfig
  35. err := json.Unmarshal([]byte("{\"auth\":\"noauth\", \"ip\":\"8.8.8.8\"}"), &config)
  36. assert.Error(err).IsNil()
  37. assert.Bool(config.IsNoAuth()).IsTrue()
  38. assert.Bool(config.IsPassword()).IsFalse()
  39. assert.String(config.IP().String()).Equals("8.8.8.8")
  40. assert.Bool(config.UDPEnabled).IsFalse()
  41. }
  42. func TestUserPassConfig(t *testing.T) {
  43. assert := unit.Assert(t)
  44. var config SocksConfig
  45. err := json.Unmarshal([]byte("{\"auth\":\"password\", \"accounts\":[{\"user\":\"x\", \"pass\":\"y\"}], \"udp\":true}"), &config)
  46. assert.Error(err).IsNil()
  47. assert.Bool(config.IsNoAuth()).IsFalse()
  48. assert.Bool(config.IsPassword()).IsTrue()
  49. assert.Bool(config.HasAccount("x", "y")).IsTrue()
  50. assert.Bool(config.UDPEnabled).IsTrue()
  51. }