httpUtilTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. /**
  5. * Tests for FreshRSS_http_Util
  6. */
  7. class httpUtilTest extends \PHPUnit\Framework\TestCase {
  8. #[DataProvider('provideUrlsIgnoringHttps')]
  9. public function test_compareUrlIgnoringHttps(string $url1, string $url2, bool $expected): void {
  10. self::assertEquals($expected, FreshRSS_http_Util::compareUrlIgnoringHttps($url1, $url2) === 0);
  11. }
  12. #[DataProvider('provideUrlsForRetryAfter')]
  13. public function test_getRetryAfterFile(string $url1, string $url2, bool $sameFile): void {
  14. $getRetryAfterFile = new ReflectionMethod(FreshRSS_http_Util::class, 'getRetryAfterFile');
  15. self::assertSame($sameFile, $getRetryAfterFile->invoke(null, $url1, '') === $getRetryAfterFile->invoke(null, $url2, ''));
  16. }
  17. /** @return array<string,array{string,string,bool}> */
  18. public static function provideUrlsForRetryAfter(): array {
  19. return [
  20. // A public server waits as a whole, per port
  21. 'public server' => ['https://198.51.100.7/feed1', 'https://198.51.100.7/feed2?a=1&b=2', true],
  22. 'public server, other port' => ['https://198.51.100.7/feed', 'https://198.51.100.7:8443/feed', false],
  23. // A server on a local network waits URL by URL
  24. 'local IP address' => ['http://192.168.1.2/feed1', 'http://192.168.1.2/feed2', false],
  25. 'local domain' => ['http://rss-bridge.lan/?bridge=A', 'http://rss-bridge.lan/?bridge=B', false],
  26. 'local, same URL' => ['http://192.168.1.2/feed', 'http://192.168.1.2/feed', true],
  27. ];
  28. }
  29. #[DataProvider('provideCidrRanges')]
  30. public function test_checkCIDR(string $ip, string $range, bool $expected): void {
  31. $checkCIDR = new ReflectionMethod(FreshRSS_http_Util::class, 'checkCIDR');
  32. self::assertEquals($expected, $checkCIDR->invoke(null, $ip, $range));
  33. }
  34. /** @return list<array{string,string,bool}> */
  35. public static function provideCidrRanges(): array {
  36. return [
  37. // A range without a subnet is a single address
  38. ['192.168.1.1', '192.168.1.1', true],
  39. ['192.168.1.2', '192.168.1.1', false],
  40. ['2001:db8::1', '2001:db8::1', true],
  41. ['2001:db8::2', '2001:db8::1', false],
  42. // An explicit subnet keeps working
  43. ['192.168.1.1', '192.168.1.1/32', true],
  44. ['192.168.1.42', '192.168.1.0/24', true],
  45. ['192.168.2.42', '192.168.1.0/24', false],
  46. ['192.168.1.1', '0.0.0.0/0', true],
  47. ['2001:db8::1', '2001:db8::/32', true],
  48. // Invalid input is still rejected
  49. ['192.168.1.1', '', false],
  50. ['192.168.1.1', '192.168.1.1/', false],
  51. ['192.168.1.1', '192.168.1.1/33', false],
  52. ['192.168.1.1', '192.168.1.1/abc', false],
  53. ['192.168.1.1', 'gateway.localdomain', false],
  54. ['192.168.1.1', '2001:db8::1', false],
  55. ['not-an-ip', '192.168.1.1', false],
  56. ];
  57. }
  58. /** @return list<array{string,string,bool}> */
  59. public static function provideUrlsIgnoringHttps(): array {
  60. return [
  61. // Only the scheme differs → equal
  62. ['http://www.blogger.com/feeds/1/posts', 'https://www.blogger.com/feeds/1/posts', true],
  63. ['https://example.net/feed.xml?a=1&b=2', 'http://example.net/feed.xml?a=1&b=2', true],
  64. ['HTTP://Example.net/Feed', 'https://Example.net/Feed', true],
  65. ['HTTPS://Example.net/Feed', 'http://Example.net/Feed', true],
  66. // Fully identical → equal
  67. ['https://example.net/feed', 'https://example.net/feed', true],
  68. ['', '', true],
  69. // Path differs → not equal (scheme-only tolerance must not hide real mismatches)
  70. ['http://example.net/a', 'https://example.net/b', false],
  71. // Trailing slash is a path difference → not equal
  72. ['http://example.net/', 'https://example.net', false],
  73. // Host differs → not equal
  74. ['http://a.example.net/feed', 'https://b.example.net/feed', false],
  75. // Query differs → not equal
  76. ['https://example.net/feed?a=1', 'http://example.net/feed?a=2', false],
  77. // Non-http(s) schemes are compared as-is
  78. ['ftp://example.net/feed', 'https://example.net/feed', false],
  79. ];
  80. }
  81. }