fieldrule_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package json
  2. import (
  3. "testing"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  6. "github.com/v2ray/v2ray-core/testing/unit"
  7. )
  8. func TestDomainMatching(t *testing.T) {
  9. assert := unit.Assert(t)
  10. rule := &FieldRule{
  11. Domain: "v2ray.com",
  12. }
  13. dest := v2net.NewTCPDestination(v2net.DomainAddress("www.v2ray.com", 80))
  14. assert.Bool(rule.Apply(dest)).IsTrue()
  15. }
  16. func TestPortMatching(t *testing.T) {
  17. assert := unit.Assert(t)
  18. rule := &FieldRule{
  19. Port: &v2nettesting.PortRange{
  20. FromValue: 0,
  21. ToValue: 100,
  22. },
  23. }
  24. dest := v2net.NewTCPDestination(v2net.DomainAddress("www.v2ray.com", 80))
  25. assert.Bool(rule.Apply(dest)).IsTrue()
  26. }
  27. func TestIPMatching(t *testing.T) {
  28. assert := unit.Assert(t)
  29. rawJson := `{
  30. "type": "field",
  31. "ip": "10.0.0.0/8",
  32. "tag": "test"
  33. }`
  34. rule := parseRule([]byte(rawJson))
  35. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 80))
  36. assert.Bool(rule.Apply(dest)).IsTrue()
  37. }
  38. func TestPortNotMatching(t *testing.T) {
  39. assert := unit.Assert(t)
  40. rawJson := `{
  41. "type": "field",
  42. "port": "80-100",
  43. "tag": "test"
  44. }`
  45. rule := parseRule([]byte(rawJson))
  46. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 79))
  47. assert.Bool(rule.Apply(dest)).IsFalse()
  48. }
  49. func TestDomainNotMatching(t *testing.T) {
  50. assert := unit.Assert(t)
  51. rawJson := `{
  52. "type": "field",
  53. "domain": "google.com",
  54. "tag": "test"
  55. }`
  56. rule := parseRule([]byte(rawJson))
  57. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}, 79))
  58. assert.Bool(rule.Apply(dest)).IsFalse()
  59. }