PercentEncoderTest.php 2.0 KB

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