turning static IP test cases into property based ones with random IPv4 and IPv6 addresses

This commit is contained in:
El RIDO 2017-02-26 12:19:47 +01:00
parent 59adfc1962
commit e41f0a7561
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92

View file

@ -59,14 +59,49 @@ class Vizhash16x16Test extends PHPUnit_Framework_TestCase
); );
} }
public function testVizhashGeneratesUniquePngsPerIp() public function testVizhashGeneratesUniquePngsPerIpv4Hash()
{ {
$vz = new Vizhash16x16(); $this->forAll(
$pngdata = $vz->generate(hash('sha512', '127.0.0.1')); Generator\vector(2, Generator\vector(4, Generator\byte()))
file_put_contents($this->_file, $pngdata); )->then(
$finfo = new finfo(FILEINFO_MIME_TYPE); function ($ips)
$this->assertEquals('image/png', $finfo->file($this->_file)); {
$this->assertNotEquals($pngdata, $vz->generate(hash('sha512', '2001:1620:2057:dead:beef::cafe:babe'))); $hash1 = hash('sha512', implode('.', $ips[0]));
$this->assertEquals($pngdata, $vz->generate(hash('sha512', '127.0.0.1'))); $hash2 = hash('sha512', implode('.', $ips[1]));
$vz = new Vizhash16x16();
$pngdata = $vz->generate($hash1);
file_put_contents($this->_file, $pngdata);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$this->assertEquals('image/png', $finfo->file($this->_file));
if ($hash1 !== $hash2)
{
$this->assertNotEquals($pngdata, $vz->generate($hash2));
}
$this->assertEquals($pngdata, $vz->generate($hash1));
}
);
}
public function testVizhashGeneratesUniquePngsPerIpv6Hash()
{
$this->forAll(
Generator\vector(2, Generator\vector(16, Generator\byte()))
)->then(
function ($ips)
{
$hash1 = hash('sha512', inet_ntop(implode(array_map('chr', $ips[0]))));
$hash2 = hash('sha512', inet_ntop(implode(array_map('chr', $ips[1]))));
$vz = new Vizhash16x16();
$pngdata = $vz->generate($hash1);
file_put_contents($this->_file, $pngdata);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$this->assertEquals('image/png', $finfo->file($this->_file));
if ($hash1 !== $hash2)
{
$this->assertNotEquals($pngdata, $vz->generate($hash2));
}
$this->assertEquals($pngdata, $vz->generate($hash1));
}
);
} }
} }