HostTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // takes a URI formatted host and validates it
  3. class HTMLPurifier_AttrDef_URI_HostTest extends HTMLPurifier_AttrDefHarness
  4. {
  5. public function test()
  6. {
  7. $this->def = new HTMLPurifier_AttrDef_URI_Host();
  8. $this->assertDef('[2001:DB8:0:0:8:800:200C:417A]'); // IPv6
  9. $this->assertDef('124.15.6.89'); // IPv4
  10. $this->assertDef('www.google.com'); // reg-name
  11. // more domain name tests
  12. $this->assertDef('test.');
  13. $this->assertDef('sub.test.');
  14. $this->assertDef('.test', false);
  15. $this->assertDef('ff');
  16. $this->assertDef('1f'); // per RFC 1123
  17. // See also http://serverfault.com/questions/638260/is-it-valid-for-a-hostname-to-start-with-a-digit
  18. $this->assertDef('-f', false);
  19. $this->assertDef('f1');
  20. $this->assertDef('f-', false);
  21. $this->assertDef('sub.ff');
  22. $this->assertDef('sub.1f'); // per RFC 1123
  23. $this->assertDef('sub.-f', false);
  24. $this->assertDef('sub.f1');
  25. $this->assertDef('sub.f-', false);
  26. $this->assertDef('ff.top');
  27. $this->assertDef('1f.top');
  28. $this->assertDef('-f.top', false);
  29. $this->assertDef('ff.top');
  30. $this->assertDef('f1.top');
  31. $this->assertDef('f1_f2.ex.top', false);
  32. $this->assertDef('f-.top', false);
  33. $this->assertDef('1a');
  34. $this->assertDef("\xE4\xB8\xAD\xE6\x96\x87.com.cn", 'xn--fiq228c.com.cn', true);
  35. }
  36. public function testIDNA()
  37. {
  38. if (!$GLOBALS['HTMLPurifierTest']['Net_IDNA2'] && !function_exists("idn_to_ascii")) {
  39. return false;
  40. }
  41. $this->config->set('Core.EnableIDNA', true);
  42. $this->assertDef("\xE4\xB8\xAD\xE6\x96\x87.com.cn", "xn--fiq228c.com.cn");
  43. $this->assertDef("faß.de", "xn--fa-hia.de");
  44. $this->assertDef("\xe2\x80\x85.com", false); // rejected
  45. }
  46. function testAllowUnderscore() {
  47. $this->config->set('Core.AllowHostnameUnderscore', true);
  48. $this->assertDef("foo_bar.example.com");
  49. $this->assertDef("foo_.example.com", false);
  50. }
  51. }
  52. // vim: et sw=4 sts=4