2015-08-27 15:41:21 -04:00
|
|
|
<?php
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
use PrivateBin\Data\Database;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
class DatabaseTest extends PHPUnit_Framework_TestCase
|
2015-08-27 15:41:21 -04:00
|
|
|
{
|
|
|
|
private $_model;
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
private $_path;
|
|
|
|
|
2015-09-27 08:36:20 -04:00
|
|
|
private $_options = array(
|
|
|
|
'dsn' => 'sqlite::memory:',
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
|
|
|
|
2015-08-27 15:41:21 -04:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
|
|
|
$this->_model = Database::getInstance($this->_options);
|
2015-08-27 15:41:21 -04:00
|
|
|
}
|
|
|
|
|
2016-07-15 11:02:59 -04:00
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2016-08-09 05:54:42 -04:00
|
|
|
if (is_dir($this->_path)) {
|
|
|
|
Helper::rmDir($this->_path);
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2016-07-15 11:02:59 -04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:41:21 -04:00
|
|
|
public function testDatabaseBasedDataStoreWorks()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->delete(Helper::getPasteId());
|
2015-09-26 06:29:27 -04:00
|
|
|
|
|
|
|
// storing pastes
|
2016-08-09 05:54:42 -04:00
|
|
|
$paste = Helper::getPaste(array('expire_date' => 1344803344));
|
|
|
|
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
|
|
|
|
$this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
|
|
|
|
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
|
|
|
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
|
|
|
$this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(Helper::getPasteId()));
|
2015-08-27 15:41:21 -04:00
|
|
|
|
|
|
|
// storing comments
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
|
|
|
|
$this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()) !== false, 'store comment');
|
|
|
|
$this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment exists after storing it');
|
|
|
|
$comment = json_decode(json_encode(Helper::getComment()));
|
|
|
|
$comment->id = Helper::getCommentId();
|
|
|
|
$comment->parentid = Helper::getPasteId();
|
2015-08-27 15:41:21 -04:00
|
|
|
$this->assertEquals(
|
|
|
|
array($comment->meta->postdate => $comment),
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->readComments(Helper::getPasteId())
|
2015-08-27 15:41:21 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// deleting pastes
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->delete(Helper::getPasteId());
|
|
|
|
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
|
|
|
|
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment was deleted with paste');
|
|
|
|
$this->assertFalse($this->_model->read(Helper::getPasteId()), 'paste can no longer be found');
|
2015-08-27 15:41:21 -04:00
|
|
|
}
|
|
|
|
|
2015-09-26 06:29:27 -04:00
|
|
|
public function testDatabaseBasedAttachmentStoreWorks()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->delete(Helper::getPasteId());
|
|
|
|
$original = $paste = Helper::getPasteWithAttachment(array('expire_date' => 1344803344));
|
2015-09-27 08:36:20 -04:00
|
|
|
$paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true;
|
2015-09-26 06:29:27 -04:00
|
|
|
$paste['meta']['attachment'] = $paste['attachment'];
|
|
|
|
$paste['meta']['attachmentname'] = $paste['attachmentname'];
|
|
|
|
unset($paste['attachment'], $paste['attachmentname']);
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
|
|
|
|
$this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
|
|
|
|
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
|
|
|
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
|
|
|
$this->assertEquals(json_decode(json_encode($original)), $this->_model->read(Helper::getPasteId()));
|
2015-09-26 06:29:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-15 11:02:59 -04:00
|
|
|
public function testPurge()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->_model->delete(Helper::getPasteId());
|
|
|
|
$expired = Helper::getPaste(array('expire_date' => 1344803344));
|
|
|
|
$paste = Helper::getPaste(array('expire_date' => time() + 3600));
|
2016-07-15 11:02:59 -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-15 11:02:59 -04:00
|
|
|
$ids[$key] = substr(md5($key), 0, 16);
|
2016-07-18 08:47:32 -04:00
|
|
|
$this->_model->delete($ids[$key]);
|
2016-07-15 11:02:59 -04:00
|
|
|
$this->assertFalse($this->_model->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-15 11:02:59 -04:00
|
|
|
$this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2016-07-15 11:02:59 -04:00
|
|
|
$this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
|
|
|
|
}
|
|
|
|
$this->assertTrue($this->_model->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->exists($id), "paste $key exists after purge");
|
|
|
|
$this->_model->delete($id);
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2016-07-19 02:40:33 -04:00
|
|
|
$this->assertFalse($this->_model->exists($id), "paste $key was purged");
|
2016-07-15 11:02:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:41:21 -04:00
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetIbmInstance()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance(array(
|
2015-08-27 15:41:21 -04:00
|
|
|
'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetInformixInstance()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance(array(
|
2015-08-27 15:41:21 -04:00
|
|
|
'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetMssqlInstance()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance(array(
|
2015-08-27 15:41:21 -04:00
|
|
|
'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetMysqlInstance()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance(array(
|
2015-08-27 15:41:21 -04:00
|
|
|
'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetOciInstance()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance(array(
|
2015-08-27 15:41:21 -04:00
|
|
|
'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetPgsqlInstance()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance(array(
|
2015-08-27 15:41:21 -04:00
|
|
|
'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 5
|
|
|
|
*/
|
|
|
|
public function testGetFooInstance()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance(array(
|
2015-08-27 15:41:21 -04:00
|
|
|
'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
|
|
|
|
));
|
|
|
|
}
|
2015-09-27 08:36:20 -04:00
|
|
|
|
2016-07-13 03:41:45 -04:00
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingDsn()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['dsn']);
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance($options);
|
2016-07-13 03:41:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingUsr()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['usr']);
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance($options);
|
2016-07-13 03:41:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingPwd()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['pwd']);
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance($options);
|
2016-07-13 03:41:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingOpt()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['opt']);
|
2016-08-09 05:54:42 -04:00
|
|
|
Database::getInstance($options);
|
2016-07-13 03:41:45 -04:00
|
|
|
}
|
|
|
|
|
2016-07-18 08:47:32 -04:00
|
|
|
public function testOldAttachments()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
mkdir($this->_path);
|
|
|
|
$path = $this->_path . DIRECTORY_SEPARATOR . 'attachement-test.sq3';
|
|
|
|
if (is_file($path)) unlink($path);
|
2016-07-18 08:47:32 -04:00
|
|
|
$this->_options['dsn'] = 'sqlite:' . $path;
|
|
|
|
$this->_options['tbl'] = 'bar_';
|
2016-08-09 05:54:42 -04:00
|
|
|
$model = Database::getInstance($this->_options);
|
2016-07-18 08:47:32 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$original = $paste = Helper::getPasteWithAttachment(array('expire_date' => 1344803344));
|
2016-07-18 08:47:32 -04:00
|
|
|
$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(
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::getPasteId(),
|
2016-07-18 08:47:32 -04:00
|
|
|
$paste['data'],
|
|
|
|
$paste['meta']['postdate'],
|
|
|
|
1344803344,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
json_encode($meta),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$statement->closeCursor();
|
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertTrue($model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
|
|
|
$this->assertEquals(json_decode(json_encode($original)), $model->read(Helper::getPasteId()));
|
2016-07-18 08:47:32 -04:00
|
|
|
|
2016-08-09 05:54:42 -04:00
|
|
|
Helper::rmDir($this->_path);
|
2016-07-18 08:47:32 -04:00
|
|
|
}
|
|
|
|
|
2015-09-27 08:36:20 -04:00
|
|
|
public function testTableUpgrade()
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
mkdir($this->_path);
|
|
|
|
$path = $this->_path . DIRECTORY_SEPARATOR . 'db-test.sq3';
|
|
|
|
if (is_file($path)) unlink($path);
|
2015-09-27 08:36:20 -04:00
|
|
|
$this->_options['dsn'] = 'sqlite:' . $path;
|
|
|
|
$this->_options['tbl'] = 'foo_';
|
|
|
|
$db = new PDO(
|
|
|
|
$this->_options['dsn'],
|
|
|
|
$this->_options['usr'],
|
|
|
|
$this->_options['pwd'],
|
|
|
|
$this->_options['opt']
|
|
|
|
);
|
|
|
|
$db->exec(
|
|
|
|
'CREATE TABLE foo_paste ( ' .
|
|
|
|
'dataid CHAR(16), ' .
|
|
|
|
'data TEXT, ' .
|
|
|
|
'postdate INT, ' .
|
|
|
|
'expiredate INT, ' .
|
|
|
|
'opendiscussion INT, ' .
|
|
|
|
'burnafterreading INT );'
|
|
|
|
);
|
2016-07-18 08:47:32 -04:00
|
|
|
$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 );"
|
|
|
|
);
|
2016-08-09 05:54:42 -04:00
|
|
|
$this->assertInstanceOf(Database::class, Database::getInstance($this->_options));
|
|
|
|
Helper::rmDir($this->_path);
|
2015-09-27 08:36:20 -04:00
|
|
|
}
|
2015-08-27 15:41:21 -04:00
|
|
|
}
|