XmppValidateTest.php 7.6 KB

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