Files
coolify/tests/Unit/ValidHostnameTest.php
Andras Bacsai 6ae45684f9 feat(v5): add server reconciliation and canvas APIs
Split V5 dashboard behavior into domain controllers and policies,
add agent token rotation/revocation, status reconciliation jobs,
ingress firewall syncing, and canvas connection APIs.

Add migrations for V5 status tracking, server capabilities, resource
connection aliases, and revoked agent tokens.
2026-07-06 17:40:37 +02:00

81 lines
3.3 KiB
PHP

<?php
use App\Rules\ValidHostname;
it('accepts valid RFC 1123 hostnames', function (string $hostname) {
$rule = new ValidHostname;
$failCalled = false;
$rule->validate('server_name', $hostname, function () use (&$failCalled) {
$failCalled = true;
});
expect($failCalled)->toBeFalse();
})->with([
'simple hostname' => 'myserver',
'hostname with hyphen' => 'my-server',
'hostname with numbers' => 'server123',
'hostname starting with number' => '123server',
'all numeric hostname' => '12345',
'fqdn' => 'server.example.com',
'subdomain' => 'web.app.example.com',
'max label length' => str_repeat('a', 63),
'max total length' => str_repeat('a', 63).'.'.str_repeat('b', 63).'.'.str_repeat('c', 63).'.'.str_repeat('d', 59),
'uppercase hostname' => 'MyServer',
'mixed case fqdn' => 'MyServer.Example.COM',
]);
it('rejects invalid RFC 1123 hostnames', function (string $hostname, string $expectedError) {
$rule = new ValidHostname;
$failCalled = false;
$errorMessage = '';
$rule->validate('server_name', $hostname, function ($message) use (&$failCalled, &$errorMessage) {
$failCalled = true;
$errorMessage = $message;
});
expect($failCalled)->toBeTrue();
expect($errorMessage)->toContain($expectedError);
})->with([
'underscore' => ['my_server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'starts with hyphen' => ['-myserver', 'cannot start or end with a hyphen'],
'ends with hyphen' => ['myserver-', 'cannot start or end with a hyphen'],
'starts with dot' => ['.myserver', 'cannot start or end with a dot'],
'ends with dot' => ['myserver.', 'cannot start or end with a dot'],
'consecutive dots' => ['my..server', 'consecutive dots'],
'too long total' => [str_repeat('a', 254), 'must not exceed 253 characters'],
'label too long' => [str_repeat('a', 64), 'must be 1-63 characters'],
'empty label' => ['my..server', 'consecutive dots'],
'special characters' => ['my@server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'space' => ['my server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'shell metacharacters' => ['my;server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'newline before dot' => ["evil\n.com", 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'newline inside label' => ["evil\ncom", 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'carriage return' => ["evil\r.com", 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'tab character' => ["evil\t.com", 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'caddy block injection' => ["evil.com {\n} http://x", 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
]);
it('accepts empty hostname', function () {
$rule = new ValidHostname;
$failCalled = false;
$rule->validate('server_name', '', function () use (&$failCalled) {
$failCalled = true;
});
expect($failCalled)->toBeFalse();
});
it('trims whitespace before validation', function () {
$rule = new ValidHostname;
$failCalled = false;
$rule->validate('server_name', ' myserver ', function () use (&$failCalled) {
$failCalled = true;
});
expect($failCalled)->toBeFalse();
});