XmppValidateTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. namespace Tests\Unit;
  17. if (!defined('INSTALLDIR')) {
  18. define('INSTALLDIR', dirname(dirname(__DIR__)));
  19. }
  20. if (!defined('GNUSOCIAL')) {
  21. define('GNUSOCIAL', true);
  22. }
  23. if (!defined('STATUSNET')) { // Compatibility
  24. define('STATUSNET', true);
  25. }
  26. use GNUsocial;
  27. use PHPUnit\Framework\TestCase;
  28. use XmppPlugin;
  29. require_once INSTALLDIR . '/lib/common.php';
  30. require_once INSTALLDIR . '/plugins/Xmpp/XmppPlugin.php';
  31. final class XmppValidateTest extends TestCase
  32. {
  33. public function setUp()
  34. {
  35. if (!array_key_exists('Xmpp', GNUsocial::getActivePlugins())) {
  36. $this->markTestSkipped('XmppPlugin is not enabled.');
  37. }
  38. }
  39. /**
  40. * @dataProvider validationCases
  41. * @param $jid
  42. * @param $validFull
  43. * @param $validBase
  44. */
  45. public function testValidate($jid, $validFull, $validBase)
  46. {
  47. $xmpp = new TestXmppPlugin();
  48. $this->assertEquals($validFull || $validBase, $xmpp->validate($jid));
  49. $this->assertEquals($validFull, $xmpp->validateFullJid($jid), "validating as full or base JID");
  50. $this->assertEquals($validBase, $xmpp->validateBaseJid($jid), "validating as base JID only");
  51. }
  52. /**
  53. * @dataProvider normalizationCases
  54. * @param $jid
  55. * @param $expected
  56. */
  57. public function testNormalize($jid, $expected)
  58. {
  59. $xmpp = new XmppPlugin();
  60. $this->assertEquals($expected, $xmpp->normalize($jid));
  61. }
  62. /**
  63. * @dataProvider domainCheckCases()
  64. * @param $domain
  65. * @param $expected
  66. * @param $note
  67. */
  68. public function testDomainCheck($domain, $expected, $note)
  69. {
  70. $xmpp = new TestXmppPlugin();
  71. $this->assertEquals($expected, $xmpp->checkDomain($domain), $note);
  72. }
  73. static public function validationCases()
  74. {
  75. $long1023 = "long1023" . str_repeat('x', 1023 - 8);
  76. $long1024 = "long1024" . str_repeat('x', 1024 - 8);
  77. return array(
  78. // Our own test cases for standard things & those mentioned in bug reports
  79. // (jid, valid_full, valid_base)
  80. array('user@example.com', true, true),
  81. array('user@example.com/resource', true, false),
  82. array('user with spaces@example.com', false, false), // not kosher
  83. array('user.@example.com', true, true), // "common in intranets"
  84. array('example.com', true, true),
  85. array('example.com/resource', true, false),
  86. array('jabchat', true, true),
  87. array("$long1023@$long1023/$long1023", true, false), // max 1023 "bytes" per portion per spec. Do they really mean bytes though?
  88. array("$long1024@$long1023/$long1023", false, false),
  89. array("$long1023@$long1024/$long1023", false, false),
  90. array("$long1023@$long1023/$long1024", false, false),
  91. // Borrowed from test_jabber_jutil.c in libpurple
  92. array("gmail.com", true, true),
  93. array("gmail.com/Test", true, false),
  94. array("gmail.com/Test@", true, false),
  95. array("gmail.com/@", true, false),
  96. array("gmail.com/Test@alkjaweflkj", true, false),
  97. array("mark.doliner@gmail.com", true, true),
  98. array("mark.doliner@gmail.com/Test12345", true, false),
  99. array("mark.doliner@gmail.com/Test@12345", true, false),
  100. array("mark.doliner@gmail.com/Te/st@12@//345", true, false),
  101. array("わいど@conference.jabber.org", true, true),
  102. array("まりるーむ@conference.jabber.org", true, true),
  103. array("mark.doliner@gmail.com/まりるーむ", true, false),
  104. array("mark.doliner@gmail/stuff.org", true, false),
  105. array("stuart@nödåtXäYZ.se", true, true),
  106. array("stuart@nödåtXäYZ.se/まりるーむ", true, false),
  107. array("mark.doliner@わいど.org", true, true),
  108. array("nick@まつ.おおかみ.net", true, true),
  109. array("paul@10.0.42.230/s", true, false),
  110. array("paul@[::1]", true, true), /* IPv6 */
  111. array("paul@[2001:470:1f05:d58::2]", true, true),
  112. array("paul@[2001:470:1f05:d58::2]/foo", true, false),
  113. array("pa=ul@10.0.42.230", true, true),
  114. array("pa,ul@10.0.42.230", true, true),
  115. array("@gmail.com", false, false),
  116. array("@@gmail.com", false, false),
  117. array("mark.doliner@@gmail.com/Test12345", false, false),
  118. array("mark@doliner@gmail.com/Test12345", false, false),
  119. array("@gmail.com/Test@12345", false, false),
  120. array("/Test@12345", false, false),
  121. array("mark.doliner@", false, false),
  122. array("mark.doliner/", false, false),
  123. array("mark.doliner@gmail_stuff.org", false, false),
  124. array("mark.doliner@gmail[stuff.org", false, false),
  125. array("mark.doliner@gmail\\stuff.org", false, false),
  126. array("paul@[::1]124", false, false),
  127. array("paul@2[::1]124/as", false, false),
  128. array("paul@まつ.おおかみ/\x01", false, false),
  129. /*
  130. * RFC 3454 Section 6 reads, in part,
  131. * "If a string contains any RandALCat character, the
  132. * string MUST NOT contain any LCat character."
  133. * The character is U+066D (ARABIC FIVE POINTED STAR).
  134. */
  135. // Leaving this one commented out for the moment
  136. // as it shouldn't hurt anything for our purposes.
  137. //array("foo@example.com/٭simplexe٭", false, false)
  138. );
  139. }
  140. static public function normalizationCases()
  141. {
  142. return array(
  143. // Borrowed from test_jabber_jutil.c in libpurple
  144. array('PaUL@DaRkRain42.org', 'paul@darkrain42.org'),
  145. array('PaUL@DaRkRain42.org/', 'paul@darkrain42.org'),
  146. array('PaUL@DaRkRain42.org/resource', 'paul@darkrain42.org'),
  147. // Also adapted from libpurple tests...
  148. array('Ф@darkrain42.org', 'ф@darkrain42.org'),
  149. array('paul@Өarkrain.org', 'paul@өarkrain.org'),
  150. );
  151. }
  152. static public function domainCheckCases()
  153. {
  154. return array(
  155. array('gmail.com', true, 'known SRV record'),
  156. array('jabber.org', true, 'known SRV record'),
  157. array('status.net', true, 'known SRV record'),
  158. array('status.leuksman.com', true, 'known no SRV record but valid domain'),
  159. );
  160. }
  161. }
  162. class TestXmppPlugin extends XmppPlugin
  163. {
  164. public function checkDomain($domain)
  165. {
  166. return parent::checkDomain($domain);
  167. }
  168. public function validateBaseJid($jid, $check_domain = false)
  169. {
  170. return parent::validateBaseJid($jid, $check_domain);
  171. }
  172. public function validateFullJid($jid, $check_domain = false)
  173. {
  174. return parent::validateFullJid($jid, $check_domain);
  175. }
  176. }