fieldrule_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. rule := &FieldRule{
  29. Domain: NewStringList("v2ray.com"),
  30. }
  31. dest := v2net.NewTCPDestination(v2net.DomainAddress("www.v2ray.com", 80))
  32. assert.Bool(rule.Apply(dest)).IsTrue()
  33. }
  34. func TestPortMatching(t *testing.T) {
  35. v2testing.Current(t)
  36. rule := &FieldRule{
  37. Port: &v2nettesting.PortRange{
  38. FromValue: 0,
  39. ToValue: 100,
  40. },
  41. }
  42. dest := v2net.NewTCPDestination(v2net.DomainAddress("www.v2ray.com", 80))
  43. assert.Bool(rule.Apply(dest)).IsTrue()
  44. }
  45. func TestIPMatching(t *testing.T) {
  46. v2testing.Current(t)
  47. rawJson := `{
  48. "type": "field",
  49. "ip": "10.0.0.0/8",
  50. "tag": "test"
  51. }`
  52. rule := parseRule([]byte(rawJson))
  53. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 80))
  54. assert.Bool(rule.Apply(dest)).IsTrue()
  55. }
  56. func TestIPListMatching(t *testing.T) {
  57. v2testing.Current(t)
  58. rawJson := `{
  59. "type": "field",
  60. "ip": ["10.0.0.0/8", "192.168.0.0/16"],
  61. "tag": "test"
  62. }`
  63. rule := parseRule([]byte(rawJson))
  64. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{192, 168, 1, 1}, 80))
  65. assert.Bool(rule.Apply(dest)).IsTrue()
  66. }
  67. func TestPortNotMatching(t *testing.T) {
  68. v2testing.Current(t)
  69. rawJson := `{
  70. "type": "field",
  71. "port": "80-100",
  72. "tag": "test"
  73. }`
  74. rule := parseRule([]byte(rawJson))
  75. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 79))
  76. assert.Bool(rule.Apply(dest)).IsFalse()
  77. }
  78. func TestDomainNotMatching(t *testing.T) {
  79. v2testing.Current(t)
  80. rawJson := `{
  81. "type": "field",
  82. "domain": ["google.com", "v2ray.com"],
  83. "tag": "test"
  84. }`
  85. rule := parseRule([]byte(rawJson))
  86. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 80))
  87. assert.Bool(rule.Apply(dest)).IsFalse()
  88. }
  89. func TestDomainNotMatchingDomain(t *testing.T) {
  90. v2testing.Current(t)
  91. rawJson := `{
  92. "type": "field",
  93. "domain": ["google.com", "v2ray.com"],
  94. "tag": "test"
  95. }`
  96. rule := parseRule([]byte(rawJson))
  97. dest := v2net.NewTCPDestination(v2net.DomainAddress("baidu.com", 80))
  98. assert.Bool(rule.Apply(dest)).IsFalse()
  99. }