diff options
| author | Simon Rettberg | 2026-04-29 14:12:46 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2026-04-29 14:12:46 +0200 |
| commit | e9dd3b47e64f43d967a08cfc78efdffa95130a95 (patch) | |
| tree | 7320ae4709724ccd769ebf9a6368565640afe85d /tests/Inc | |
| parent | [runmode] Add UUID to selected clients, close dropdown on select (diff) | |
| parent | [locationinfo] Use dedicated list permission for extdevices (diff) | |
| download | slx-admin-e9dd3b47e64f43d967a08cfc78efdffa95130a95.tar.gz slx-admin-e9dd3b47e64f43d967a08cfc78efdffa95130a95.tar.xz slx-admin-e9dd3b47e64f43d967a08cfc78efdffa95130a95.zip | |
Merge branch 'master' of git.openslx.org:openslx-ng/slx-admin
Diffstat (limited to 'tests/Inc')
| -rw-r--r-- | tests/Inc/GetClientIpTest.php | 89 | ||||
| -rw-r--r-- | tests/Inc/IpUtilTest.php | 27 |
2 files changed, 116 insertions, 0 deletions
diff --git a/tests/Inc/GetClientIpTest.php b/tests/Inc/GetClientIpTest.php new file mode 100644 index 00000000..22287ff3 --- /dev/null +++ b/tests/Inc/GetClientIpTest.php @@ -0,0 +1,89 @@ +<?php + +use PHPUnit\Framework\TestCase; + +class GetClientIpTest extends TestCase +{ + protected function setUp(): void + { + Property::reset(); + unset($_SERVER['REMOTE_ADDR']); + unset($_SERVER['HTTP_X_FORWARDED_FOR']); + Util::getClientIp(true); + } + + + public function testGetClientIpDirect(): void + { + $_SERVER['REMOTE_ADDR'] = '1.2.3.4'; + $this->assertSame('1.2.3.4', Util::getClientIp()); + } + + public function testGetClientIpTrustedProxy(): void + { + $_SERVER['REMOTE_ADDR'] = '10.0.0.1'; + $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.2.3.4'; + Property::$values['webinterface.proxies-trusted'] = json_encode(['10.0.0.1' => true]); + + $this->assertSame('1.2.3.4', Util::getClientIp()); + } + + public function testGetClientIpUntrustedProxy(): void + { + $_SERVER['REMOTE_ADDR'] = '10.2'; // Not in trusted list + $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.2.3.4'; + Property::$values['webinterface.proxies-trusted'] = json_encode(['10.0.0.1' => true]); + + $this->assertSame('10.0.0.2', Util::getClientIp()); + } + + public function testGetClientIpChain(): void + { + // Chain: Client (1.1.1.1) -> Proxy1 (2.2.2.2) -> Proxy2 (3.3.3.3) -> WebServer + $_SERVER['REMOTE_ADDR'] = '::ffff:3.3.3.3'; + $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.1.1.1, 2.2.2.2'; + Property::$values['webinterface.proxies-trusted'] = json_encode([ + '3.3.3.3' => true, + '2.2.2.2' => true + ]); + + // We want it to return '1.1.1.1' + $this->assertSame('1.1.1.1', Util::getClientIp()); + } + + public function testGetClientIpLongChain(): void + { + // Chain: Client (1.1.1.1) -> UntrustedProxy (4.4.4.4) -> Proxy1 (2.2.2.2) -> Proxy2 (3.3.3.3) -> WebServer + $_SERVER['REMOTE_ADDR'] = '3.3.3.3'; + $_SERVER['HTTP_X_FORWARDED_FOR'] = '9.9.9.9, 1.1.1.1, 4.4.4.4, ::ffff:2.2.2.2'; + Property::$values['webinterface.proxies-trusted'] = json_encode([ + '1.1.1.1' => true, + '3.3.3.3' => true, + '2.2.2.2' => true + ]); + + // Should return 4.4.4.4 because it's the first untrusted from the right + $this->assertSame('4.4.4.4', Util::getClientIp()); + } + + public function testGetClientIpIpv6Mapping(): void + { + $_SERVER['REMOTE_ADDR'] = '::ffff:10.0.0.1'; + $_SERVER['HTTP_X_FORWARDED_FOR'] = '::ffff:1.2.3.4'; + Property::$values['webinterface.proxies-trusted'] = json_encode(['10.0.0.1' => true]); + + $this->assertSame('1.2.3.4', Util::getClientIp()); + } + + public function testGetClientIpIpv6MappingInChain(): void + { + $_SERVER['REMOTE_ADDR'] = '3.3.3.3'; + $_SERVER['HTTP_X_FORWARDED_FOR'] = '9.9.9.9, ::ffff:1.1.1.1, ::ffff:2.2.2.2'; + Property::$values['webinterface.proxies-trusted'] = json_encode([ + '3.3.3.3' => true, + '2.2.2.2' => true + ]); + + $this->assertSame('1.1.1.1', Util::getClientIp()); + } +} diff --git a/tests/Inc/IpUtilTest.php b/tests/Inc/IpUtilTest.php index 8c65a0c3..11f30a5c 100644 --- a/tests/Inc/IpUtilTest.php +++ b/tests/Inc/IpUtilTest.php @@ -57,4 +57,31 @@ class IpUtilTest extends TestCase IpUtil::rangeToCidr($start, $end) ); } + + public function testNormalizeIp(): void + { + $this->assertSame('1.0.0.1', IpUtil::normalizeIp('1.1')); + $this->assertSame('1.2.2.88', IpUtil::normalizeIp('1.2.600')); + $this->assertSame('1.219.1.1', IpUtil::normalizeIp('1.0333.1.1')); + $this->assertSame('127.0.0.1', IpUtil::normalizeIp('::ffff:127.0.0.1')); + $this->assertSame('127.0.0.1', IpUtil::normalizeIp('0x7f.0.0.1')); + $this->assertSame('127.0.0.1', IpUtil::normalizeIp('0177.0.0.1')); + $this->assertSame('127.0.0.1', IpUtil::normalizeIp('2130706433')); + $this->assertSame('127.0.0.1', IpUtil::normalizeIp('0x7f000001')); + $this->assertSame('::1', IpUtil::normalizeIp('::1')); + $this->assertNull(IpUtil::normalizeIp('256.1.1.1')); + $this->assertNull(IpUtil::normalizeIp('1.2.3.4.5')); + $this->assertNull(IpUtil::normalizeIp('0x100000000')); + $this->assertNull(IpUtil::normalizeIp('not an ip')); + $this->assertNull(IpUtil::normalizeIp('::ffff:256.1.1.1')); + } + + public function testIsValidIp(): void + { + $this->assertTrue(IpUtil::isValidIp('1.1')); + $this->assertTrue(IpUtil::isValidIp('127.0.0.1')); + $this->assertTrue(IpUtil::isValidIp('::1')); + $this->assertFalse(IpUtil::isValidIp('256.1.1.1')); + $this->assertFalse(IpUtil::isValidIp('not an ip')); + } } |
