JidValidateTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  3. print "This script must be run from the command line\n";
  4. exit();
  5. }
  6. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  7. define('GNUSOCIAL', true);
  8. define('STATUSNET', true); // compatibility
  9. mb_internal_encoding('UTF-8'); // @fixme this probably belongs in common.php?
  10. require_once INSTALLDIR . '/lib/common.php';
  11. require_once INSTALLDIR . '/lib/jabber.php';
  12. class JidValidateTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider validationCases
  16. *
  17. */
  18. public function testValidate($jid, $validFull, $validBase)
  19. {
  20. $this->assertEquals($validFull, jabber_valid_full_jid($jid), "validating as full or base JID");
  21. $this->assertEquals($validBase, jabber_valid_base_jid($jid), "validating as base JID only");
  22. }
  23. /**
  24. * @dataProvider normalizationCases
  25. *
  26. */
  27. public function testNormalize($jid, $expected)
  28. {
  29. $this->assertEquals($expected, jabber_normalize_jid($jid));
  30. }
  31. /**
  32. * @dataProvider domainCheckCases()
  33. */
  34. public function testDomainCheck($domain, $expected, $note)
  35. {
  36. $this->assertEquals($expected, jabber_check_domain($domain), $note);
  37. }
  38. static public function validationCases()
  39. {
  40. $long1023 = "long1023" . str_repeat('x', 1023 - 8);
  41. $long1024 = "long1024" . str_repeat('x', 1024 - 8);
  42. return array(
  43. // Our own test cases for standard things & those mentioned in bug reports
  44. // (jid, valid_full, valid_base)
  45. array('user@example.com', true, true),
  46. array('user@example.com/resource', true, false),
  47. array('user with spaces@example.com', false, false), // not kosher
  48. array('user.@example.com', true, true), // "common in intranets"
  49. array('example.com', true, true),
  50. array('example.com/resource', true, false),
  51. array('jabchat', true, true),
  52. array("$long1023@$long1023/$long1023", true, false), // max 1023 "bytes" per portion per spec. Do they really mean bytes though?
  53. array("$long1024@$long1023/$long1023", false, false),
  54. array("$long1023@$long1024/$long1023", false, false),
  55. array("$long1023@$long1023/$long1024", false, false),
  56. // Borrowed from test_jabber_jutil.c in libpurple
  57. array("gmail.com", true, true),
  58. array("gmail.com/Test", true, false),
  59. array("gmail.com/Test@", true, false),
  60. array("gmail.com/@", true, false),
  61. array("gmail.com/Test@alkjaweflkj", true, false),
  62. array("mark.doliner@gmail.com", true, true),
  63. array("mark.doliner@gmail.com/Test12345", true, false),
  64. array("mark.doliner@gmail.com/Test@12345", true, false),
  65. array("mark.doliner@gmail.com/Te/st@12@//345", true, false),
  66. array("わいど@conference.jabber.org", true, true),
  67. array("まりるーむ@conference.jabber.org", true, true),
  68. array("mark.doliner@gmail.com/まりるーむ", true, false),
  69. array("mark.doliner@gmail/stuff.org", true, false),
  70. array("stuart@nödåtXäYZ.se", true, true),
  71. array("stuart@nödåtXäYZ.se/まりるーむ", true, false),
  72. array("mark.doliner@わいど.org", true, true),
  73. array("nick@まつ.おおかみ.net", true, true),
  74. array("paul@10.0.42.230/s", true, false),
  75. array("paul@[::1]", true, true), /* IPv6 */
  76. array("paul@[2001:470:1f05:d58::2]", true, true),
  77. array("paul@[2001:470:1f05:d58::2]/foo", true, false),
  78. array("pa=ul@10.0.42.230", true, true),
  79. array("pa,ul@10.0.42.230", true, true),
  80. array("@gmail.com", false, false),
  81. array("@@gmail.com", false, false),
  82. array("mark.doliner@@gmail.com/Test12345", false, false),
  83. array("mark@doliner@gmail.com/Test12345", false, false),
  84. array("@gmail.com/Test@12345", false, false),
  85. array("/Test@12345", false, false),
  86. array("mark.doliner@", false, false),
  87. array("mark.doliner/", false, false),
  88. array("mark.doliner@gmail_stuff.org", false, false),
  89. array("mark.doliner@gmail[stuff.org", false, false),
  90. array("mark.doliner@gmail\\stuff.org", false, false),
  91. array("paul@[::1]124", false, false),
  92. array("paul@2[::1]124/as", false, false),
  93. array("paul@まつ.おおかみ/\x01", false, false),
  94. /*
  95. * RFC 3454 Section 6 reads, in part,
  96. * "If a string contains any RandALCat character, the
  97. * string MUST NOT contain any LCat character."
  98. * The character is U+066D (ARABIC FIVE POINTED STAR).
  99. */
  100. // Leaving this one commented out for the moment
  101. // as it shouldn't hurt anything for our purposes.
  102. //array("foo@example.com/٭simplexe٭", false, false)
  103. );
  104. }
  105. static public function normalizationCases()
  106. {
  107. return array(
  108. // Borrowed from test_jabber_jutil.c in libpurple
  109. array('PaUL@DaRkRain42.org', 'paul@darkrain42.org'),
  110. array('PaUL@DaRkRain42.org/', 'paul@darkrain42.org'),
  111. array('PaUL@DaRkRain42.org/resource', 'paul@darkrain42.org'),
  112. // Also adapted from libpurple tests...
  113. array('Ф@darkrain42.org', 'ф@darkrain42.org'),
  114. array('paul@Өarkrain.org', 'paul@өarkrain.org'),
  115. );
  116. }
  117. static public function domainCheckCases()
  118. {
  119. return array(
  120. array('gmail.com', true, 'known SRV record'),
  121. array('jabber.org', true, 'known SRV record'),
  122. array('status.net', true, 'known SRV record'),
  123. array('status.leuksman.com', true, 'known no SRV record but valid domain'),
  124. );
  125. }
  126. }