9db77f72c6f9c1354b7faf7bc10632915f0449b8
[lhc/web/wiklou.git] / tests / 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
8 public function testValidIPs() {
9 foreach ( range( 0, 255 ) as $i ) {
10 $a = sprintf( "%03d", $i );
11 $b = sprintf( "%02d", $i );
12 $c = sprintf( "%01d", $i );
13 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
14 $ip = "$f.$f.$f.$f";
15 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
16 }
17 }
18 }
19
20 public function testInvalidIPs() {
21 foreach ( range( 256, 999 ) as $i ) {
22 $a = sprintf( "%03d", $i );
23 $b = sprintf( "%02d", $i );
24 $c = sprintf( "%01d", $i );
25 foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
26 $ip = "$f.$f.$f.$f";
27 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
28 }
29 }
30 }
31
32 public function testBogusIPs() {
33 $invalid = array(
34 'www.xn--var-xla.net',
35 '216.17.184.G',
36 '216.17.184.1.',
37 '216.17.184',
38 '216.17.184.',
39 '256.17.184.1'
40 );
41 foreach ( $invalid as $i ) {
42 $this->assertFalse( IP::isValid( $i ), "$i is an invalid IPv4 address" );
43 }
44 }
45
46 public function testPrivateIPs() {
47 $private = array( '10.0.0.1', '172.16.0.1', '192.168.0.1' );
48 foreach ( $private as $p ) {
49 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
50 }
51 }
52 }