Follow up r76708
[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 ), 'Boolean false is not an IP' );
10 $this->assertFalse( IP::isIPAddress( true ), 'Boolean true is not an IP' );
11 $this->assertFalse( IP::isIPAddress( "" ), 'Empty string is not an IP' );
12 $this->assertFalse( IP::isIPAddress( 'abc' ), 'Garbage IP string' );
13 $this->assertFalse( IP::isIPAddress( ':' ), 'Single ":" is not an IP' );
14 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
15 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
16 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
17 $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
18 $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
19 $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
20
21 $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
22 $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
23 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
24 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
25 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
26
27 $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
28 '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
29 foreach ( $validIPs as $ip ) {
30 $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
31 }
32 }
33
34 public function testisIPv6() {
35 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
36 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
37 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
38 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
39 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
40 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
41 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
42 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
43 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
44 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
45
46 $this->assertFalse( IP::isIPv6( ':::' ) );
47 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
48 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
49 $this->assertTrue( IP::isIPv6( '::0' ) );
50 $this->assertTrue( IP::isIPv6( '::fc' ) );
51 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
52 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
53 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
54 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
55 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
56 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
57 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
58 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
59
60 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
61 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
62 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
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 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
70 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
71
72 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
73 }
74
75 public function testisIPv4() {
76 $this->assertFalse( IP::isIPv4( false ), 'Boolean false is not an IP' );
77 $this->assertFalse( IP::isIPv4( true ), 'Boolean true is not an IP' );
78 $this->assertFalse( IP::isIPv4( "" ), 'Empty string is not an IP' );
79 $this->assertFalse( IP::isIPv4( 'abc' ) );
80 $this->assertFalse( IP::isIPv4( ':' ) );
81 $this->assertFalse( IP::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
82 $this->assertFalse( IP::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
83 $this->assertFalse( IP::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
84
85 $this->assertTrue( IP::isIPv4( '124.24.52.13' ) );
86 $this->assertTrue( IP::isIPv4( '1.24.52.13' ) );
87 $this->assertTrue( IP::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
88 }
89
90 // tests isValid()
91 public function testValidIPs() {
92 foreach ( range( 0, 255 ) as $i ) {
93 $a = sprintf( "%03d", $i );
94 $b = sprintf( "%02d", $i );
95 $c = sprintf( "%01d", $i );
96 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
97 $ip = "$f.$f.$f.$f";
98 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
99 }
100 }
101 foreach ( range( 0x0, 0xFFFF ) as $i ) {
102 $a = sprintf( "%04x", $i );
103 $b = sprintf( "%03x", $i );
104 $c = sprintf( "%02x", $i );
105 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
106 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
107 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv6 address" );
108 }
109 }
110 }
111
112 // tests isValid()
113 public function testInvalidIPs() {
114 // Out of range...
115 foreach ( range( 256, 999 ) 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->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
122 }
123 }
124 foreach ( range( 'g', 'z' ) as $i ) {
125 $a = sprintf( "%04s", $i );
126 $b = sprintf( "%03s", $i );
127 $c = sprintf( "%02s", $i );
128 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
129 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
130 $this->assertFalse( IP::isValid( $ip ) , "$ip is not a valid IPv6 address" );
131 }
132 }
133 // Have CIDR
134 $ipCIDRs = array(
135 '212.35.31.121/32',
136 '212.35.31.121/18',
137 '212.35.31.121/24',
138 '::ff:d:321:5/96',
139 'ff::d3:321:5/116',
140 'c:ff:12:1:ea:d:321:5/120',
141 );
142 foreach ( $ipCIDRs as $i ) {
143 $this->assertFalse( IP::isValid( $i ),
144 "$i is an invalid IP address because it is a block" );
145 }
146 // Incomplete/garbage
147 $invalid = array(
148 'www.xn--var-xla.net',
149 '216.17.184.G',
150 '216.17.184.1.',
151 '216.17.184',
152 '216.17.184.',
153 '256.17.184.1'
154 );
155 foreach ( $invalid as $i ) {
156 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
157 }
158 }
159
160 // tests isValidBlock()
161 public function testValidBlocks() {
162 $valid = array(
163 '116.17.184.5/32',
164 '0.17.184.5/30',
165 '16.17.184.1/24',
166 '30.242.52.14/1',
167 '10.232.52.13/8',
168 '::e:f:2001/96',
169 '::c:f:2001/128',
170 '::10:f:2001/70',
171 '::fe:f:2001/1',
172 '::6d:f:2001/8',
173 );
174 foreach ( $valid as $i ) {
175 $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
176 }
177 }
178
179 // tests isValidBlock()
180 public function testInvalidBlocks() {
181 $invalid = array(
182 '116.17.184.5/33',
183 '0.17.184.5/130',
184 '16.17.184.1/-1',
185 '30.242.52.14/0',
186 '10.232.52.13/*',
187 '7.232.52.13/ab',
188 '11.232.52.13/',
189 '::e:f:2001/129',
190 '::c:f:2001/228',
191 '::10:f:2001/-1',
192 '::fe:f:2001/0',
193 '::6d:f:2001/*',
194 '::86:f:2001/ab',
195 '::23:f:2001/',
196 );
197 foreach ( $invalid as $i ) {
198 $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
199 }
200 }
201
202 // test wrapper around ip2long which might return -1 or false depending on PHP version
203 public function testip2longWrapper() {
204 // fixme : add more tests ?
205 $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
206 $i = 'IN.VA.LI.D';
207 $this->assertFalse( IP::toUnSigned( $i ) );
208 }
209
210 // tests isPublic()
211 public function testPrivateIPs() {
212 $private = array( 'fc::3', 'fc::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
213 foreach ( $private as $p ) {
214 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
215 }
216 }
217
218 // Private wrapper used to test CIDR Parsing.
219 private function assertFalseCIDR( $CIDR, $msg='' ) {
220 $ff = array( false, false );
221 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
222 }
223
224 // Private wrapper to test network shifting using only dot notation
225 private function assertNet( $expected, $CIDR ) {
226 $parse = IP::parseCIDR( $CIDR );
227 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
228 }
229
230 public function testHexToQuad() {
231 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '00000001' ) );
232 $this->assertEquals( '255.0.0.0' , IP::hexToQuad( 'FF000000' ) );
233 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
234 $this->assertEquals( '10.188.222.255' , IP::hexToQuad( '0ABCDEFF' ) );
235 // hex not left-padded...
236 $this->assertEquals( '0.0.0.0' , IP::hexToQuad( '0' ) );
237 $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '1' ) );
238 $this->assertEquals( '0.0.0.255' , IP::hexToQuad( 'FF' ) );
239 $this->assertEquals( '0.0.255.0' , IP::hexToQuad( 'FF00' ) );
240 }
241
242 public function testHexToOctet() {
243 $this->assertEquals( '0:0:0:0:0:0:0:1',
244 IP::hexToOctet( '00000000000000000000000000000001' ) );
245 $this->assertEquals( '0:0:0:0:0:0:FF:3',
246 IP::hexToOctet( '00000000000000000000000000FF0003' ) );
247 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
248 IP::hexToOctet( '000000000000000000000000FF000006' ) );
249 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
250 IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
251 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
252 IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
253 // hex not left-padded...
254 $this->assertEquals( '0:0:0:0:0:0:0:0' , IP::hexToOctet( '0' ) );
255 $this->assertEquals( '0:0:0:0:0:0:0:1' , IP::hexToOctet( '1' ) );
256 $this->assertEquals( '0:0:0:0:0:0:0:FF' , IP::hexToOctet( 'FF' ) );
257 $this->assertEquals( '0:0:0:0:0:0:0:FFD0' , IP::hexToOctet( 'FFD0' ) );
258 $this->assertEquals( '0:0:0:0:0:0:FA00:0' , IP::hexToOctet( 'FA000000' ) );
259 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
260 }
261
262 /*
263 * IP::parseCIDR() returns an array containing a signed IP address
264 * representing the network mask and the bit mask.
265 */
266 function testCIDRParsing() {
267 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
268 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
269
270 // Verify if statement
271 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
272 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
273 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
274 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
275
276 // Check internal logic
277 # 0 mask always result in array(0,0)
278 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('192.0.0.2/0') );
279 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0') );
280 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('255.255.255.255/0') );
281
282 // FIXME : add more tests.
283
284 # This part test network shifting
285 $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
286 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
287 $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
288 $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
289 $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
290 $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
291 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
292 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
293 $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
294 }
295 }