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