r73748 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73747‎ | r73748 | r73749 >
Date:16:09, 25 September 2010
Author:hashar
Status:deferred
Tags:
Comment:
adds testing for IP and Xml classes
Modified paths:
  • /trunk/phase3/maintenance/tests/phpunit/includes/IPTest.php (modified) (history)
  • /trunk/phase3/maintenance/tests/phpunit/includes/XmlTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/phpunit/includes/XmlTest.php
@@ -2,6 +2,20 @@
33
44 class XmlTest extends PHPUnit_Framework_TestCase {
55
 6+ public function testExpandAttributes() {
 7+ $this->assertNull( Xml::expandAttributes(null),
 8+ 'Converting a null list of attributes'
 9+ );
 10+ $this->assertEquals( '', Xml::expandAttributes( array() ),
 11+ 'Converting an empty list of attributes'
 12+ );
 13+ }
 14+
 15+ public function testExpandAttributesException() {
 16+ $this->setExpectedException('MWException');
 17+ Xml::expandAttributes('string');
 18+ }
 19+
620 function testElementOpen() {
721 $this->assertEquals(
822 '<element>',
@@ -26,6 +40,12 @@
2741 );
2842 }
2943
 44+ public function testEscapeTagsOnly() {
 45+ $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
 46+ 'replace " > and < with their HTML entitites'
 47+ );
 48+ }
 49+
3050 function testElementAttributes() {
3151 $this->assertEquals(
3252 '<element key="value" <>="&lt;&gt;">',
@@ -113,3 +133,7 @@
114134 );
115135 }
116136 }
 137+
 138+// TODO
 139+class XmlSelectTest extends PHPUnit_Framework_TestCase {
 140+}
Index: trunk/phase3/maintenance/tests/phpunit/includes/IPTest.php
@@ -4,7 +4,27 @@
55 */
66
77 class IPTest extends PHPUnit_Framework_TestCase {
 8+ // not sure it should be tested with boolean false. hashar 20100924
 9+ public function testisIPAddress() {
 10+ $this->assertFalse( IP::isIPAddress( false ) );
 11+ $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
 12+ $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
 13+ $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
 14+ }
815
 16+ /**
 17+ * @expectedException MWException
 18+ */
 19+ public function testArrayIsNotIPAddress() {
 20+ IP::isIPAddress( array('') );
 21+ }
 22+ /**
 23+ * @expectedException MWException
 24+ */
 25+ public function testArrayIsNotIPv6() {
 26+ IP::isIPv6( array('') );
 27+ }
 28+
929 public function testValidIPs() {
1030 foreach ( range( 0, 255 ) as $i ) {
1131 $a = sprintf( "%03d", $i );
@@ -43,10 +63,81 @@
4464 }
4565 }
4666
 67+ // test wrapper around ip2long which might return -1 or false depending on PHP version
 68+ public function testip2longWrapper() {
 69+ // fixme : add more tests ?
 70+ $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
 71+ $this->assertEquals( -1 , IP::toSigned( '255.255.255.255' )) ;
 72+ $i = 'IN.VA.LI.D';
 73+ $this->assertFalse( IP::toUnSigned( $i ) );
 74+ $this->assertFalse( IP::toSigned( $i ) );
 75+ }
 76+
4777 public function testPrivateIPs() {
4878 $private = array( '10.0.0.1', '172.16.0.1', '192.168.0.1' );
4979 foreach ( $private as $p ) {
5080 $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
5181 }
5282 }
 83+
 84+ // Private wrapper used to test CIDR Parsing.
 85+ private function assertFalseCIDR( $CIDR, $msg='' ) {
 86+ $ff = array( false, false );
 87+ $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
 88+ }
 89+
 90+ // Private wrapper to test network shifting using only dot notation
 91+ private function assertNet( $expected, $CIDR ) {
 92+ $parse = IP::parseCIDR( $CIDR );
 93+ $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
 94+ }
 95+
 96+
 97+ public function testHexToQuad() {
 98+ $this->assertEquals( '0.0.0.0' , IP::hexToQuad( '0' ) );
 99+ $this->assertEquals( '0.0.0.1' , IP::hexToQuad( '00000001' ) );
 100+ $this->assertEquals( '255.0.0.0' , IP::hexToQuad( 'FF000000' ) );
 101+ $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
 102+ $this->assertEquals( '10.188.222.255' , IP::hexToQuad( '0ABCDEFF' ) );
 103+
 104+ $this->assertNotEquals( '0.0.0.1' , IP::hexToQuad( '1' ) );
 105+ $this->assertNotEquals( '0.0.0.255' , IP::hexToQuad( 'FF' ) );
 106+ $this->assertNotEquals( '0.0.255.0' , IP::hexToQuad( 'FF00' ) );
 107+ }
 108+
 109+ /*
 110+ * IP::parseCIDR() returns an array containing a signed IP address
 111+ * representing the network mask and the bit mask.
 112+ */
 113+ function testCIDRParsing() {
 114+ $this->assertFalseCIDR( '192.0.2.0' , "missing mask" );
 115+ $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
 116+
 117+ // code calls IP::toSigned()
 118+
 119+ // Verify if statement
 120+ $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
 121+ $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
 122+ $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
 123+ $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
 124+
 125+ // Check internal logic
 126+ # 0 mask always result in array(0,0)
 127+ $this->assertEquals( array( 0, 0 ), IP::parseCIDR('192.0.0.2/0') );
 128+ $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0') );
 129+ $this->assertEquals( array( 0, 0 ), IP::parseCIDR('255.255.255.255/0') );
 130+
 131+ // FIXME : add more tests.
 132+
 133+ # This part test network shifting
 134+ $this->assertNet( '192.0.0.0' , '192.0.0.2/24' );
 135+ $this->assertNet( '192.168.5.0', '192.168.5.13/24');
 136+ $this->assertNet( '10.0.0.160' , '10.0.0.161/28' );
 137+ $this->assertNet( '10.0.0.0' , '10.0.0.3/28' );
 138+ $this->assertNet( '10.0.0.0' , '10.0.0.3/30' );
 139+ $this->assertNet( '10.0.0.4' , '10.0.0.4/30' );
 140+ $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
 141+ $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
 142+ $this->assertNet( '134.0.0.0' , '134.0.5.1/8' );
 143+ }
53144 }

Status & tagging log