URITest.php 4.8 KB

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