You are here

class CspSettingsFormTest in Content-Security-Policy 8

Test CSP Settings Form.

@coversDefaultClass \Drupal\csp\Form\CspSettingsForm @group csp

Hierarchy

Expanded class hierarchy of CspSettingsFormTest

File

tests/src/Unit/Form/CspSettingsFormTest.php, line 14

Namespace

Drupal\Tests\csp\Unit\Form
View source
class CspSettingsFormTest extends UnitTestCase {

  /**
   * Data provider of URLs for host source validity.
   *
   * @return array[]
   *   An array of [URL, isValid] tuples.
   */
  public function urlDataProvider() {
    return [
      'tld' => [
        'com',
        FALSE,
      ],
      'wildcard_tld' => [
        '*.com',
        FALSE,
      ],
      'bare' => [
        'example.com',
        TRUE,
      ],
      'bare_port' => [
        'example.com:1234',
        TRUE,
      ],
      'bare_path' => [
        'example.com/baz',
        TRUE,
      ],
      'empty_path' => [
        'example.com/',
        TRUE,
      ],
      'bare_path_query' => [
        'example.com/baz?foo=false',
        FALSE,
      ],
      'bare_wild_subdomain' => [
        '*.example.com',
        TRUE,
      ],
      'inner_wild_subdomain' => [
        'foo.*.example.com',
        FALSE,
      ],
      'wild_tld' => [
        'example.*',
        FALSE,
      ],
      'subdomain' => [
        'foo.example.com',
        TRUE,
      ],
      'subdomains' => [
        'foo.bar.example.com',
        TRUE,
      ],
      'subdomains_path' => [
        'foo.bar.example.com/baz',
        TRUE,
      ],
      'http' => [
        'http://example.com',
        TRUE,
      ],
      'https' => [
        'https://example.com',
        TRUE,
      ],
      'ws' => [
        'ws://example.com',
        TRUE,
      ],
      'wss' => [
        'wss://example.com',
        TRUE,
      ],
      'https_port' => [
        'https://example.com:1234',
        TRUE,
      ],
      'https_port_path' => [
        'https://example.com:1234/baz',
        TRUE,
      ],
      'https_wild_subdomain' => [
        'https://*.example.com',
        TRUE,
      ],
      'ipv4' => [
        '192.168.0.1',
        TRUE,
      ],
      'https_ipv4' => [
        'https://192.168.0.1',
        TRUE,
      ],
      'https_ipv4_path' => [
        'https://192.168.0.1/baz',
        TRUE,
      ],
      'https_ipv4_port' => [
        'https://192.168.0.1:1234',
        TRUE,
      ],
      'ipv6' => [
        '[fd42:92f4:7eb8:c821:f685:9190:bf44:b2f5]',
        TRUE,
      ],
      'ipv6_short' => [
        '[fd42:92f4:7eb8:c821::b2f5]',
        TRUE,
      ],
      'https_ipv6' => [
        'https://[fd42:92f4:7eb8:c821:f685:9190:bf44:b2f5]',
        TRUE,
      ],
      'https_ipv6_short' => [
        'https://[fd42:92f4:7eb8:c821::b2f5]',
        TRUE,
      ],
      'https_ipv6_port' => [
        'https://[fd42:92f4:7eb8:c821:f685:9190:bf44:b2f5]:1234',
        TRUE,
      ],
      'https_ipv6_short_port' => [
        'https://[fd42:92f4:7eb8:c821::b2f5]:1234',
        TRUE,
      ],
      'https_ipv6_port_path' => [
        'https://[fd42:92f4:7eb8:c821:f685:9190:bf44:b2f5]:1234/baz',
        TRUE,
      ],
      'localhost' => [
        'localhost',
        TRUE,
      ],
      'https_localhost' => [
        'https://localhost',
        TRUE,
      ],
      'https_localhost_path' => [
        'https://localhost/baz',
        TRUE,
      ],
      'https_localhost_port' => [
        'https://localhost:1234',
        TRUE,
      ],
      'https_localhost_port_path' => [
        'https://localhost:1234/baz',
        TRUE,
      ],
      'wild_port' => [
        'example.com:*',
        TRUE,
      ],
      'wild_subdomain_wild_port' => [
        '*.example.com:*',
        TRUE,
      ],
      'empty_port' => [
        'example.com:',
        FALSE,
      ],
      'letter_port' => [
        'example.com:b33f',
        FALSE,
      ],
      // @see https://www.w3.org/TR/CSP3/#grammardef-scheme-part
      // @see https://tools.ietf.org/html/rfc3986#section-3.1
      'other_protocol' => [
        'example://localhost',
        TRUE,
      ],
      'edge_case_protocol' => [
        'example-foo.123+bar://localhost',
        TRUE,
      ],
      'protocol_numeric_first_char' => [
        '1example://localhost',
        FALSE,
      ],
      'protocol_invalid_symbol' => [
        'ex@mple://localhost',
        FALSE,
      ],
    ];
  }

  /**
   * Valid host source values.
   *
   * @param string $url
   *   A URL.
   * @param bool $valid
   *   TRUE if the url should be valid.
   *
   * @dataProvider urlDataProvider
   */
  public function testIsValidHost(string $url, bool $valid = TRUE) {
    $this
      ->assertEquals($valid, HostValidator::isValidHost($url));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CspSettingsFormTest::testIsValidHost public function Valid host source values.
CspSettingsFormTest::urlDataProvider public function Data provider of URLs for host source validity.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340