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