You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URITest.php 6.5KB

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