MediaWiki  1.23.12
IPTest.php
Go to the documentation of this file.
1 <?php
12 class IPTest extends MediaWikiTestCase {
17  public function testisIPAddress() {
18  $this->assertFalse( IP::isIPAddress( false ), 'Boolean false is not an IP' );
19  $this->assertFalse( IP::isIPAddress( true ), 'Boolean true is not an IP' );
20  $this->assertFalse( IP::isIPAddress( "" ), 'Empty string is not an IP' );
21  $this->assertFalse( IP::isIPAddress( 'abc' ), 'Garbage IP string' );
22  $this->assertFalse( IP::isIPAddress( ':' ), 'Single ":" is not an IP' );
23  $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1' ), 'IPv6 with a double :: occurrence' );
24  $this->assertFalse(
25  IP::isIPAddress( '2001:0DB8::A:1::' ),
26  'IPv6 with a double :: occurrence, last at end'
27  );
28  $this->assertFalse(
29  IP::isIPAddress( '::2001:0DB8::5:1' ),
30  'IPv6 with a double :: occurrence, firt at beginning'
31  );
32  $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
33  $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
34  $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
35  $this->assertFalse( IP::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
36 
37  $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
38  $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
39  $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
40  $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
41  $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
42 
43  $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
44  '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
45  foreach ( $validIPs as $ip ) {
46  $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
47  }
48  }
49 
53  public function testisIPv6() {
54  $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
55  $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
56  $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
57  $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
58 
59  $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
60  $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
61  $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
62  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
63  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
64  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
65 
66  $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
67  $this->assertFalse(
68  IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ),
69  'IPv6 with 9 words ending with "::"'
70  );
71 
72  $this->assertFalse( IP::isIPv6( ':::' ) );
73  $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
74 
75  $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
76  $this->assertTrue( IP::isIPv6( '::0' ) );
77  $this->assertTrue( IP::isIPv6( '::fc' ) );
78  $this->assertTrue( IP::isIPv6( '::fc:100' ) );
79  $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
80  $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
81  $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
82  $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
83  $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
84 
85  $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
86  $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
87 
88  $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
89  $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
90  $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
91 
92  $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
93  $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
94  $this->assertTrue( IP::isIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' ) );
95  $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
96  $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
97  $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
98  $this->assertTrue( IP::isIPv6( '2001::df' ), 'IPv6 with "::" and 2 words' );
99  $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
100  $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
101 
102  $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
103  $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
104 
105  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
106  }
107 
111  public function testisIPv4() {
112  $this->assertFalse( IP::isIPv4( false ), 'Boolean false is not an IP' );
113  $this->assertFalse( IP::isIPv4( true ), 'Boolean true is not an IP' );
114  $this->assertFalse( IP::isIPv4( "" ), 'Empty string is not an IP' );
115  $this->assertFalse( IP::isIPv4( 'abc' ) );
116  $this->assertFalse( IP::isIPv4( ':' ) );
117  $this->assertFalse( IP::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
118  $this->assertFalse( IP::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
119  $this->assertFalse( IP::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
120 
121  $this->assertTrue( IP::isIPv4( '124.24.52.13' ) );
122  $this->assertTrue( IP::isIPv4( '1.24.52.13' ) );
123  $this->assertTrue( IP::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
124  }
125 
129  public function testValidIPs() {
130  foreach ( range( 0, 255 ) as $i ) {
131  $a = sprintf( "%03d", $i );
132  $b = sprintf( "%02d", $i );
133  $c = sprintf( "%01d", $i );
134  foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
135  $ip = "$f.$f.$f.$f";
136  $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv4 address" );
137  }
138  }
139  foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
140  $a = sprintf( "%04x", $i );
141  $b = sprintf( "%03x", $i );
142  $c = sprintf( "%02x", $i );
143  foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
144  $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
145  $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv6 address" );
146  }
147  }
148  // test with some abbreviations
149  $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
150  $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
151  $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
152  $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
153 
154  $this->assertTrue( IP::isValid( 'fc:100::' ) );
155  $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
156  $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
157 
158  $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
159  $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
160  $this->assertTrue( IP::isValid( '2001::df' ), 'IPv6 with "::" and 2 words' );
161  $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
162  $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
163  $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
164  $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
165 
166  $this->assertFalse(
167  IP::isValid( 'fc:100:a:d:1:e:ac:0::' ),
168  'IPv6 with 8 words ending with "::"'
169  );
170  $this->assertFalse(
171  IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ),
172  'IPv6 with 9 words ending with "::"'
173  );
174  }
175 
179  public function testInvalidIPs() {
180  // Out of range...
181  foreach ( range( 256, 999 ) as $i ) {
182  $a = sprintf( "%03d", $i );
183  $b = sprintf( "%02d", $i );
184  $c = sprintf( "%01d", $i );
185  foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
186  $ip = "$f.$f.$f.$f";
187  $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
188  }
189  }
190  foreach ( range( 'g', 'z' ) as $i ) {
191  $a = sprintf( "%04s", $i );
192  $b = sprintf( "%03s", $i );
193  $c = sprintf( "%02s", $i );
194  foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
195  $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
196  $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv6 address" );
197  }
198  }
199  // Have CIDR
200  $ipCIDRs = array(
201  '212.35.31.121/32',
202  '212.35.31.121/18',
203  '212.35.31.121/24',
204  '::ff:d:321:5/96',
205  'ff::d3:321:5/116',
206  'c:ff:12:1:ea:d:321:5/120',
207  );
208  foreach ( $ipCIDRs as $i ) {
209  $this->assertFalse( IP::isValid( $i ),
210  "$i is an invalid IP address because it is a block" );
211  }
212  // Incomplete/garbage
213  $invalid = array(
214  'www.xn--var-xla.net',
215  '216.17.184.G',
216  '216.17.184.1.',
217  '216.17.184',
218  '216.17.184.',
219  '256.17.184.1'
220  );
221  foreach ( $invalid as $i ) {
222  $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
223  }
224  }
225 
229  public function testValidBlocks() {
230  $valid = array(
231  '116.17.184.5/32',
232  '0.17.184.5/30',
233  '16.17.184.1/24',
234  '30.242.52.14/1',
235  '10.232.52.13/8',
236  '30.242.52.14/0',
237  '::e:f:2001/96',
238  '::c:f:2001/128',
239  '::10:f:2001/70',
240  '::fe:f:2001/1',
241  '::6d:f:2001/8',
242  '::fe:f:2001/0',
243  );
244  foreach ( $valid as $i ) {
245  $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
246  }
247  }
248 
252  public function testInvalidBlocks() {
253  $invalid = array(
254  '116.17.184.5/33',
255  '0.17.184.5/130',
256  '16.17.184.1/-1',
257  '10.232.52.13/*',
258  '7.232.52.13/ab',
259  '11.232.52.13/',
260  '::e:f:2001/129',
261  '::c:f:2001/228',
262  '::10:f:2001/-1',
263  '::6d:f:2001/*',
264  '::86:f:2001/ab',
265  '::23:f:2001/',
266  );
267  foreach ( $invalid as $i ) {
268  $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
269  }
270  }
271 
276  public function testSanitizeIP( $expected, $input ) {
277  $result = IP::sanitizeIP( $input );
278  $this->assertEquals( $expected, $result );
279  }
280 
284  public static function provideSanitizeIP() {
285  return array(
286  array( '0.0.0.0', '0.0.0.0' ),
287  array( '0.0.0.0', '00.00.00.00' ),
288  array( '0.0.0.0', '000.000.000.000' ),
289  array( '141.0.11.253', '141.000.011.253' ),
290  array( '1.2.4.5', '1.2.4.5' ),
291  array( '1.2.4.5', '01.02.04.05' ),
292  array( '1.2.4.5', '001.002.004.005' ),
293  array( '10.0.0.1', '010.0.000.1' ),
294  array( '80.72.250.4', '080.072.250.04' ),
295  array( 'Foo.1000.00', 'Foo.1000.00'),
296  array( 'Bar.01', 'Bar.01'),
297  array( 'Bar.010', 'Bar.010'),
298  array( null, ''),
299  array( null, ' ')
300  );
301  }
302 
307  public function testToUnsigned( $expected, $input ) {
308  $result = IP::toUnsigned( $input );
309  $this->assertTrue( $result === false || is_string( $result ) || is_int( $result ) );
310  $this->assertEquals( $expected, $result );
311  }
312 
316  public static function provideToUnsigned() {
317  return array(
318  array( 1, '0.0.0.1' ),
319  array( 16909060, '1.2.3.4' ),
320  array( 2130706433, '127.0.0.1' ),
321  array( '2147483648', '128.0.0.0' ),
322  array( 2130706440, '127.0.0.08' ),
323  array( 2130706441, '127.0.0.09' ),
324  array( '3735931646', '222.173.202.254' ),
325  array( pow( 2, 32 ) - 1, '255.255.255.255' ),
326  array( false, 'IN.VA.LI.D' ),
327  array( 1, '::1' ),
328  array( '42540766452641154071740215577757643572', '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ),
329  array( '42540766452641154071740215577757643572', '2001:db8:85a3::8a2e:0370:7334' ),
330  array( false, 'IN:VA::LI:D' ),
331  array( false, ':::1' )
332  );
333  }
334 
339  public function testToHex( $expected, $input ) {
340  $result = IP::toHex( $input );
341  $this->assertTrue( $result === false || is_string( $result ) );
342  $this->assertEquals( $expected, $result );
343  }
344 
348  public static function provideToHex() {
349  return array(
350  array( '00000001', '0.0.0.1' ),
351  array( '01020304', '1.2.3.4' ),
352  array( '7F000001', '127.0.0.1' ),
353  array( '80000000', '128.0.0.0' ),
354  array( 'DEADCAFE', '222.173.202.254' ),
355  array( 'FFFFFFFF', '255.255.255.255' ),
356  array( '8D000BFD', '141.000.11.253' ),
357  array( false, 'IN.VA.LI.D' ),
358  array( 'v6-00000000000000000000000000000001', '::1' ),
359  array( 'v6-20010DB885A3000000008A2E03707334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ),
360  array( 'v6-20010DB885A3000000008A2E03707334', '2001:db8:85a3::8a2e:0370:7334' ),
361  array( false, 'IN:VA::LI:D' ),
362  array( false, ':::1' )
363  );
364  }
365 
369  public function testPrivateIPs() {
370  $private = array( 'fc00::3', 'fc00::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
371  foreach ( $private as $p ) {
372  $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
373  }
374  $public = array( '2001:5c0:1000:a::133', 'fc::3', '00FC::' );
375  foreach ( $public as $p ) {
376  $this->assertTrue( IP::isPublic( $p ), "$p is a public IP address" );
377  }
378  }
379 
380  // Private wrapper used to test CIDR Parsing.
381  private function assertFalseCIDR( $CIDR, $msg = '' ) {
382  $ff = array( false, false );
383  $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
384  }
385 
386  // Private wrapper to test network shifting using only dot notation
387  private function assertNet( $expected, $CIDR ) {
388  $parse = IP::parseCIDR( $CIDR );
389  $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
390  }
391 
395  public function testHexToQuad() {
396  $this->assertEquals( '0.0.0.1', IP::hexToQuad( '00000001' ) );
397  $this->assertEquals( '255.0.0.0', IP::hexToQuad( 'FF000000' ) );
398  $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
399  $this->assertEquals( '10.188.222.255', IP::hexToQuad( '0ABCDEFF' ) );
400  // hex not left-padded...
401  $this->assertEquals( '0.0.0.0', IP::hexToQuad( '0' ) );
402  $this->assertEquals( '0.0.0.1', IP::hexToQuad( '1' ) );
403  $this->assertEquals( '0.0.0.255', IP::hexToQuad( 'FF' ) );
404  $this->assertEquals( '0.0.255.0', IP::hexToQuad( 'FF00' ) );
405  }
406 
410  public function testHexToOctet() {
411  $this->assertEquals( '0:0:0:0:0:0:0:1',
412  IP::hexToOctet( '00000000000000000000000000000001' ) );
413  $this->assertEquals( '0:0:0:0:0:0:FF:3',
414  IP::hexToOctet( '00000000000000000000000000FF0003' ) );
415  $this->assertEquals( '0:0:0:0:0:0:FF00:6',
416  IP::hexToOctet( '000000000000000000000000FF000006' ) );
417  $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
418  IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
419  $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
420  IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
421  // hex not left-padded...
422  $this->assertEquals( '0:0:0:0:0:0:0:0', IP::hexToOctet( '0' ) );
423  $this->assertEquals( '0:0:0:0:0:0:0:1', IP::hexToOctet( '1' ) );
424  $this->assertEquals( '0:0:0:0:0:0:0:FF', IP::hexToOctet( 'FF' ) );
425  $this->assertEquals( '0:0:0:0:0:0:0:FFD0', IP::hexToOctet( 'FFD0' ) );
426  $this->assertEquals( '0:0:0:0:0:0:FA00:0', IP::hexToOctet( 'FA000000' ) );
427  $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
428  }
429 
435  public function testCIDRParsing() {
436  $this->assertFalseCIDR( '192.0.2.0', "missing mask" );
437  $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
438 
439  // Verify if statement
440  $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
441  $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
442  $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
443  $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
444 
445  // Check internal logic
446  # 0 mask always result in array(0,0)
447  $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '192.0.0.2/0' ) );
448  $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '0.0.0.0/0' ) );
449  $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '255.255.255.255/0' ) );
450 
451  // @todo FIXME: Add more tests.
452 
453  # This part test network shifting
454  $this->assertNet( '192.0.0.0', '192.0.0.2/24' );
455  $this->assertNet( '192.168.5.0', '192.168.5.13/24' );
456  $this->assertNet( '10.0.0.160', '10.0.0.161/28' );
457  $this->assertNet( '10.0.0.0', '10.0.0.3/28' );
458  $this->assertNet( '10.0.0.0', '10.0.0.3/30' );
459  $this->assertNet( '10.0.0.4', '10.0.0.4/30' );
460  $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
461  $this->assertNet( '10.128.0.0', '10.135.0.0/9' );
462  $this->assertNet( '134.0.0.0', '134.0.5.1/8' );
463  }
464 
468  public function testIPCanonicalizeOnValidIp() {
469  $this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ),
470  'Canonicalization of a valid IP returns it unchanged' );
471  }
472 
477  $this->assertEquals(
478  '192.0.2.152',
479  IP::canonicalize( '::ffff:192.0.2.152' )
480  );
481  $this->assertEquals(
482  '192.0.2.152',
483  IP::canonicalize( '::192.0.2.152' )
484  );
485  }
486 
492  public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
493  $this->assertEquals(
494  $expected,
495  IP::isInRange( $addr, $range ),
496  $message
497  );
498  }
499 
501  public static function provideIPsAndRanges() {
502  # Format: (expected boolean, address, range, optional message)
503  return array(
504  # IPv4
505  array( true, '192.0.2.0', '192.0.2.0/24', 'Network address' ),
506  array( true, '192.0.2.77', '192.0.2.0/24', 'Simple address' ),
507  array( true, '192.0.2.255', '192.0.2.0/24', 'Broadcast address' ),
508 
509  array( false, '0.0.0.0', '192.0.2.0/24' ),
510  array( false, '255.255.255', '192.0.2.0/24' ),
511 
512  # IPv6
513  array( false, '::1', '2001:DB8::/32' ),
514  array( false, '::', '2001:DB8::/32' ),
515  array( false, 'FE80::1', '2001:DB8::/32' ),
516 
517  array( true, '2001:DB8::', '2001:DB8::/32' ),
518  array( true, '2001:0DB8::', '2001:DB8::/32' ),
519  array( true, '2001:DB8::1', '2001:DB8::/32' ),
520  array( true, '2001:0DB8::1', '2001:DB8::/32' ),
521  array( true, '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
522  '2001:DB8::/32' ),
523 
524  array( false, '2001:0DB8:F::', '2001:DB8::/96' ),
525  );
526  }
527 
532  public function testSplitHostAndPort( $expected, $input, $description ) {
533  $this->assertEquals( $expected, IP::splitHostAndPort( $input ), $description );
534  }
535 
539  public static function provideSplitHostAndPort() {
540  return array(
541  array( false, '[', 'Unclosed square bracket' ),
542  array( false, '[::', 'Unclosed square bracket 2' ),
543  array( array( '::', false ), '::', 'Bare IPv6 0' ),
544  array( array( '::1', false ), '::1', 'Bare IPv6 1' ),
545  array( array( '::', false ), '[::]', 'Bracketed IPv6 0' ),
546  array( array( '::1', false ), '[::1]', 'Bracketed IPv6 1' ),
547  array( array( '::1', 80 ), '[::1]:80', 'Bracketed IPv6 with port' ),
548  array( false, '::x', 'Double colon but no IPv6' ),
549  array( array( 'x', 80 ), 'x:80', 'Hostname and port' ),
550  array( false, 'x:x', 'Hostname and invalid port' ),
551  array( array( 'x', false ), 'x', 'Plain hostname' )
552  );
553  }
554 
559  public function testCombineHostAndPort( $expected, $input, $description ) {
560  list( $host, $port, $defaultPort ) = $input;
561  $this->assertEquals(
562  $expected,
563  IP::combineHostAndPort( $host, $port, $defaultPort ),
564  $description );
565  }
566 
570  public static function provideCombineHostAndPort() {
571  return array(
572  array( '[::1]', array( '::1', 2, 2 ), 'IPv6 default port' ),
573  array( '[::1]:2', array( '::1', 2, 3 ), 'IPv6 non-default port' ),
574  array( 'x', array( 'x', 2, 2 ), 'Normal default port' ),
575  array( 'x:2', array( 'x', 2, 3 ), 'Normal non-default port' ),
576  );
577  }
578 
583  public function testSanitizeRange( $input, $expected, $description ) {
584  $this->assertEquals( $expected, IP::sanitizeRange( $input ), $description );
585  }
586 
590  public static function provideIPCIDRs() {
591  return array(
592  array( '35.56.31.252/16', '35.56.0.0/16', 'IPv4 range' ),
593  array( '135.16.21.252/24', '135.16.21.0/24', 'IPv4 range' ),
594  array( '5.36.71.252/32', '5.36.71.252/32', 'IPv4 silly range' ),
595  array( '5.36.71.252', '5.36.71.252', 'IPv4 non-range' ),
596  array( '0:1:2:3:4:c5:f6:7/96', '0:1:2:3:4:C5:0:0/96', 'IPv6 range' ),
597  array( '0:1:2:3:4:5:6:7/120', '0:1:2:3:4:5:6:0/120', 'IPv6 range' ),
598  array( '0:e1:2:3:4:5:e6:7/128', '0:E1:2:3:4:5:E6:7/128', 'IPv6 silly range' ),
599  array( '0:c1:A2:3:4:5:c6:7', '0:C1:A2:3:4:5:C6:7', 'IPv6 non range' ),
600  );
601  }
602 
607  public function testPrettifyIP( $ip, $prettified ) {
608  $this->assertEquals( $prettified, IP::prettifyIP( $ip ), "Prettify of $ip" );
609  }
610 
614  public static function provideIPsToPrettify() {
615  return array(
616  array( '0:0:0:0:0:0:0:0', '::' ),
617  array( '0:0:0::0:0:0', '::' ),
618  array( '0:0:0:1:0:0:0:0', '0:0:0:1::' ),
619  array( '0:0::f', '::f' ),
620  array( '0::0:0:0:33:fef:b', '::33:fef:b' ),
621  array( '3f:535:0:0:0:0:e:fbb', '3f:535::e:fbb' ),
622  array( '0:0:fef:0:0:0:e:fbb', '0:0:fef::e:fbb' ),
623  array( 'abbc:2004::0:0:0:0', 'abbc:2004::' ),
624  array( 'cebc:2004:f:0:0:0:0:0', 'cebc:2004:f::' ),
625  array( '0:0:0:0:0:0:0:0/16', '::/16' ),
626  array( '0:0:0::0:0:0/64', '::/64' ),
627  array( '0:0::f/52', '::f/52' ),
628  array( '::0:0:33:fef:b/52', '::33:fef:b/52' ),
629  array( '3f:535:0:0:0:0:e:fbb/48', '3f:535::e:fbb/48' ),
630  array( '0:0:fef:0:0:0:e:fbb/96', '0:0:fef::e:fbb/96' ),
631  array( 'abbc:2004:0:0::0:0/40', 'abbc:2004::/40' ),
632  array( 'aebc:2004:f:0:0:0:0:0/80', 'aebc:2004:f::/80' ),
633  );
634  }
635 }
IP\toHex
static toHex( $ip)
Return a zero-padded upper case hexadecimal representation of an IP address.
Definition: IP.php:456
IP\hexToOctet
static hexToOctet( $ip_hex)
Converts a hexadecimal number to an IPv6 address in octet notation.
Definition: IP.php:339
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
IPTest\testValidIPs
testValidIPs()
@covers IP::isValid
Definition: IPTest.php:129
IPTest\assertNet
assertNet( $expected, $CIDR)
Definition: IPTest.php:387
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
IPTest\testCIDRParsing
testCIDRParsing()
IP::parseCIDR() returns an array containing a signed IP address representing the network mask and the...
Definition: IPTest.php:435
IP\combineHostAndPort
static combineHostAndPort( $host, $port, $defaultPort=false)
Given a host name and a port, combine them into host/port string like you might find in a URL.
Definition: IP.php:298
$f
$f
Definition: UtfNormalTest2.php:38
IP\canonicalize
static canonicalize( $addr)
Convert some unusual representations of IPv4 addresses to their canonical dotted quad representation.
Definition: IP.php:735
IPTest\testValidBlocks
testValidBlocks()
@covers IP::isValidBlock
Definition: IPTest.php:229
IPTest\testSanitizeRange
testSanitizeRange( $input, $expected, $description)
Test for IP::sanitizeRange() @dataProvider provideIPCIDRs.
Definition: IPTest.php:583
IPTest\testisIPAddress
testisIPAddress()
not sure it should be tested with boolean false.
Definition: IPTest.php:17
IP\isIPv6
static isIPv6( $ip)
Given a string, determine if it as valid IP in IPv6 only.
Definition: IP.php:85
IPTest\provideToUnsigned
static provideToUnsigned()
Provider for IP::testToUnsigned()
Definition: IPTest.php:316
IPTest\provideSplitHostAndPort
static provideSplitHostAndPort()
Provider for IP::splitHostAndPort()
Definition: IPTest.php:539
IPTest\testHexToOctet
testHexToOctet()
@covers IP::hexToOctet
Definition: IPTest.php:410
IPTest\testIPCanonicalizeMappedAddress
testIPCanonicalizeMappedAddress()
@covers IP::canonicalize
Definition: IPTest.php:476
IPTest\testInvalidBlocks
testInvalidBlocks()
@covers IP::isValidBlock
Definition: IPTest.php:252
IPTest\testHexToQuad
testHexToQuad()
@covers IP::hexToQuad
Definition: IPTest.php:395
IPTest\testPrettifyIP
testPrettifyIP( $ip, $prettified)
Test for IP::prettifyIP() @dataProvider provideIPsToPrettify.
Definition: IPTest.php:607
IP\hexToQuad
static hexToQuad( $ip_hex)
Converts a hexadecimal number to an IPv4 address in quad-dotted notation.
Definition: IP.php:359
IPTest\testIPCanonicalizeOnValidIp
testIPCanonicalizeOnValidIp()
@covers IP::canonicalize
Definition: IPTest.php:468
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
IPTest\testToHex
testToHex( $expected, $input)
@covers IP::toHex @dataProvider provideToHex
Definition: IPTest.php:339
IPTest\provideSanitizeIP
static provideSanitizeIP()
Provider for IP::testSanitizeIP()
Definition: IPTest.php:284
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
IP\isInRange
static isInRange( $addr, $range)
Determine if a given IPv4/IPv6 address is in a given CIDR network.
Definition: IP.php:717
IPTest\testPrivateIPs
testPrivateIPs()
@covers IP::isPublic
Definition: IPTest.php:369
IP\isPublic
static isPublic( $ip)
Determine if an IP address really is an IP address, and if it is public, i.e.
Definition: IP.php:381
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
IP\prettifyIP
static prettifyIP( $ip)
Prettify an IP for display to end users.
Definition: IP.php:196
IPTest\testSplitHostAndPort
testSplitHostAndPort( $expected, $input, $description)
Test for IP::splitHostAndPort().
Definition: IPTest.php:532
IP\toUnsigned
static toUnsigned( $ip)
Given an IP address in dotted-quad/octet notation, returns an unsigned integer.
Definition: IP.php:495
IP\splitHostAndPort
static splitHostAndPort( $both)
Given a host/port string, like one might find in the host part of a URL per RFC 2732,...
Definition: IP.php:249
IPTest\testIPIsInRange
testIPIsInRange( $expected, $addr, $range, $message='')
Issues there are most probably from IP::toHex() or IP::parseRange() @covers IP::isInRange @dataProvid...
Definition: IPTest.php:492
IPTest\testisIPv6
testisIPv6()
@covers IP::isIPv6
Definition: IPTest.php:53
IPTest\assertFalseCIDR
assertFalseCIDR( $CIDR, $msg='')
Definition: IPTest.php:381
IPTest\provideIPCIDRs
static provideIPCIDRs()
Provider for IP::testSanitizeRange()
Definition: IPTest.php:590
IPTest\provideCombineHostAndPort
static provideCombineHostAndPort()
Provider for IP::combineHostAndPort()
Definition: IPTest.php:570
IPTest\testCombineHostAndPort
testCombineHostAndPort( $expected, $input, $description)
Test for IP::combineHostAndPort() @dataProvider provideCombineHostAndPort.
Definition: IPTest.php:559
IP\isValid
static isValid( $ip)
Validate an IP address.
Definition: IP.php:108
IPTest\provideIPsAndRanges
static provideIPsAndRanges()
Provider for testIPIsInRange()
Definition: IPTest.php:501
IP\isIPv4
static isIPv4( $ip)
Given a string, determine if it as valid IP in IPv4 only.
Definition: IP.php:96
IP\parseCIDR
static parseCIDR( $range)
Convert a network specification in CIDR notation to an integer network and a number of bits.
Definition: IP.php:531
IPTest
Tests for IP validity functions.
Definition: IPTest.php:12
IPTest\testInvalidIPs
testInvalidIPs()
@covers IP::isValid
Definition: IPTest.php:179
IP\sanitizeIP
static sanitizeIP( $ip)
Convert an IP into a verbose, uppercase, normalized form.
Definition: IP.php:135
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
IPTest\testisIPv4
testisIPv4()
@covers IP::isIPv4
Definition: IPTest.php:111
IPTest\testSanitizeIP
testSanitizeIP( $expected, $input)
@covers IP::sanitizeIP @dataProvider provideSanitizeIP
Definition: IPTest.php:276
IPTest\testToUnsigned
testToUnsigned( $expected, $input)
@covers IP::toUnsigned @dataProvider provideToUnsigned
Definition: IPTest.php:307
IP\isValidBlock
static isValidBlock( $ipblock)
Validate an IP Block (valid address WITH a valid prefix).
Definition: IP.php:121
IPTest\provideToHex
static provideToHex()
Provider for IP::testToHex()
Definition: IPTest.php:348
IP\sanitizeRange
static sanitizeRange( $range)
Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111....
Definition: IP.php:773
IP\isIPAddress
static isIPAddress( $ip)
Determine if a string is as valid IP address or network (CIDR prefix).
Definition: IP.php:74
IPTest\provideIPsToPrettify
static provideIPsToPrettify()
Provider for IP::testPrettifyIP()
Definition: IPTest.php:614