fieldrule_test.go 2.4 KB

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