<?phpnamespace Tests;use App\Model\Address;use App\Model\Connection;use App\Model\LocalNetwork;use PHPUnit\Framework\TestCase;/** Addresses checked without the filter extension (the browser's PHP has none: the VNC console died on filter_var()). */final class AddressTest extends TestCase{ public function testIpAddressesAreToldApartFromNames(): void { foreach (['192.168.1.10', '10.0.0.1', '255.255.255.255', '::1', 'fe80::1', '2001:db8::8a2e:370:7334', '::ffff:10.0.0.1', '[fe80::1]', '2001:0db8:0000:0000:0000:ff00:0042:8329'] as $ip) { self::assertTrue(Address::isIp($ip), $ip); } foreach (['host01.example.prod', 'xcp-ng-01', '256.1.1.1', '1.2.3', '01.2.3.4', 'fe80:::1', '1::2::3', 'abcd', ''] as $name) { self::assertFalse(Address::isIp($name), $name); } } public function testMailAddresses(): void { self::assertTrue(Address::isEmail('[email protected]')); self::assertTrue(Address::isEmail('[email protected]')); self::assertFalse(Address::isEmail('ops@')); self::assertFalse(Address::isEmail('ops example.com')); self::assertFalse(Address::isEmail('ops@localhost')); } public function testTheConsoleNamingStillWorks(): void { self::assertSame('host02.example.prod', Connection::sameNaming('host01.example.prod', ['host01'], ['host02'])); self::assertNull(Connection::sameNaming('192.168.1.11', ['host01'], ['host02']), 'an address typed: no naming to apply'); } public function testLocalAddressesAreTheOnesTheServerBridgeCannotReach(): void { foreach (['192.168.1.10', '10.0.0.1', '172.16.0.5', '172.31.255.254', '127.0.0.1', '169.254.10.1', '100.64.0.1', '::1', '[::1]', 'fe80::1', 'fd12:3456::1', 'fc00::1', '::ffff:192.168.0.2', 'localhost', 'xcp-ng-01', 'xcp.local', 'pool.lan', 'nas.home', 'xen.internal', 'host.localdomain', 'box.home.arpa', 'XCP.LAN.'] as $local) { self::assertTrue(Address::isLocal($local), $local); } foreach (['8.8.8.8', '172.32.0.1', '172.15.0.1', '192.169.1.1', '100.128.0.1', '2001:db8::1', '::ffff:8.8.8.8', 'host01.example.prod', 'xcp-ng-01.example.com', 'local.example.com', ''] as $public) { self::assertFalse(Address::isLocal($public), $public); } } public function testALocalServerInTheBrowserNeedsTheRuntimeSwitchedToLocal(): void { $ready = \JumPHP\Net\RuntimeLink::READY; // the desktop, the demo pool, a public address, the local network on: nothing to say self::assertNull(LocalNetwork::problem('192.168.1.10', 'http', false, 'missing', false)); self::assertNull(LocalNetwork::problem('xcp-demo.local', 'demo', true, 'missing', false)); self::assertNull(LocalNetwork::problem('xcp.example.com', 'http', true, 'missing', false)); self::assertNull(LocalNetwork::problem('192.168.1.10', 'http', true, $ready, false)); // no runtime: install it $text = (string)LocalNetwork::problem('192.168.1.10', 'http', true, 'missing', false); self::assertStringContainsString("'192.168.1.10' is an address of your local network", $text); self::assertStringContainsString('Install and start the JumPHP runtime', $text); self::assertStringContainsString('(required)', $text); // the SSH tunnel goes through the same bridge self::assertNotNull(LocalNetwork::problem('10.0.0.2', 'ssh', true, 'missing', false)); // installed, not allowed yet self::assertStringContainsString('allow this app', (string)LocalNetwork::problem('10.0.0.2', 'http', true, 'running', false)); // ready, but the badge's switch on Server self::assertStringContainsString('switch it to "Local"', (string)LocalNetwork::problem('10.0.0.2', 'http', true, $ready, true)); self::assertSame(LocalNetwork::STEP_SWITCH, LocalNetwork::step($ready, true)); self::assertSame('Install the JumPHP runtime…', LocalNetwork::buttonText('missing')); }}