View source
<?php
namespace Drupal\Tests\Core\DrupalKernel;
use Drupal\Core\DrupalKernel;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
class ValidateHostnameTest extends UnitTestCase {
public function testValidateHostname($hostname, $message, $expected = FALSE) {
$server = [
'HTTP_HOST' => $hostname,
];
$request = new Request([], [], [], [], [], $server);
$validated_hostname = DrupalKernel::validateHostname($request);
$this
->assertSame($expected, $validated_hostname, $message);
}
public function providerTestValidateHostname() {
$data = [];
$data[] = [
'security/.drupal.org:80',
'HTTP_HOST with / is invalid',
];
$data[] = [
'security/.drupal.org:80',
'HTTP_HOST with / is invalid',
];
$data[] = [
'security\\.drupal.org:80',
'HTTP_HOST with \\ is invalid',
];
$data[] = [
'security<.drupal.org:80',
'HTTP_HOST with < is invalid',
];
$data[] = [
'security..drupal.org:80',
'HTTP_HOST with .. is invalid',
];
$data[] = [
str_repeat('x', 1000) . '.security.drupal.org:80',
'HTTP_HOST with more than 1000 characters is invalid.',
];
$data[] = [
str_repeat('x.', 100) . 'security.drupal.org:80',
'HTTP_HOST with more than 100 subdomains is invalid.',
];
$data[] = [
'security.drupal.org:80' . str_repeat(':x', 100),
'HTTP_HOST with more than 100 port separators is invalid.',
];
$data[] = [
'security.drupal.org:80',
'Properly formed HTTP_HOST is valid.',
TRUE,
];
$data[] = [
'72.21.91.99:80',
'Properly formed HTTP_HOST with IPv4 address valid.',
TRUE,
];
$data[] = [
'2607:f8b0:4004:803::1002:80',
'Properly formed HTTP_HOST with IPv6 address valid.',
TRUE,
];
$data[] = [
'[::1]:80',
'HTTP_HOST containing IPv6 loopback is valid.',
TRUE,
];
return $data;
}
}