concluding work on configuration test generator for #16. Replaced a few

die()s in the code with Exception, making it possible to test properly.
Fixed some outdated unit tests.
This commit is contained in:
El RIDO 2015-08-29 20:29:14 +02:00
parent 99dbb22e21
commit 2d0668af03
7 changed files with 18005 additions and 170 deletions

View File

@ -17,7 +17,7 @@ syntaxhighlighting = true
; (optional) set a syntax highlighting theme, as found in css/prettify/
; syntaxhighlightingtheme = "sons-of-obsidian"
; preselect the burn-after-reading feature by default, defaults to false
; preselect the burn-after-reading feature, defaults to false
burnafterreadingselected = false
; size limit per paste or comment in bytes, defaults to 2 Mibibytes

View File

@ -77,7 +77,7 @@ class zerobin
public function __construct()
{
if (version_compare(PHP_VERSION, '5.2.6') < 0)
die('ZeroBin requires php 5.2.6 or above to work. Sorry.');
throw new Exception('ZeroBin requires php 5.2.6 or above to work. Sorry.', 1);
// in case stupid admin has left magic_quotes enabled in php.ini
if (get_magic_quotes_gpc())
@ -131,9 +131,9 @@ class zerobin
$this->_conf = parse_ini_file(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', true);
foreach (array('main', 'model') as $section) {
if (!array_key_exists($section, $this->_conf)) die(
"ZeroBin requires configuration section [$section] to be present in configuration file."
);
if (!array_key_exists($section, $this->_conf)) {
throw new Exception("ZeroBin requires configuration section [$section] to be present in configuration file.", 2);
}
}
$this->_model = $this->_conf['model']['class'];
}
@ -402,6 +402,9 @@ class zerobin
return;
}
// show the same error message if the paste expired or does not exist
$genericError = 'Paste does not exist, has expired or has been deleted.';
// Check that paste exists.
if ($this->_model()->exists($dataid))
{
@ -416,7 +419,7 @@ class zerobin
{
// Delete the paste
$this->_model()->delete($dataid);
$this->_error = 'Paste does not exist, has expired or has been deleted.';
$this->_error = $genericError;
}
// If no error, return the paste.
else
@ -451,7 +454,7 @@ class zerobin
}
else
{
$this->_error = 'Paste does not exist or has expired.';
$this->_error = $genericError;
}
}

View File

@ -11,12 +11,17 @@
*/
include 'bootstrap.php';
$vrd = array('view', 'read', 'delete');
$vcud = array('view', 'create', 'read', 'delete');
new configurationTestGenerator(array(
'main/opendiscussion' => array(
array(
'setting' => true,
'tests' => array(
array(
'conditions' => array('steps' => $vrd),
'type' => 'NotTag',
'args' => array(
array(
@ -28,9 +33,26 @@ new configurationTestGenerator(array(
'$content',
'outputs enabled discussion correctly'
),
), array(
'conditions' => array('steps' => array('create'), 'traffic/limit' => 10),
'settings' => array('$_POST["opendiscussion"] = "neither 1 nor 0"'),
'type' => 'Equals',
'args' => array(
1,
'$response["status"]',
'when discussions are enabled, but invalid flag posted, fail to create paste'
),
), array(
'conditions' => array('steps' => array('create'), 'traffic/limit' => 10),
'settings' => array('$_POST["opendiscussion"] = "neither 1 nor 0"'),
'type' => 'False',
'args' => array(
'$this->_model->exists(self::$pasteid)',
'when discussions are enabled, but invalid flag posted, paste is not created'
),
),
),
'affects' => array('view')
'affects' => $vcud
), array(
'setting' => false,
'tests' => array(
@ -48,7 +70,7 @@ new configurationTestGenerator(array(
),
),
),
'affects' => array('view')
'affects' => $vrd
),
),
'main/syntaxhighlighting' => array(
@ -63,7 +85,7 @@ new configurationTestGenerator(array(
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/prettify/prettify.css#',
'href' => 'regexp:#css/prettify/prettify\.css#',
),
),
'$content',
@ -76,7 +98,7 @@ new configurationTestGenerator(array(
'tag' => 'script',
'attributes' => array(
'type' => 'text/javascript',
'src' => 'regexp:#js/prettify.js#'
'src' => 'regexp:#js/prettify\.js#'
),
),
'$content',
@ -84,7 +106,7 @@ new configurationTestGenerator(array(
),
),
),
'affects' => array('view'),
'affects' => $vrd,
), array(
'setting' => false,
'tests' => array(
@ -96,7 +118,7 @@ new configurationTestGenerator(array(
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/prettify/prettify.css#',
'href' => 'regexp:#css/prettify/prettify\.css#',
),
),
'$content',
@ -109,7 +131,7 @@ new configurationTestGenerator(array(
'tag' => 'script',
'attributes' => array(
'type' => 'text/javascript',
'src' => 'regexp:#js/prettify.js#',
'src' => 'regexp:#js/prettify\.js#',
),
),
'$content',
@ -117,7 +139,7 @@ new configurationTestGenerator(array(
),
),
),
'affects' => array('view'),
'affects' => $vrd,
),
),
'main/syntaxhighlightingtheme' => array(
@ -133,14 +155,13 @@ new configurationTestGenerator(array(
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/prettify/sons-of-obsidian.css#',
'href' => 'regexp:#css/prettify/sons-of-obsidian\.css#',
),
),
'$content',
'outputs prettify theme stylesheet correctly',
),
),
array(
), array(
'conditions' => array('main/syntaxhighlighting' => false),
'type' => 'NotTag',
'args' => array(
@ -149,7 +170,7 @@ new configurationTestGenerator(array(
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/prettify/sons-of-obsidian.css#',
'href' => 'regexp:#css/prettify/sons-of-obsidian\.css#',
),
),
'$content',
@ -157,7 +178,7 @@ new configurationTestGenerator(array(
),
),
),
'affects' => array('view'),
'affects' => $vrd,
), array(
'setting' => null, // option not set
'tests' => array(
@ -169,7 +190,7 @@ new configurationTestGenerator(array(
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/prettify/sons-of-obsidian.css#',
'href' => 'regexp:#css/prettify/sons-of-obsidian\.css#',
),
),
'$content',
@ -177,7 +198,218 @@ new configurationTestGenerator(array(
),
),
),
'affects' => $vrd,
),
),
'main/burnafterreadingselected' => array(
array(
'setting' => true,
'tests' => array(
array(
'type' => 'Tag',
'args' => array(
array(
'id' => 'burnafterreading',
'attributes' => array(
'checked' => 'checked',
),
),
'$content',
'preselects burn after reading option',
),
),
),
'affects' => array('view'),
), array(
'setting' => false,
'tests' => array(
array(
'type' => 'NotTag',
'args' => array(
array(
'id' => 'burnafterreading',
'attributes' => array(
'checked' => 'checked',
),
),
'$content',
'burn after reading option is unchecked',
),
),
),
'affects' => array('view'),
),
),
'main/template' => array(
array(
'setting' => 'page',
'tests' => array(
array(
'type' => 'Tag',
'args' => array(
array(
'tag' => 'link',
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/zerobin\.css#',
),
),
'$content',
'outputs "page" stylesheet correctly',
),
), array(
'type' => 'NotTag',
'args' => array(
array(
'tag' => 'link',
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/bootstrap/bootstrap-\d[\d\.]+\d\.css#',
),
),
'$content',
'removes "bootstrap" stylesheet correctly',
),
),
),
'affects' => $vrd,
), array(
'setting' => 'bootstrap',
'tests' => array(
array(
'type' => 'NotTag',
'args' => array(
array(
'tag' => 'link',
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/zerobin.css#',
),
),
'$content',
'removes "page" stylesheet correctly',
),
), array(
'type' => 'Tag',
'args' => array(
array(
'tag' => 'link',
'attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
'href' => 'regexp:#css/bootstrap/bootstrap-\d[\d\.]+\d\.css#',
),
),
'$content',
'outputs "bootstrap" stylesheet correctly',
),
),
),
'affects' => $vrd,
),
),
'main/sizelimit' => array(
array(
'setting' => 10,
'tests' => array(
array(
'conditions' => array('steps' => array('create'), 'traffic/limit' => 10),
'type' => 'Equals',
'args' => array(
1,
'$response["status"]',
'when sizelimit limit exceeded, fail to create paste'
),
),
),
'affects' => array('create')
), array(
'setting' => 2097152,
'tests' => array(
array(
'conditions' => array('steps' => array('create'), 'traffic/limit' => 0, 'main/burnafterreadingselected' => true),
'settings' => array('sleep(3)'),
'type' => 'Equals',
'args' => array(
0,
'$response["status"]',
'when sizelimit limit is not reached, successfully create paste'
),
), array(
'conditions' => array('steps' => array('create'), 'traffic/limit' => 0, 'main/burnafterreadingselected' => true),
'settings' => array('sleep(3)'),
'type' => 'True',
'args' => array(
'$this->_model->exists($response["id"])',
'when sizelimit limit is not reached, paste exists after posting data'
),
),
),
'affects' => array('create')
),
),
'traffic/limit' => array(
array(
'setting' => 0,
'tests' => array(
array(
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
'type' => 'Equals',
'args' => array(
0,
'$response["status"]',
'when traffic limit is disabled, successfully create paste'
),
), array(
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
'type' => 'True',
'args' => array(
'$this->_model->exists($response["id"])',
'when traffic limit is disabled, paste exists after posting data'
),
),
),
'affects' => array('create')
), array(
'setting' => 10,
'tests' => array(
array(
'conditions' => array('steps' => array('create')),
'type' => 'Equals',
'args' => array(
1,
'$response["status"]',
'when traffic limit is on and we do not wait, fail to create paste'
),
),
),
'affects' => array('create')
), array(
'setting' => 2,
'tests' => array(
array(
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
'settings' => array('sleep(3)'),
'type' => 'Equals',
'args' => array(
0,
'$response["status"]',
'when traffic limit is on and we wait, successfully create paste'
),
), array(
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
'settings' => array('sleep(3)'),
'type' => 'True',
'args' => array(
'$this->_model->exists($response["id"])',
'when traffic limit is on and we wait, paste exists after posting data'
),
),
),
'affects' => array('create')
),
),
));
@ -233,115 +465,31 @@ class configurationTestGenerator
$fullOptions = array_replace_recursive($defaultOptions, $conf['options']);
$options = helper::var_export_min($fullOptions, true);
foreach ($conf['affects'] as $step) {
$step = ucfirst($step);
switch ($step) {
case 'Create':
$code .= <<<EOT
/**
* @runInSeparateProcess
*/
public function test$step$key()
{
\$this->reset($options);
\$_POST = self::\$paste;
\$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new zerobin;
\$content = ob_get_contents();
\$response = json_decode(\$content, true);
\$this->assertEquals(\$response['status'], 0, 'outputs status');
\$this->assertEquals(
\$response['deletetoken'],
hash_hmac('sha1', \$response['id'], serversalt::get()),
'outputs valid delete token'
);
\$this->assertTrue(\$this->_model->exists(\$response['id']), 'paste exists after posting data');
EOT;
break;
case 'Read':
$code .= <<<EOT
/**
* @runInSeparateProcess
*/
public function test$step$key()
{
\$this->reset($options);
\$this->_model->create(self::\$pasteid, self::\$paste);
\$_SERVER['QUERY_STRING'] = self::\$pasteid;
ob_start();
new zerobin;
\$content = ob_get_contents();
\$this->assertTag(
array(
'id' => 'cipherdata',
'content' => htmlspecialchars(json_encode(self::\$paste), ENT_NOQUOTES)
),
\$content,
'outputs data correctly'
);
EOT;
break;
case 'Delete':
$code .= <<<EOT
/**
* @runInSeparateProcess
*/
public function test$step$key()
{
\$this->reset($options);
\$this->_model->create(self::$\pasteid, self::$\paste);
\$this->assertTrue(\$this->_model->exists(self::$\pasteid), 'paste exists before deleting data');
\$_GET['pasteid'] = self::$\pasteid;
\$_GET['deletetoken'] = hash_hmac('sha1', self::$\pasteid, serversalt::get());
ob_start();
new zerobin;
\$content = ob_get_contents();
\$this->assertTag(
array(
'id' => 'status',
'content' => 'Paste was properly deleted'
),
\$content,
'outputs deleted status correctly'
);
\$this->assertFalse(\$this->_model->exists(self::\$pasteid), 'paste successfully deleted');
EOT;
break;
// view
default:
$code .= <<<EOT
/**
* @runInSeparateProcess
*/
public function test$step$key()
{
\$this->reset($options);
ob_start();
new zerobin;
\$content = ob_get_contents();
EOT;
}
$testCode = $preCode = array();
foreach ($conf['tests'] as $tests) {
foreach ($tests as $test) {
foreach ($tests[0] as $test) {
// skip if test does not affect this step
if (!in_array($step, $tests[1])) {
continue;
}
// skip if not all test conditions are met
if (array_key_exists('conditions', $test)) {
while(list($path, $setting) = each($test['conditions'])) {
list($section, $option) = explode('/', $path);
if ($fullOptions[$section][$option] !== $setting) {
while (list($path, $setting) = each($test['conditions'])) {
if ($path == 'steps' && !in_array($step, $setting)) {
continue 2;
} elseif($path != 'steps') {
list($section, $option) = explode('/', $path);
if ($fullOptions[$section][$option] !== $setting) {
continue 2;
}
}
}
}
if (array_key_exists('settings', $test)) {
foreach ($test['settings'] as $setting) {
$preCode[$setting] = $setting;
}
}
$args = array();
foreach ($test['args'] as $arg) {
if (is_string($arg) && strpos($arg, '$') === 0) {
@ -350,12 +498,12 @@ EOT;
$args[] = helper::var_export_min($arg, true);
}
}
$type = $test['type'];
$args = implode(', ', $args);
$code .= " \$this->assert$type($args);" . PHP_EOL;
$testCode[] = array($test['type'], implode(', ', $args));
}
}
$code .= ' }' . PHP_EOL . PHP_EOL;
$code .= $this->_getFunction(
ucfirst($step), $key, $options, $preCode, $testCode
);
}
}
$code .= '}' . PHP_EOL;
@ -376,7 +524,7 @@ EOT;
*/
class configurationTest extends PHPUnit_Framework_TestCase
{
private static $pasteid = '501f02e9eeb8bcec';
private static $pasteid = '5e9bc25c89fb3bf9';
private static $paste = array(
'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
@ -418,9 +566,116 @@ class configurationTest extends PHPUnit_Framework_TestCase
helper::createIniFile($this->_conf, $configuration);
}
EOT;
}
/**
* get unit tests function block
*
* @param string $step
* @param int $key
* @param array $options
* @param array $preCode
* @param array $testCode
* @return string
*/
private function _getFunction($step, $key, &$options, $preCode, $testCode)
{
if (count($testCode) == 0) {
echo "skipping creation of test$step$key, no valid tests found for configuration: $options". PHP_EOL;
return '';
}
$preString = $testString = '';
foreach ($preCode as $setting) {
$preString .= " $setting;" . PHP_EOL;
}
foreach ($testCode as $test) {
$type = $test[0];
$args = $test[1];
$testString .= " \$this->assert$type($args);" . PHP_EOL;
}
$code = <<<EOT
/**
* @runInSeparateProcess
*/
public function test$step$key()
{
\$this->reset($options);
EOT;
// step specific initialization
switch ($step) {
case 'Create':
$code .= PHP_EOL . <<<'EOT'
$_POST = self::$paste;
$_SERVER['REMOTE_ADDR'] = '::1';
EOT;
break;
case 'Read':
$code .= PHP_EOL . <<<'EOT'
$this->_model->create(self::$pasteid, self::$paste);
$_SERVER['QUERY_STRING'] = self::$pasteid;
EOT;
break;
case 'Delete':
$code .= PHP_EOL . <<<'EOT'
$this->_model->create(self::$pasteid, self::$paste);
$this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
$_GET['pasteid'] = self::$pasteid;
$_GET['deletetoken'] = hash_hmac('sha1', self::$pasteid, serversalt::get());
EOT;
break;
}
// all steps
$code .= PHP_EOL . $preString;
$code .= <<<'EOT'
ob_start();
new zerobin;
$content = ob_get_contents();
EOT;
// step specific tests
switch ($step) {
case 'Create':
$code .= <<<'EOT'
$response = json_decode($content, true);
EOT;
break;
case 'Read':
$code .= <<<'EOT'
$this->assertTag(
array(
'id' => 'cipherdata',
'content' => htmlspecialchars(json_encode(self::$paste), ENT_NOQUOTES)
),
$content,
'outputs data correctly'
);
EOT;
break;
case 'Delete':
$code .= <<<'EOT'
$this->assertTag(
array(
'id' => 'status',
'content' => 'Paste was properly deleted'
),
$content,
'outputs deleted status correctly'
);
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
EOT;
break;
}
return $code . PHP_EOL . PHP_EOL . $testString . ' }' . PHP_EOL . PHP_EOL;
}
/**
* recursive function to generate configurations based on options
*
@ -485,7 +740,7 @@ EOT;
throw new Exception("Endless loop or error in options detected: option '$option' already exists with setting '$val' in one of the configurations!");
}
$configuration['options'][$section][$option] = $setting['setting'];
$configuration['tests'][$option] = $setting['tests'];
$configuration['tests'][$option] = array($setting['tests'], $setting['affects']);
foreach ($setting['affects'] as $affects) {
if (!in_array($affects, $configuration['affects'])) {
$configuration['affects'][] = $affects;

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
<?php
class zerobinTest extends PHPUnit_Framework_TestCase
{
private static $pasteid = '501f02e9eeb8bcec';
private static $pasteid = '5e9bc25c89fb3bf9';
private static $paste = array(
'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
@ -11,6 +11,17 @@ class zerobinTest extends PHPUnit_Framework_TestCase
),
);
private static $commentid = '5a52eebf11c4c94b';
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;
public function setUp()
@ -33,6 +44,9 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$_SERVER = array();
if ($this->_model->exists(self::$pasteid))
$this->_model->delete(self::$pasteid);
$conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
if (is_file($conf . '.bak'))
rename($conf . '.bak', $conf);
}
/**
@ -54,6 +68,63 @@ class zerobinTest extends PHPUnit_Framework_TestCase
);
}
/**
* @runInSeparateProcess
*/
public function testHtaccess()
{
$this->reset();
$dirs = array('cfg', 'lib');
foreach ($dirs as $dir) {
$file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
@unlink($file);
}
ob_start();
new zerobin;
$content = ob_get_contents();
foreach ($dirs as $dir) {
$file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
$this->assertFileExists(
$file,
"$dir htaccess recreated"
);
}
}
/**
* @expectedException Exception
* @expectedExceptionCode 2
*/
public function testConf()
{
$this->reset();
$conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
if (!is_file($conf . '.bak') && is_file($conf))
rename($conf, $conf . '.bak');
file_put_contents($conf, '');
ob_start();
new zerobin;
$content = ob_get_contents();
}
/**
* @runInSeparateProcess
*/
public function testConfMissingExpireLabel()
{
$this->reset();
$conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
$options = parse_ini_file($conf, true);
$options['expire_options']['foobar123'] = 10;
if (!is_file($conf . '.bak') && is_file($conf))
rename($conf, $conf . '.bak');
helper::createIniFile($conf, $options);
ini_set('magic_quotes_gpc', 1);
ob_start();
new zerobin;
$content = ob_get_contents();
}
/**
* @runInSeparateProcess
*/
@ -66,15 +137,199 @@ class zerobinTest extends PHPUnit_Framework_TestCase
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals($response['status'], 0, 'outputs status');
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertEquals(
$response['deletetoken'],
hash_hmac('sha1', $response['id'], serversalt::get()),
$response['deletetoken'],
'outputs valid delete token'
);
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateValidExpire()
{
$this->reset();
$_POST = self::$paste;
$_POST['expire'] = '5min';
$_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertEquals(
hash_hmac('sha1', $response['id'], serversalt::get()),
$response['deletetoken'],
'outputs valid delete token'
);
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateInvalidExpire()
{
$this->reset();
$_POST = self::$paste;
$_POST['expire'] = 'foo';
$_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertEquals(
hash_hmac('sha1', $response['id'], serversalt::get()),
$response['deletetoken'],
'outputs valid delete token'
);
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateInvalidBurn()
{
$this->reset();
$_POST = self::$paste;
$_POST['burnafterreading'] = 'neither 1 nor 0';
$_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateInvalidOpenDiscussion()
{
$this->reset();
$_POST = self::$paste;
$_POST['opendiscussion'] = 'neither 1 nor 0';
$_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateValidNick()
{
$this->reset();
$_POST = self::$paste;
$_POST['nickname'] = self::$comment['meta']['nickname'];
$_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertEquals(
hash_hmac('sha1', $response['id'], serversalt::get()),
$response['deletetoken'],
'outputs valid delete token'
);
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateInvalidNick()
{
$this->reset();
$_POST = self::$paste;
$_POST['nickname'] = 'foo';
$_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateComment()
{
$this->reset();
$_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = self::$pasteid;
$_SERVER['REMOTE_ADDR'] = '::1';
$this->_model->create(self::$pasteid, self::$paste);
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, $response['id']), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateCommentDiscussionDisabled()
{
$this->reset();
$_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = self::$pasteid;
$_SERVER['REMOTE_ADDR'] = '::1';
$paste = self::$paste;
$paste['meta']['opendiscussion'] = false;
$this->_model->create(self::$pasteid, $paste);
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateCommentInvalidPaste()
{
$this->reset();
$_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = self::$pasteid;
$_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
@ -96,6 +351,92 @@ class zerobinTest extends PHPUnit_Framework_TestCase
);
}
/**
* @runInSeparateProcess
*/
public function testReadInvalidId()
{
$this->reset();
$_SERVER['QUERY_STRING'] = 'foo';
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'errormessage',
'content' => 'Invalid paste ID'
),
$content,
'outputs error correctly'
);
}
/**
* @runInSeparateProcess
*/
public function testReadNonexisting()
{
$this->reset();
$_SERVER['QUERY_STRING'] = self::$pasteid;
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'errormessage',
'content' => 'Paste does not exist'
),
$content,
'outputs error correctly'
);
}
/**
* @runInSeparateProcess
*/
public function testReadExpired()
{
$this->reset();
$expiredPaste = self::$paste;
$expiredPaste['meta']['expire_date'] = $expiredPaste['meta']['postdate'];
$this->_model->create(self::$pasteid, $expiredPaste);
$_SERVER['QUERY_STRING'] = self::$pasteid;
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'errormessage',
'content' => 'Paste does not exist'
),
$content,
'outputs error correctly'
);
}
/**
* @runInSeparateProcess
*/
public function testReadBurn()
{
$this->reset();
$burnPaste = self::$paste;
$burnPaste['meta']['burnafterreading'] = true;
$this->_model->create(self::$pasteid, $burnPaste);
$_SERVER['QUERY_STRING'] = self::$pasteid;
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'cipherdata',
'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
),
$content,
'outputs data correctly'
);
}
/**
* @runInSeparateProcess
*/
@ -119,4 +460,71 @@ class zerobinTest extends PHPUnit_Framework_TestCase
);
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
}
/**
* @runInSeparateProcess
*/
public function testDeleteInvalidId()
{
$this->reset();
$this->_model->create(self::$pasteid, self::$paste);
$_GET['pasteid'] = 'foo';
$_GET['deletetoken'] = 'bar';
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'errormessage',
'content' => 'Invalid paste ID'
),
$content,
'outputs delete error correctly'
);
$this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
}
/**
* @runInSeparateProcess
*/
public function testDeleteInexistantId()
{
$this->reset();
$_GET['pasteid'] = self::$pasteid;
$_GET['deletetoken'] = 'bar';
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'errormessage',
'content' => 'Paste does not exist'
),
$content,
'outputs delete error correctly'
);
}
/**
* @runInSeparateProcess
*/
public function testDeleteInvalidToken()
{
$this->reset();
$this->_model->create(self::$pasteid, self::$paste);
$_GET['pasteid'] = self::$pasteid;
$_GET['deletetoken'] = 'bar';
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'errormessage',
'content' => 'Wrong deletion token'
),
$content,
'outputs delete error correctly'
);
$this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
}
}

View File

@ -1,7 +1,7 @@
<?php
class zerobin_dataTest extends PHPUnit_Framework_TestCase
{
private static $pasteid = '501f02e9eeb8bcec';
private static $pasteid = '5e9bc25c89fb3bf9';
private static $paste = array(
'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
@ -12,7 +12,7 @@ class zerobin_dataTest extends PHPUnit_Framework_TestCase
),
);
private static $commentid = 'c47efb4741195f42';
private static $commentid = '5a52eebf11c4c94b';
private static $comment = array(
'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',

View File

@ -1,7 +1,7 @@
<?php
class zerobin_dbTest extends PHPUnit_Framework_TestCase
{
private static $pasteid = '501f02e9eeb8bcec';
private static $pasteid = '5e9bc25c89fb3bf9';
private static $paste = array(
'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
@ -12,7 +12,7 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase
),
);
private static $commentid = 'c47efb4741195f42';
private static $commentid = '5a52eebf11c4c94b';
private static $comment = array(
'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',