fieldrule_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package json
  2. import (
  3. "testing"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. v2testing "github.com/v2ray/v2ray-core/testing"
  6. "github.com/v2ray/v2ray-core/testing/assert"
  7. )
  8. func TestDomainMatching(t *testing.T) {
  9. v2testing.Current(t)
  10. rawJson := `{
  11. "type": "field",
  12. "domain": ["google.com", "regexp:v2ray.com$"],
  13. "tag": "test"
  14. }`
  15. rule := parseRule([]byte(rawJson))
  16. dest := v2net.TCPDestination(v2net.DomainAddress("www.v2ray.com"), 80)
  17. assert.Bool(rule.Apply(dest)).IsTrue()
  18. }
  19. func TestPortMatching(t *testing.T) {
  20. v2testing.Current(t)
  21. rule := &FieldRule{
  22. Port: &v2net.PortRange{
  23. From: 0,
  24. To: 100,
  25. },
  26. }
  27. dest := v2net.TCPDestination(v2net.DomainAddress("www.v2ray.com"), 80)
  28. assert.Bool(rule.Apply(dest)).IsTrue()
  29. }
  30. func TestIPMatching(t *testing.T) {
  31. v2testing.Current(t)
  32. rawJson := `{
  33. "type": "field",
  34. "ip": "10.0.0.0/8",
  35. "tag": "test"
  36. }`
  37. rule := parseRule([]byte(rawJson))
  38. dest := v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)
  39. assert.Bool(rule.Apply(dest)).IsTrue()
  40. }
  41. func TestIPListMatching(t *testing.T) {
  42. v2testing.Current(t)
  43. rawJson := `{
  44. "type": "field",
  45. "ip": ["10.0.0.0/8", "192.168.0.0/16"],
  46. "tag": "test"
  47. }`
  48. rule := parseRule([]byte(rawJson))
  49. dest := v2net.TCPDestination(v2net.IPAddress([]byte{192, 168, 1, 1}), 80)
  50. assert.Bool(rule.Apply(dest)).IsTrue()
  51. }
  52. func TestPortNotMatching(t *testing.T) {
  53. v2testing.Current(t)
  54. rawJson := `{
  55. "type": "field",
  56. "port": "80-100",
  57. "tag": "test"
  58. }`
  59. rule := parseRule([]byte(rawJson))
  60. dest := v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 79)
  61. assert.Bool(rule.Apply(dest)).IsFalse()
  62. }
  63. func TestDomainNotMatching(t *testing.T) {
  64. v2testing.Current(t)
  65. rawJson := `{
  66. "type": "field",
  67. "domain": ["google.com", "v2ray.com"],
  68. "tag": "test"
  69. }`
  70. rule := parseRule([]byte(rawJson))
  71. dest := v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)
  72. assert.Bool(rule.Apply(dest)).IsFalse()
  73. }
  74. func TestDomainNotMatchingDomain(t *testing.T) {
  75. v2testing.Current(t)
  76. rawJson := `{
  77. "type": "field",
  78. "domain": ["google.com", "v2ray.com"],
  79. "tag": "test"
  80. }`
  81. rule := parseRule([]byte(rawJson))
  82. dest := v2net.TCPDestination(v2net.DomainAddress("baidu.com"), 80)
  83. assert.Bool(rule.Apply(dest)).IsFalse()
  84. dest = v2net.TCPDestination(v2net.DomainAddress("www.google.com"), 80)
  85. assert.Bool(rule.Apply(dest)).IsTrue()
  86. }