fieldrule_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package json
  2. import (
  3. "encoding/json"
  4. "testing"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  7. v2testing "github.com/v2ray/v2ray-core/testing"
  8. "github.com/v2ray/v2ray-core/testing/assert"
  9. )
  10. func TestStringListParsingList(t *testing.T) {
  11. v2testing.Current(t)
  12. rawJson := `["a", "b", "c", "d"]`
  13. var strList StringList
  14. err := json.Unmarshal([]byte(rawJson), &strList)
  15. assert.Error(err).IsNil()
  16. assert.Int(strList.Len()).Equals(4)
  17. }
  18. func TestStringListParsingString(t *testing.T) {
  19. v2testing.Current(t)
  20. rawJson := `"abcd"`
  21. var strList StringList
  22. err := json.Unmarshal([]byte(rawJson), &strList)
  23. assert.Error(err).IsNil()
  24. assert.Int(strList.Len()).Equals(1)
  25. }
  26. func TestDomainMatching(t *testing.T) {
  27. v2testing.Current(t)
  28. rawJson := `{
  29. "type": "field",
  30. "domain": ["google.com", "regexp:v2ray.com$"],
  31. "tag": "test"
  32. }`
  33. rule := parseRule([]byte(rawJson))
  34. dest := v2net.TCPDestination(v2net.DomainAddress("www.v2ray.com"), 80)
  35. assert.Bool(rule.Apply(dest)).IsTrue()
  36. }
  37. func TestPortMatching(t *testing.T) {
  38. v2testing.Current(t)
  39. rule := &FieldRule{
  40. Port: &v2nettesting.PortRange{
  41. FromValue: 0,
  42. ToValue: 100,
  43. },
  44. }
  45. dest := v2net.TCPDestination(v2net.DomainAddress("www.v2ray.com"), 80)
  46. assert.Bool(rule.Apply(dest)).IsTrue()
  47. }
  48. func TestIPMatching(t *testing.T) {
  49. v2testing.Current(t)
  50. rawJson := `{
  51. "type": "field",
  52. "ip": "10.0.0.0/8",
  53. "tag": "test"
  54. }`
  55. rule := parseRule([]byte(rawJson))
  56. dest := v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)
  57. assert.Bool(rule.Apply(dest)).IsTrue()
  58. }
  59. func TestIPListMatching(t *testing.T) {
  60. v2testing.Current(t)
  61. rawJson := `{
  62. "type": "field",
  63. "ip": ["10.0.0.0/8", "192.168.0.0/16"],
  64. "tag": "test"
  65. }`
  66. rule := parseRule([]byte(rawJson))
  67. dest := v2net.TCPDestination(v2net.IPAddress([]byte{192, 168, 1, 1}), 80)
  68. assert.Bool(rule.Apply(dest)).IsTrue()
  69. }
  70. func TestPortNotMatching(t *testing.T) {
  71. v2testing.Current(t)
  72. rawJson := `{
  73. "type": "field",
  74. "port": "80-100",
  75. "tag": "test"
  76. }`
  77. rule := parseRule([]byte(rawJson))
  78. dest := v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 79)
  79. assert.Bool(rule.Apply(dest)).IsFalse()
  80. }
  81. func TestDomainNotMatching(t *testing.T) {
  82. v2testing.Current(t)
  83. rawJson := `{
  84. "type": "field",
  85. "domain": ["google.com", "v2ray.com"],
  86. "tag": "test"
  87. }`
  88. rule := parseRule([]byte(rawJson))
  89. dest := v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)
  90. assert.Bool(rule.Apply(dest)).IsFalse()
  91. }
  92. func TestDomainNotMatchingDomain(t *testing.T) {
  93. v2testing.Current(t)
  94. rawJson := `{
  95. "type": "field",
  96. "domain": ["google.com", "v2ray.com"],
  97. "tag": "test"
  98. }`
  99. rule := parseRule([]byte(rawJson))
  100. dest := v2net.TCPDestination(v2net.DomainAddress("baidu.com"), 80)
  101. assert.Bool(rule.Apply(dest)).IsFalse()
  102. dest = v2net.TCPDestination(v2net.DomainAddress("www.google.com"), 80)
  103. assert.Bool(rule.Apply(dest)).IsTrue()
  104. }