MediaWiki  master
IPTest.php
Go to the documentation of this file.
1 <?php
12 
14 
19  public function testIsNotIPAddress( $val, $desc ) {
20  $this->assertFalse( IP::isIPAddress( $val ), $desc );
21  }
22 
26  public function provideInvalidIPs() {
27  return [
28  [ false, 'Boolean false is not an IP' ],
29  [ true, 'Boolean true is not an IP' ],
30  [ '', 'Empty string is not an IP' ],
31  [ 'abc', 'Garbage IP string' ],
32  [ ':', 'Single ":" is not an IP' ],
33  [ '2001:0DB8::A:1::1', 'IPv6 with a double :: occurrence' ],
34  [ '2001:0DB8::A:1::', 'IPv6 with a double :: occurrence, last at end' ],
35  [ '::2001:0DB8::5:1', 'IPv6 with a double :: occurrence, firt at beginning' ],
36  [ '124.24.52', 'IPv4 not enough quads' ],
37  [ '24.324.52.13', 'IPv4 out of range' ],
38  [ '.24.52.13', 'IPv4 starts with period' ],
39  [ 'fc:100:300', 'IPv6 with only 3 words' ],
40  ];
41  }
42 
46  public function testisIPAddress() {
47  $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
48  $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
49  $this->assertTrue( IP::isIPAddress( '74.24.52.13/20' ), 'IPv4 range' );
50  $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
51  $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
52 
53  $validIPs = [ 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
54  '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' ];
55  foreach ( $validIPs as $ip ) {
56  $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
57  }
58  }
59 
63  public function testisIPv6() {
64  $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
65  $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
66  $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
67  $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
68 
69  $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
70  $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
71  $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
72  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
73  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
74  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
75 
76  $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
77  $this->assertFalse(
78  IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ),
79  'IPv6 with 9 words ending with "::"'
80  );
81 
82  $this->assertFalse( IP::isIPv6( ':::' ) );
83  $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
84 
85  $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
86  $this->assertTrue( IP::isIPv6( '::0' ) );
87  $this->assertTrue( IP::isIPv6( '::fc' ) );
88  $this->assertTrue( IP::isIPv6( '::fc:100' ) );
89  $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
90  $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
91  $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
92  $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
93  $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
94 
95  $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
96  $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
97 
98  $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
99  $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
100  $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
101 
102  $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
103  $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
104  $this->assertTrue( IP::isIPv6( 'fc::100:a:d' ), 'IPv6 with "::" and 4 words' );
105  $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
106  $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
107  $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
108  $this->assertTrue( IP::isIPv6( '2001::df' ), 'IPv6 with "::" and 2 words' );
109  $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
110  $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
111 
112  $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
113  $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
114 
115  $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
116  }
117 
122  public function testisNotIPv4( $bogusIP, $desc ) {
123  $this->assertFalse( IP::isIPv4( $bogusIP ), $desc );
124  }
125 
126  public function provideInvalidIPv4Addresses() {
127  return [
128  [ false, 'Boolean false is not an IP' ],
129  [ true, 'Boolean true is not an IP' ],
130  [ '', 'Empty string is not an IP' ],
131  [ 'abc', 'Letters are not an IP' ],
132  [ ':', 'A colon is not an IP' ],
133  [ '124.24.52', 'IPv4 not enough quads' ],
134  [ '24.324.52.13', 'IPv4 out of range' ],
135  [ '.24.52.13', 'IPv4 starts with period' ],
136  ];
137  }
138 
143  public function testIsIPv4( $ip, $desc ) {
144  $this->assertTrue( IP::isIPv4( $ip ), $desc );
145  }
146 
150  public function provideValidIPv4Address() {
151  return [
152  [ '124.24.52.13', 'Valid IPv4 address' ],
153  [ '1.24.52.13', 'Another valid IPv4 address' ],
154  [ '74.24.52.13/20', 'An IPv4 range' ],
155  ];
156  }
157 
161  public function testValidIPs() {
162  foreach ( range( 0, 255 ) as $i ) {
163  $a = sprintf( "%03d", $i );
164  $b = sprintf( "%02d", $i );
165  $c = sprintf( "%01d", $i );
166  foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
167  $ip = "$f.$f.$f.$f";
168  $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv4 address" );
169  }
170  }
171  foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
172  $a = sprintf( "%04x", $i );
173  $b = sprintf( "%03x", $i );
174  $c = sprintf( "%02x", $i );
175  foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
176  $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
177  $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv6 address" );
178  }
179  }
180  // test with some abbreviations
181  $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
182  $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
183  $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
184  $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
185 
186  $this->assertTrue( IP::isValid( 'fc:100::' ) );
187  $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
188  $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
189 
190  $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
191  $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
192  $this->assertTrue( IP::isValid( '2001::df' ), 'IPv6 with "::" and 2 words' );
193  $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
194  $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
195  $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
196  $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
197 
198  $this->assertFalse(
199  IP::isValid( 'fc:100:a:d:1:e:ac:0::' ),
200  'IPv6 with 8 words ending with "::"'
201  );
202  $this->assertFalse(
203  IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ),
204  'IPv6 with 9 words ending with "::"'
205  );
206  }
207 
211  public function testInvalidIPs() {
212  // Out of range...
213  foreach ( range( 256, 999 ) as $i ) {
214  $a = sprintf( "%03d", $i );
215  $b = sprintf( "%02d", $i );
216  $c = sprintf( "%01d", $i );
217  foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
218  $ip = "$f.$f.$f.$f";
219  $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
220  }
221  }
222  foreach ( range( 'g', 'z' ) as $i ) {
223  $a = sprintf( "%04s", $i );
224  $b = sprintf( "%03s", $i );
225  $c = sprintf( "%02s", $i );
226  foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
227  $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
228  $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv6 address" );
229  }
230  }
231  // Have CIDR
232  $ipCIDRs = [
233  '212.35.31.121/32',
234  '212.35.31.121/18',
235  '212.35.31.121/24',
236  '::ff:d:321:5/96',
237  'ff::d3:321:5/116',
238  'c:ff:12:1:ea:d:321:5/120',
239  ];
240  foreach ( $ipCIDRs as $i ) {
241  $this->assertFalse( IP::isValid( $i ),
242  "$i is an invalid IP address because it is a range" );
243  }
244  // Incomplete/garbage
245  $invalid = [
246  'www.xn--var-xla.net',
247  '216.17.184.G',
248  '216.17.184.1.',
249  '216.17.184',
250  '216.17.184.',
251  '256.17.184.1'
252  ];
253  foreach ( $invalid as $i ) {
254  $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
255  }
256  }
257 
261  public function provideValidRanges() {
262  return [
263  [ '116.17.184.5/32' ],
264  [ '0.17.184.5/30' ],
265  [ '16.17.184.1/24' ],
266  [ '30.242.52.14/1' ],
267  [ '10.232.52.13/8' ],
268  [ '30.242.52.14/0' ],
269  [ '::e:f:2001/96' ],
270  [ '::c:f:2001/128' ],
271  [ '::10:f:2001/70' ],
272  [ '::fe:f:2001/1' ],
273  [ '::6d:f:2001/8' ],
274  [ '::fe:f:2001/0' ],
275  ];
276  }
277 
282  public function testValidRanges( $range ) {
283  $this->assertTrue( IP::isValidRange( $range ), "$range is a valid IP range" );
284  }
285 
290  public function testInvalidRanges( $invalid ) {
291  $this->assertFalse( IP::isValidRange( $invalid ), "$invalid is not a valid IP range" );
292  }
293 
294  public function provideInvalidRanges() {
295  return [
296  [ '116.17.184.5/33' ],
297  [ '0.17.184.5/130' ],
298  [ '16.17.184.1/-1' ],
299  [ '10.232.52.13/*' ],
300  [ '7.232.52.13/ab' ],
301  [ '11.232.52.13/' ],
302  [ '::e:f:2001/129' ],
303  [ '::c:f:2001/228' ],
304  [ '::10:f:2001/-1' ],
305  [ '::6d:f:2001/*' ],
306  [ '::86:f:2001/ab' ],
307  [ '::23:f:2001/' ],
308  ];
309  }
310 
315  public function testSanitizeIP( $expected, $input ) {
317  $this->assertEquals( $expected, $result );
318  }
319 
323  public static function provideSanitizeIP() {
324  return [
325  [ '0.0.0.0', '0.0.0.0' ],
326  [ '0.0.0.0', '00.00.00.00' ],
327  [ '0.0.0.0', '000.000.000.000' ],
328  [ '0.0.0.0/24', '000.000.000.000/24' ],
329  [ '141.0.11.253', '141.000.011.253' ],
330  [ '1.2.4.5', '1.2.4.5' ],
331  [ '1.2.4.5', '01.02.04.05' ],
332  [ '1.2.4.5', '001.002.004.005' ],
333  [ '10.0.0.1', '010.0.000.1' ],
334  [ '80.72.250.4', '080.072.250.04' ],
335  [ 'Foo.1000.00', 'Foo.1000.00' ],
336  [ 'Bar.01', 'Bar.01' ],
337  [ 'Bar.010', 'Bar.010' ],
338  [ null, '' ],
339  [ null, ' ' ]
340  ];
341  }
342 
347  public function testToHex( $expected, $input ) {
348  $result = IP::toHex( $input );
349  $this->assertTrue( $result === false || is_string( $result ) );
350  $this->assertEquals( $expected, $result );
351  }
352 
356  public static function provideToHex() {
357  return [
358  [ '00000001', '0.0.0.1' ],
359  [ '01020304', '1.2.3.4' ],
360  [ '7F000001', '127.0.0.1' ],
361  [ '80000000', '128.0.0.0' ],
362  [ 'DEADCAFE', '222.173.202.254' ],
363  [ 'FFFFFFFF', '255.255.255.255' ],
364  [ '8D000BFD', '141.000.11.253' ],
365  [ false, 'IN.VA.LI.D' ],
366  [ 'v6-00000000000000000000000000000001', '::1' ],
367  [ 'v6-20010DB885A3000000008A2E03707334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ],
368  [ 'v6-20010DB885A3000000008A2E03707334', '2001:db8:85a3::8a2e:0370:7334' ],
369  [ false, 'IN:VA::LI:D' ],
370  [ false, ':::1' ]
371  ];
372  }
373 
378  public function testIsPublic( $expected, $input ) {
380  $this->assertEquals( $expected, $result );
381  }
382 
386  public static function provideIsPublic() {
387  return [
388  [ false, 'fc00::3' ], # RFC 4193 (local)
389  [ false, 'fc00::ff' ], # RFC 4193 (local)
390  [ false, '127.1.2.3' ], # loopback
391  [ false, '::1' ], # loopback
392  [ false, 'fe80::1' ], # link-local
393  [ false, '169.254.1.1' ], # link-local
394  [ false, '10.0.0.1' ], # RFC 1918 (private)
395  [ false, '172.16.0.1' ], # RFC 1918 (private)
396  [ false, '192.168.0.1' ], # RFC 1918 (private)
397  [ true, '2001:5c0:1000:a::133' ], # public
398  [ true, 'fc::3' ], # public
399  [ true, '00FC::' ] # public
400  ];
401  }
402 
403  // Private wrapper used to test CIDR Parsing.
404  private function assertFalseCIDR( $CIDR, $msg = '' ) {
405  $ff = [ false, false ];
406  $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
407  }
408 
409  // Private wrapper to test network shifting using only dot notation
410  private function assertNet( $expected, $CIDR ) {
411  $parse = IP::parseCIDR( $CIDR );
412  $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
413  }
414 
419  public function testHexToQuad( $ip, $hex ) {
420  $this->assertEquals( $ip, IP::hexToQuad( $hex ) );
421  }
422 
426  public function provideIPsandHexes() {
427  return [
428  [ '0.0.0.1', '00000001' ],
429  [ '255.0.0.0', 'FF000000' ],
430  [ '255.255.255.255', 'FFFFFFFF' ],
431  [ '10.188.222.255', '0ABCDEFF' ],
432  // hex not left-padded...
433  [ '0.0.0.0', '0' ],
434  [ '0.0.0.1', '1' ],
435  [ '0.0.0.255', 'FF' ],
436  [ '0.0.255.0', 'FF00' ],
437  ];
438  }
439 
444  public function testHexToOctet( $octet, $hex ) {
445  $this->assertEquals( $octet, IP::hexToOctet( $hex ) );
446  }
447 
451  public function provideOctetsAndHexes() {
452  return [
453  [ '0:0:0:0:0:0:0:1', '00000000000000000000000000000001' ],
454  [ '0:0:0:0:0:0:FF:3', '00000000000000000000000000FF0003' ],
455  [ '0:0:0:0:0:0:FF00:6', '000000000000000000000000FF000006' ],
456  [ '0:0:0:0:0:0:FCCF:FAFF', '000000000000000000000000FCCFFAFF' ],
457  [ 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF', 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ],
458  // hex not left-padded...
459  [ '0:0:0:0:0:0:0:0', '0' ],
460  [ '0:0:0:0:0:0:0:1', '1' ],
461  [ '0:0:0:0:0:0:0:FF', 'FF' ],
462  [ '0:0:0:0:0:0:0:FFD0', 'FFD0' ],
463  [ '0:0:0:0:0:0:FA00:0', 'FA000000' ],
464  [ '0:0:0:0:0:0:FCCF:FAFF', 'FCCFFAFF' ],
465  ];
466  }
467 
473  public function testCIDRParsing() {
474  $this->assertFalseCIDR( '192.0.2.0', "missing mask" );
475  $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
476 
477  // Verify if statement
478  $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
479  $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
480  $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
481  $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
482 
483  // Check internal logic
484  # 0 mask always result in [ 0, 0 ]
485  $this->assertEquals( [ 0, 0 ], IP::parseCIDR( '192.0.0.2/0' ) );
486  $this->assertEquals( [ 0, 0 ], IP::parseCIDR( '0.0.0.0/0' ) );
487  $this->assertEquals( [ 0, 0 ], IP::parseCIDR( '255.255.255.255/0' ) );
488 
489  // @todo FIXME: Add more tests.
490 
491  # This part test network shifting
492  $this->assertNet( '192.0.0.0', '192.0.0.2/24' );
493  $this->assertNet( '192.168.5.0', '192.168.5.13/24' );
494  $this->assertNet( '10.0.0.160', '10.0.0.161/28' );
495  $this->assertNet( '10.0.0.0', '10.0.0.3/28' );
496  $this->assertNet( '10.0.0.0', '10.0.0.3/30' );
497  $this->assertNet( '10.0.0.4', '10.0.0.4/30' );
498  $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
499  $this->assertNet( '10.128.0.0', '10.135.0.0/9' );
500  $this->assertNet( '134.0.0.0', '134.0.5.1/8' );
501  }
502 
506  public function testIPCanonicalizeOnValidIp() {
507  $this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ),
508  'Canonicalization of a valid IP returns it unchanged' );
509  }
510 
515  $this->assertEquals(
516  '192.0.2.152',
517  IP::canonicalize( '::ffff:192.0.2.152' )
518  );
519  $this->assertEquals(
520  '192.0.2.152',
521  IP::canonicalize( '::192.0.2.152' )
522  );
523  }
524 
530  public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
531  $this->assertEquals(
532  $expected,
533  IP::isInRange( $addr, $range ),
534  $message
535  );
536  }
537 
539  public static function provideIPsAndRanges() {
540  # Format: (expected boolean, address, range, optional message)
541  return [
542  # IPv4
543  [ true, '192.0.2.0', '192.0.2.0/24', 'Network address' ],
544  [ true, '192.0.2.77', '192.0.2.0/24', 'Simple address' ],
545  [ true, '192.0.2.255', '192.0.2.0/24', 'Broadcast address' ],
546 
547  [ false, '0.0.0.0', '192.0.2.0/24' ],
548  [ false, '255.255.255', '192.0.2.0/24' ],
549 
550  # IPv6
551  [ false, '::1', '2001:DB8::/32' ],
552  [ false, '::', '2001:DB8::/32' ],
553  [ false, 'FE80::1', '2001:DB8::/32' ],
554 
555  [ true, '2001:DB8::', '2001:DB8::/32' ],
556  [ true, '2001:0DB8::', '2001:DB8::/32' ],
557  [ true, '2001:DB8::1', '2001:DB8::/32' ],
558  [ true, '2001:0DB8::1', '2001:DB8::/32' ],
559  [ true, '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
560  '2001:DB8::/32' ],
561 
562  [ false, '2001:0DB8:F::', '2001:DB8::/96' ],
563  ];
564  }
565 
570  public function testSplitHostAndPort( $expected, $input, $description ) {
571  $this->assertEquals( $expected, IP::splitHostAndPort( $input ), $description );
572  }
573 
577  public static function provideSplitHostAndPort() {
578  return [
579  [ false, '[', 'Unclosed square bracket' ],
580  [ false, '[::', 'Unclosed square bracket 2' ],
581  [ [ '::', false ], '::', 'Bare IPv6 0' ],
582  [ [ '::1', false ], '::1', 'Bare IPv6 1' ],
583  [ [ '::', false ], '[::]', 'Bracketed IPv6 0' ],
584  [ [ '::1', false ], '[::1]', 'Bracketed IPv6 1' ],
585  [ [ '::1', 80 ], '[::1]:80', 'Bracketed IPv6 with port' ],
586  [ false, '::x', 'Double colon but no IPv6' ],
587  [ [ 'x', 80 ], 'x:80', 'Hostname and port' ],
588  [ false, 'x:x', 'Hostname and invalid port' ],
589  [ [ 'x', false ], 'x', 'Plain hostname' ]
590  ];
591  }
592 
597  public function testCombineHostAndPort( $expected, $input, $description ) {
598  list( $host, $port, $defaultPort ) = $input;
599  $this->assertEquals(
600  $expected,
601  IP::combineHostAndPort( $host, $port, $defaultPort ),
602  $description );
603  }
604 
608  public static function provideCombineHostAndPort() {
609  return [
610  [ '[::1]', [ '::1', 2, 2 ], 'IPv6 default port' ],
611  [ '[::1]:2', [ '::1', 2, 3 ], 'IPv6 non-default port' ],
612  [ 'x', [ 'x', 2, 2 ], 'Normal default port' ],
613  [ 'x:2', [ 'x', 2, 3 ], 'Normal non-default port' ],
614  ];
615  }
616 
621  public function testSanitizeRange( $input, $expected, $description ) {
622  $this->assertEquals( $expected, IP::sanitizeRange( $input ), $description );
623  }
624 
628  public static function provideIPCIDRs() {
629  return [
630  [ '35.56.31.252/16', '35.56.0.0/16', 'IPv4 range' ],
631  [ '135.16.21.252/24', '135.16.21.0/24', 'IPv4 range' ],
632  [ '5.36.71.252/32', '5.36.71.252/32', 'IPv4 silly range' ],
633  [ '5.36.71.252', '5.36.71.252', 'IPv4 non-range' ],
634  [ '0:1:2:3:4:c5:f6:7/96', '0:1:2:3:4:C5:0:0/96', 'IPv6 range' ],
635  [ '0:1:2:3:4:5:6:7/120', '0:1:2:3:4:5:6:0/120', 'IPv6 range' ],
636  [ '0:e1:2:3:4:5:e6:7/128', '0:E1:2:3:4:5:E6:7/128', 'IPv6 silly range' ],
637  [ '0:c1:A2:3:4:5:c6:7', '0:C1:A2:3:4:5:C6:7', 'IPv6 non range' ],
638  ];
639  }
640 
645  public function testPrettifyIP( $ip, $prettified ) {
646  $this->assertEquals( $prettified, IP::prettifyIP( $ip ), "Prettify of $ip" );
647  }
648 
652  public static function provideIPsToPrettify() {
653  return [
654  [ '0:0:0:0:0:0:0:0', '::' ],
655  [ '0:0:0::0:0:0', '::' ],
656  [ '0:0:0:1:0:0:0:0', '0:0:0:1::' ],
657  [ '0:0::f', '::f' ],
658  [ '0::0:0:0:33:fef:b', '::33:fef:b' ],
659  [ '3f:535:0:0:0:0:e:fbb', '3f:535::e:fbb' ],
660  [ '0:0:fef:0:0:0:e:fbb', '0:0:fef::e:fbb' ],
661  [ 'abbc:2004::0:0:0:0', 'abbc:2004::' ],
662  [ 'cebc:2004:f:0:0:0:0:0', 'cebc:2004:f::' ],
663  [ '0:0:0:0:0:0:0:0/16', '::/16' ],
664  [ '0:0:0::0:0:0/64', '::/64' ],
665  [ '0:0::f/52', '::f/52' ],
666  [ '::0:0:33:fef:b/52', '::33:fef:b/52' ],
667  [ '3f:535:0:0:0:0:e:fbb/48', '3f:535::e:fbb/48' ],
668  [ '0:0:fef:0:0:0:e:fbb/96', '0:0:fef::e:fbb/96' ],
669  [ 'abbc:2004:0:0::0:0/40', 'abbc:2004::/40' ],
670  [ 'aebc:2004:f:0:0:0:0:0/80', 'aebc:2004:f::/80' ],
671  ];
672  }
673 }
testisIPAddress()
IP::isIPAddress.
Definition: IPTest.php:46
testHexToQuad( $ip, $hex)
IP::hexToQuad provideIPsAndHexes.
Definition: IPTest.php:419
static isValidRange( $ipRange)
Validate an IP range (valid address with a valid CIDR prefix).
Definition: IP.php:125
static isIPAddress( $ip)
Determine if a string is as valid IP address or network (CIDR prefix).
Definition: IP.php:77
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
provideInvalidIPv4Addresses()
Definition: IPTest.php:126
static toHex( $ip)
Return a zero-padded upper case hexadecimal representation of an IP address.
Definition: IP.php:404
if(is_array( $mode)) switch( $mode) $input
static provideIPCIDRs()
Provider for IP::testSanitizeRange()
Definition: IPTest.php:628
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
testIsNotIPAddress( $val, $desc)
IP::isIPAddress provideInvalidIPs.
Definition: IPTest.php:19
provideInvalidIPs()
Provide a list of things that aren&#39;t IP addresses.
Definition: IPTest.php:26
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testisIPv6()
IP::isIPv6.
Definition: IPTest.php:63
testInvalidIPs()
IP::isValid.
Definition: IPTest.php:211
testCombineHostAndPort( $expected, $input, $description)
IP::combineHostAndPort() provideCombineHostAndPort.
Definition: IPTest.php:597
static canonicalize( $addr)
Convert some unusual representations of IPv4 addresses to their canonical dotted quad representation...
Definition: IP.php:674
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:302
static provideSanitizeIP()
Provider for IP::testSanitizeIP()
Definition: IPTest.php:323
static sanitizeRange( $range)
Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111.113.151/24 -> 127.111.113.0/24.
Definition: IP.php:712
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. 'ImgAuthModifyHeaders':Executed just before a file is streamed to a user via img_auth.php, allowing headers to be modified beforehand. $title:LinkTarget object & $headers:HTTP headers(name=> value, names are case insensitive). Two headers get special handling:If-Modified-Since(value must be a valid HTTP date) and Range(must be of the form "bytes=(\*-\*)") will be honored when streaming the file. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. '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 '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 '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 '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. '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 IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() '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 Sanitizer::validateEmail(), 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 '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:Array with elements of the form "language:title" in the order that they will be output. & $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. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. 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:1970
static hexToQuad( $ip_hex)
Converts a hexadecimal number to an IPv4 address in quad-dotted notation.
Definition: IP.php:353
testSanitizeRange( $input, $expected, $description)
IP::sanitizeRange() provideIPCIDRs.
Definition: IPTest.php:621
provideValidIPv4Address()
Provide some IPv4 addresses and ranges.
Definition: IPTest.php:150
testisNotIPv4( $bogusIP, $desc)
IP::isIPv4 provideInvalidIPv4Addresses.
Definition: IPTest.php:122
testSanitizeIP( $expected, $input)
IP::sanitizeIP provideSanitizeIP.
Definition: IPTest.php:315
static provideIPsToPrettify()
Provider for IP::testPrettifyIP()
Definition: IPTest.php:652
testSplitHostAndPort( $expected, $input, $description)
IP::splitHostAndPort() provideSplitHostAndPort.
Definition: IPTest.php:570
static sanitizeIP( $ip)
Convert an IP into a verbose, uppercase, normalized form.
Definition: IP.php:139
testValidIPs()
IP::isValid.
Definition: IPTest.php:161
static isValid( $ip)
Validate an IP address.
Definition: IP.php:111
static provideSplitHostAndPort()
Provider for IP::splitHostAndPort()
Definition: IPTest.php:577
assertNet( $expected, $CIDR)
Definition: IPTest.php:410
testCIDRParsing()
IP::parseCIDR() returns an array containing a signed IP address representing the network mask and the...
Definition: IPTest.php:473
static provideIPsAndRanges()
Provider for testIPIsInRange()
Definition: IPTest.php:539
static provideToHex()
Provider for IP::testToHex()
Definition: IPTest.php:356
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
static splitHostAndPort( $both)
Given a host/port string, like one might find in the host part of a URL per RFC 2732, split the hostname part and the port part and return an array with an element for each.
Definition: IP.php:253
static isInRange( $addr, $range)
Determine if a given IPv4/IPv6 address is in a given CIDR network.
Definition: IP.php:637
provideIPsandHexes()
Provide some IP addresses and their equivalent hex representations.
Definition: IPTest.php:426
testIPCanonicalizeOnValidIp()
IP::canonicalize.
Definition: IPTest.php:506
testInvalidRanges( $invalid)
IP::isValidRange provideInvalidRanges.
Definition: IPTest.php:290
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
testToHex( $expected, $input)
IP::toHex provideToHex.
Definition: IPTest.php:347
testIsIPv4( $ip, $desc)
IP::isIPv4 provideValidIPv4Address.
Definition: IPTest.php:143
provideInvalidRanges()
Definition: IPTest.php:294
static hexToOctet( $ip_hex)
Converts a hexadecimal number to an IPv6 address in octet notation.
Definition: IP.php:333
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static isIPv6( $ip)
Given a string, determine if it as valid IP in IPv6 only.
Definition: IP.php:88
provideOctetsAndHexes()
Provide some hex and octet representations of the same IPs.
Definition: IPTest.php:451
static prettifyIP( $ip)
Prettify an IP for display to end users.
Definition: IP.php:200
static provideIsPublic()
Provider for IP::testIsPublic()
Definition: IPTest.php:386
static isIPv4( $ip)
Given a string, determine if it as valid IP in IPv4 only.
Definition: IP.php:99
testIPIsInRange( $expected, $addr, $range, $message='')
Issues there are most probably from IP::toHex() or IP::parseRange() IP::isInRange provideIPsAndRanges...
Definition: IPTest.php:530
testIPCanonicalizeMappedAddress()
IP::canonicalize.
Definition: IPTest.php:514
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template to be included in the link
Definition: hooks.txt:3039
$f
Definition: router.php:79
testIsPublic( $expected, $input)
IP::isPublic provideIsPublic.
Definition: IPTest.php:378
testHexToOctet( $octet, $hex)
IP::hexToOctet provideOctetsAndHexes.
Definition: IPTest.php:444
static provideCombineHostAndPort()
Provider for IP::combineHostAndPort()
Definition: IPTest.php:608
assertFalseCIDR( $CIDR, $msg='')
Definition: IPTest.php:404
provideValidRanges()
Provide some valid IP ranges.
Definition: IPTest.php:261
testValidRanges( $range)
IP::isValidRange provideValidRanges.
Definition: IPTest.php:282
static parseCIDR( $range)
Convert a network specification in CIDR notation to an integer network and a number of bits...
Definition: IP.php:457
testPrettifyIP( $ip, $prettified)
IP::prettifyIP() provideIPsToPrettify.
Definition: IPTest.php:645
Tests for IP validity functions.
Definition: IPTest.php:11
static isPublic( $ip)
Determine if an IP address really is an IP address, and if it is public, i.e.
Definition: IP.php:375