fieldrule_test.go 2.9 KB

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