adds testing for IP and Xml classes
authorAntoine Musso <hashar@users.mediawiki.org>
Sat, 25 Sep 2010 16:09:16 +0000 (16:09 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Sat, 25 Sep 2010 16:09:16 +0000 (16:09 +0000)
maintenance/tests/phpunit/includes/IPTest.php
maintenance/tests/phpunit/includes/XmlTest.php

index 9db77f7..2336086 100644 (file)
@@ -4,6 +4,26 @@
  */
 
 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 ) {
@@ -43,10 +63,81 @@ class IPTest extends PHPUnit_Framework_TestCase {
                }
        }
 
+       // 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'  ); 
+       }
 }
index 330e60c..a91799c 100644 (file)
@@ -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 @@ class XmlTest extends PHPUnit_Framework_TestCase {
                );
        }
 
+       public function testEscapeTagsOnly() {
+               $this->assertEquals( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
+                       'replace " > and < with their HTML entitites'
+               );
+       }
+
        function testElementAttributes() {
                $this->assertEquals(
                        '<element key="value" <>="&lt;&gt;">',
@@ -113,3 +133,7 @@ class XmlTest extends PHPUnit_Framework_TestCase {
                );
        }
 }
+
+// TODO
+class XmlSelectTest extends PHPUnit_Framework_TestCase {
+}