XmppValidateTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 . '/plugins/Xmpp/XmppPlugin.php';
  12. class XmppValidateTest extends PHPUnit_Framework_TestCase
  13. {
  14. public function setUp()
  15. {
  16. if(!array_key_exists('Xmpp', GNUsocial::getActivePlugins())){
  17. $this->markTestSkipped('XmppPlugin is not enabled.');
  18. }
  19. }
  20. /**
  21. * @dataProvider validationCases
  22. *
  23. */
  24. public function testValidate($jid, $validFull, $validBase)
  25. {
  26. $xmpp = new TestXmppPlugin();
  27. $this->assertEquals($validFull || $validBase, $xmpp->validate($jid));
  28. $this->assertEquals($validFull, $xmpp->validateFullJid($jid), "validating as full or base JID");
  29. $this->assertEquals($validBase, $xmpp->validateBaseJid($jid), "validating as base JID only");
  30. }
  31. /**
  32. * @dataProvider normalizationCases
  33. *
  34. */
  35. public function testNormalize($jid, $expected)
  36. {
  37. $xmpp = new XmppPlugin();
  38. $this->assertEquals($expected, $xmpp->normalize($jid));
  39. }
  40. /**
  41. * @dataProvider domainCheckCases()
  42. */
  43. public function testDomainCheck($domain, $expected, $note)
  44. {
  45. $xmpp = new TestXmppPlugin();
  46. $this->assertEquals($expected, $xmpp->checkDomain($domain), $note);
  47. }
  48. static public function validationCases()
  49. {
  50. $long1023 = "long1023" . str_repeat('x', 1023 - 8);
  51. $long1024 = "long1024" . str_repeat('x', 1024 - 8);
  52. return array(
  53. // Our own test cases for standard things & those mentioned in bug reports
  54. // (jid, valid_full, valid_base)
  55. array('user@example.com', true, true),
  56. array('user@example.com/resource', true, false),
  57. array('user with spaces@example.com', false, false), // not kosher
  58. array('user.@example.com', true, true), // "common in intranets"
  59. array('example.com', true, true),
  60. array('example.com/resource', true, false),
  61. array('jabchat', true, true),
  62. array("$long1023@$long1023/$long1023", true, false), // max 1023 "bytes" per portion per spec. Do they really mean bytes though?
  63. array("$long1024@$long1023/$long1023", false, false),
  64. array("$long1023@$long1024/$long1023", false, false),
  65. array("$long1023@$long1023/$long1024", false, false),
  66. // Borrowed from test_jabber_jutil.c in libpurple
  67. array("gmail.com", true, true),
  68. array("gmail.com/Test", true, false),
  69. array("gmail.com/Test@", true, false),
  70. array("gmail.com/@", true, false),
  71. array("gmail.com/Test@alkjaweflkj", true, false),
  72. array("mark.doliner@gmail.com", true, true),
  73. array("mark.doliner@gmail.com/Test12345", true, false),
  74. array("mark.doliner@gmail.com/Test@12345", true, false),
  75. array("mark.doliner@gmail.com/Te/st@12@//345", true, false),
  76. array("わいど@conference.jabber.org", true, true),
  77. array("まりるーむ@conference.jabber.org", true, true),
  78. array("mark.doliner@gmail.com/まりるーむ", true, false),
  79. array("mark.doliner@gmail/stuff.org", true, false),
  80. array("stuart@nödåtXäYZ.se", true, true),
  81. array("stuart@nödåtXäYZ.se/まりるーむ", true, false),
  82. array("mark.doliner@わいど.org", true, true),
  83. array("nick@まつ.おおかみ.net", true, true),
  84. array("paul@10.0.42.230/s", true, false),
  85. array("paul@[::1]", true, true), /* IPv6 */
  86. array("paul@[2001:470:1f05:d58::2]", true, true),
  87. array("paul@[2001:470:1f05:d58::2]/foo", true, false),
  88. array("pa=ul@10.0.42.230", true, true),
  89. array("pa,ul@10.0.42.230", true, true),
  90. array("@gmail.com", false, false),
  91. array("@@gmail.com", false, false),
  92. array("mark.doliner@@gmail.com/Test12345", false, false),
  93. array("mark@doliner@gmail.com/Test12345", false, false),
  94. array("@gmail.com/Test@12345", false, false),
  95. array("/Test@12345", false, false),
  96. array("mark.doliner@", false, false),
  97. array("mark.doliner/", false, false),
  98. array("mark.doliner@gmail_stuff.org", false, false),
  99. array("mark.doliner@gmail[stuff.org", false, false),
  100. array("mark.doliner@gmail\\stuff.org", false, false),
  101. array("paul@[::1]124", false, false),
  102. array("paul@2[::1]124/as", false, false),
  103. array("paul@まつ.おおかみ/\x01", false, false),
  104. /*
  105. * RFC 3454 Section 6 reads, in part,
  106. * "If a string contains any RandALCat character, the
  107. * string MUST NOT contain any LCat character."
  108. * The character is U+066D (ARABIC FIVE POINTED STAR).
  109. */
  110. // Leaving this one commented out for the moment
  111. // as it shouldn't hurt anything for our purposes.
  112. //array("foo@example.com/٭simplexe٭", false, false)
  113. );
  114. }
  115. static public function normalizationCases()
  116. {
  117. return array(
  118. // Borrowed from test_jabber_jutil.c in libpurple
  119. array('PaUL@DaRkRain42.org', 'paul@darkrain42.org'),
  120. array('PaUL@DaRkRain42.org/', 'paul@darkrain42.org'),
  121. array('PaUL@DaRkRain42.org/resource', 'paul@darkrain42.org'),
  122. // Also adapted from libpurple tests...
  123. array('Ф@darkrain42.org', 'ф@darkrain42.org'),
  124. array('paul@Өarkrain.org', 'paul@өarkrain.org'),
  125. );
  126. }
  127. static public function domainCheckCases()
  128. {
  129. return array(
  130. array('gmail.com', true, 'known SRV record'),
  131. array('jabber.org', true, 'known SRV record'),
  132. array('status.net', true, 'known SRV record'),
  133. array('status.leuksman.com', true, 'known no SRV record but valid domain'),
  134. );
  135. }
  136. }
  137. class TestXmppPlugin extends XmppPlugin {
  138. public function checkDomain($domain)
  139. {
  140. return parent::checkDomain($domain);
  141. }
  142. public function validateBaseJid($jid, $check_domain=false)
  143. {
  144. return parent::validateBaseJid($jid, $check_domain);
  145. }
  146. public function validateFullJid($jid, $check_domain=false)
  147. {
  148. return parent::validateFullJid($jid, $check_domain);
  149. }
  150. }