url_safety_test.go 997 B

123456789101112131415161718192021222324252627282930313233343536
  1. package service
  2. import (
  3. "net"
  4. "testing"
  5. )
  6. func TestRejectAllBlockedIPsNeedsOnlyOneUsableAddress(t *testing.T) {
  7. teredo := net.IPAddr{IP: net.ParseIP("2001::1")}
  8. public4 := net.IPAddr{IP: net.ParseIP("142.250.74.36")}
  9. private4 := net.IPAddr{IP: net.ParseIP("10.0.0.1")}
  10. cases := []struct {
  11. name string
  12. ips []net.IPAddr
  13. wantErr string
  14. }{
  15. {"poisoned AAAA next to healthy A", []net.IPAddr{teredo, public4}, ""},
  16. {"all blocked", []net.IPAddr{teredo, private4}, "host h.example resolves to blocked private/internal address 2001::1"},
  17. {"no addresses", nil, "host h.example has no IP addresses"},
  18. }
  19. for _, tc := range cases {
  20. t.Run(tc.name, func(t *testing.T) {
  21. err := rejectAllBlockedIPs("h.example", tc.ips)
  22. if tc.wantErr == "" {
  23. if err != nil {
  24. t.Fatalf("rejectAllBlockedIPs() = %v, want nil", err)
  25. }
  26. return
  27. }
  28. if err == nil || err.Error() != tc.wantErr {
  29. t.Fatalf("rejectAllBlockedIPs() = %v, want %q", err, tc.wantErr)
  30. }
  31. })
  32. }
  33. }