XmppValidateTest.php 7.3 KB

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