ip_limit_allowlist_agreement_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package job
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  5. )
  6. // Save-time validation and scan-time parsing must accept exactly the same set:
  7. // anything the validator lets through and the parser drops is silently unprotected.
  8. func TestAllowlistValidatorAndParserAgree(t *testing.T) {
  9. for _, entry := range []string{
  10. "198.51.100.7",
  11. "198.51.100.0/24",
  12. "2001:db8::1",
  13. "2001:db8::/32",
  14. "198.51.100.0/024",
  15. "not-an-address",
  16. "198.51.100.0/33",
  17. } {
  18. accepted := entity.CheckNetipAddrOrPrefixList(entry, "invalid:") == nil
  19. parsed := len(parseIpLimitAllowlist(entry).prefixes)+len(parseIpLimitAllowlist(entry).addrs) > 0
  20. if accepted != parsed {
  21. t.Errorf("%q: validator=%v parser=%v — a disagreement leaves the entry silently unprotected", entry, accepted, parsed)
  22. }
  23. }
  24. }
  25. // An IPv4-mapped prefix used to parse but never match, because contains() unmaps
  26. // the queried address and Prefix.Contains is false across bit lengths.
  27. func TestAllowlistMatchesIPv4MappedPrefix(t *testing.T) {
  28. list := parseIpLimitAllowlist("::ffff:198.51.100.0/120")
  29. if !list.contains("198.51.100.5") {
  30. t.Fatal("an IPv4-mapped entry matched nothing: it protects no one")
  31. }
  32. }