URITest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @todo Aim for complete code coverage with mocks
  4. */
  5. class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
  6. {
  7. public function setUp()
  8. {
  9. $this->def = new HTMLPurifier_AttrDef_URI();
  10. parent::setUp();
  11. }
  12. public function testIntegration()
  13. {
  14. $this->assertDef('http://www.google.com/');
  15. $this->assertDef('http:', '');
  16. $this->assertDef('http:/foo', '/foo');
  17. $this->assertDef('javascript:bad_stuff();', false);
  18. $this->assertDef('ftp://www.example.com/');
  19. $this->assertDef('news:rec.alt');
  20. $this->assertDef('nntp://news.example.com/324234');
  21. $this->assertDef('mailto:bob@example.com');
  22. $this->assertDef('tel:+15555555555');
  23. $this->assertDef('tel:+15555 555 555', 'tel:+15555555555');
  24. $this->assertDef('tel:+15555%20555%20555', 'tel:+15555555555');
  25. }
  26. public function testIntegrationWithPercentEncoder()
  27. {
  28. $this->assertDef(
  29. 'http://www.example.com/%56%fc%GJ%5%FC',
  30. 'http://www.example.com/V%FC%25GJ%255%FC'
  31. );
  32. }
  33. public function testPercentEncoding()
  34. {
  35. $this->assertDef(
  36. 'http:colon:mercenary',
  37. 'colon%3Amercenary'
  38. );
  39. }
  40. public function testPercentEncodingPreserve()
  41. {
  42. $this->assertDef(
  43. 'http://www.example.com/abcABC123-_.!~*()\''
  44. );
  45. }
  46. public function testEmbeds()
  47. {
  48. $this->def = new HTMLPurifier_AttrDef_URI(true);
  49. $this->assertDef('http://sub.example.com/alas?foo=asd');
  50. $this->assertDef('mailto:foo@example.com', false);
  51. }
  52. public function testConfigMunge()
  53. {
  54. $this->config->set('URI.Munge', 'http://www.google.com/url?q=%s');
  55. $this->assertDef(
  56. 'http://www.example.com/',
  57. 'http://www.google.com/url?q=http%3A%2F%2Fwww.example.com%2F'
  58. );
  59. $this->assertDef('index.html');
  60. $this->assertDef('javascript:foobar();', false);
  61. }
  62. public function testDefaultSchemeRemovedInBlank()
  63. {
  64. $this->assertDef('http:', '');
  65. }
  66. public function testDefaultSchemeRemovedInRelativeURI()
  67. {
  68. $this->assertDef('http:/foo/bar', '/foo/bar');
  69. }
  70. public function testDefaultSchemeNotRemovedInAbsoluteURI()
  71. {
  72. $this->assertDef('http://example.com/foo/bar');
  73. }
  74. public function testDefaultSchemeNull()
  75. {
  76. $this->config->set('URI.DefaultScheme', null);
  77. $this->assertDef('foo', false);
  78. }
  79. public function testAltSchemeNotRemoved()
  80. {
  81. $this->assertDef('mailto:this-looks-like-a-path@example.com');
  82. }
  83. public function testResolveNullSchemeAmbiguity()
  84. {
  85. $this->assertDef('///foo', '/foo');
  86. }
  87. public function testResolveNullSchemeDoubleAmbiguity()
  88. {
  89. $this->config->set('URI.Host', 'example.com');
  90. $this->assertDef('////foo', '//example.com//foo');
  91. }
  92. public function testURIDefinitionValidation()
  93. {
  94. $parser = new HTMLPurifier_URIParser();
  95. $uri = $parser->parse('http://example.com');
  96. $this->config->set('URI.DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation');
  97. generate_mock_once('HTMLPurifier_URIDefinition');
  98. $uri_def = new HTMLPurifier_URIDefinitionMock();
  99. $uri_def->expectOnce('filter', array($uri, '*', '*'));
  100. $uri_def->returns('filter', true, array($uri, '*', '*'));
  101. $uri_def->expectOnce('postFilter', array($uri, '*', '*'));
  102. $uri_def->returns('postFilter', true, array($uri, '*', '*'));
  103. $uri_def->setup = true;
  104. // Since definitions are no longer passed by reference, we need
  105. // to muck around with the cache to insert our mock. This is
  106. // technically a little bad, since the cache shouldn't change
  107. // behavior, but I don't feel too good about letting users
  108. // overload entire definitions.
  109. generate_mock_once('HTMLPurifier_DefinitionCache');
  110. $cache_mock = new HTMLPurifier_DefinitionCacheMock();
  111. $cache_mock->returns('get', $uri_def);
  112. generate_mock_once('HTMLPurifier_DefinitionCacheFactory');
  113. $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock();
  114. $old = HTMLPurifier_DefinitionCacheFactory::instance();
  115. HTMLPurifier_DefinitionCacheFactory::instance($factory_mock);
  116. $factory_mock->returns('create', $cache_mock);
  117. $this->assertDef('http://example.com');
  118. HTMLPurifier_DefinitionCacheFactory::instance($old);
  119. }
  120. public function test_make()
  121. {
  122. $factory = new HTMLPurifier_AttrDef_URI();
  123. $def = $factory->make('');
  124. $def2 = new HTMLPurifier_AttrDef_URI();
  125. $this->assertIdentical($def, $def2);
  126. $def = $factory->make('embedded');
  127. $def2 = new HTMLPurifier_AttrDef_URI(true);
  128. $this->assertIdentical($def, $def2);
  129. }
  130. /*
  131. public function test_validate_configWhitelist()
  132. {
  133. $this->config->set('URI.HostPolicy', 'DenyAll');
  134. $this->config->set('URI.HostWhitelist', array(null, 'google.com'));
  135. $this->assertDef('http://example.com/fo/google.com', false);
  136. $this->assertDef('server.txt');
  137. $this->assertDef('ftp://www.google.com/?t=a');
  138. $this->assertDef('http://google.com.tricky.spamsite.net', false);
  139. }
  140. */
  141. }
  142. // vim: et sw=4 sts=4