httpUtilTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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('provideCidrRanges')]
  13. public function test_checkCIDR(string $ip, string $range, bool $expected): void {
  14. $checkCIDR = new ReflectionMethod(FreshRSS_http_Util::class, 'checkCIDR');
  15. self::assertEquals($expected, $checkCIDR->invoke(null, $ip, $range));
  16. }
  17. /** @return list<array{string,string,bool}> */
  18. public static function provideCidrRanges(): array {
  19. return [
  20. // A range without a subnet is a single address
  21. ['192.168.1.1', '192.168.1.1', true],
  22. ['192.168.1.2', '192.168.1.1', false],
  23. ['2001:db8::1', '2001:db8::1', true],
  24. ['2001:db8::2', '2001:db8::1', false],
  25. // An explicit subnet keeps working
  26. ['192.168.1.1', '192.168.1.1/32', true],
  27. ['192.168.1.42', '192.168.1.0/24', true],
  28. ['192.168.2.42', '192.168.1.0/24', false],
  29. ['192.168.1.1', '0.0.0.0/0', true],
  30. ['2001:db8::1', '2001:db8::/32', true],
  31. // Invalid input is still rejected
  32. ['192.168.1.1', '', false],
  33. ['192.168.1.1', '192.168.1.1/', false],
  34. ['192.168.1.1', '192.168.1.1/33', false],
  35. ['192.168.1.1', '192.168.1.1/abc', false],
  36. ['192.168.1.1', 'gateway.localdomain', false],
  37. ['192.168.1.1', '2001:db8::1', false],
  38. ['not-an-ip', '192.168.1.1', false],
  39. ];
  40. }
  41. /** @return list<array{string,string,bool}> */
  42. public static function provideUrlsIgnoringHttps(): array {
  43. return [
  44. // Only the scheme differs → equal
  45. ['http://www.blogger.com/feeds/1/posts', 'https://www.blogger.com/feeds/1/posts', true],
  46. ['https://example.net/feed.xml?a=1&b=2', 'http://example.net/feed.xml?a=1&b=2', true],
  47. ['HTTP://Example.net/Feed', 'https://Example.net/Feed', true],
  48. ['HTTPS://Example.net/Feed', 'http://Example.net/Feed', true],
  49. // Fully identical → equal
  50. ['https://example.net/feed', 'https://example.net/feed', true],
  51. ['', '', true],
  52. // Path differs → not equal (scheme-only tolerance must not hide real mismatches)
  53. ['http://example.net/a', 'https://example.net/b', false],
  54. // Trailing slash is a path difference → not equal
  55. ['http://example.net/', 'https://example.net', false],
  56. // Host differs → not equal
  57. ['http://a.example.net/feed', 'https://b.example.net/feed', false],
  58. // Query differs → not equal
  59. ['https://example.net/feed?a=1', 'http://example.net/feed?a=2', false],
  60. // Non-http(s) schemes are compared as-is
  61. ['ftp://example.net/feed', 'https://example.net/feed', false],
  62. ];
  63. }
  64. }