fieldrule_test.go 1005 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. }