* ipaddresses : add compressIP to compress an IPv6 address (remove the 0 and add the :: if possible) * ipaddresses : add networkFirstIP and networkLastIP to get the first and last address of a network. In IPv4, the first address is network address and the last address is broadcast address. * ipaddresses : add ipInNetwork to know if the provided ip address is in or outside the network and cidr provided git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4026 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
501 lines
14 KiB
PHP
501 lines
14 KiB
PHP
<?php
|
|
/** DomFramework - Tests
|
|
@package domframework
|
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
|
|
|
/** Test the ipaddresses.php file */
|
|
class test_ipaddresses extends PHPUnit_Framework_TestCase
|
|
{
|
|
public function test_validIPAddress1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPAddress ("::");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPAddress2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPAddress ("1::");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPAddress3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPAddress ("::1");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPAddress4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPAddress ("2001::1");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPAddress5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPAddress ("1.2.3.4");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPAddress6 ()
|
|
{
|
|
$this->setExpectedException ("Exception");
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPAddress ("");
|
|
}
|
|
public function test_validIPAddress7 ()
|
|
{
|
|
$this->setExpectedException ("Exception");
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPAddress (array ());
|
|
}
|
|
|
|
public function test_validIPv4Address1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv4Address ("::");
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validIPv4Address2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv4Address ("1.2.3.4");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
|
|
public function test_validIPv6Address1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6Address ("1.2.3.4");
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validIPv6Address2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6Address ("::");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPv6Address3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6Address ("1::");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPv6Address4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6Address ("::1");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPv6Address5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6Address ("1::1");
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPv6Address6 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6Address ("1:1:1");
|
|
$this->assertSame (false, $res);
|
|
}
|
|
|
|
public function test_validCIDR1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validCIDR (-1);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validCIDR2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validCIDR (129);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validCIDR3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validCIDR (128);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validCIDR4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validCIDR (0);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
|
|
public function test_validIPv4CIDR1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv4CIDR (-1);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validIPv4CIDR2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv4CIDR (33);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validIPv4CIDR3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv4CIDR (32);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPv4CIDR4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv4CIDR (0);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
|
|
public function test_validIPv6CIDR1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6CIDR (-1);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validIPv6CIDR2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6CIDR (129);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_validIPv6CIDR3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6CIDR (128);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_validIPv6CIDR4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->validIPv6CIDR (0);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
|
|
public function test_compressIP1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->compressIP ("::");
|
|
$this->assertSame ("::", $res);
|
|
}
|
|
public function test_compressIP2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->compressIP ("::1");
|
|
$this->assertSame ("::1", $res);
|
|
}
|
|
public function test_compressIP3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->compressIP ("2::1");
|
|
$this->assertSame ("2::1", $res);
|
|
}
|
|
public function test_compressIP4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->compressIP ("2::");
|
|
$this->assertSame ("2::", $res);
|
|
}
|
|
public function test_compressIP5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->compressIP ("2:1:0:3::0:1");
|
|
$this->assertSame ("2:1:0:3::1", $res);
|
|
}
|
|
public function test_compressIP6 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->compressIP ("2:1:0:3:0000::1");
|
|
$this->assertSame ("2:1:0:3::1", $res);
|
|
}
|
|
|
|
public function test_uncompressIPv6a ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->uncompressIPv6 ("1::1");
|
|
$this->assertSame ("1:0:0:0:0:0:0:1", $res);
|
|
}
|
|
public function test_uncompressIPv6b ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->uncompressIPv6 ("1::");
|
|
$this->assertSame ("1:0:0:0:0:0:0:0", $res);
|
|
}
|
|
public function test_uncompressIPv6c ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->uncompressIPv6 ("::");
|
|
$this->assertSame ("0:0:0:0:0:0:0:0", $res);
|
|
}
|
|
public function test_uncompressIPv6d ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->uncompressIPv6 ("1.2.3.4");
|
|
$this->assertSame ("1.2.3.4", $res);
|
|
}
|
|
|
|
public function test_groupIPv6a ()
|
|
{
|
|
$this->setExpectedException ("Exception");
|
|
$i = new ipaddresses ();
|
|
$res = $i->groupIPv6 ("1.2.3.4");
|
|
}
|
|
public function test_groupIPv6b ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->groupIPv6 ("0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.".
|
|
"0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f");
|
|
$this->assertSame ("0123:4567:89ab:cdef:0123:4567:89ab:cdef", $res);
|
|
}
|
|
|
|
public function test_completeAddressWithZero1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->completeAddressWithZero ("::");
|
|
$this->assertSame ("0000:0000:0000:0000:0000:0000:0000:0000", $res);
|
|
}
|
|
public function test_completeAddressWithZero2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->completeAddressWithZero ("::1");
|
|
$this->assertSame ("0000:0000:0000:0000:0000:0000:0000:0001", $res);
|
|
}
|
|
public function test_completeAddressWithZero3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->completeAddressWithZero ("1::");
|
|
$this->assertSame ("0001:0000:0000:0000:0000:0000:0000:0000", $res);
|
|
}
|
|
public function test_completeAddressWithZero4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->completeAddressWithZero ("1::1");
|
|
$this->assertSame ("0001:0000:0000:0000:0000:0000:0000:0001", $res);
|
|
}
|
|
public function test_completeAddressWithZero5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->completeAddressWithZero ("1:222::1");
|
|
$this->assertSame ("0001:0222:0000:0000:0000:0000:0000:0001", $res);
|
|
}
|
|
public function test_completeAddressWithZero6 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->completeAddressWithZero ("1.2.3.4");
|
|
$this->assertSame ("1.2.3.4", $res);
|
|
}
|
|
|
|
// TODO : cidrToBin
|
|
// TODO : str_base_convert
|
|
|
|
public function test_reverseIPAddress1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->reverseIPAddress ("1.2.3.4");
|
|
$this->assertSame ("4.3.2.1", $res);
|
|
}
|
|
public function test_reverseIPAddress2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->reverseIPAddress ("::");
|
|
$this->assertSame ("0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0", $res);
|
|
}
|
|
public function test_reverseIPAddress3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->reverseIPAddress ("::1");
|
|
$this->assertSame ("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0", $res);
|
|
}
|
|
public function test_reverseIPAddress4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->reverseIPAddress ("2::1");
|
|
$this->assertSame ("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0", $res);
|
|
}
|
|
public function test_reverseIPAddress5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->reverseIPAddress ("2::abcd:1");
|
|
$this->assertSame ("1.0.0.0.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0", $res);
|
|
}
|
|
|
|
public function test_netmask2cidr1 ()
|
|
{
|
|
$this->setExpectedException ("Exception");
|
|
$i = new ipaddresses ();
|
|
$res = $i->netmask2cidr (0);
|
|
}
|
|
public function test_netmask2cidr2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->netmask2cidr ("255.255.255.0");
|
|
$this->assertSame (24, $res);
|
|
}
|
|
public function test_netmask2cidr3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->netmask2cidr ("255.255.255.255");
|
|
$this->assertSame (32, $res);
|
|
}
|
|
public function test_netmask2cidr4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->netmask2cidr ("255.255.255.255");
|
|
$this->assertSame (32, $res);
|
|
}
|
|
public function test_netmask2cidr5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->netmask2cidr ("255.0.0.0");
|
|
$this->assertSame (8, $res);
|
|
}
|
|
public function test_netmask2cidr6 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->netmask2cidr ("127.0.0.0");
|
|
$this->assertSame (7, $res);
|
|
}
|
|
public function test_netmask2cidr7 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->netmask2cidr ("63.0.0.0");
|
|
$this->assertSame (6, $res);
|
|
}
|
|
|
|
public function test_ipInNetwork1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->ipInNetwork ("127.0.0.1", "127.1.0.0", 8);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_ipInNetwork2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->ipInNetwork ("192.168.1.1", "127.1.0.0", 8);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_ipInNetwork3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->ipInNetwork ("2001:660:530d:201::1", "2001:660:530d:201::", 64);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_ipInNetwork4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->ipInNetwork ("2001:660:530d:203::1", "2001:660:530d:201::", 64);
|
|
$this->assertSame (false, $res);
|
|
}
|
|
public function test_ipInNetwork5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->ipInNetwork ("2001:660:530d:203::1", "2001::", 0);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
public function test_ipInNetwork6 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->ipInNetwork ("192.168.1.1", "127.0.0.0", 0);
|
|
$this->assertSame (true, $res);
|
|
}
|
|
|
|
public function test_networkFirstIP1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("192.168.1.21", 24);
|
|
$this->assertSame ($res, "192.168.1.0");
|
|
}
|
|
public function test_networkFirstIP2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("192.168.1.21", 0);
|
|
$this->assertSame ($res, "0.0.0.0");
|
|
}
|
|
public function test_networkFirstIP3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("192.168.1.21", 32);
|
|
$this->assertSame ($res, "192.168.1.21");
|
|
}
|
|
public function test_networkFirstIP4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("192.168.1.21", 31);
|
|
$this->assertSame ($res, "192.168.1.20");
|
|
}
|
|
public function test_networkFirstIP5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("2001:660:530d:201::125", 64);
|
|
$this->assertSame ($res, "2001:660:530d:201::");
|
|
}
|
|
public function test_networkFirstIP6 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("2001:660:530d:201::125", 0);
|
|
$this->assertSame ($res, "::");
|
|
}
|
|
public function test_networkFirstIP7 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("2001:660:530d:201::125", 128);
|
|
$this->assertSame ($res, "2001:660:530d:201::125");
|
|
}
|
|
public function test_networkFirstIP8 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkFirstIP ("2001:660:530d:201::125", 127);
|
|
$this->assertSame ($res, "2001:660:530d:201::124");
|
|
}
|
|
public function test_networkLastIP1 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("192.168.1.21", 24);
|
|
$this->assertSame ($res, "192.168.1.255");
|
|
}
|
|
public function test_networkLastIP2 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("192.168.1.21", 0);
|
|
$this->assertSame ($res, "255.255.255.255");
|
|
}
|
|
public function test_networkLastIP3 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("192.168.1.21", 32);
|
|
$this->assertSame ($res, "192.168.1.21");
|
|
}
|
|
public function test_networkLastIP4 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("192.168.1.21", 31);
|
|
$this->assertSame ($res, "192.168.1.21");
|
|
}
|
|
public function test_networkLastIP5 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("2001:660:530d:201::125", 64);
|
|
$this->assertSame ($res, "2001:660:530d:201:ffff:ffff:ffff:ffff");
|
|
}
|
|
public function test_networkLastIP6 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("2001:660:530d:201::125", 0);
|
|
$this->assertSame ($res, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
|
|
}
|
|
public function test_networkLastIP7 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("2001:660:530d:201::125", 128);
|
|
$this->assertSame ($res, "2001:660:530d:201::125");
|
|
}
|
|
public function test_networkLastIP8 ()
|
|
{
|
|
$i = new ipaddresses ();
|
|
$res = $i->networkLastIP ("2001:660:530d:201::125", 127);
|
|
$this->assertSame ($res, "2001:660:530d:201::125");
|
|
}
|
|
}
|