2015-09-26 11:57:46 -04:00
|
|
|
<?php
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Configuration;
|
|
|
|
use PrivateBin\Data\Database;
|
|
|
|
use PrivateBin\Model;
|
|
|
|
use PrivateBin\Model\Paste;
|
|
|
|
use PrivateBin\Vizhash16x16;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
class ModelTest extends PHPUnit_Framework_TestCase
|
2015-09-26 11:57:46 -04:00
|
|
|
{
|
|
|
|
private $_conf;
|
|
|
|
|
|
|
|
private $_model;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::confRestore();
|
2015-09-26 11:57:46 -04:00
|
|
|
$options = parse_ini_file(CONF, true);
|
2016-07-18 08:47:32 -04:00
|
|
|
$options['purge']['limit'] = 0;
|
2015-09-26 11:57:46 -04:00
|
|
|
$options['model'] = array(
|
2016-08-09 05:54:42 -04:00
|
|
|
'class' => 'Database',
|
2015-09-26 11:57:46 -04:00
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite::memory:',
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::confBackup();
|
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$this->_conf = new Configuration;
|
|
|
|
$this->_model = new Model($this->_conf);
|
2015-09-26 21:03:55 -04:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '::1';
|
2015-09-26 11:57:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicWorkflow()
|
|
|
|
{
|
|
|
|
// storing pastes
|
2016-08-09 05:54:42 -04:00
|
|
|
$pasteData = Helper::getPaste();
|
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$this->assertFalse($paste->exists(), 'paste does not yet exist');
|
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->setOpendiscussion();
|
|
|
|
$paste->setFormatter($pasteData['meta']['formatter']);
|
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$this->assertTrue($paste->exists(), 'paste exists after storing it');
|
|
|
|
$paste = $paste->get();
|
2015-09-26 21:03:55 -04:00
|
|
|
$this->assertEquals($pasteData['data'], $paste->data);
|
|
|
|
foreach (array('opendiscussion', 'formatter') as $key) {
|
|
|
|
$this->assertEquals($pasteData['meta'][$key], $paste->meta->$key);
|
2015-09-26 11:57:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// storing comments
|
2016-08-09 05:54:42 -04:00
|
|
|
$commentData = Helper::getComment();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$this->assertFalse($comment->exists(), 'comment does not yet exist');
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$comment->setData($commentData['data']);
|
|
|
|
$comment->setNickname($commentData['meta']['nickname']);
|
|
|
|
$comment->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$this->assertTrue($comment->exists(), 'comment exists after storing it');
|
|
|
|
$comment = $comment->get();
|
|
|
|
$this->assertEquals($commentData['data'], $comment->data);
|
|
|
|
$this->assertEquals($commentData['meta']['nickname'], $comment->meta->nickname);
|
|
|
|
|
|
|
|
// deleting pastes
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$this->assertFalse($paste->exists(), 'paste successfully deleted');
|
|
|
|
$this->assertEquals(array(), $paste->getComments(), 'comment was deleted with paste');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
2015-09-26 21:03:55 -04:00
|
|
|
* @expectedExceptionCode 75
|
2015-09-26 11:57:46 -04:00
|
|
|
*/
|
|
|
|
public function testPasteDuplicate()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$pasteData = Helper::getPaste();
|
2015-09-26 11:57:46 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-26 11:57:46 -04:00
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->setOpendiscussion();
|
|
|
|
$paste->setFormatter($pasteData['meta']['formatter']);
|
|
|
|
$paste->store();
|
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->setOpendiscussion();
|
|
|
|
$paste->setFormatter($pasteData['meta']['formatter']);
|
|
|
|
$paste->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
2015-09-26 21:03:55 -04:00
|
|
|
* @expectedExceptionCode 69
|
2015-09-26 11:57:46 -04:00
|
|
|
*/
|
|
|
|
public function testCommentDuplicate()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$pasteData = Helper::getPaste();
|
|
|
|
$commentData = Helper::getComment();
|
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-26 11:57:46 -04:00
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->setOpendiscussion();
|
|
|
|
$paste->setFormatter($pasteData['meta']['formatter']);
|
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$comment->setData($commentData['data']);
|
|
|
|
$comment->setNickname($commentData['meta']['nickname']);
|
|
|
|
$comment->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$comment->setData($commentData['data']);
|
|
|
|
$comment->setNickname($commentData['meta']['nickname']);
|
|
|
|
$comment->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImplicitDefaults()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$pasteData = Helper::getPaste();
|
|
|
|
$commentData = Helper::getComment();
|
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-26 11:57:46 -04:00
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->setBurnafterreading();
|
|
|
|
$paste->setOpendiscussion();
|
|
|
|
// not setting a formatter, should use default one
|
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId())->get(); // ID was set based on data
|
2015-09-26 21:03:55 -04:00
|
|
|
$this->assertEquals(true, property_exists($paste->meta, 'burnafterreading') && $paste->meta->burnafterreading, 'burn after reading takes precendence');
|
|
|
|
$this->assertEquals(false, property_exists($paste->meta, 'opendiscussion') && $paste->meta->opendiscussion, 'opendiscussion is disabled');
|
2015-09-26 11:57:46 -04:00
|
|
|
$this->assertEquals($this->_conf->getKey('defaultformatter'), $paste->meta->formatter, 'default formatter is set');
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-26 21:03:55 -04:00
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
2015-09-27 08:36:20 -04:00
|
|
|
$paste->setBurnafterreading('0');
|
2015-09-26 21:03:55 -04:00
|
|
|
$paste->setOpendiscussion();
|
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$vz = new Vizhash16x16();
|
2015-09-26 11:57:46 -04:00
|
|
|
$pngdata = 'data:image/png;base64,' . base64_encode($vz->generate($_SERVER['REMOTE_ADDR']));
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2015-09-26 11:57:46 -04:00
|
|
|
$comment->setData($commentData['data']);
|
|
|
|
$comment->setNickname($commentData['meta']['nickname']);
|
|
|
|
$comment->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId())->get();
|
2015-09-26 11:57:46 -04:00
|
|
|
$this->assertEquals($pngdata, $comment->meta->vizhash, 'nickname triggers vizhash to be set');
|
|
|
|
}
|
2015-09-26 21:03:55 -04:00
|
|
|
|
|
|
|
public function testPasteIdValidation()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
|
|
|
|
$this->assertFalse(Paste::isValidId('foo'), 'invalid hex values');
|
|
|
|
$this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
|
2015-09-26 21:03:55 -04:00
|
|
|
}
|
2015-09-27 08:36:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 62
|
|
|
|
*/
|
|
|
|
public function testInvalidComment()
|
|
|
|
{
|
|
|
|
$paste = $this->_model->getPaste();
|
2016-08-09 05:54:42 -04:00
|
|
|
$paste->getComment(Helper::getPasteId());
|
2015-09-27 08:36:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExpiration()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$pasteData = Helper::getPaste();
|
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-27 08:36:20 -04:00
|
|
|
$this->assertFalse($paste->exists(), 'paste does not yet exist');
|
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->setExpiration('5min'); // = 300 seconds
|
|
|
|
$paste->store();
|
|
|
|
|
|
|
|
$paste = $paste->get();
|
|
|
|
$this->assertEquals(300, $paste->meta->remaining_time, 'remaining time is set correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 64
|
|
|
|
*/
|
|
|
|
public function testCommentDeletion()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$pasteData = Helper::getPaste();
|
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-27 08:36:20 -04:00
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->store();
|
2016-08-09 05:54:42 -04:00
|
|
|
$paste->getComment(Helper::getPasteId())->delete();
|
2015-09-27 08:36:20 -04:00
|
|
|
}
|
2016-07-18 04:14:38 -04:00
|
|
|
|
2016-07-18 08:47:32 -04:00
|
|
|
public function testPurge()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$conf = new Configuration;
|
|
|
|
$store = Database::getInstance($conf->getSection('model_options'));
|
|
|
|
$store->delete(Helper::getPasteId());
|
|
|
|
$expired = Helper::getPaste(array('expire_date' => 1344803344));
|
|
|
|
$paste = Helper::getPaste(array('expire_date' => time() + 3600));
|
2016-07-18 08:47:32 -04:00
|
|
|
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
|
|
|
$ids = array();
|
2016-07-26 02:19:35 -04:00
|
|
|
foreach ($keys as $key) {
|
2016-07-18 08:47:32 -04:00
|
|
|
$ids[$key] = substr(md5($key), 0, 16);
|
|
|
|
$store->delete($ids[$key]);
|
|
|
|
$this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
|
2016-07-26 02:19:35 -04:00
|
|
|
if (in_array($key, array('x', 'y', 'z'))) {
|
2016-07-18 08:47:32 -04:00
|
|
|
$this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2016-07-18 08:47:32 -04:00
|
|
|
$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);
|
2016-07-26 02:19:35 -04:00
|
|
|
foreach ($ids as $key => $id) {
|
|
|
|
if (in_array($key, array('x', 'y', 'z'))) {
|
2016-07-19 02:40:33 -04:00
|
|
|
$this->assertTrue($this->_model->getPaste($id)->exists(), "paste $key exists after purge");
|
|
|
|
$this->_model->getPaste($id)->delete();
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2016-07-19 02:40:33 -04:00
|
|
|
$this->assertFalse($this->_model->getPaste($id)->exists(), "paste $key was purged");
|
2016-07-18 08:47:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 04:14:38 -04:00
|
|
|
public function testCommentWithDisabledVizhash()
|
|
|
|
{
|
|
|
|
$options = parse_ini_file(CONF, true);
|
|
|
|
$options['main']['vizhash'] = false;
|
|
|
|
$options['model'] = array(
|
|
|
|
'class' => 'privatebin_db',
|
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite::memory:',
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::confBackup();
|
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$model = new Model(new Configuration);
|
2016-07-18 04:14:38 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$pasteData = Helper::getPaste();
|
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $model->getPaste(Helper::getPasteId());
|
2016-07-18 04:14:38 -04:00
|
|
|
$this->assertFalse($paste->exists(), 'paste does not yet exist');
|
|
|
|
|
|
|
|
$paste = $model->getPaste();
|
|
|
|
$paste->setData($pasteData['data']);
|
|
|
|
$paste->setOpendiscussion();
|
|
|
|
$paste->setFormatter($pasteData['meta']['formatter']);
|
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$paste = $model->getPaste(Helper::getPasteId());
|
2016-07-18 04:14:38 -04:00
|
|
|
$this->assertTrue($paste->exists(), 'paste exists after storing it');
|
|
|
|
$paste = $paste->get();
|
|
|
|
$this->assertEquals($pasteData['data'], $paste->data);
|
|
|
|
foreach (array('opendiscussion', 'formatter') as $key) {
|
|
|
|
$this->assertEquals($pasteData['meta'][$key], $paste->meta->$key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// storing comments
|
2016-08-09 05:54:42 -04:00
|
|
|
$commentData = Helper::getComment();
|
|
|
|
$paste = $model->getPaste(Helper::getPasteId());
|
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
|
2016-07-18 04:14:38 -04:00
|
|
|
$this->assertFalse($comment->exists(), 'comment does not yet exist');
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2016-07-18 04:14:38 -04:00
|
|
|
$comment->setData($commentData['data']);
|
|
|
|
$comment->setNickname($commentData['meta']['nickname']);
|
|
|
|
$comment->store();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
|
2016-07-18 04:14:38 -04:00
|
|
|
$this->assertTrue($comment->exists(), 'comment exists after storing it');
|
|
|
|
$comment = $comment->get();
|
|
|
|
$this->assertEquals($commentData['data'], $comment->data);
|
|
|
|
$this->assertEquals($commentData['meta']['nickname'], $comment->meta->nickname);
|
|
|
|
$this->assertFalse(property_exists($comment->meta, 'vizhash'), 'vizhash was not generated');
|
|
|
|
}
|
2016-07-11 08:15:20 -04:00
|
|
|
}
|