b54222de2a45427e07ba56426584cbf8de88dd8b
[lhc/web/wiklou.git] / tests / phpunit / includes / IPTest.php
1 <?php
2 /*
3 * Tests for IP validity functions. Ported from /t/inc/IP.t by avar.
4 */
5
6 class IPTest extends MediaWikiTestCase {
7 /**
8 * not sure it should be tested with boolean false. hashar 20100924
9 */
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' );
23
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 "::"' );
29
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" );
34 }
35 }
36
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' );
42
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::' ) );
49
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 "::"' );
52
53 $this->assertFalse( IP::isIPv6( ':::' ) );
54 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
55
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' ) );
65
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' );
68
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' );
72
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' );
82
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' );
85
86 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
87 }
88
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' );
98
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' ) );
102 }
103
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 ) {
110 $ip = "$f.$f.$f.$f";
111 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
112 }
113 }
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" );
121 }
122 }
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' );
128
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::' ) );
132
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' );
140
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 "::"' );
143 }
144
145 public function testInvalidIPs() {
146 // Out of range...
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 ) {
152 $ip = "$f.$f.$f.$f";
153 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
154 }
155 }
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" );
163 }
164 }
165 // Have CIDR
166 $ipCIDRs = array(
167 '212.35.31.121/32',
168 '212.35.31.121/18',
169 '212.35.31.121/24',
170 '::ff:d:321:5/96',
171 'ff::d3:321:5/116',
172 'c:ff:12:1:ea:d:321:5/120',
173 );
174 foreach ( $ipCIDRs as $i ) {
175 $this->assertFalse( IP::isValid( $i ),
176 "$i is an invalid IP address because it is a block" );
177 }
178 // Incomplete/garbage
179 $invalid = array(
180 'www.xn--var-xla.net',
181 '216.17.184.G',
182 '216.17.184.1.',
183 '216.17.184',
184 '216.17.184.',
185 '256.17.184.1'
186 );
187 foreach ( $invalid as $i ) {
188 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
189 }
190 }
191
192 public function testValidBlocks() {
193 $valid = array(
194 '116.17.184.5/32',
195 '0.17.184.5/30',
196 '16.17.184.1/24',
197 '30.242.52.14/1',
198 '10.232.52.13/8',
199 '30.242.52.14/0',
200 '::e:f:2001/96',
201 '::c:f:2001/128',
202 '::10:f:2001/70',
203 '::fe:f:2001/1',
204 '::6d:f:2001/8',
205 '::fe:f:2001/0',
206 );
207 foreach ( $valid as $i ) {
208 $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
209 }
210 }
211
212 public function testInvalidBlocks() {
213 $invalid = array(
214 '116.17.184.5/33',
215 '0.17.184.5/130',
216 '16.17.184.1/-1',
217 '10.232.52.13/*',
218 '7.232.52.13/ab',
219 '11.232.52.13/',
220 '::e:f:2001/129',
221 '::c:f:2001/228',
222 '::10:f:2001/-1',
223 '::6d:f:2001/*',
224 '::86:f:2001/ab',
225 '::23:f:2001/',
226 );
227 foreach ( $invalid as $i ) {
228 $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
229 }
230 }
231
232 /**
233 * Improve IP::sanitizeIP() code coverage
234 * @todo Most probably incomplete
235 */
236 public function testSanitizeIP() {
237 $this->assertNull( IP::sanitizeIP('') );
238 $this->assertNull( IP::sanitizeIP(' ') );
239 }
240
241 /**
242 * test wrapper around ip2long which might return -1 or false depending on PHP version
243 */
244 public function testip2longWrapper() {
245 // fixme : add more tests ?
246 $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
247 $i = 'IN.VA.LI.D';
248 $this->assertFalse( IP::toUnSigned( $i ) );
249 }
250
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" );
255 }
256 }
257
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 );
262 }
263
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" );
268 }
269
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' ) );
280 }
281
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' ) );
300 }
301
302 /*
303 * IP::parseCIDR() returns an array containing a signed IP address
304 * representing the network mask and the bit mask.
305 */
306 function testCIDRParsing() {
307 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
308 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
309
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" );
315
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') );
321
322 // FIXME : add more tests.
323
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' );
334 }
335
336
337 /**
338 * @covers IP::canonicalize
339 */
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' );
343 }
344
345 /**
346 * @covers IP::canonicalize
347 */
348 public function testIPCanonicalizeMappedAddress() {
349 $this->assertEquals(
350 '192.0.2.152',
351 IP::canonicalize( '::ffff:192.0.2.152' )
352 );
353 $this->assertEquals(
354 '192.0.2.152',
355 IP::canonicalize( '::192.0.2.152' )
356 );
357 }
358
359 /**
360 * Issues there are most probably from IP::toHex() or IP::parseRange()
361 * @covers IP::isInRange
362 * @dataProvider provideIPsAndRanges
363 */
364 public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
365 $this->assertEquals(
366 $expected,
367 IP::isInRange( $addr, $range ),
368 $message
369 );
370 }
371
372 /** Provider for testIPIsInRange() */
373 function provideIPsAndRanges() {
374 # Format: (expected boolean, address, range, optional message)
375 return array(
376 # IPv4
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' ),
380
381 array( false, '0.0.0.0' , '192.0.2.0/24' ),
382 array( false, '255.255.255' , '192.0.2.0/24' ),
383
384 # IPv6
385 array( false, '::1' , '2001:DB8::/32' ),
386 array( false, '::' , '2001:DB8::/32' ),
387 array( false, 'FE80::1', '2001:DB8::/32' ),
388
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',
394 '2001:DB8::/32' ),
395
396 array( false, '2001:0DB8:F::', '2001:DB8::/96' ),
397 );
398 }
399 }