config_test.go 1.9 KB

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