URITest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. class HTMLPurifier_URITest extends HTMLPurifier_URIHarness
  3. {
  4. protected function createURI($uri) {
  5. $parser = new HTMLPurifier_URIParser();
  6. return $parser->parse($uri);
  7. }
  8. function test_construct() {
  9. $uri1 = new HTMLPurifier_URI('HTTP', 'bob', 'example.com', '23', '/foo', 'bar=2', 'slash');
  10. $uri2 = new HTMLPurifier_URI('http', 'bob', 'example.com', 23, '/foo', 'bar=2', 'slash');
  11. $this->assertIdentical($uri1, $uri2);
  12. }
  13. protected $oldRegistry;
  14. protected function &setUpSchemeRegistryMock() {
  15. $this->oldRegistry = HTMLPurifier_URISchemeRegistry::instance();
  16. generate_mock_once('HTMLPurifier_URIScheme');
  17. generate_mock_once('HTMLPurifier_URISchemeRegistry');
  18. $registry = HTMLPurifier_URISchemeRegistry::instance(
  19. new HTMLPurifier_URISchemeRegistryMock()
  20. );
  21. return $registry;
  22. }
  23. protected function setUpSchemeMock($name) {
  24. $registry = $this->setUpSchemeRegistryMock();
  25. $scheme_mock = new HTMLPurifier_URISchemeMock();
  26. $registry->setReturnValue('getScheme', $scheme_mock, array($name, '*', '*'));
  27. return $scheme_mock;
  28. }
  29. protected function setUpNoValidSchemes() {
  30. $registry = $this->setUpSchemeRegistryMock();
  31. $registry->setReturnValue('getScheme', false, array('*', '*', '*'));
  32. }
  33. protected function tearDownSchemeRegistryMock() {
  34. HTMLPurifier_URISchemeRegistry::instance($this->oldRegistry);
  35. }
  36. function test_getSchemeObj() {
  37. $scheme_mock = $this->setUpSchemeMock('http');
  38. $uri = $this->createURI('http:');
  39. $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
  40. $this->assertIdentical($scheme_obj, $scheme_mock);
  41. $this->tearDownSchemeRegistryMock();
  42. }
  43. function test_getSchemeObj_invalidScheme() {
  44. $this->setUpNoValidSchemes();
  45. $uri = $this->createURI('http:');
  46. $result = $uri->getSchemeObj($this->config, $this->context);
  47. $this->assertIdentical($result, false);
  48. $this->tearDownSchemeRegistryMock();
  49. }
  50. function test_getSchemaObj_defaultScheme() {
  51. $scheme = 'foobar';
  52. $scheme_mock = $this->setUpSchemeMock($scheme);
  53. $this->config->set('URI.DefaultScheme', $scheme);
  54. $uri = $this->createURI('hmm');
  55. $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
  56. $this->assertIdentical($scheme_obj, $scheme_mock);
  57. $this->tearDownSchemeRegistryMock();
  58. }
  59. function test_getSchemaObj_invalidDefaultScheme() {
  60. $this->setUpNoValidSchemes();
  61. $this->config->set('URI.DefaultScheme', 'foobar');
  62. $uri = $this->createURI('hmm');
  63. $this->expectError('Default scheme object "foobar" was not readable');
  64. $result = $uri->getSchemeObj($this->config, $this->context);
  65. $this->assertIdentical($result, false);
  66. $this->tearDownSchemeRegistryMock();
  67. }
  68. protected function assertToString($expect_uri, $scheme, $userinfo, $host, $port, $path, $query, $fragment) {
  69. $uri = new HTMLPurifier_URI($scheme, $userinfo, $host, $port, $path, $query, $fragment);
  70. $string = $uri->toString();
  71. $this->assertIdentical($string, $expect_uri);
  72. }
  73. function test_toString_full() {
  74. $this->assertToString(
  75. 'http://bob@example.com:300/foo?bar=baz#fragment',
  76. 'http', 'bob', 'example.com', 300, '/foo', 'bar=baz', 'fragment'
  77. );
  78. }
  79. function test_toString_scheme() {
  80. $this->assertToString(
  81. 'http:',
  82. 'http', null, null, null, '', null, null
  83. );
  84. }
  85. function test_toString_authority() {
  86. $this->assertToString(
  87. '//bob@example.com:8080',
  88. null, 'bob', 'example.com', 8080, '', null, null
  89. );
  90. }
  91. function test_toString_path() {
  92. $this->assertToString(
  93. '/path/to',
  94. null, null, null, null, '/path/to', null, null
  95. );
  96. }
  97. function test_toString_query() {
  98. $this->assertToString(
  99. '?q=string',
  100. null, null, null, null, '', 'q=string', null
  101. );
  102. }
  103. function test_toString_fragment() {
  104. $this->assertToString(
  105. '#fragment',
  106. null, null, null, null, '', null, 'fragment'
  107. );
  108. }
  109. protected function assertValidation($uri, $expect_uri = true) {
  110. if ($expect_uri === true) $expect_uri = $uri;
  111. $uri = $this->createURI($uri);
  112. $result = $uri->validate($this->config, $this->context);
  113. if ($expect_uri === false) {
  114. $this->assertFalse($result);
  115. } else {
  116. $this->assertTrue($result);
  117. $this->assertIdentical($uri->toString(), $expect_uri);
  118. }
  119. }
  120. function test_validate_overlongPort() {
  121. $this->assertValidation('http://example.com:65536', 'http://example.com');
  122. }
  123. function test_validate_zeroPort() {
  124. $this->assertValidation('http://example.com:00', 'http://example.com');
  125. }
  126. function test_validate_invalidHostThatLooksLikeIPv6() {
  127. $this->assertValidation('http://[2001:0db8:85z3:08d3:1319:8a2e:0370:7334]', '');
  128. }
  129. function test_validate_removeRedundantScheme() {
  130. $this->assertValidation('http:foo:/:', 'foo%3A/:');
  131. }
  132. function test_validate_username() {
  133. $this->assertValidation("http://user\xE3\x91\x94:@foo.com", 'http://user%E3%91%94:@foo.com');
  134. }
  135. function test_validate_path_abempty() {
  136. $this->assertValidation("http://host/\xE3\x91\x94:", 'http://host/%E3%91%94:');
  137. }
  138. function test_validate_path_absolute() {
  139. $this->assertValidation("/\xE3\x91\x94:", '/%E3%91%94:');
  140. }
  141. function test_validate_path_rootless() {
  142. $this->assertValidation("mailto:\xE3\x91\x94:", 'mailto:%E3%91%94:');
  143. }
  144. function test_validate_path_noscheme() {
  145. $this->assertValidation("\xE3\x91\x94", '%E3%91%94');
  146. }
  147. function test_validate_query() {
  148. $this->assertValidation("?/\xE3\x91\x94", '?/%E3%91%94');
  149. }
  150. function test_validate_fragment() {
  151. $this->assertValidation("#/\xE3\x91\x94", '#/%E3%91%94');
  152. }
  153. function test_validate_path_empty() {
  154. $this->assertValidation('http://google.com');
  155. }
  156. }
  157. // vim: et sw=4 sts=4