3 * Tests for IP validity functions. Ported from /t/inc/IP.t by avar.
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 $this->assertFalse( IP
::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
22 $this->assertTrue( IP
::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
23 $this->assertTrue( IP
::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
24 $this->assertTrue( IP
::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
25 $this->assertTrue( IP
::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
26 $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" );
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->assertFalse( IP
::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
39 $this->assertFalse( IP
::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
40 $this->assertTrue( IP
::isIPv6( 'fc:100::' ) );
41 $this->assertTrue( IP
::isIPv6( 'fc:100:a::' ) );
42 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d::' ) );
43 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1::' ) );
44 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1:e::' ) );
45 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
46 $this->assertFalse( IP
::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
47 $this->assertFalse( IP
::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
49 $this->assertFalse( IP
::isIPv6( ':::' ) );
50 $this->assertFalse( IP
::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
51 $this->assertTrue( IP
::isIPv6( '::' ), 'IPv6 zero address' );
52 $this->assertTrue( IP
::isIPv6( '::0' ) );
53 $this->assertTrue( IP
::isIPv6( '::fc' ) );
54 $this->assertTrue( IP
::isIPv6( '::fc:100' ) );
55 $this->assertTrue( IP
::isIPv6( '::fc:100:a' ) );
56 $this->assertTrue( IP
::isIPv6( '::fc:100:a:d' ) );
57 $this->assertTrue( IP
::isIPv6( '::fc:100:a:d:1' ) );
58 $this->assertTrue( IP
::isIPv6( '::fc:100:a:d:1:e' ) );
59 $this->assertTrue( IP
::isIPv6( '::fc:100:a:d:1:e:ac' ) );
60 $this->assertFalse( IP
::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
61 $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->assertFalse( IP
::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
73 $this->assertFalse( IP
::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
75 $this->assertTrue( IP
::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
78 public function testisIPv4() {
79 $this->assertFalse( IP
::isIPv4( false ), 'Boolean false is not an IP' );
80 $this->assertFalse( IP
::isIPv4( true ), 'Boolean true is not an IP' );
81 $this->assertFalse( IP
::isIPv4( "" ), 'Empty string is not an IP' );
82 $this->assertFalse( IP
::isIPv4( 'abc' ) );
83 $this->assertFalse( IP
::isIPv4( ':' ) );
84 $this->assertFalse( IP
::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
85 $this->assertFalse( IP
::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
86 $this->assertFalse( IP
::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
88 $this->assertTrue( IP
::isIPv4( '124.24.52.13' ) );
89 $this->assertTrue( IP
::isIPv4( '1.24.52.13' ) );
90 $this->assertTrue( IP
::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
94 public function testValidIPs() {
95 foreach ( range( 0, 255 ) as $i ) {
96 $a = sprintf( "%03d", $i );
97 $b = sprintf( "%02d", $i );
98 $c = sprintf( "%01d", $i );
99 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
101 $this->assertTrue( IP
::isValid( $ip ) , "$ip is a valid IPv4 address" );
104 foreach ( range( 0x0, 0xFFFF ) as $i ) {
105 $a = sprintf( "%04x", $i );
106 $b = sprintf( "%03x", $i );
107 $c = sprintf( "%02x", $i );
108 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
109 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
110 $this->assertTrue( IP
::isValid( $ip ) , "$ip is a valid IPv6 address" );
116 public function testInvalidIPs() {
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 ) {
124 $this->assertFalse( IP
::isValid( $ip ), "$ip is not a valid IPv4 address" );
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" );
143 'c:ff:12:1:ea:d:321:5/120',
145 foreach ( $ipCIDRs as $i ) {
146 $this->assertFalse( IP
::isValid( $i ),
147 "$i is an invalid IP address because it is a block" );
149 // Incomplete/garbage
151 'www.xn--var-xla.net',
158 foreach ( $invalid as $i ) {
159 $this->assertFalse( IP
::isValid( $i ), "$i is an invalid IP address" );
163 // tests isValidBlock()
164 public function testValidBlocks() {
179 foreach ( $valid as $i ) {
180 $this->assertTrue( IP
::isValidBlock( $i ), "$i is a valid IP block" );
184 // tests isValidBlock()
185 public function testInvalidBlocks() {
200 foreach ( $invalid as $i ) {
201 $this->assertFalse( IP
::isValidBlock( $i ), "$i is not a valid IP block" );
205 // test wrapper around ip2long which might return -1 or false depending on PHP version
206 public function testip2longWrapper() {
207 // fixme : add more tests ?
208 $this->assertEquals( pow(2,32) - 1, IP
::toUnsigned( '255.255.255.255' ));
210 $this->assertFalse( IP
::toUnSigned( $i ) );
214 public function testPrivateIPs() {
215 $private = array( 'fc::3', 'fc::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
216 foreach ( $private as $p ) {
217 $this->assertFalse( IP
::isPublic( $p ), "$p is not a public IP address" );
221 // Private wrapper used to test CIDR Parsing.
222 private function assertFalseCIDR( $CIDR, $msg='' ) {
223 $ff = array( false, false );
224 $this->assertEquals( $ff, IP
::parseCIDR( $CIDR ), $msg );
227 // Private wrapper to test network shifting using only dot notation
228 private function assertNet( $expected, $CIDR ) {
229 $parse = IP
::parseCIDR( $CIDR );
230 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
233 public function testHexToQuad() {
234 $this->assertEquals( '0.0.0.1' , IP
::hexToQuad( '00000001' ) );
235 $this->assertEquals( '255.0.0.0' , IP
::hexToQuad( 'FF000000' ) );
236 $this->assertEquals( '255.255.255.255', IP
::hexToQuad( 'FFFFFFFF' ) );
237 $this->assertEquals( '10.188.222.255' , IP
::hexToQuad( '0ABCDEFF' ) );
238 // hex not left-padded...
239 $this->assertEquals( '0.0.0.0' , IP
::hexToQuad( '0' ) );
240 $this->assertEquals( '0.0.0.1' , IP
::hexToQuad( '1' ) );
241 $this->assertEquals( '0.0.0.255' , IP
::hexToQuad( 'FF' ) );
242 $this->assertEquals( '0.0.255.0' , IP
::hexToQuad( 'FF00' ) );
245 public function testHexToOctet() {
246 $this->assertEquals( '0:0:0:0:0:0:0:1',
247 IP
::hexToOctet( '00000000000000000000000000000001' ) );
248 $this->assertEquals( '0:0:0:0:0:0:FF:3',
249 IP
::hexToOctet( '00000000000000000000000000FF0003' ) );
250 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
251 IP
::hexToOctet( '000000000000000000000000FF000006' ) );
252 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
253 IP
::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
254 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
255 IP
::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
256 // hex not left-padded...
257 $this->assertEquals( '0:0:0:0:0:0:0:0' , IP
::hexToOctet( '0' ) );
258 $this->assertEquals( '0:0:0:0:0:0:0:1' , IP
::hexToOctet( '1' ) );
259 $this->assertEquals( '0:0:0:0:0:0:0:FF' , IP
::hexToOctet( 'FF' ) );
260 $this->assertEquals( '0:0:0:0:0:0:0:FFD0' , IP
::hexToOctet( 'FFD0' ) );
261 $this->assertEquals( '0:0:0:0:0:0:FA00:0' , IP
::hexToOctet( 'FA000000' ) );
262 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP
::hexToOctet( 'FCCFFAFF' ) );
266 * IP::parseCIDR() returns an array containing a signed IP address
267 * representing the network mask and the bit mask.
269 function testCIDRParsing() {
270 $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
271 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
273 // Verify if statement
274 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
275 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
276 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
277 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
279 // Check internal logic
280 # 0 mask always result in array(0,0)
281 $this->assertEquals( array( 0, 0 ), IP
::parseCIDR('192.0.0.2/0') );
282 $this->assertEquals( array( 0, 0 ), IP
::parseCIDR('0.0.0.0/0') );
283 $this->assertEquals( array( 0, 0 ), IP
::parseCIDR('255.255.255.255/0') );
285 // FIXME : add more tests.
287 # This part test network shifting
288 $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
289 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
290 $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
291 $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
292 $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
293 $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
294 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
295 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
296 $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );