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.

72 lines
2.1KB

  1. <?php
  2. class HTMLPurifier_PercentEncoderTest extends HTMLPurifier_Harness
  3. {
  4. protected $PercentEncoder;
  5. protected $func;
  6. public function setUp()
  7. {
  8. $this->PercentEncoder = new HTMLPurifier_PercentEncoder();
  9. $this->func = '';
  10. }
  11. public function assertDecode($string, $expect = true)
  12. {
  13. if ($expect === true) $expect = $string;
  14. $this->assertIdentical($this->PercentEncoder->{$this->func}($string), $expect);
  15. }
  16. public function test_normalize()
  17. {
  18. $this->func = 'normalize';
  19. $this->assertDecode('Aw.../-$^8'); // no change
  20. $this->assertDecode('%41%77%7E%2D%2E%5F', 'Aw~-._'); // decode unreserved chars
  21. $this->assertDecode('%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D'); // preserve reserved chars
  22. $this->assertDecode('%2b', '%2B'); // normalize to uppercase
  23. $this->assertDecode('%2B2B%3A3A'); // extra text
  24. $this->assertDecode('%2b2B%4141', '%2B2BA41'); // extra text, with normalization
  25. $this->assertDecode('%', '%25'); // normalize stray percent sign
  26. $this->assertDecode('%5%25', '%255%25'); // permaturely terminated encoding
  27. $this->assertDecode('%GJ', '%25GJ'); // invalid hexadecimal chars
  28. // contested behavior, if this changes, we'll also have to have
  29. // outbound encoding
  30. $this->assertDecode('%FC'); // not reserved or unreserved, preserve
  31. }
  32. public function assertEncode($string, $expect = true, $preserve = false)
  33. {
  34. if ($expect === true) $expect = $string;
  35. $encoder = new HTMLPurifier_PercentEncoder($preserve);
  36. $result = $encoder->encode($string);
  37. $this->assertIdentical($result, $expect);
  38. }
  39. public function test_encode_noChange()
  40. {
  41. $this->assertEncode('abc012-_~.');
  42. }
  43. public function test_encode_encode()
  44. {
  45. $this->assertEncode('>', '%3E');
  46. }
  47. public function test_encode_preserve()
  48. {
  49. $this->assertEncode('<>', '<%3E', '<');
  50. }
  51. public function test_encode_low()
  52. {
  53. $this->assertEncode("\1", '%01');
  54. }
  55. }
  56. // vim: et sw=4 sts=4