]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/JidValidateTest.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / tests / JidValidateTest.php
1 <?php
2
3 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
4     print "This script must be run from the command line\n";
5     exit();
6 }
7
8 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
9 define('GNUSOCIAL', true);
10 define('STATUSNET', true);  // compatibility
11
12 mb_internal_encoding('UTF-8'); // @fixme this probably belongs in common.php?
13
14 require_once INSTALLDIR . '/lib/common.php';
15 require_once INSTALLDIR . '/lib/jabber.php';
16
17 class JidValidateTest extends PHPUnit_Framework_TestCase
18 {
19     /**
20      * @dataProvider validationCases
21      *
22      */
23     public function testValidate($jid, $validFull, $validBase)
24     {
25         $this->assertEquals($validFull, jabber_valid_full_jid($jid), "validating as full or base JID");
26
27         $this->assertEquals($validBase, jabber_valid_base_jid($jid), "validating as base JID only");
28     }
29
30     /**
31      * @dataProvider normalizationCases
32      *
33      */
34     public function testNormalize($jid, $expected)
35     {
36         $this->assertEquals($expected, jabber_normalize_jid($jid));
37     }
38
39     /**
40      * @dataProvider domainCheckCases()
41      */
42     public function testDomainCheck($domain, $expected, $note)
43     {
44         $this->assertEquals($expected, jabber_check_domain($domain), $note);
45     }
46
47     static public function validationCases()
48     {
49         $long1023 = "long1023" . str_repeat('x', 1023 - 8);
50         $long1024 = "long1024" . str_repeat('x', 1024 - 8);
51         return array(
52             // Our own test cases for standard things & those mentioned in bug reports
53             // (jid, valid_full, valid_base)
54             array('user@example.com', true, true),
55             array('user@example.com/resource', true, false),
56             array('user with spaces@example.com', false, false), // not kosher
57
58             array('user.@example.com', true, true), // "common in intranets"
59             array('example.com', true, true),
60             array('example.com/resource', true, false),
61             array('jabchat', true, true),
62             
63             array("$long1023@$long1023/$long1023", true, false), // max 1023 "bytes" per portion per spec. Do they really mean bytes though?
64             array("$long1024@$long1023/$long1023", false, false),
65             array("$long1023@$long1024/$long1023", false, false),
66             array("$long1023@$long1023/$long1024", false, false),
67
68             // Borrowed from test_jabber_jutil.c in libpurple
69             array("gmail.com", true, true),
70             array("gmail.com/Test", true, false),
71             array("gmail.com/Test@", true, false),
72             array("gmail.com/@", true, false),
73             array("gmail.com/Test@alkjaweflkj", true, false),
74             array("mark.doliner@gmail.com", true, true),
75             array("mark.doliner@gmail.com/Test12345", true, false),
76             array("mark.doliner@gmail.com/Test@12345", true, false),
77             array("mark.doliner@gmail.com/Te/st@12@//345", true, false),
78             array("わいど@conference.jabber.org", true, true),
79             array("まりるーむ@conference.jabber.org", true, true),
80             array("mark.doliner@gmail.com/まりるーむ", true, false),
81             array("mark.doliner@gmail/stuff.org", true, false),
82             array("stuart@nödåtXäYZ.se", true, true),
83             array("stuart@nödåtXäYZ.se/まりるーむ", true, false),
84             array("mark.doliner@わいど.org", true, true),
85             array("nick@まつ.おおかみ.net", true, true),
86             array("paul@10.0.42.230/s", true, false),
87             array("paul@[::1]", true, true), /* IPv6 */
88             array("paul@[2001:470:1f05:d58::2]", true, true),
89             array("paul@[2001:470:1f05:d58::2]/foo", true, false),
90             array("pa=ul@10.0.42.230", true, true),
91             array("pa,ul@10.0.42.230", true, true),
92
93             array("@gmail.com", false, false),
94             array("@@gmail.com", false, false),
95             array("mark.doliner@@gmail.com/Test12345", false, false),
96             array("mark@doliner@gmail.com/Test12345", false, false),
97             array("@gmail.com/Test@12345", false, false),
98             array("/Test@12345", false, false),
99             array("mark.doliner@", false, false),
100             array("mark.doliner/", false, false),
101             array("mark.doliner@gmail_stuff.org", false, false),
102             array("mark.doliner@gmail[stuff.org", false, false),
103             array("mark.doliner@gmail\\stuff.org", false, false),
104             array("paul@[::1]124", false, false),
105             array("paul@2[::1]124/as", false, false),
106             array("paul@まつ.おおかみ/\x01", false, false),
107
108             /*
109              * RFC 3454 Section 6 reads, in part,
110              * "If a string contains any RandALCat character, the
111              *  string MUST NOT contain any LCat character."
112              * The character is U+066D (ARABIC FIVE POINTED STAR).
113              */
114             // Leaving this one commented out for the moment
115             // as it shouldn't hurt anything for our purposes.
116             //array("foo@example.com/٭simplexe٭", false, false)
117         );
118     }
119     
120     static public function normalizationCases()
121     {
122         return array(
123             // Borrowed from test_jabber_jutil.c in libpurple
124             array('PaUL@DaRkRain42.org', 'paul@darkrain42.org'),
125             array('PaUL@DaRkRain42.org/', 'paul@darkrain42.org'),
126             array('PaUL@DaRkRain42.org/resource', 'paul@darkrain42.org'),
127
128             // Also adapted from libpurple tests...
129             array('Ф@darkrain42.org', 'ф@darkrain42.org'),
130             array('paul@Өarkrain.org', 'paul@өarkrain.org'),
131         );
132     }
133
134     static public function domainCheckCases()
135     {
136         return array(
137             array('gmail.com', true, 'known SRV record'),
138             array('jabber.org', true, 'known SRV record'),
139             array('status.net', true, 'known SRV record'),
140             array('status.leuksman.com', true, 'known no SRV record but valid domain'),
141         );
142     }
143
144
145 }
146