b54222de2a45427e07ba56426584cbf8de88dd8b
3 * Tests for IP validity functions. Ported from /t/inc/IP.t by avar.
6 class IPTest
extends MediaWikiTestCase
{
8 * not sure it should be tested with boolean false. hashar 20100924
10 public function testisIPAddress() {
11 $this->assertFalse( IP
::isIPAddress( false ), 'Boolean false is not an IP' );
12 $this->assertFalse( IP
::isIPAddress( true ), 'Boolean true is not an IP' );
13 $this->assertFalse( IP
::isIPAddress( "" ), 'Empty string is not an IP' );
14 $this->assertFalse( IP
::isIPAddress( 'abc' ), 'Garbage IP string' );
15 $this->assertFalse( IP
::isIPAddress( ':' ), 'Single ":" is not an IP' );
16 $this->assertFalse( IP
::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
17 $this->assertFalse( IP
::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
18 $this->assertFalse( IP
::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
19 $this->assertFalse( IP
::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
20 $this->assertFalse( IP
::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
21 $this->assertFalse( IP
::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
22 $this->assertFalse( IP
::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
24 $this->assertTrue( IP
::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
25 $this->assertTrue( IP
::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
26 $this->assertTrue( IP
::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
27 $this->assertTrue( IP
::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
28 $this->assertTrue( IP
::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
30 $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
31 '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
32 foreach ( $validIPs as $ip ) {
33 $this->assertTrue( IP
::isIPAddress( $ip ), "$ip is a valid IP address" );
37 public function testisIPv6() {
38 $this->assertFalse( IP
::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
39 $this->assertFalse( IP
::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
40 $this->assertFalse( IP
::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
41 $this->assertFalse( IP
::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
43 $this->assertTrue( IP
::isIPv6( 'fc:100::' ) );
44 $this->assertTrue( IP
::isIPv6( 'fc:100:a::' ) );
45 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d::' ) );
46 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1::' ) );
47 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1:e::' ) );
48 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
50 $this->assertFalse( IP
::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
51 $this->assertFalse( IP
::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
53 $this->assertFalse( IP
::isIPv6( ':::' ) );
54 $this->assertFalse( IP
::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
56 $this->assertTrue( IP
::isIPv6( '::' ), 'IPv6 zero address' );
57 $this->assertTrue( IP
::isIPv6( '::0' ) );
58 $this->assertTrue( IP
::isIPv6( '::fc' ) );
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' ) );
66 $this->assertFalse( IP
::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
67 $this->assertFalse( IP
::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
69 $this->assertFalse( IP
::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
70 $this->assertFalse( IP
::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
71 $this->assertFalse( IP
::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
73 $this->assertTrue( IP
::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
74 $this->assertTrue( IP
::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
75 $this->assertTrue( IP
::isIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' ) );
76 $this->assertTrue( IP
::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
77 $this->assertTrue( IP
::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
78 $this->assertTrue( IP
::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
79 $this->assertTrue( IP
::isIPv6( '2001::df'), 'IPv6 with "::" and 2 words' );
80 $this->assertTrue( IP
::isIPv6( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
81 $this->assertTrue( IP
::isIPv6( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
83 $this->assertFalse( IP
::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
84 $this->assertFalse( IP
::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
86 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
89 public function testisIPv4() {
90 $this->assertFalse( IP
::isIPv4( false ), 'Boolean false is not an IP' );
91 $this->assertFalse( IP
::isIPv4( true ), 'Boolean true is not an IP' );
92 $this->assertFalse( IP
::isIPv4( "" ), 'Empty string is not an IP' );
93 $this->assertFalse( IP
::isIPv4( 'abc' ) );
94 $this->assertFalse( IP
::isIPv4( ':' ) );
95 $this->assertFalse( IP
::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
96 $this->assertFalse( IP
::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
97 $this->assertFalse( IP
::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
99 $this->assertTrue( IP
::isIPv4( '124.24.52.13' ) );
100 $this->assertTrue( IP
::isIPv4( '1.24.52.13' ) );
101 $this->assertTrue( IP
::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
104 public function testValidIPs() {
105 foreach ( range( 0, 255 ) as $i ) {
106 $a = sprintf( "%03d", $i );
107 $b = sprintf( "%02d", $i );
108 $c = sprintf( "%01d", $i );
109 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
111 $this->assertTrue( IP
::isValid( $ip ) , "$ip is a valid IPv4 address" );
114 foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
115 $a = sprintf( "%04x", $i );
116 $b = sprintf( "%03x", $i );
117 $c = sprintf( "%02x", $i );
118 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
119 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
120 $this->assertTrue( IP
::isValid( $ip ) , "$ip is a valid IPv6 address" );
123 // test with some abbreviations
124 $this->assertFalse( IP
::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
125 $this->assertFalse( IP
::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
126 $this->assertFalse( IP
::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
127 $this->assertFalse( IP
::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
129 $this->assertTrue( IP
::isValid( 'fc:100::' ) );
130 $this->assertTrue( IP
::isValid( 'fc:100:a:d:1:e::' ) );
131 $this->assertTrue( IP
::isValid( 'fc:100:a:d:1:e:ac::' ) );
133 $this->assertTrue( IP
::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
134 $this->assertTrue( IP
::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
135 $this->assertTrue( IP
::isValid( '2001::df'), 'IPv6 with "::" and 2 words' );
136 $this->assertTrue( IP
::isValid( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
137 $this->assertTrue( IP
::isValid( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
138 $this->assertTrue( IP
::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
139 $this->assertTrue( IP
::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
141 $this->assertFalse( IP
::isValid( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
142 $this->assertFalse( IP
::isValid( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
145 public function testInvalidIPs() {
147 foreach ( range( 256, 999 ) as $i ) {
148 $a = sprintf( "%03d", $i );
149 $b = sprintf( "%02d", $i );
150 $c = sprintf( "%01d", $i );
151 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
153 $this->assertFalse( IP
::isValid( $ip ), "$ip is not a valid IPv4 address" );
156 foreach ( range( 'g', 'z' ) as $i ) {
157 $a = sprintf( "%04s", $i );
158 $b = sprintf( "%03s", $i );
159 $c = sprintf( "%02s", $i );
160 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
161 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
162 $this->assertFalse( IP
::isValid( $ip ) , "$ip is not a valid IPv6 address" );
172 'c:ff:12:1:ea:d:321:5/120',
174 foreach ( $ipCIDRs as $i ) {
175 $this->assertFalse( IP
::isValid( $i ),
176 "$i is an invalid IP address because it is a block" );
178 // Incomplete/garbage
180 'www.xn--var-xla.net',
187 foreach ( $invalid as $i ) {
188 $this->assertFalse( IP
::isValid( $i ), "$i is an invalid IP address" );
192 public function testValidBlocks() {
207 foreach ( $valid as $i ) {
208 $this->assertTrue( IP
::isValidBlock( $i ), "$i is a valid IP block" );
212 public function testInvalidBlocks() {
227 foreach ( $invalid as $i ) {
228 $this->assertFalse( IP
::isValidBlock( $i ), "$i is not a valid IP block" );
233 * Improve IP::sanitizeIP() code coverage
234 * @todo Most probably incomplete
236 public function testSanitizeIP() {
237 $this->assertNull( IP
::sanitizeIP('') );
238 $this->assertNull( IP
::sanitizeIP(' ') );
242 * test wrapper around ip2long which might return -1 or false depending on PHP version
244 public function testip2longWrapper() {
245 // fixme : add more tests ?
246 $this->assertEquals( pow(2,32) - 1, IP
::toUnsigned( '255.255.255.255' ));
248 $this->assertFalse( IP
::toUnSigned( $i ) );
251 public function testPrivateIPs() {
252 $private = array( 'fc::3', 'fc::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
253 foreach ( $private as $p ) {
254 $this->assertFalse( IP
::isPublic( $p ), "$p is not a public IP address" );
258 // Private wrapper used to test CIDR Parsing.
259 private function assertFalseCIDR( $CIDR, $msg='' ) {
260 $ff = array( false, false );
261 $this->assertEquals( $ff, IP
::parseCIDR( $CIDR ), $msg );
264 // Private wrapper to test network shifting using only dot notation
265 private function assertNet( $expected, $CIDR ) {
266 $parse = IP
::parseCIDR( $CIDR );
267 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
270 public function testHexToQuad() {
271 $this->assertEquals( '0.0.0.1' , IP
::hexToQuad( '00000001' ) );
272 $this->assertEquals( '255.0.0.0' , IP
::hexToQuad( 'FF000000' ) );
273 $this->assertEquals( '255.255.255.255', IP
::hexToQuad( 'FFFFFFFF' ) );
274 $this->assertEquals( '10.188.222.255' , IP
::hexToQuad( '0ABCDEFF' ) );
275 // hex not left-padded...
276 $this->assertEquals( '0.0.0.0' , IP
::hexToQuad( '0' ) );
277 $this->assertEquals( '0.0.0.1' , IP
::hexToQuad( '1' ) );
278 $this->assertEquals( '0.0.0.255' , IP
::hexToQuad( 'FF' ) );
279 $this->assertEquals( '0.0.255.0' , IP
::hexToQuad( 'FF00' ) );
282 public function testHexToOctet() {
283 $this->assertEquals( '0:0:0:0:0:0:0:1',
284 IP
::hexToOctet( '00000000000000000000000000000001' ) );
285 $this->assertEquals( '0:0:0:0:0:0:FF:3',
286 IP
::hexToOctet( '00000000000000000000000000FF0003' ) );
287 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
288 IP
::hexToOctet( '000000000000000000000000FF000006' ) );
289 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
290 IP
::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
291 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
292 IP
::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
293 // hex not left-padded...
294 $this->assertEquals( '0:0:0:0:0:0:0:0' , IP
::hexToOctet( '0' ) );
295 $this->assertEquals( '0:0:0:0:0:0:0:1' , IP
::hexToOctet( '1' ) );
296 $this->assertEquals( '0:0:0:0:0:0:0:FF' , IP
::hexToOctet( 'FF' ) );
297 $this->assertEquals( '0:0:0:0:0:0:0:FFD0' , IP
::hexToOctet( 'FFD0' ) );
298 $this->assertEquals( '0:0:0:0:0:0:FA00:0' , IP
::hexToOctet( 'FA000000' ) );
299 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP
::hexToOctet( 'FCCFFAFF' ) );
303 * IP::parseCIDR() returns an array containing a signed IP address
304 * representing the network mask and the bit mask.
306 function testCIDRParsing() {
307 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
308 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
310 // Verify if statement
311 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
312 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
313 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
314 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
316 // Check internal logic
317 # 0 mask always result in array(0,0)
318 $this->assertEquals( array( 0, 0 ), IP
::parseCIDR('192.0.0.2/0') );
319 $this->assertEquals( array( 0, 0 ), IP
::parseCIDR('0.0.0.0/0') );
320 $this->assertEquals( array( 0, 0 ), IP
::parseCIDR('255.255.255.255/0') );
322 // FIXME : add more tests.
324 # This part test network shifting
325 $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
326 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
327 $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
328 $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
329 $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
330 $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
331 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
332 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
333 $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
338 * @covers IP::canonicalize
340 public function testIPCanonicalizeOnValidIp() {
341 $this->assertEquals( '192.0.2.152', IP
::canonicalize( '192.0.2.152' ),
342 'Canonicalization of a valid IP returns it unchanged' );
346 * @covers IP::canonicalize
348 public function testIPCanonicalizeMappedAddress() {
351 IP
::canonicalize( '::ffff:192.0.2.152' )
355 IP
::canonicalize( '::192.0.2.152' )
360 * Issues there are most probably from IP::toHex() or IP::parseRange()
361 * @covers IP::isInRange
362 * @dataProvider provideIPsAndRanges
364 public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
367 IP
::isInRange( $addr, $range ),
372 /** Provider for testIPIsInRange() */
373 function provideIPsAndRanges() {
374 # Format: (expected boolean, address, range, optional message)
377 array( true , '192.0.2.0' , '192.0.2.0/24', 'Network address' ),
378 array( true , '192.0.2.77' , '192.0.2.0/24', 'Simple address' ),
379 array( true , '192.0.2.255' , '192.0.2.0/24', 'Broadcast address' ),
381 array( false, '0.0.0.0' , '192.0.2.0/24' ),
382 array( false, '255.255.255' , '192.0.2.0/24' ),
385 array( false, '::1' , '2001:DB8::/32' ),
386 array( false, '::' , '2001:DB8::/32' ),
387 array( false, 'FE80::1', '2001:DB8::/32' ),
389 array( true , '2001:DB8::' , '2001:DB8::/32' ),
390 array( true , '2001:0DB8::' , '2001:DB8::/32' ),
391 array( true , '2001:DB8::1' , '2001:DB8::/32' ),
392 array( true , '2001:0DB8::1', '2001:DB8::/32' ),
393 array( true , '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
396 array( false, '2001:0DB8:F::', '2001:DB8::/96' ),