PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/phpunit/includes/IPTest.php

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