correct number of tests fu r94609
[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 * @covers IP::isIPAddress
10 */
11 public function testisIPAddress() {
12 $this->assertFalse( IP::isIPAddress( false ), 'Boolean false is not an IP' );
13 $this->assertFalse( IP::isIPAddress( true ), 'Boolean true is not an IP' );
14 $this->assertFalse( IP::isIPAddress( "" ), 'Empty string is not an IP' );
15 $this->assertFalse( IP::isIPAddress( 'abc' ), 'Garbage IP string' );
16 $this->assertFalse( IP::isIPAddress( ':' ), 'Single ":" is not an IP' );
17 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
18 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
19 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
20 $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
21 $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
22 $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
23 $this->assertFalse( IP::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
24
25 $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
26 $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
27 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
28 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
29 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
30
31 $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
32 '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
33 foreach ( $validIPs as $ip ) {
34 $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
35 }
36 }
37
38 /**
39 * @covers IP::isIPv6
40 */
41 public function testisIPv6() {
42 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
43 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
44 $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
45 $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
46
47 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
48 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
49 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
50 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
51 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
52 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
53
54 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
55 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
56
57 $this->assertFalse( IP::isIPv6( ':::' ) );
58 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
59
60 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
61 $this->assertTrue( IP::isIPv6( '::0' ) );
62 $this->assertTrue( IP::isIPv6( '::fc' ) );
63 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
64 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
65 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
66 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
67 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
68 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
69
70 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
71 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
72
73 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
74 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
75 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
76
77 $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
78 $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
79 $this->assertTrue( IP::isIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' ) );
80 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
81 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
82 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
83 $this->assertTrue( IP::isIPv6( '2001::df'), 'IPv6 with "::" and 2 words' );
84 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
85 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
86
87 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
88 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
89
90 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
91 }
92
93 /**
94 * @covers IP::isIPv4
95 */
96 public function testisIPv4() {
97 $this->assertFalse( IP::isIPv4( false ), 'Boolean false is not an IP' );
98 $this->assertFalse( IP::isIPv4( true ), 'Boolean true is not an IP' );
99 $this->assertFalse( IP::isIPv4( "" ), 'Empty string is not an IP' );
100 $this->assertFalse( IP::isIPv4( 'abc' ) );
101 $this->assertFalse( IP::isIPv4( ':' ) );
102 $this->assertFalse( IP::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
103 $this->assertFalse( IP::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
104 $this->assertFalse( IP::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
105
106 $this->assertTrue( IP::isIPv4( '124.24.52.13' ) );
107 $this->assertTrue( IP::isIPv4( '1.24.52.13' ) );
108 $this->assertTrue( IP::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
109 }
110
111 /**
112 * @covers IP::isValid
113 */
114 public function testValidIPs() {
115 foreach ( range( 0, 255 ) as $i ) {
116 $a = sprintf( "%03d", $i );
117 $b = sprintf( "%02d", $i );
118 $c = sprintf( "%01d", $i );
119 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
120 $ip = "$f.$f.$f.$f";
121 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
122 }
123 }
124 foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
125 $a = sprintf( "%04x", $i );
126 $b = sprintf( "%03x", $i );
127 $c = sprintf( "%02x", $i );
128 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
129 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
130 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv6 address" );
131 }
132 }
133 // test with some abbreviations
134 $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
135 $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
136 $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
137 $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
138
139 $this->assertTrue( IP::isValid( 'fc:100::' ) );
140 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
141 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
142
143 $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
144 $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
145 $this->assertTrue( IP::isValid( '2001::df'), 'IPv6 with "::" and 2 words' );
146 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
147 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
148 $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
149 $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
150
151 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
152 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
153 }
154
155 /**
156 * @covers IP::isValid
157 */
158 public function testInvalidIPs() {
159 // Out of range...
160 foreach ( range( 256, 999 ) as $i ) {
161 $a = sprintf( "%03d", $i );
162 $b = sprintf( "%02d", $i );
163 $c = sprintf( "%01d", $i );
164 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
165 $ip = "$f.$f.$f.$f";
166 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
167 }
168 }
169 foreach ( range( 'g', 'z' ) as $i ) {
170 $a = sprintf( "%04s", $i );
171 $b = sprintf( "%03s", $i );
172 $c = sprintf( "%02s", $i );
173 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
174 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
175 $this->assertFalse( IP::isValid( $ip ) , "$ip is not a valid IPv6 address" );
176 }
177 }
178 // Have CIDR
179 $ipCIDRs = array(
180 '212.35.31.121/32',
181 '212.35.31.121/18',
182 '212.35.31.121/24',
183 '::ff:d:321:5/96',
184 'ff::d3:321:5/116',
185 'c:ff:12:1:ea:d:321:5/120',
186 );
187 foreach ( $ipCIDRs as $i ) {
188 $this->assertFalse( IP::isValid( $i ),
189 "$i is an invalid IP address because it is a block" );
190 }
191 // Incomplete/garbage
192 $invalid = array(
193 'www.xn--var-xla.net',
194 '216.17.184.G',
195 '216.17.184.1.',
196 '216.17.184',
197 '216.17.184.',
198 '256.17.184.1'
199 );
200 foreach ( $invalid as $i ) {
201 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
202 }
203 }
204
205 /**
206 * @covers IP::isValidBlock
207 */
208 public function testValidBlocks() {
209 $valid = array(
210 '116.17.184.5/32',
211 '0.17.184.5/30',
212 '16.17.184.1/24',
213 '30.242.52.14/1',
214 '10.232.52.13/8',
215 '30.242.52.14/0',
216 '::e:f:2001/96',
217 '::c:f:2001/128',
218 '::10:f:2001/70',
219 '::fe:f:2001/1',
220 '::6d:f:2001/8',
221 '::fe:f:2001/0',
222 );
223 foreach ( $valid as $i ) {
224 $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
225 }
226 }
227
228 /**
229 * @covers IP::isValidBlock
230 */
231 public function testInvalidBlocks() {
232 $invalid = array(
233 '116.17.184.5/33',
234 '0.17.184.5/130',
235 '16.17.184.1/-1',
236 '10.232.52.13/*',
237 '7.232.52.13/ab',
238 '11.232.52.13/',
239 '::e:f:2001/129',
240 '::c:f:2001/228',
241 '::10:f:2001/-1',
242 '::6d:f:2001/*',
243 '::86:f:2001/ab',
244 '::23:f:2001/',
245 );
246 foreach ( $invalid as $i ) {
247 $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
248 }
249 }
250
251 /**
252 * Improve IP::sanitizeIP() code coverage
253 * @todo Most probably incomplete
254 */
255 public function testSanitizeIP() {
256 $this->assertNull( IP::sanitizeIP('') );
257 $this->assertNull( IP::sanitizeIP(' ') );
258 }
259
260 /**
261 * test wrapper around ip2long which might return -1 or false depending on PHP version
262 * @covers IP::toUnsigned
263 */
264 public function testip2longWrapper() {
265 // @todo FIXME: Add more tests ?
266 $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
267 $i = 'IN.VA.LI.D';
268 $this->assertFalse( IP::toUnSigned( $i ) );
269 }
270
271 /**
272 * @covers IP::isPublic
273 */
274 public function testPrivateIPs() {
275 $private = array( 'fc::3', 'fc::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
276 foreach ( $private as $p ) {
277 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
278 }
279 }
280
281 // Private wrapper used to test CIDR Parsing.
282 private function assertFalseCIDR( $CIDR, $msg='' ) {
283 $ff = array( false, false );
284 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
285 }
286
287 // Private wrapper to test network shifting using only dot notation
288 private function assertNet( $expected, $CIDR ) {
289 $parse = IP::parseCIDR( $CIDR );
290 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
291 }
292
293 /**
294 * @covers IP::hexToQuad
295 */
296 public function testHexToQuad() {
297 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '00000001' ) );
298 $this->assertEquals( '255.0.0.0' , IP::hexToQuad( 'FF000000' ) );
299 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
300 $this->assertEquals( '10.188.222.255' , IP::hexToQuad( '0ABCDEFF' ) );
301 // hex not left-padded...
302 $this->assertEquals( '0.0.0.0' , IP::hexToQuad( '0' ) );
303 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '1' ) );
304 $this->assertEquals( '0.0.0.255' , IP::hexToQuad( 'FF' ) );
305 $this->assertEquals( '0.0.255.0' , IP::hexToQuad( 'FF00' ) );
306 }
307
308 /**
309 * @covers IP::hexToOctet
310 */
311 public function testHexToOctet() {
312 $this->assertEquals( '0:0:0:0:0:0:0:1',
313 IP::hexToOctet( '00000000000000000000000000000001' ) );
314 $this->assertEquals( '0:0:0:0:0:0:FF:3',
315 IP::hexToOctet( '00000000000000000000000000FF0003' ) );
316 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
317 IP::hexToOctet( '000000000000000000000000FF000006' ) );
318 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
319 IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
320 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
321 IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
322 // hex not left-padded...
323 $this->assertEquals( '0:0:0:0:0:0:0:0' , IP::hexToOctet( '0' ) );
324 $this->assertEquals( '0:0:0:0:0:0:0:1' , IP::hexToOctet( '1' ) );
325 $this->assertEquals( '0:0:0:0:0:0:0:FF' , IP::hexToOctet( 'FF' ) );
326 $this->assertEquals( '0:0:0:0:0:0:0:FFD0' , IP::hexToOctet( 'FFD0' ) );
327 $this->assertEquals( '0:0:0:0:0:0:FA00:0' , IP::hexToOctet( 'FA000000' ) );
328 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
329 }
330
331 /*
332 * IP::parseCIDR() returns an array containing a signed IP address
333 * representing the network mask and the bit mask.
334 * @covers IP::parseCIDR
335 */
336 function testCIDRParsing() {
337 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
338 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
339
340 // Verify if statement
341 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
342 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
343 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
344 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
345
346 // Check internal logic
347 # 0 mask always result in array(0,0)
348 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('192.0.0.2/0') );
349 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0') );
350 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('255.255.255.255/0') );
351
352 // @todo FIXME: Add more tests.
353
354 # This part test network shifting
355 $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
356 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
357 $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
358 $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
359 $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
360 $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
361 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
362 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
363 $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
364 }
365
366
367 /**
368 * @covers IP::canonicalize
369 */
370 public function testIPCanonicalizeOnValidIp() {
371 $this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ),
372 'Canonicalization of a valid IP returns it unchanged' );
373 }
374
375 /**
376 * @covers IP::canonicalize
377 */
378 public function testIPCanonicalizeMappedAddress() {
379 $this->assertEquals(
380 '192.0.2.152',
381 IP::canonicalize( '::ffff:192.0.2.152' )
382 );
383 $this->assertEquals(
384 '192.0.2.152',
385 IP::canonicalize( '::192.0.2.152' )
386 );
387 }
388
389 /**
390 * Issues there are most probably from IP::toHex() or IP::parseRange()
391 * @covers IP::isInRange
392 * @dataProvider provideIPsAndRanges
393 */
394 public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
395 $this->assertEquals(
396 $expected,
397 IP::isInRange( $addr, $range ),
398 $message
399 );
400 }
401
402 /** Provider for testIPIsInRange() */
403 function provideIPsAndRanges() {
404 # Format: (expected boolean, address, range, optional message)
405 return array(
406 # IPv4
407 array( true , '192.0.2.0' , '192.0.2.0/24', 'Network address' ),
408 array( true , '192.0.2.77' , '192.0.2.0/24', 'Simple address' ),
409 array( true , '192.0.2.255' , '192.0.2.0/24', 'Broadcast address' ),
410
411 array( false, '0.0.0.0' , '192.0.2.0/24' ),
412 array( false, '255.255.255' , '192.0.2.0/24' ),
413
414 # IPv6
415 array( false, '::1' , '2001:DB8::/32' ),
416 array( false, '::' , '2001:DB8::/32' ),
417 array( false, 'FE80::1', '2001:DB8::/32' ),
418
419 array( true , '2001:DB8::' , '2001:DB8::/32' ),
420 array( true , '2001:0DB8::' , '2001:DB8::/32' ),
421 array( true , '2001:DB8::1' , '2001:DB8::/32' ),
422 array( true , '2001:0DB8::1', '2001:DB8::/32' ),
423 array( true , '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
424 '2001:DB8::/32' ),
425
426 array( false, '2001:0DB8:F::', '2001:DB8::/96' ),
427 );
428 }
429
430 /**
431 * Test for IP::splitHostAndPort().
432 * @dataProvider provideSplitHostAndPort
433 */
434 function testSplitHostAndPort( $expected, $input, $description ) {
435 $this->assertEquals( $expected, IP::splitHostAndPort( $input ), $description );
436 }
437
438 /**
439 * Provider for IP::splitHostAndPort()
440 */
441 function provideSplitHostAndPort() {
442 return array(
443 array( false, '[', 'Unclosed square bracket' ),
444 array( false, '[::', 'Unclosed square bracket 2' ),
445 array( array( '::', false ), '::', 'Bare IPv6 0' ),
446 array( array( '::1', false ), '::1', 'Bare IPv6 1' ),
447 array( array( '::', false ), '[::]', 'Bracketed IPv6 0' ),
448 array( array( '::1', false ), '[::1]', 'Bracketed IPv6 1' ),
449 array( array( '::1', 80 ), '[::1]:80', 'Bracketed IPv6 with port' ),
450 array( false, '::x', 'Double colon but no IPv6' ),
451 array( array( 'x', 80 ), 'x:80', 'Hostname and port' ),
452 array( false, 'x:x', 'Hostname and invalid port' ),
453 array( array( 'x', false ), 'x', 'Plain hostname' )
454 );
455 }
456
457 /**
458 * Test for IP::combineHostAndPort()
459 * @dataProvider provideCombineHostAndPort
460 */
461 function testCombineHostAndPort( $expected, $input, $description ) {
462 list( $host, $port, $defaultPort ) = $input;
463 $this->assertEquals(
464 $expected,
465 IP::combineHostAndPort( $host, $port, $defaultPort ),
466 $description );
467 }
468
469 /**
470 * Provider for IP::combineHostAndPort()
471 */
472 function provideCombineHostAndPort() {
473 return array(
474 array( '[::1]', array( '::1', 2, 2 ), 'IPv6 default port' ),
475 array( '[::1]:2', array( '::1', 2, 3 ), 'IPv6 non-default port' ),
476 array( 'x', array( 'x', 2, 2 ), 'Normal default port' ),
477 array( 'x:2', array( 'x', 2, 3 ), 'Normal non-default port' ),
478 );
479 }
480
481 }