]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/Core/XmppValidateTest.php
[CORE] Make tests great gain
[quix0rs-gnu-social.git] / tests / Core / XmppValidateTest.php
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
17 namespace Tests\Unit;
18
19 if (!defined('INSTALLDIR')) {
20     define('INSTALLDIR', dirname(dirname(__DIR__)));
21 }
22 if (!defined('GNUSOCIAL')) {
23     define('GNUSOCIAL', true);
24 }
25 if (!defined('STATUSNET')) { // Compatibility
26     define('STATUSNET', true);
27 }
28
29 use GNUsocial;
30 use PHPUnit\Framework\TestCase;
31 use XmppPlugin;
32
33 require_once INSTALLDIR . '/lib/common.php';
34
35 require_once INSTALLDIR . '/plugins/Xmpp/XmppPlugin.php';
36
37 final class XmppValidateTest extends TestCase
38 {
39     public function setUp()
40     {
41         if (!array_key_exists('Xmpp', GNUsocial::getActivePlugins())) {
42             $this->markTestSkipped('XmppPlugin is not enabled.');
43         }
44     }
45
46     /**
47      * @dataProvider validationCases
48      * @param $jid
49      * @param $validFull
50      * @param $validBase
51      */
52     public function testValidate($jid, $validFull, $validBase)
53     {
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");
58     }
59
60     /**
61      * @dataProvider normalizationCases
62      * @param $jid
63      * @param $expected
64      */
65     public function testNormalize($jid, $expected)
66     {
67         $xmpp = new XmppPlugin();
68         $this->assertEquals($expected, $xmpp->normalize($jid));
69     }
70
71     /**
72      * @dataProvider domainCheckCases()
73      * @param $domain
74      * @param $expected
75      * @param $note
76      */
77     public function testDomainCheck($domain, $expected, $note)
78     {
79         $xmpp = new TestXmppPlugin();
80         $this->assertEquals($expected, $xmpp->checkDomain($domain), $note);
81     }
82
83     static public function validationCases()
84     {
85         $long1023 = "long1023" . str_repeat('x', 1023 - 8);
86         $long1024 = "long1024" . str_repeat('x', 1024 - 8);
87         return array(
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
93
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),
98
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),
103
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),
128
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),
143
144             /*
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).
149              */
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)
153         );
154     }
155
156     static public function normalizationCases()
157     {
158         return array(
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'),
163
164             // Also adapted from libpurple tests...
165             array('Ф@darkrain42.org', 'ф@darkrain42.org'),
166             array('paul@Өarkrain.org', 'paul@өarkrain.org'),
167         );
168     }
169
170     static public function domainCheckCases()
171     {
172         return array(
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'),
177         );
178     }
179
180
181 }
182
183 class TestXmppPlugin extends XmppPlugin
184 {
185     public function checkDomain($domain)
186     {
187         return parent::checkDomain($domain);
188     }
189
190     public function validateBaseJid($jid, $check_domain = false)
191     {
192         return parent::validateBaseJid($jid, $check_domain);
193     }
194
195     public function validateFullJid($jid, $check_domain = false)
196     {
197         return parent::validateFullJid($jid, $check_domain);
198     }
199 }