From e41f0a7561baa97ae616fccefa017d88abeec2d2 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 26 Feb 2017 12:19:47 +0100 Subject: [PATCH] turning static IP test cases into property based ones with random IPv4 and IPv6 addresses --- tst/Vizhash16x16Test.php | 51 +++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/tst/Vizhash16x16Test.php b/tst/Vizhash16x16Test.php index 417d0923..6a6c8a71 100644 --- a/tst/Vizhash16x16Test.php +++ b/tst/Vizhash16x16Test.php @@ -59,14 +59,49 @@ class Vizhash16x16Test extends PHPUnit_Framework_TestCase ); } - public function testVizhashGeneratesUniquePngsPerIp() + public function testVizhashGeneratesUniquePngsPerIpv4Hash() { - $vz = new Vizhash16x16(); - $pngdata = $vz->generate(hash('sha512', '127.0.0.1')); - file_put_contents($this->_file, $pngdata); - $finfo = new finfo(FILEINFO_MIME_TYPE); - $this->assertEquals('image/png', $finfo->file($this->_file)); - $this->assertNotEquals($pngdata, $vz->generate(hash('sha512', '2001:1620:2057:dead:beef::cafe:babe'))); - $this->assertEquals($pngdata, $vz->generate(hash('sha512', '127.0.0.1'))); + $this->forAll( + Generator\vector(2, Generator\vector(4, Generator\byte())) + )->then( + function ($ips) + { + $hash1 = hash('sha512', implode('.', $ips[0])); + $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)); + } + ); } }