config_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. }
  19. func TestDefaultIPAddress(t *testing.T) {
  20. assert := unit.Assert(t)
  21. socksConfig := jsonconfig.CreateConfig("socks", config.TypeInbound).(*SocksConfig)
  22. assert.String(socksConfig.IP().String()).Equals("127.0.0.1")
  23. }
  24. func TestIPAddressParsing(t *testing.T) {
  25. assert := unit.Assert(t)
  26. var ipAddress IPAddress
  27. err := json.Unmarshal([]byte("\"1.2.3.4\""), &ipAddress)
  28. assert.Error(err).IsNil()
  29. assert.String(net.IP(ipAddress).String()).Equals("1.2.3.4")
  30. }
  31. func TestNoAuthConfig(t *testing.T) {
  32. assert := unit.Assert(t)
  33. var config SocksConfig
  34. err := json.Unmarshal([]byte("{\"auth\":\"noauth\", \"ip\":\"8.8.8.8\"}"), &config)
  35. assert.Error(err).IsNil()
  36. assert.Bool(config.IsNoAuth()).IsTrue()
  37. assert.Bool(config.IsPassword()).IsFalse()
  38. assert.String(config.IP().String()).Equals("8.8.8.8")
  39. assert.Bool(config.UDPEnabled).IsFalse()
  40. }
  41. func TestUserPassConfig(t *testing.T) {
  42. assert := unit.Assert(t)
  43. var config SocksConfig
  44. err := json.Unmarshal([]byte("{\"auth\":\"password\", \"accounts\":[{\"user\":\"x\", \"pass\":\"y\"}], \"udp\":true}"), &config)
  45. assert.Error(err).IsNil()
  46. assert.Bool(config.IsNoAuth()).IsFalse()
  47. assert.Bool(config.IsPassword()).IsTrue()
  48. assert.Bool(config.HasAccount("x", "y")).IsTrue()
  49. assert.Bool(config.UDPEnabled).IsTrue()
  50. }