Index: trunk/phase3/maintenance/tests/phpunit/includes/XmlTest.php |
— | — | @@ -2,6 +2,20 @@ |
3 | 3 | |
4 | 4 | class XmlTest extends PHPUnit_Framework_TestCase { |
5 | 5 | |
| 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 | + |
6 | 20 | function testElementOpen() { |
7 | 21 | $this->assertEquals( |
8 | 22 | '<element>', |
— | — | @@ -26,6 +40,12 @@ |
27 | 41 | ); |
28 | 42 | } |
29 | 43 | |
| 44 | + public function testEscapeTagsOnly() { |
| 45 | + $this->assertEquals( '"><', Xml::escapeTagsOnly( '"><' ), |
| 46 | + 'replace " > and < with their HTML entitites' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
30 | 50 | function testElementAttributes() { |
31 | 51 | $this->assertEquals( |
32 | 52 | '<element key="value" <>="<>">', |
— | — | @@ -113,3 +133,7 @@ |
114 | 134 | ); |
115 | 135 | } |
116 | 136 | } |
| 137 | + |
| 138 | +// TODO |
| 139 | +class XmlSelectTest extends PHPUnit_Framework_TestCase { |
| 140 | +} |
Index: trunk/phase3/maintenance/tests/phpunit/includes/IPTest.php |
— | — | @@ -4,7 +4,27 @@ |
5 | 5 | */ |
6 | 6 | |
7 | 7 | 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 | + } |
8 | 15 | |
| 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 | + |
9 | 29 | public function testValidIPs() { |
10 | 30 | foreach ( range( 0, 255 ) as $i ) { |
11 | 31 | $a = sprintf( "%03d", $i ); |
— | — | @@ -43,10 +63,81 @@ |
44 | 64 | } |
45 | 65 | } |
46 | 66 | |
| 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 | + |
47 | 77 | public function testPrivateIPs() { |
48 | 78 | $private = array( '10.0.0.1', '172.16.0.1', '192.168.0.1' ); |
49 | 79 | foreach ( $private as $p ) { |
50 | 80 | $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" ); |
51 | 81 | } |
52 | 82 | } |
| 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 | + } |
53 | 144 | } |