2015-08-15 12:32:31 -04:00
|
|
|
<?php
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
|
|
|
use PrivateBin\Vizhash16x16;
|
2017-02-26 05:21:52 -05:00
|
|
|
use Eris\Generator;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
class Vizhash16x16Test extends PHPUnit_Framework_TestCase
|
2015-08-15 12:32:31 -04:00
|
|
|
{
|
2017-02-26 05:21:52 -05:00
|
|
|
use Eris\TestTrait;
|
|
|
|
|
2015-08-15 12:32:31 -04:00
|
|
|
private $_file;
|
|
|
|
|
|
|
|
private $_path;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!is_dir($this->_path)) {
|
|
|
|
mkdir($this->_path);
|
|
|
|
}
|
2015-08-27 15:41:21 -04:00
|
|
|
$this->_file = $this->_path . DIRECTORY_SEPARATOR . 'vizhash.png';
|
2016-08-09 05:54:42 -04:00
|
|
|
ServerSalt::setPath($this->_path);
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2015-08-27 15:41:21 -04:00
|
|
|
chmod($this->_path, 0700);
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::rmDir($this->_path);
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
|
|
|
|
2017-02-26 05:21:52 -05:00
|
|
|
public function testVizhashGeneratesPngs()
|
|
|
|
{
|
|
|
|
$this->forAll(
|
|
|
|
Generator\string(),
|
|
|
|
Generator\string()
|
|
|
|
)->then(
|
|
|
|
function ($string1, $string2)
|
|
|
|
{
|
|
|
|
$vz = new Vizhash16x16();
|
|
|
|
$pngdata = $vz->generate($string1);
|
|
|
|
|
2017-02-26 06:47:54 -05:00
|
|
|
if (empty($string1)) {
|
2017-02-26 05:21:52 -05:00
|
|
|
$this->assertEquals($pngdata, '');
|
|
|
|
} else {
|
|
|
|
$this->assertNotEquals($pngdata, '');
|
|
|
|
file_put_contents($this->_file, $pngdata);
|
|
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
|
|
$this->assertEquals('image/png', $finfo->file($this->_file));
|
2017-02-26 06:47:54 -05:00
|
|
|
if ($string1 !== $string2) {
|
2017-02-26 05:21:52 -05:00
|
|
|
$this->assertNotEquals($pngdata, $string2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->assertEquals($pngdata, $vz->generate($string1));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-26 06:19:47 -05:00
|
|
|
public function testVizhashGeneratesUniquePngsPerIpv4Hash()
|
2015-08-15 12:32:31 -04:00
|
|
|
{
|
2017-02-26 06:19:47 -05:00
|
|
|
$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));
|
2017-02-26 06:47:54 -05:00
|
|
|
if ($hash1 !== $hash2) {
|
2017-02-26 06:19:47 -05:00
|
|
|
$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));
|
2017-02-26 06:47:54 -05:00
|
|
|
if ($hash1 !== $hash2) {
|
2017-02-26 06:19:47 -05:00
|
|
|
$this->assertNotEquals($pngdata, $vz->generate($hash2));
|
|
|
|
}
|
|
|
|
$this->assertEquals($pngdata, $vz->generate($hash1));
|
|
|
|
}
|
|
|
|
);
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
|
|
|
}
|