Follow up r75245.
[lhc/web/wiklou.git] / maintenance / 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 PHPUnit_Framework_TestCase {
7 // not sure it should be tested with boolean false. hashar 20100924
8 public function testisIPAddress() {
9 $this->assertFalse( IP::isIPAddress( false ) );
10 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
11 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
12 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
13 }
14
15 /**
16 * @expectedException MWException
17 */
18 public function testArrayIsNotIPAddress() {
19 IP::isIPAddress( array('') );
20 }
21 /**
22 * @expectedException MWException
23 */
24 public function testArrayIsNotIPv6() {
25 IP::isIPv6( array('') );
26 }
27
28 public function testValidIPs() {
29 foreach ( range( 0, 255 ) as $i ) {
30 $a = sprintf( "%03d", $i );
31 $b = sprintf( "%02d", $i );
32 $c = sprintf( "%01d", $i );
33 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
34 $ip = "$f.$f.$f.$f";
35 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
36 }
37 }
38 }
39
40 public function testInvalidIPs() {
41 foreach ( range( 256, 999 ) as $i ) {
42 $a = sprintf( "%03d", $i );
43 $b = sprintf( "%02d", $i );
44 $c = sprintf( "%01d", $i );
45 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
46 $ip = "$f.$f.$f.$f";
47 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
48 }
49 }
50 }
51
52 public function testBogusIPs() {
53 $invalid = array(
54 'www.xn--var-xla.net',
55 '216.17.184.G',
56 '216.17.184.1.',
57 '216.17.184',
58 '216.17.184.',
59 '256.17.184.1'
60 );
61 foreach ( $invalid as $i ) {
62 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IPv4 address" );
63 }
64 }
65
66 // test wrapper around ip2long which might return -1 or false depending on PHP version
67 public function testip2longWrapper() {
68 // fixme : add more tests ?
69 $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
70 $this->assertEquals( -1 , IP::toSigned( '255.255.255.255' )) ;
71 $i = 'IN.VA.LI.D';
72 $this->assertFalse( IP::toUnSigned( $i ) );
73 $this->assertFalse( IP::toSigned( $i ) );
74 }
75
76 public function testPrivateIPs() {
77 $private = array( '10.0.0.1', '172.16.0.1', '192.168.0.1' );
78 foreach ( $private as $p ) {
79 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
80 }
81 }
82
83 // Private wrapper used to test CIDR Parsing.
84 private function assertFalseCIDR( $CIDR, $msg='' ) {
85 $ff = array( false, false );
86 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
87 }
88
89 // Private wrapper to test network shifting using only dot notation
90 private function assertNet( $expected, $CIDR ) {
91 $parse = IP::parseCIDR( $CIDR );
92 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
93 }
94
95
96 public function testHexToQuad() {
97 $this->assertEquals( '0.0.0.0' , IP::hexToQuad( '0' ) );
98 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '00000001' ) );
99 $this->assertEquals( '255.0.0.0' , IP::hexToQuad( 'FF000000' ) );
100 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
101 $this->assertEquals( '10.188.222.255' , IP::hexToQuad( '0ABCDEFF' ) );
102
103 $this->assertNotEquals( '0.0.0.1' , IP::hexToQuad( '1' ) );
104 $this->assertNotEquals( '0.0.0.255' , IP::hexToQuad( 'FF' ) );
105 $this->assertNotEquals( '0.0.255.0' , IP::hexToQuad( 'FF00' ) );
106 }
107
108 /*
109 * IP::parseCIDR() returns an array containing a signed IP address
110 * representing the network mask and the bit mask.
111 */
112 function testCIDRParsing() {
113 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
114 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
115
116 // code calls IP::toSigned()
117
118 // Verify if statement
119 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
120 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
121 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
122 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
123
124 // Check internal logic
125 # 0 mask always result in array(0,0)
126 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('192.0.0.2/0') );
127 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0') );
128 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('255.255.255.255/0') );
129
130 // FIXME : add more tests.
131
132 # This part test network shifting
133 $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
134 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
135 $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
136 $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
137 $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
138 $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
139 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
140 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
141 $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
142 }
143 }