* Changes in IP.php:
[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( "" ) );
11 $this->assertFalse( IP::isIPAddress( 'abc' ) );
12 $this->assertFalse( IP::isIPAddress( ':' ) );
13 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
14 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
15 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
16 $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
17 $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
18 $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
19
20 $this->assertTrue( IP::isIPAddress( 'fc:100::' ) );
21 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac::' ) );
22 $this->assertTrue( IP::isIPAddress( '::' ), 'IPv6 zero address' );
23 $this->assertTrue( IP::isIPAddress( '::fc' ) );
24 $this->assertTrue( IP::isIPAddress( '::fc:100:a:d:1:e:ac' ) );
25 $this->assertTrue( IP::isIPAddress( 'fc::100' ) );
26 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac' ) );
27 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96', 'IPv6 range with "::"' ) );
28 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0' ) );
29 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24', 'IPv6 range' ) );
30 $this->assertTrue( IP::isIPAddress( '124.24.52.13' ) );
31 $this->assertTrue( IP::isIPAddress( '1.24.52.13' ) );
32 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
33 }
34
35 public function testisIPv6() {
36 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
37 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
38 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
39 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
40 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
41 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
42 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
43 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
44 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 ending it "::" with 8 words' );
45 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words' );
46
47 $this->assertFalse( IP::isIPv6( ':::' ) );
48 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
49 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
50 $this->assertTrue( IP::isIPv6( '::0' ) );
51 $this->assertTrue( IP::isIPv6( '::fc' ) );
52 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
53 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
54 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
55 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
56 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
57 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
58 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 octets' );
59 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 octets' );
60
61 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
62 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending in lone ":"' );
63 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
64 $this->assertTrue( IP::isIPv6( 'fc::100' ) );
65 $this->assertTrue( IP::isIPv6( 'fc::100:a' ) );
66 $this->assertTrue( IP::isIPv6( 'fc::100:a:d' ) );
67 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ) );
68 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ) );
69 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ) );
70 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 octets' );
71 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 octets' );
72
73 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
74 }
75
76 public function testValidIPs() {
77 foreach ( range( 0, 255 ) as $i ) {
78 $a = sprintf( "%03d", $i );
79 $b = sprintf( "%02d", $i );
80 $c = sprintf( "%01d", $i );
81 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
82 $ip = "$f.$f.$f.$f";
83 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
84 }
85 }
86 foreach ( range( 0, 65535 ) as $i ) {
87 $a = sprintf( "%04x", $i );
88 $b = sprintf( "%03x", $i );
89 $c = sprintf( "%02x", $i );
90 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
91 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
92 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv6 address" );
93 }
94 }
95 }
96
97 public function testInvalidIPs() {
98 foreach ( range( 256, 999 ) as $i ) {
99 $a = sprintf( "%03d", $i );
100 $b = sprintf( "%02d", $i );
101 $c = sprintf( "%01d", $i );
102 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
103 $ip = "$f.$f.$f.$f";
104 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
105 }
106 }
107 foreach ( range( 'g', 'z' ) as $i ) {
108 $a = sprintf( "%04", $i );
109 $b = sprintf( "%03", $i );
110 $c = sprintf( "%02", $i );
111 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
112 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
113 $this->assertFalse( IP::isValid( $ip ) , "$ip is not a valid IPv6 address" );
114 }
115 }
116 }
117
118 public function testBogusIPs() {
119 $invalid = array(
120 'www.xn--var-xla.net',
121 '216.17.184.G',
122 '216.17.184.1.',
123 '216.17.184',
124 '216.17.184.',
125 '256.17.184.1'
126 );
127 foreach ( $invalid as $i ) {
128 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IPv4 address" );
129 }
130 }
131
132 // test wrapper around ip2long which might return -1 or false depending on PHP version
133 public function testip2longWrapper() {
134 // fixme : add more tests ?
135 $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
136 $this->assertEquals( -1 , IP::toSigned( '255.255.255.255' )) ;
137 $i = 'IN.VA.LI.D';
138 $this->assertFalse( IP::toUnSigned( $i ) );
139 $this->assertFalse( IP::toSigned( $i ) );
140 }
141
142 public function testPrivateIPs() {
143 $private = array( '10.0.0.1', '172.16.0.1', '192.168.0.1' );
144 foreach ( $private as $p ) {
145 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
146 }
147 }
148
149 // Private wrapper used to test CIDR Parsing.
150 private function assertFalseCIDR( $CIDR, $msg='' ) {
151 $ff = array( false, false );
152 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
153 }
154
155 // Private wrapper to test network shifting using only dot notation
156 private function assertNet( $expected, $CIDR ) {
157 $parse = IP::parseCIDR( $CIDR );
158 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
159 }
160
161 public function testHexToQuad() {
162 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '00000001' ) );
163 $this->assertEquals( '255.0.0.0' , IP::hexToQuad( 'FF000000' ) );
164 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
165 $this->assertEquals( '10.188.222.255' , IP::hexToQuad( '0ABCDEFF' ) );
166 // hex not left-padded...
167 $this->assertEquals( '0.0.0.0' , IP::hexToQuad( '0' ) );
168 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '1' ) );
169 $this->assertEquals( '0.0.0.255' , IP::hexToQuad( 'FF' ) );
170 $this->assertEquals( '0.0.255.0' , IP::hexToQuad( 'FF00' ) );
171 }
172
173 public function testHexToOctet() {
174 $this->assertEquals( '0:0:0:0:0:0:0:1',
175 IP::hexToOctet( '00000000000000000000000000000001' ) );
176 $this->assertEquals( '0:0:0:0:0:0:FF:3',
177 IP::hexToOctet( '00000000000000000000000000FF0003' ) );
178 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
179 IP::hexToOctet( '000000000000000000000000FF000006' ) );
180 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
181 IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
182 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
183 IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
184 // hex not left-padded...
185 $this->assertEquals( '0:0:0:0:0:0:0:0' , IP::hexToOctet( '0' ) );
186 $this->assertEquals( '0:0:0:0:0:0:0:1' , IP::hexToOctet( '1' ) );
187 $this->assertEquals( '0:0:0:0:0:0:0:FF' , IP::hexToOctet( 'FF' ) );
188 $this->assertEquals( '0:0:0:0:0:0:0:FFD0' , IP::hexToOctet( 'FFD0' ) );
189 $this->assertEquals( '0:0:0:0:0:0:FA00:0' , IP::hexToOctet( 'FA000000' ) );
190 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
191 }
192
193 /*
194 * IP::parseCIDR() returns an array containing a signed IP address
195 * representing the network mask and the bit mask.
196 */
197 function testCIDRParsing() {
198 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
199 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
200
201 // code calls IP::toSigned()
202
203 // Verify if statement
204 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
205 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
206 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
207 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
208
209 // Check internal logic
210 # 0 mask always result in array(0,0)
211 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('192.0.0.2/0') );
212 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0') );
213 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('255.255.255.255/0') );
214
215 // FIXME : add more tests.
216
217 # This part test network shifting
218 $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
219 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
220 $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
221 $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
222 $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
223 $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
224 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
225 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
226 $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
227 }
228 }