package fetcher import ( "github.com/stretchr/testify/assert" "net" "testing" ) func TestIsReservedAddress(test *testing.T) { reservedAddresses := []struct { name string address string }{ {"loopback ipv4", "127.0.0.1"}, {"loopback ipv4 alternate", "127.0.0.2"}, {"private 10.x", "10.0.0.1"}, {"private 10.x deep", "10.255.255.255"}, {"private 172.16.x", "172.16.0.1"}, {"private 172.31.x", "172.31.255.255"}, {"private 192.168.x", "192.168.1.1"}, {"link-local", "169.254.1.1"}, {"null address", "0.0.0.1"}, {"ipv6 loopback", "::1"}, {"ipv6 unique local fc", "fc00::1"}, {"ipv6 unique local fd", "fd00::1"}, {"ipv6 link-local", "fe80::1"}, } for _, testCase := range reservedAddresses { test.Run(testCase.name, func(test *testing.T) { parsedIP := net.ParseIP(testCase.address) assert.True(test, isReservedAddress(parsedIP), "expected %s to be reserved", testCase.address) }) } } func TestIsNotReservedAddress(test *testing.T) { publicAddresses := []struct { name string address string }{ {"google dns", "8.8.8.8"}, {"cloudflare dns", "1.1.1.1"}, {"random public", "93.184.216.34"}, {"public 172.32", "172.32.0.1"}, {"public ipv6", "2001:db8::1"}, } for _, testCase := range publicAddresses { test.Run(testCase.name, func(test *testing.T) { parsedIP := net.ParseIP(testCase.address) assert.False(test, isReservedAddress(parsedIP), "expected %s to not be reserved", testCase.address) }) } } func TestValidateFeedURLRejectsUnsupportedSchemes(test *testing.T) { unsupportedURLs := []string{ "ftp://example.com/feed", "file:///etc/passwd", "javascript:alert(1)", "data:text/html,hello", } for _, feedURL := range unsupportedURLs { test.Run(feedURL, func(test *testing.T) { assert.Error(test, ValidateFeedURL(feedURL)) }) } } func TestValidateFeedURLRejectsEmptyHostname(test *testing.T) { assert.Error(test, ValidateFeedURL("http:///path")) } func TestValidateFeedURLRejectsPrivateIPs(test *testing.T) { privateURLs := []string{ "http://127.0.0.1/feed", "https://10.0.0.1/feed", "http://192.168.1.1/feed", "http://172.16.0.1/feed", "http://[::1]/feed", } for _, feedURL := range privateURLs { test.Run(feedURL, func(test *testing.T) { assert.Error(test, ValidateFeedURL(feedURL)) }) } } func TestValidateFeedURLAcceptsPublicIPs(test *testing.T) { publicURLs := []string{ "http://93.184.216.34/feed", "https://8.8.8.8/feed", } for _, feedURL := range publicURLs { test.Run(feedURL, func(test *testing.T) { assert.NoError(test, ValidateFeedURL(feedURL)) }) } } func TestValidateFeedURLAcceptsHTTPAndHTTPS(test *testing.T) { assert.NoError(test, ValidateFeedURL("http://93.184.216.34/feed")) assert.NoError(test, ValidateFeedURL("https://93.184.216.34/feed")) } func TestValidateFeedURLRejectsInvalidURL(test *testing.T) { assert.Error(test, ValidateFeedURL("://broken")) }