mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-03-22 13:10:41 +08:00
improving code coverage and unit testing
This commit is contained in:
parent
ff0c55c0d6
commit
b53efda635
|
@ -107,8 +107,7 @@ dir = PATH "data"
|
||||||
[purge]
|
[purge]
|
||||||
; minimum time limit between two purgings of expired pastes, it is only
|
; minimum time limit between two purgings of expired pastes, it is only
|
||||||
; triggered when pastes are created
|
; triggered when pastes are created
|
||||||
; Set this to 0 to disable purging. Set it to run more frequently, if you are
|
; Set this to 0 to run a purge every time a paste is created.
|
||||||
; running a large site.
|
|
||||||
limit = 300
|
limit = 300
|
||||||
|
|
||||||
; maximum amount of expired pastes to delete in one purge
|
; maximum amount of expired pastes to delete in one purge
|
||||||
|
|
|
@ -243,7 +243,8 @@ class privatebin_data extends privatebin_abstract
|
||||||
}
|
}
|
||||||
|
|
||||||
$secondKey = array_rand($secondLevel);
|
$secondKey = array_rand($secondLevel);
|
||||||
$path = self::$_dir . $firstLevel[$firstKey] . '/' . $secondLevel[$secondKey];
|
$path = self::$_dir . $firstLevel[$firstKey] .
|
||||||
|
DIRECTORY_SEPARATOR . $secondLevel[$secondKey];
|
||||||
if (!is_dir($path)) continue;
|
if (!is_dir($path)) continue;
|
||||||
$thirdLevel = array_filter(
|
$thirdLevel = array_filter(
|
||||||
scandir($path),
|
scandir($path),
|
||||||
|
@ -310,7 +311,8 @@ class privatebin_data extends privatebin_abstract
|
||||||
*/
|
*/
|
||||||
private static function _dataid2path($dataid)
|
private static function _dataid2path($dataid)
|
||||||
{
|
{
|
||||||
return self::$_dir . substr($dataid,0,2) . '/' . substr($dataid,2,2) . '/';
|
return self::$_dir . substr($dataid,0,2) . DIRECTORY_SEPARATOR .
|
||||||
|
substr($dataid,2,2) . DIRECTORY_SEPARATOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -325,7 +327,8 @@ class privatebin_data extends privatebin_abstract
|
||||||
*/
|
*/
|
||||||
private static function _dataid2discussionpath($dataid)
|
private static function _dataid2discussionpath($dataid)
|
||||||
{
|
{
|
||||||
return self::_dataid2path($dataid) . $dataid . '.discussion/';
|
return self::_dataid2path($dataid) . $dataid .
|
||||||
|
'.discussion' . DIRECTORY_SEPARATOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -338,7 +341,8 @@ class privatebin_data extends privatebin_abstract
|
||||||
*/
|
*/
|
||||||
private static function _isFirstLevelDir($element)
|
private static function _isFirstLevelDir($element)
|
||||||
{
|
{
|
||||||
return self::_isSecondLevelDir($element) && is_dir(self::$_dir . '/' . $element);
|
return self::_isSecondLevelDir($element) &&
|
||||||
|
is_dir(self::$_dir . DIRECTORY_SEPARATOR . $element);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -607,8 +607,8 @@ class privatebin_db extends privatebin_abstract
|
||||||
"postdate INT$after_key );"
|
"postdate INT$after_key );"
|
||||||
);
|
);
|
||||||
self::$_db->exec(
|
self::$_db->exec(
|
||||||
'CREATE INDEX parent ON ' . self::_sanitizeIdentifier('comment') .
|
'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
|
||||||
'(pasteid);'
|
self::_sanitizeIdentifier('comment') . '(pasteid);'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -689,14 +689,17 @@ class privatebin_db extends privatebin_abstract
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
self::$_db->exec(
|
self::$_db->exec(
|
||||||
'CREATE UNIQUE INDEX primary ON ' . self::_sanitizeIdentifier('paste') . '(dataid);'
|
'CREATE UNIQUE INDEX IF NOT EXISTS paste_dataid ON ' .
|
||||||
|
self::_sanitizeIdentifier('paste') . '(dataid);'
|
||||||
);
|
);
|
||||||
self::$_db->exec(
|
self::$_db->exec(
|
||||||
'CREATE UNIQUE INDEX primary ON ' . self::_sanitizeIdentifier('comment') . '(dataid);'
|
'CREATE UNIQUE INDEX IF NOT EXISTS comment_dataid ON ' .
|
||||||
|
self::_sanitizeIdentifier('comment') . '(dataid);'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
self::$_db->exec(
|
self::$_db->exec(
|
||||||
'CREATE INDEX parent ON ' . self::_sanitizeIdentifier('comment') . '(pasteid);'
|
'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
|
||||||
|
self::_sanitizeIdentifier('comment') . '(pasteid);'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,9 @@ class modelTest extends PHPUnit_Framework_TestCase
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
/* Setup Routine */
|
/* Setup Routine */
|
||||||
|
helper::confRestore();
|
||||||
$options = parse_ini_file(CONF, true);
|
$options = parse_ini_file(CONF, true);
|
||||||
|
$options['purge']['limit'] = 0;
|
||||||
$options['model'] = array(
|
$options['model'] = array(
|
||||||
'class' => 'privatebin_db',
|
'class' => 'privatebin_db',
|
||||||
);
|
);
|
||||||
|
@ -209,6 +211,45 @@ class modelTest extends PHPUnit_Framework_TestCase
|
||||||
$paste->getComment(helper::getPasteId())->delete();
|
$paste->getComment(helper::getPasteId())->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testPurge()
|
||||||
|
{
|
||||||
|
$conf = new configuration;
|
||||||
|
$store = privatebin_db::getInstance($conf->getSection('model_options'));
|
||||||
|
$store->delete(helper::getPasteId());
|
||||||
|
$expired = helper::getPaste(array('expire_date' => 1344803344));
|
||||||
|
$paste = helper::getPaste(array('expire_date' => time() + 3600));
|
||||||
|
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
||||||
|
$ids = array();
|
||||||
|
foreach ($keys as $key)
|
||||||
|
{
|
||||||
|
$ids[$key] = substr(md5($key), 0, 16);
|
||||||
|
$store->delete($ids[$key]);
|
||||||
|
$this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
|
||||||
|
if (in_array($key, array('x', 'y', 'z')))
|
||||||
|
{
|
||||||
|
$this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$this->assertTrue($store->create($ids[$key], $expired), "store $key paste");
|
||||||
|
}
|
||||||
|
$this->assertTrue($store->exists($ids[$key]), "paste $key exists after storing it");
|
||||||
|
}
|
||||||
|
$this->_model->purge(10);
|
||||||
|
foreach ($ids as $key => $id)
|
||||||
|
{
|
||||||
|
if (in_array($key, array('x', 'y', 'z')))
|
||||||
|
{
|
||||||
|
$this->assertTrue($this->_model->getPaste($ids[$key])->exists(), "paste $key exists after purge");
|
||||||
|
$this->_model->getPaste($ids[$key])->delete();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$this->assertFalse($this->_model->getPaste($ids[$key])->exists(), "paste $key was purged");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function testCommentWithDisabledVizhash()
|
public function testCommentWithDisabledVizhash()
|
||||||
{
|
{
|
||||||
$options = parse_ini_file(CONF, true);
|
$options = parse_ini_file(CONF, true);
|
||||||
|
|
|
@ -7,7 +7,6 @@ class privatebinTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/* Setup Routine */
|
/* Setup Routine */
|
||||||
$this->_model = privatebin_data::getInstance(array('dir' => PATH . 'data'));
|
$this->_model = privatebin_data::getInstance(array('dir' => PATH . 'data'));
|
||||||
serversalt::setPath(PATH . 'data');
|
|
||||||
$this->reset();
|
$this->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -456,6 +455,28 @@ class privatebinTest extends PHPUnit_Framework_TestCase
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testCreateTooSoon()
|
||||||
|
{
|
||||||
|
$this->reset();
|
||||||
|
$_POST = helper::getPaste();
|
||||||
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
||||||
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||||||
|
$_SERVER['REMOTE_ADDR'] = '::1';
|
||||||
|
ob_start();
|
||||||
|
new privatebin;
|
||||||
|
ob_end_clean();
|
||||||
|
$this->_model->delete(helper::getPasteId());
|
||||||
|
ob_start();
|
||||||
|
new privatebin;
|
||||||
|
$content = ob_get_contents();
|
||||||
|
$response = json_decode($content, true);
|
||||||
|
$this->assertEquals(1, $response['status'], 'outputs error status');
|
||||||
|
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @runInSeparateProcess
|
* @runInSeparateProcess
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -65,6 +65,7 @@ class privatebin_dataTest extends PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
public function testPurge()
|
public function testPurge()
|
||||||
{
|
{
|
||||||
|
mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
|
||||||
$expired = helper::getPaste(array('expire_date' => 1344803344));
|
$expired = helper::getPaste(array('expire_date' => 1344803344));
|
||||||
$paste = helper::getPaste(array('expire_date' => time() + 3600));
|
$paste = helper::getPaste(array('expire_date' => time() + 3600));
|
||||||
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
||||||
|
|
|
@ -78,6 +78,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
||||||
foreach ($keys as $key)
|
foreach ($keys as $key)
|
||||||
{
|
{
|
||||||
$ids[$key] = substr(md5($key), 0, 16);
|
$ids[$key] = substr(md5($key), 0, 16);
|
||||||
|
$this->_model->delete($ids[$key]);
|
||||||
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
|
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
|
||||||
if (in_array($key, array('x', 'y', 'z')))
|
if (in_array($key, array('x', 'y', 'z')))
|
||||||
{
|
{
|
||||||
|
@ -95,6 +96,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
||||||
if (in_array($key, array('x', 'y', 'z')))
|
if (in_array($key, array('x', 'y', 'z')))
|
||||||
{
|
{
|
||||||
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after purge");
|
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after purge");
|
||||||
|
$this->_model->delete($ids[$key]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -224,10 +226,53 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
||||||
privatebin_db::getInstance($options);
|
privatebin_db::getInstance($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testOldAttachments()
|
||||||
|
{
|
||||||
|
mkdir(PATH . 'data');
|
||||||
|
$path = PATH . 'data' . DIRECTORY_SEPARATOR . 'attachement-test.sq3';
|
||||||
|
@unlink($path);
|
||||||
|
$this->_options['dsn'] = 'sqlite:' . $path;
|
||||||
|
$this->_options['tbl'] = 'bar_';
|
||||||
|
$model = privatebin_db::getInstance($this->_options);
|
||||||
|
|
||||||
|
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
|
||||||
|
$paste['meta']['attachment'] = $paste['attachment'];
|
||||||
|
$paste['meta']['attachmentname'] = $paste['attachmentname'];
|
||||||
|
unset($paste['attachment'], $paste['attachmentname']);
|
||||||
|
$meta = $paste['meta'];
|
||||||
|
|
||||||
|
$db = new PDO(
|
||||||
|
$this->_options['dsn'],
|
||||||
|
$this->_options['usr'],
|
||||||
|
$this->_options['pwd'],
|
||||||
|
$this->_options['opt']
|
||||||
|
);
|
||||||
|
$statement = $db->prepare('INSERT INTO bar_paste VALUES(?,?,?,?,?,?,?,?,?)');
|
||||||
|
$statement->execute(
|
||||||
|
array(
|
||||||
|
helper::getPasteId(),
|
||||||
|
$paste['data'],
|
||||||
|
$paste['meta']['postdate'],
|
||||||
|
1344803344,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
json_encode($meta),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$statement->closeCursor();
|
||||||
|
|
||||||
|
$this->assertTrue($model->exists(helper::getPasteId()), 'paste exists after storing it');
|
||||||
|
$this->assertEquals(json_decode(json_encode($original)), $model->read(helper::getPasteId()));
|
||||||
|
|
||||||
|
helper::rmdir(PATH . 'data');
|
||||||
|
}
|
||||||
|
|
||||||
public function testTableUpgrade()
|
public function testTableUpgrade()
|
||||||
{
|
{
|
||||||
mkdir(PATH . 'data');
|
mkdir(PATH . 'data');
|
||||||
$path = PATH . 'data/db-test.sq3';
|
$path = PATH . 'data' . DIRECTORY_SEPARATOR . 'db-test.sq3';
|
||||||
@unlink($path);
|
@unlink($path);
|
||||||
$this->_options['dsn'] = 'sqlite:' . $path;
|
$this->_options['dsn'] = 'sqlite:' . $path;
|
||||||
$this->_options['tbl'] = 'foo_';
|
$this->_options['tbl'] = 'foo_';
|
||||||
|
@ -246,7 +291,17 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
||||||
'opendiscussion INT, ' .
|
'opendiscussion INT, ' .
|
||||||
'burnafterreading INT );'
|
'burnafterreading INT );'
|
||||||
);
|
);
|
||||||
|
$db->exec(
|
||||||
|
'CREATE TABLE foo_comment ( ' .
|
||||||
|
"dataid CHAR(16) NOT NULL, " .
|
||||||
|
'pasteid CHAR(16), ' .
|
||||||
|
'parentid CHAR(16), ' .
|
||||||
|
'data BLOB, ' .
|
||||||
|
'nickname BLOB, ' .
|
||||||
|
'vizhash BLOB, ' .
|
||||||
|
"postdate INT );"
|
||||||
|
);
|
||||||
privatebin_db::getInstance($this->_options);
|
privatebin_db::getInstance($this->_options);
|
||||||
@unlink($path);
|
helper::rmdir(PATH . 'data');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ class privatebinWithDbTest extends privatebinTest
|
||||||
/* Setup Routine */
|
/* Setup Routine */
|
||||||
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
||||||
if(!is_dir($this->_path)) mkdir($this->_path);
|
if(!is_dir($this->_path)) mkdir($this->_path);
|
||||||
$this->_options['dsn'] = 'sqlite:' . $this->_path . '/tst.sq3';
|
$this->_options['dsn'] = 'sqlite:' . $this->_path . DIRECTORY_SEPARATOR . 'tst.sq3';
|
||||||
$this->_model = privatebin_db::getInstance($this->_options);
|
$this->_model = privatebin_db::getInstance($this->_options);
|
||||||
$this->reset();
|
$this->reset();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user