2015-08-27 21:41:21 +02:00
|
|
|
<?php
|
2016-07-11 11:58:15 +02:00
|
|
|
class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
2015-08-27 21:41:21 +02:00
|
|
|
{
|
|
|
|
private $_model;
|
|
|
|
|
2015-09-27 14:36:20 +02:00
|
|
|
private $_options = array(
|
|
|
|
'dsn' => 'sqlite::memory:',
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
|
|
|
|
2015-08-27 21:41:21 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-07-11 11:58:15 +02:00
|
|
|
$this->_model = privatebin_db::getInstance($this->_options);
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDatabaseBasedDataStoreWorks()
|
|
|
|
{
|
2015-09-26 12:29:27 +02:00
|
|
|
$this->_model->delete(helper::getPasteId());
|
|
|
|
|
|
|
|
// storing pastes
|
2015-09-21 22:32:52 +02: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 21:41:21 +02:00
|
|
|
|
|
|
|
// storing comments
|
2015-09-21 22:32:52 +02: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()));
|
2015-10-18 14:37:58 +02:00
|
|
|
$comment->id = helper::getCommentId();
|
|
|
|
$comment->parentid = helper::getPasteId();
|
2015-08-27 21:41:21 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array($comment->meta->postdate => $comment),
|
2015-09-21 22:32:52 +02:00
|
|
|
$this->_model->readComments(helper::getPasteId())
|
2015-08-27 21:41:21 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// deleting pastes
|
2015-09-21 22:32:52 +02: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 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
2015-09-26 12:29:27 +02:00
|
|
|
public function testDatabaseBasedAttachmentStoreWorks()
|
|
|
|
{
|
|
|
|
$this->_model->delete(helper::getPasteId());
|
|
|
|
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
|
2015-09-27 14:36:20 +02:00
|
|
|
$paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true;
|
2015-09-26 12:29:27 +02:00
|
|
|
$paste['meta']['attachment'] = $paste['attachment'];
|
|
|
|
$paste['meta']['attachmentname'] = $paste['attachmentname'];
|
|
|
|
unset($paste['attachment'], $paste['attachmentname']);
|
|
|
|
$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-08-27 21:41:21 +02:00
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetIbmInstance()
|
|
|
|
{
|
2016-07-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance(array(
|
2015-08-27 21:41:21 +02:00
|
|
|
'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetInformixInstance()
|
|
|
|
{
|
2016-07-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance(array(
|
2015-08-27 21:41:21 +02:00
|
|
|
'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetMssqlInstance()
|
|
|
|
{
|
2016-07-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance(array(
|
2015-08-27 21:41:21 +02:00
|
|
|
'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetMysqlInstance()
|
|
|
|
{
|
2016-07-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance(array(
|
2015-08-27 21:41:21 +02:00
|
|
|
'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetOciInstance()
|
|
|
|
{
|
2016-07-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance(array(
|
2015-08-27 21:41:21 +02:00
|
|
|
'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PDOException
|
|
|
|
*/
|
|
|
|
public function testGetPgsqlInstance()
|
|
|
|
{
|
2016-07-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance(array(
|
2015-08-27 21:41:21 +02:00
|
|
|
'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 5
|
|
|
|
*/
|
|
|
|
public function testGetFooInstance()
|
|
|
|
{
|
2016-07-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance(array(
|
2015-08-27 21:41:21 +02:00
|
|
|
'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
|
|
|
|
));
|
|
|
|
}
|
2015-09-27 14:36:20 +02:00
|
|
|
|
2016-07-13 09:41:45 +02:00
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingDsn()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['dsn']);
|
|
|
|
privatebin_db::getInstance($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingUsr()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['usr']);
|
|
|
|
privatebin_db::getInstance($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingPwd()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['pwd']);
|
|
|
|
privatebin_db::getInstance($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 6
|
|
|
|
*/
|
|
|
|
public function testMissingOpt()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
unset($options['opt']);
|
|
|
|
privatebin_db::getInstance($options);
|
|
|
|
}
|
|
|
|
|
2015-09-27 14:36:20 +02:00
|
|
|
public function testTableUpgrade()
|
|
|
|
{
|
|
|
|
$path = PATH . 'data/db-test.sq3';
|
|
|
|
@unlink($path);
|
|
|
|
$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-11 11:58:15 +02:00
|
|
|
privatebin_db::getInstance($this->_options);
|
2015-09-27 14:36:20 +02:00
|
|
|
@unlink($path);
|
|
|
|
}
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|