config_json_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // +build json
  2. package router_test
  3. import (
  4. "testing"
  5. . "v2ray.com/core/app/router"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/testing/assert"
  8. )
  9. func TestDomainRule(t *testing.T) {
  10. assert := assert.On(t)
  11. rule := ParseRule([]byte(`{
  12. "type": "field",
  13. "domain": [
  14. "ooxx.com",
  15. "oxox.com",
  16. "regexp:\\.cn$"
  17. ],
  18. "network": "tcp",
  19. "outboundTag": "direct"
  20. }`))
  21. assert.Pointer(rule).IsNotNil()
  22. cond, err := rule.BuildCondition()
  23. assert.Error(err).IsNil()
  24. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80))).IsTrue()
  25. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.DomainAddress("www.aabb.com"), 80))).IsFalse()
  26. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80))).IsFalse()
  27. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.DomainAddress("www.12306.cn"), 80))).IsTrue()
  28. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.DomainAddress("www.acn.com"), 80))).IsFalse()
  29. }
  30. func TestIPRule(t *testing.T) {
  31. assert := assert.On(t)
  32. rule := ParseRule([]byte(`{
  33. "type": "field",
  34. "ip": [
  35. "10.0.0.0/8",
  36. "192.0.0.0/24"
  37. ],
  38. "network": "tcp",
  39. "outboundTag": "direct"
  40. }`))
  41. assert.Pointer(rule).IsNotNil()
  42. cond, err := rule.BuildCondition()
  43. assert.Error(err).IsNil()
  44. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80))).IsFalse()
  45. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80))).IsTrue()
  46. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80))).IsFalse()
  47. assert.Bool(cond.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80))).IsTrue()
  48. }