From ad700513230b30d3099bb17be93895d70d90a19d Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sat, 15 Aug 2015 18:32:31 +0200 Subject: [PATCH] reviewed unit tests, fixing line endings, added more tests --- lib/persistence.php | 2 +- tst/RainTPL.php | 206 ++++++++++++++++++++--------------------- tst/auto.php | 16 ++-- tst/filter.php | 94 +++++++++---------- tst/phpunit.xml | 2 +- tst/sjcl.php | 42 ++++----- tst/trafficlimiter.php | 63 +++++++------ tst/vizhash16x16.php | 82 ++++++++-------- tst/zerobin/data.php | 140 ++++++++++++++-------------- tst/zerobin/db.php | 6 +- 10 files changed, 328 insertions(+), 325 deletions(-) diff --git a/lib/persistence.php b/lib/persistence.php index 42a66749..58faadfc 100644 --- a/lib/persistence.php +++ b/lib/persistence.php @@ -48,7 +48,7 @@ abstract class persistence public static function getPath($filename = null) { if(strlen($filename)) { - return self::$_path . '/' . $filename; + return self::$_path . DIRECTORY_SEPARATOR . $filename; } else { return self::$_path; } diff --git a/tst/RainTPL.php b/tst/RainTPL.php index 39272e04..8892a68c 100644 --- a/tst/RainTPL.php +++ b/tst/RainTPL.php @@ -1,103 +1,103 @@ - '5 minutes', - '1hour' => '1 hour', - 'never' => 'Never', - ); - - private static $expire_default = '1hour'; - - private static $version = 'Version 1.2.3'; - - private $_content; - - public function setUp() - { - /* Setup Routine */ - $page = new RainTPL; - $page::configure(array('cache_dir' => 'tmp/')); - - $page = new RainTPL; - // We escape it here because ENT_NOQUOTES can't be used in RainTPL templates. - $page->assign('CIPHERDATA', htmlspecialchars(self::$data, ENT_NOQUOTES)); - $page->assign('ERROR', self::$error); - $page->assign('STATUS', self::$status); - $page->assign('VERSION', self::$version); - $page->assign('BURNAFTERREADINGSELECTED', false); - $page->assign('OPENDISCUSSION', false); - $page->assign('SYNTAXHIGHLIGHTING', true); - $page->assign('EXPIRE', self::$expire); - $page->assign('EXPIREDEFAULT', self::$expire_default); - ob_start(); - $page->draw('page'); - $this->_content = ob_get_contents(); - // run a second time from cache - $page->cache('page'); - $page->draw('page'); - ob_end_clean(); - } - - public function tearDown() - { - /* Tear Down Routine */ - helper::rmdir(PATH . 'tmp'); - } - - public function testTemplateRendersCorrectly() - { - $this->assertTag( - array( - 'id' => 'cipherdata', - 'content' => htmlspecialchars(self::$data, ENT_NOQUOTES) - ), - $this->_content, - 'outputs data correctly' - ); - $this->assertTag( - array( - 'id' => 'errormessage', - 'content' => self::$error - ), - $this->_content, - 'outputs error correctly' - ); - $this->assertTag( - array( - 'id' => 'opendiscussion', - 'attributes' => array( - 'disabled' => 'disabled' - ), - ), - $this->_content, - 'disables discussions if configured' - ); - // testing version number in JS address, since other instances may not be present in different templates - $this->assertTag( - array( - 'tag' => 'script', - 'attributes' => array( - 'src' => 'js/zerobin.js?' . rawurlencode(self::$version) - ), - ), - $this->_content, - 'outputs version correctly' - ); - } - - /** - * @expectedException RainTpl_Exception - */ - public function testMissingTemplate() - { - $test = new RainTPL; - $test->draw('123456789 does not exist!'); - } -} + '5 minutes', + '1hour' => '1 hour', + 'never' => 'Never', + ); + + private static $expire_default = '1hour'; + + private static $version = 'Version 1.2.3'; + + private $_content; + + public function setUp() + { + /* Setup Routine */ + $page = new RainTPL; + $page::configure(array('cache_dir' => 'tmp/')); + + $page = new RainTPL; + // We escape it here because ENT_NOQUOTES can't be used in RainTPL templates. + $page->assign('CIPHERDATA', htmlspecialchars(self::$data, ENT_NOQUOTES)); + $page->assign('ERROR', self::$error); + $page->assign('STATUS', self::$status); + $page->assign('VERSION', self::$version); + $page->assign('BURNAFTERREADINGSELECTED', false); + $page->assign('OPENDISCUSSION', false); + $page->assign('SYNTAXHIGHLIGHTING', true); + $page->assign('EXPIRE', self::$expire); + $page->assign('EXPIREDEFAULT', self::$expire_default); + ob_start(); + $page->draw('page'); + $this->_content = ob_get_contents(); + // run a second time from cache + $page->cache('page'); + $page->draw('page'); + ob_end_clean(); + } + + public function tearDown() + { + /* Tear Down Routine */ + helper::rmdir(PATH . 'tmp'); + } + + public function testTemplateRendersCorrectly() + { + $this->assertTag( + array( + 'id' => 'cipherdata', + 'content' => htmlspecialchars(self::$data, ENT_NOQUOTES) + ), + $this->_content, + 'outputs data correctly' + ); + $this->assertTag( + array( + 'id' => 'errormessage', + 'content' => self::$error + ), + $this->_content, + 'outputs error correctly' + ); + $this->assertTag( + array( + 'id' => 'opendiscussion', + 'attributes' => array( + 'disabled' => 'disabled' + ), + ), + $this->_content, + 'disables discussions if configured' + ); + // testing version number in JS address, since other instances may not be present in different templates + $this->assertTag( + array( + 'tag' => 'script', + 'attributes' => array( + 'src' => 'js/zerobin.js?' . rawurlencode(self::$version) + ), + ), + $this->_content, + 'outputs version correctly' + ); + } + + /** + * @expectedException RainTpl_Exception + */ + public function testMissingTemplate() + { + $test = new RainTPL; + $test->draw('123456789 does not exist!'); + } +} diff --git a/tst/auto.php b/tst/auto.php index f8cb4c09..3bc99b3e 100644 --- a/tst/auto.php +++ b/tst/auto.php @@ -1,8 +1,8 @@ -assertFalse(auto::loader('foo2501bar42'), 'calling non existent class'); - } -} +assertFalse(auto::loader('foo2501bar42'), 'calling non existent class'); + } +} diff --git a/tst/filter.php b/tst/filter.php index 198a13f5..c8d42399 100644 --- a/tst/filter.php +++ b/tst/filter.php @@ -1,47 +1,47 @@ -assertEquals( - array("f'oo", "b'ar", array("fo'o", "b'ar")), - filter::stripslashes_deep(array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"))) - ); - } - - public function testFilterMakesSizesHumanlyReadable() - { - $this->assertEquals('1 B', filter::size_humanreadable(1)); - $this->assertEquals('1 000 B', filter::size_humanreadable(1000)); - $this->assertEquals('1.00 kiB', filter::size_humanreadable(1024)); - $this->assertEquals('1.21 kiB', filter::size_humanreadable(1234)); - $exponent = 1024; - $this->assertEquals('1 000.00 kiB', filter::size_humanreadable(1000 * $exponent)); - $this->assertEquals('1.00 MiB', filter::size_humanreadable(1024 * $exponent)); - $this->assertEquals('1.21 MiB', filter::size_humanreadable(1234 * $exponent)); - $exponent *= 1024; - $this->assertEquals('1 000.00 MiB', filter::size_humanreadable(1000 * $exponent)); - $this->assertEquals('1.00 GiB', filter::size_humanreadable(1024 * $exponent)); - $this->assertEquals('1.21 GiB', filter::size_humanreadable(1234 * $exponent)); - $exponent *= 1024; - $this->assertEquals('1 000.00 GiB', filter::size_humanreadable(1000 * $exponent)); - $this->assertEquals('1.00 TiB', filter::size_humanreadable(1024 * $exponent)); - $this->assertEquals('1.21 TiB', filter::size_humanreadable(1234 * $exponent)); - $exponent *= 1024; - $this->assertEquals('1 000.00 TiB', filter::size_humanreadable(1000 * $exponent)); - $this->assertEquals('1.00 PiB', filter::size_humanreadable(1024 * $exponent)); - $this->assertEquals('1.21 PiB', filter::size_humanreadable(1234 * $exponent)); - $exponent *= 1024; - $this->assertEquals('1 000.00 PiB', filter::size_humanreadable(1000 * $exponent)); - $this->assertEquals('1.00 EiB', filter::size_humanreadable(1024 * $exponent)); - $this->assertEquals('1.21 EiB', filter::size_humanreadable(1234 * $exponent)); - $exponent *= 1024; - $this->assertEquals('1 000.00 EiB', filter::size_humanreadable(1000 * $exponent)); - $this->assertEquals('1.00 ZiB', filter::size_humanreadable(1024 * $exponent)); - $this->assertEquals('1.21 ZiB', filter::size_humanreadable(1234 * $exponent)); - $exponent *= 1024; - $this->assertEquals('1 000.00 ZiB', filter::size_humanreadable(1000 * $exponent)); - $this->assertEquals('1.00 YiB', filter::size_humanreadable(1024 * $exponent)); - $this->assertEquals('1.21 YiB', filter::size_humanreadable(1234 * $exponent)); - } -} +assertEquals( + array("f'oo", "b'ar", array("fo'o", "b'ar")), + filter::stripslashes_deep(array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"))) + ); + } + + public function testFilterMakesSizesHumanlyReadable() + { + $this->assertEquals('1 B', filter::size_humanreadable(1)); + $this->assertEquals('1 000 B', filter::size_humanreadable(1000)); + $this->assertEquals('1.00 kiB', filter::size_humanreadable(1024)); + $this->assertEquals('1.21 kiB', filter::size_humanreadable(1234)); + $exponent = 1024; + $this->assertEquals('1 000.00 kiB', filter::size_humanreadable(1000 * $exponent)); + $this->assertEquals('1.00 MiB', filter::size_humanreadable(1024 * $exponent)); + $this->assertEquals('1.21 MiB', filter::size_humanreadable(1234 * $exponent)); + $exponent *= 1024; + $this->assertEquals('1 000.00 MiB', filter::size_humanreadable(1000 * $exponent)); + $this->assertEquals('1.00 GiB', filter::size_humanreadable(1024 * $exponent)); + $this->assertEquals('1.21 GiB', filter::size_humanreadable(1234 * $exponent)); + $exponent *= 1024; + $this->assertEquals('1 000.00 GiB', filter::size_humanreadable(1000 * $exponent)); + $this->assertEquals('1.00 TiB', filter::size_humanreadable(1024 * $exponent)); + $this->assertEquals('1.21 TiB', filter::size_humanreadable(1234 * $exponent)); + $exponent *= 1024; + $this->assertEquals('1 000.00 TiB', filter::size_humanreadable(1000 * $exponent)); + $this->assertEquals('1.00 PiB', filter::size_humanreadable(1024 * $exponent)); + $this->assertEquals('1.21 PiB', filter::size_humanreadable(1234 * $exponent)); + $exponent *= 1024; + $this->assertEquals('1 000.00 PiB', filter::size_humanreadable(1000 * $exponent)); + $this->assertEquals('1.00 EiB', filter::size_humanreadable(1024 * $exponent)); + $this->assertEquals('1.21 EiB', filter::size_humanreadable(1234 * $exponent)); + $exponent *= 1024; + $this->assertEquals('1 000.00 EiB', filter::size_humanreadable(1000 * $exponent)); + $this->assertEquals('1.00 ZiB', filter::size_humanreadable(1024 * $exponent)); + $this->assertEquals('1.21 ZiB', filter::size_humanreadable(1234 * $exponent)); + $exponent *= 1024; + $this->assertEquals('1 000.00 ZiB', filter::size_humanreadable(1000 * $exponent)); + $this->assertEquals('1.00 YiB', filter::size_humanreadable(1024 * $exponent)); + $this->assertEquals('1.21 YiB', filter::size_humanreadable(1234 * $exponent)); + } +} diff --git a/tst/phpunit.xml b/tst/phpunit.xml index 035b040a..eeed5e6f 100644 --- a/tst/phpunit.xml +++ b/tst/phpunit.xml @@ -14,4 +14,4 @@ - \ No newline at end of file + diff --git a/tst/sjcl.php b/tst/sjcl.php index eca5f4f2..2473ea59 100644 --- a/tst/sjcl.php +++ b/tst/sjcl.php @@ -1,21 +1,21 @@ -assertTrue(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'valid sjcl'); - $this->assertFalse(sjcl::isValid('{"iv":"$","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of iv'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"$","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of salt'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","salt":"Gx1vA2/gQ3U","ct":"$"}'), 'invalid base64 encoding of ct'); - $this->assertFalse(sjcl::isValid('{"iv":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'iv to long'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'salt to long'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA","foo":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA="}'), 'invalid additional key'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":0.9,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'unsupported version'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":100,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'not enough iterations'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":127,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid key size'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":63,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid tag length'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"!#@","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid mode'); - $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"!#@","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid cipher'); - // @note adata is not validated, except as part of the total message length - } -} +assertTrue(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'valid sjcl'); + $this->assertFalse(sjcl::isValid('{"iv":"$","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of iv'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"$","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of salt'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","salt":"Gx1vA2/gQ3U","ct":"$"}'), 'invalid base64 encoding of ct'); + $this->assertFalse(sjcl::isValid('{"iv":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'iv to long'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'salt to long'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA","foo":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA="}'), 'invalid additional key'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":0.9,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'unsupported version'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":100,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'not enough iterations'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":127,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid key size'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":63,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid tag length'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"!#@","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid mode'); + $this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"!#@","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid cipher'); + // @note adata is not validated, except as part of the total message length + } +} diff --git a/tst/trafficlimiter.php b/tst/trafficlimiter.php index 67e0a5a7..97b86555 100644 --- a/tst/trafficlimiter.php +++ b/tst/trafficlimiter.php @@ -1,30 +1,33 @@ -_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'trafficlimit'; - trafficlimiter::setPath($this->_path); - } - - public function tearDown() - { - /* Tear Down Routine */ - helper::rmdir($this->_path . DIRECTORY_SEPARATOR); - } - - public function testTrafficGetsLimited() - { - trafficlimiter::setLimit(4); - $this->assertTrue(trafficlimiter::canPass('127.0.0.1'), 'first request may pass'); - sleep(2); - $this->assertFalse(trafficlimiter::canPass('127.0.0.1'), 'second request is to fast, may not pass'); - sleep(3); - $this->assertTrue(trafficlimiter::canPass('127.0.0.1'), 'third request waited long enough and may pass'); - $this->assertTrue(trafficlimiter::canPass('2001:1620:2057:dead:beef::cafe:babe'), 'fourth request has different ip and may pass'); - $this->assertFalse(trafficlimiter::canPass('127.0.0.1'), 'fifth request is to fast, may not pass'); - } -} +_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'trafficlimit'; + trafficlimiter::setPath($this->_path); + } + + public function tearDown() + { + /* Tear Down Routine */ + helper::rmdir($this->_path . DIRECTORY_SEPARATOR); + } + + public function testTrafficGetsLimited() + { + $this->assertEquals($this->_path, trafficlimiter::getPath()); + $file = 'baz'; + $this->assertEquals($this->_path . DIRECTORY_SEPARATOR . $file, trafficlimiter::getPath($file)); + trafficlimiter::setLimit(4); + $this->assertTrue(trafficlimiter::canPass('127.0.0.1'), 'first request may pass'); + sleep(2); + $this->assertFalse(trafficlimiter::canPass('127.0.0.1'), 'second request is to fast, may not pass'); + sleep(3); + $this->assertTrue(trafficlimiter::canPass('127.0.0.1'), 'third request waited long enough and may pass'); + $this->assertTrue(trafficlimiter::canPass('2001:1620:2057:dead:beef::cafe:babe'), 'fourth request has different ip and may pass'); + $this->assertFalse(trafficlimiter::canPass('127.0.0.1'), 'fifth request is to fast, may not pass'); + } +} diff --git a/tst/vizhash16x16.php b/tst/vizhash16x16.php index cf73011c..82f06a00 100644 --- a/tst/vizhash16x16.php +++ b/tst/vizhash16x16.php @@ -1,41 +1,41 @@ -_path = PATH . 'data' . DIRECTORY_SEPARATOR; - $this->_dataDirCreated = !is_dir($this->_path); - if($this->_dataDirCreated) mkdir($this->_path); - $this->_file = $this->_path . 'vizhash.png'; - } - - public function tearDown() - { - /* Tear Down Routine */ - if($this->_dataDirCreated) { - helper::rmdir($this->_path); - } else { - if(!@unlink($this->_file)) { - throw new Exception('Error deleting file "' . $this->_file . '".'); - } - } - } - - public function testVizhashGeneratesUniquePngsPerIp() - { - $vz = new vizhash16x16(); - $pngdata = $vz->generate('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('2001:1620:2057:dead:beef::cafe:babe')); - $this->assertEquals($pngdata, $vz->generate('127.0.0.1')); - } -} +_path = PATH . 'data' . DIRECTORY_SEPARATOR; + $this->_dataDirCreated = !is_dir($this->_path); + if($this->_dataDirCreated) mkdir($this->_path); + $this->_file = $this->_path . 'vizhash.png'; + } + + public function tearDown() + { + /* Tear Down Routine */ + if($this->_dataDirCreated) { + helper::rmdir($this->_path); + } else { + if(!@unlink($this->_file)) { + throw new Exception('Error deleting file "' . $this->_file . '".'); + } + } + } + + public function testVizhashGeneratesUniquePngsPerIp() + { + $vz = new vizhash16x16(); + $pngdata = $vz->generate('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('2001:1620:2057:dead:beef::cafe:babe')); + $this->assertEquals($pngdata, $vz->generate('127.0.0.1')); + } +} diff --git a/tst/zerobin/data.php b/tst/zerobin/data.php index 7e85285a..0dce94ef 100644 --- a/tst/zerobin/data.php +++ b/tst/zerobin/data.php @@ -1,70 +1,70 @@ - '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}', - 'meta' => array( - 'postdate' => 1344803344, - 'expire_date' => 1344803644, - 'opendiscussion' => true, - ), - ); - - private static $commentid = 'c47efb4741195f42'; - - private static $comment = array( - 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}', - 'meta' => array( - 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}', - 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=', - 'postdate' => 1344803528, - ), - ); - - private $_model; - - private $_path; - - public function setUp() - { - /* Setup Routine */ - $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'zerobin_data'; - $this->_model = zerobin_data::getInstance(array('dir' => $this->_path)); - } - - public function tearDown() - { - /* Tear Down Routine */ - helper::rmdir($this->_path); - } - - public function testFileBasedDataStoreWorks() - { - // storing pastes - $this->assertFalse($this->_model->exists(self::$pasteid), 'paste does not yet exist'); - $this->assertTrue($this->_model->create(self::$pasteid, self::$paste), 'store new paste'); - $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after storing it'); - $this->assertFalse($this->_model->create(self::$pasteid, self::$paste), 'unable to store the same paste twice'); - $this->assertEquals(json_decode(json_encode(self::$paste)), $this->_model->read(self::$pasteid)); - - // storing comments - $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment does not yet exist'); - $this->assertTrue($this->_model->createComment(self::$pasteid, self::$pasteid, self::$commentid, self::$comment) !== false, 'store comment'); - $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment exists after storing it'); - $comment = json_decode(json_encode(self::$comment)); - $comment->meta->commentid = self::$commentid; - $comment->meta->parentid = self::$pasteid; - $this->assertEquals( - array($comment->meta->postdate => $comment), - $this->_model->readComments(self::$pasteid) - ); - - // deleting pastes - $this->_model->delete(self::$pasteid); - $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted'); - $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment was deleted with paste'); - $this->assertFalse($this->_model->read(self::$pasteid), 'paste can no longer be found'); - } -} + '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}', + 'meta' => array( + 'postdate' => 1344803344, + 'expire_date' => 1344803644, + 'opendiscussion' => true, + ), + ); + + private static $commentid = 'c47efb4741195f42'; + + private static $comment = array( + 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}', + 'meta' => array( + 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}', + 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=', + 'postdate' => 1344803528, + ), + ); + + private $_model; + + private $_path; + + public function setUp() + { + /* Setup Routine */ + $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'zerobin_data'; + $this->_model = zerobin_data::getInstance(array('dir' => $this->_path)); + } + + public function tearDown() + { + /* Tear Down Routine */ + helper::rmdir($this->_path); + } + + public function testFileBasedDataStoreWorks() + { + // storing pastes + $this->assertFalse($this->_model->exists(self::$pasteid), 'paste does not yet exist'); + $this->assertTrue($this->_model->create(self::$pasteid, self::$paste), 'store new paste'); + $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after storing it'); + $this->assertFalse($this->_model->create(self::$pasteid, self::$paste), 'unable to store the same paste twice'); + $this->assertEquals(json_decode(json_encode(self::$paste)), $this->_model->read(self::$pasteid)); + + // storing comments + $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment does not yet exist'); + $this->assertTrue($this->_model->createComment(self::$pasteid, self::$pasteid, self::$commentid, self::$comment) !== false, 'store comment'); + $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment exists after storing it'); + $comment = json_decode(json_encode(self::$comment)); + $comment->meta->commentid = self::$commentid; + $comment->meta->parentid = self::$pasteid; + $this->assertEquals( + array($comment->meta->postdate => $comment), + $this->_model->readComments(self::$pasteid) + ); + + // deleting pastes + $this->_model->delete(self::$pasteid); + $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted'); + $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment was deleted with paste'); + $this->assertFalse($this->_model->read(self::$pasteid), 'paste can no longer be found'); + } +} diff --git a/tst/zerobin/db.php b/tst/zerobin/db.php index 132f290a..1bb5eb0c 100644 --- a/tst/zerobin/db.php +++ b/tst/zerobin/db.php @@ -4,7 +4,7 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase private static $pasteid = '501f02e9eeb8bcec'; private static $paste = array( - 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}', + 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}', 'meta' => array( 'postdate' => 1344803344, 'expire_date' => 1344803644, @@ -15,9 +15,9 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase private static $commentid = 'c47efb4741195f42'; private static $comment = array( - 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}', + 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}', 'meta' => array( - 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}', + 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}', 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=', 'postdate' => 1344803528, ),