http://www.mediawiki.org/wiki/Special:Code/MediaWiki/73748
Revision: 73748
Author: hashar
Date: 2010-09-25 16:09:16 +0000 (Sat, 25 Sep 2010)
Log Message:
-----------
adds testing for IP and Xml classes
Modified Paths:
--------------
trunk/phase3/maintenance/tests/phpunit/includes/IPTest.php
trunk/phase3/maintenance/tests/phpunit/includes/XmlTest.php
Modified: trunk/phase3/maintenance/tests/phpunit/includes/IPTest.php
===================================================================
--- trunk/phase3/maintenance/tests/phpunit/includes/IPTest.php 2010-09-25
16:08:24 UTC (rev 73747)
+++ trunk/phase3/maintenance/tests/phpunit/includes/IPTest.php 2010-09-25
16:09:16 UTC (rev 73748)
@@ -4,7 +4,27 @@
*/
class IPTest extends PHPUnit_Framework_TestCase {
+ // not sure it should be tested with boolean false. hashar 20100924
+ public function testisIPAddress() {
+ $this->assertFalse( IP::isIPAddress( false ) );
+ $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'),
'IPv6 with a double :: occurence' );
+ $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6
with a double :: occurence, last at end' );
+ $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6
with a double :: occurence, firt at beginning' );
+ }
+ /**
+ * @expectedException MWException
+ */
+ public function testArrayIsNotIPAddress() {
+ IP::isIPAddress( array('') );
+ }
+ /**
+ * @expectedException MWException
+ */
+ public function testArrayIsNotIPv6() {
+ IP::isIPv6( array('') );
+ }
+
public function testValidIPs() {
foreach ( range( 0, 255 ) as $i ) {
$a = sprintf( "%03d", $i );
@@ -43,10 +63,81 @@
}
}
+ // test wrapper around ip2long which might return -1 or false depending
on PHP version
+ public function testip2longWrapper() {
+ // fixme : add more tests ?
+ $this->assertEquals( pow(2,32) - 1, IP::toUnsigned(
'255.255.255.255' ));
+ $this->assertEquals( -1 , IP::toSigned(
'255.255.255.255' )) ;
+ $i = 'IN.VA.LI.D';
+ $this->assertFalse( IP::toUnSigned( $i ) );
+ $this->assertFalse( IP::toSigned( $i ) );
+ }
+
public function testPrivateIPs() {
$private = array( '10.0.0.1', '172.16.0.1', '192.168.0.1' );
foreach ( $private as $p ) {
$this->assertFalse( IP::isPublic( $p ), "$p is not a
public IP address" );
}
}
+
+ // Private wrapper used to test CIDR Parsing.
+ private function assertFalseCIDR( $CIDR, $msg='' ) {
+ $ff = array( false, false );
+ $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
+ }
+
+ // Private wrapper to test network shifting using only dot notation
+ private function assertNet( $expected, $CIDR ) {
+ $parse = IP::parseCIDR( $CIDR );
+ $this->assertEquals( $expected, long2ip( $parse[0] ), "network
shifting $CIDR" );
+ }
+
+
+ public function testHexToQuad() {
+ $this->assertEquals( '0.0.0.0' , IP::hexToQuad( '0' ) );
+ $this->assertEquals( '0.0.0.1' , IP::hexToQuad(
'00000001' ) );
+ $this->assertEquals( '255.0.0.0' , IP::hexToQuad(
'FF000000' ) );
+ $this->assertEquals( '255.255.255.255', IP::hexToQuad(
'FFFFFFFF' ) );
+ $this->assertEquals( '10.188.222.255' , IP::hexToQuad(
'0ABCDEFF' ) );
+
+ $this->assertNotEquals( '0.0.0.1' , IP::hexToQuad( '1' )
);
+ $this->assertNotEquals( '0.0.0.255' , IP::hexToQuad( 'FF'
) );
+ $this->assertNotEquals( '0.0.255.0' , IP::hexToQuad(
'FF00' ) );
+ }
+
+ /*
+ * IP::parseCIDR() returns an array containing a signed IP address
+ * representing the network mask and the bit mask.
+ */
+ function testCIDRParsing() {
+ $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
+ $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
+
+ // code calls IP::toSigned()
+
+ // Verify if statement
+ $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
+ $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
+ $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
+ $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
+
+ // Check internal logic
+ # 0 mask always result in array(0,0)
+ $this->assertEquals( array( 0, 0 ),
IP::parseCIDR('192.0.0.2/0') );
+ $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0')
);
+ $this->assertEquals( array( 0, 0 ),
IP::parseCIDR('255.255.255.255/0') );
+
+ // FIXME : add more tests.
+
+ # This part test network shifting
+ $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
+ $this->assertNet( '192.168.5.0', '192.168.5.13/24');
+ $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
+ $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
+ $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
+ $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
+ $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
+ $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
+ $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
+ }
}
Modified: trunk/phase3/maintenance/tests/phpunit/includes/XmlTest.php
===================================================================
--- trunk/phase3/maintenance/tests/phpunit/includes/XmlTest.php 2010-09-25
16:08:24 UTC (rev 73747)
+++ trunk/phase3/maintenance/tests/phpunit/includes/XmlTest.php 2010-09-25
16:09:16 UTC (rev 73748)
@@ -2,6 +2,20 @@
class XmlTest extends PHPUnit_Framework_TestCase {
+ public function testExpandAttributes() {
+ $this->assertNull( Xml::expandAttributes(null),
+ 'Converting a null list of attributes'
+ );
+ $this->assertEquals( '', Xml::expandAttributes( array() ),
+ 'Converting an empty list of attributes'
+ );
+ }
+
+ public function testExpandAttributesException() {
+ $this->setExpectedException('MWException');
+ Xml::expandAttributes('string');
+ }
+
function testElementOpen() {
$this->assertEquals(
'<element>',
@@ -26,6 +40,12 @@
);
}
+ public function testEscapeTagsOnly() {
+ $this->assertEquals( '"><', Xml::escapeTagsOnly(
'"><' ),
+ 'replace " > and < with their HTML entitites'
+ );
+ }
+
function testElementAttributes() {
$this->assertEquals(
'<element key="value" <>="<>">',
@@ -113,3 +133,7 @@
);
}
}
+
+// TODO
+class XmlSelectTest extends PHPUnit_Framework_TestCase {
+}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs