r61927 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61926‎ | r61927 | r61928 >
Date:13:32, 3 February 2010
Author:demon
Status:ok
Tags:
Comment:
Port IP tests to PHPUnit format
Modified paths:
  • /trunk/phase3/tests/IPTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/tests/IPTest.php
@@ -0,0 +1,52 @@
 2+<?php
 3+/*
 4+ * Tests for IP validity functions. Ported from /t/inc/IP.t by avar.
 5+ */
 6+
 7+class IPTest extends PHPUnit_Framework_TestCase {
 8+
 9+ public function testValidIPs() {
 10+ foreach ( range( 0, 255 ) as $i ) {
 11+ $a = sprintf( "%03d", $i );
 12+ $b = sprintf( "%02d", $i );
 13+ $c = sprintf( "%01d", $i );
 14+ foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
 15+ $ip = "$f.$f.$f.$f";
 16+ $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
 17+ }
 18+ }
 19+ }
 20+
 21+ public function testInvalidIPs() {
 22+ foreach ( range( 256, 999 ) as $i ) {
 23+ $a = sprintf( "%03d", $i );
 24+ $b = sprintf( "%02d", $i );
 25+ $c = sprintf( "%01d", $i );
 26+ foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
 27+ $ip = "$f.$f.$f.$f";
 28+ $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
 29+ }
 30+ }
 31+ }
 32+
 33+ public function testBogusIPs() {
 34+ $invalid = array(
 35+ 'www.xn--var-xla.net',
 36+ '216.17.184.G',
 37+ '216.17.184.1.',
 38+ '216.17.184',
 39+ '216.17.184.',
 40+ '256.17.184.1'
 41+ );
 42+ foreach ( $invalid as $i ) {
 43+ $this->assertFalse( IP::isValid( $i ), "$i is an invalid IPv4 address" );
 44+ }
 45+ }
 46+
 47+ public function testPrivateIPs() {
 48+ $private = array( '10.0.0.1', '172.16.0.1', '192.168.0.1' );
 49+ foreach ( $private as $p ) {
 50+ $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
 51+ }
 52+ }
 53+}

Status & tagging log