* ipaddresses : add validCIDR, validIPv4CIDR, validIPv6CIDR to check the CIDR

* 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
This commit is contained in:
2018-01-10 14:34:35 +00:00
parent 20e75710c9
commit e12f7c516d
2 changed files with 507 additions and 3 deletions

View File

@@ -6,7 +6,6 @@
/** Test the ipaddresses.php file */
class test_ipaddresses extends PHPUnit_Framework_TestCase
{
public function test_validIPAddress1 ()
{
$i = new ipaddresses ();
@@ -100,6 +99,118 @@ class test_ipaddresses extends PHPUnit_Framework_TestCase
$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 ();
@@ -252,4 +363,138 @@ class test_ipaddresses extends PHPUnit_Framework_TestCase
$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");
}
}