Renamed classes for full PSR-2 compliance, some cleanup

This commit is contained in:
El RIDO 2016-08-09 11:54:42 +02:00
parent 6e558aab0a
commit b45bef8388
52 changed files with 1943 additions and 1505 deletions

View file

@ -1,6 +1,6 @@
<?php
use PrivateBin\serversalt;
use PrivateBin\Persistence\ServerSalt;
error_reporting(E_ALL | E_STRICT);
@ -20,7 +20,7 @@ if (!is_file(CONF)) {
require PATH . 'vendor/autoload.php';
class helper
class Helper
{
/**
* example ID of a paste
@ -162,23 +162,23 @@ class helper
* @param string $path
* @throws Exception
*/
public static function rmdir($path)
public static function rmDir($path)
{
$path .= DIRECTORY_SEPARATOR;
$dir = dir($path);
while (false !== ($file = $dir->read())) {
if ($file != '.' && $file != '..') {
if (is_dir($path . $file)) {
self::rmdir($path . $file);
self::rmDir($path . $file);
} elseif (is_file($path . $file)) {
if (!@unlink($path . $file)) {
if (!unlink($path . $file)) {
throw new Exception('Error deleting file "' . $path . $file . '".');
}
}
}
}
$dir->close();
if (!@rmdir($path)) {
if (!rmdir($path)) {
throw new Exception('Error deleting directory "' . $path . '".');
}
}
@ -256,12 +256,12 @@ class helper
* @param bool $return
* @return void|string
*/
public static function var_export_min($var, $return = false)
public static function varExportMin($var, $return = false)
{
if (is_array($var)) {
$toImplode = array();
foreach ($var as $key => $value) {
$toImplode[] = var_export($key, true) . ' => ' . self::var_export_min($value, true);
$toImplode[] = var_export($key, true) . ' => ' . self::varExportMin($value, true);
}
$code = 'array(' . implode(', ', $toImplode) . ')';
if ($return) {

View file

@ -1,8 +1,8 @@
<?php
use PrivateBin\configuration;
use PrivateBin\Configuration;
class configurationTest extends PHPUnit_Framework_TestCase
class ConfigurationTest extends PHPUnit_Framework_TestCase
{
private $_options;
@ -11,7 +11,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function setUp()
{
/* Setup Routine */
helper::confBackup();
Helper::confBackup();
$this->_options = configuration::getDefaults();
$this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
$this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
@ -22,27 +22,27 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function tearDown()
{
/* Tear Down Routine */
helper::confRestore();
Helper::confRestore();
}
public function testDefaultConfigFile()
{
$this->assertTrue(copy(CONF . '.bak', CONF), 'copy default configuration file');
$conf = new configuration;
$conf = new Configuration;
$this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
}
public function testHandleFreshConfigFile()
{
helper::createIniFile(CONF, $this->_options);
$conf = new configuration;
$conf = new Configuration;
$this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
}
public function testHandleMissingConfigFile()
{
@unlink(CONF);
$conf = new configuration;
$conf = new Configuration;
$this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
}
@ -53,13 +53,13 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function testHandleBlankConfigFile()
{
file_put_contents(CONF, '');
new configuration;
new Configuration;
}
public function testHandleMinimalConfigFile()
{
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$conf = new Configuration;
$this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
}
@ -70,7 +70,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function testHandleInvalidSection()
{
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$conf = new Configuration;
$conf->getKey('foo', 'bar');
}
@ -81,14 +81,14 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function testHandleInvalidKey()
{
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$conf = new Configuration;
$conf->getKey('foo');
}
public function testHandleGetKey()
{
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$conf = new Configuration;
$this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
}
@ -103,8 +103,8 @@ class configurationTest extends PHPUnit_Framework_TestCase
$options['main']['fileupload'] = 'false';
$options['expire_options']['foo'] = 'bar';
$options['formatter_options'][] = 'foo';
helper::createIniFile(CONF, $options);
$conf = new configuration;
Helper::createIniFile(CONF, $options);
$conf = new Configuration;
$original_options['expire_options']['foo'] = intval('bar');
$original_options['formatter_options'][0] = 'foo';
$this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
@ -117,7 +117,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
unset($options['expire_options']['1year']);
unset($options['expire_options']['never']);
helper::createIniFile(CONF, $options);
$conf = new configuration;
$conf = new Configuration;
$options['expire']['default'] = '5min';
$this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
}
@ -126,13 +126,13 @@ class configurationTest extends PHPUnit_Framework_TestCase
{
$options = $this->_options;
$options['model']['class'] = 'zerobin_data';
helper::createIniFile(CONF, $options);
$conf = new configuration;
$this->assertEquals('PrivateBin\data\data', $conf->getKey('class', 'model'), 'old data class gets renamed');
Helper::createIniFile(CONF, $options);
$conf = new Configuration;
$this->assertEquals('Filesystem', $conf->getKey('class', 'model'), 'old data class gets renamed');
$options['model']['class'] = 'zerobin_db';
helper::createIniFile(CONF, $options);
$conf = new configuration;
$this->assertEquals('PrivateBin\data\db', $conf->getKey('class', 'model'), 'old db class gets renamed');
Helper::createIniFile(CONF, $options);
$conf = new Configuration;
$this->assertEquals('Database', $conf->getKey('class', 'model'), 'old db class gets renamed');
}
}

View file

@ -10,12 +10,12 @@
* a fork bomb. Please save your work before executing this script.
*/
include 'bootstrap.php';
include 'Bootstrap.php';
$vrd = array('view', 'read', 'delete');
$vcud = array('view', 'create', 'read', 'delete');
new configurationTestGenerator(array(
new ConfigurationTestGenerator(array(
'main/discussion' => array(
array(
'setting' => true,
@ -355,7 +355,7 @@ new configurationTestGenerator(array(
),
));
class configurationTestGenerator
class ConfigurationTestGenerator
{
/**
* endless loop protection, since we're working with a recursive function,
@ -405,7 +405,7 @@ class configurationTestGenerator
$code = $this->_getHeader();
foreach ($this->_configurations as $key => $conf) {
$fullOptions = array_replace_recursive($defaultOptions, $conf['options']);
$options = helper::var_export_min($fullOptions, true);
$options = Helper::varExportMin($fullOptions, true);
foreach ($conf['affects'] as $step) {
$testCode = $preCode = array();
foreach ($conf['tests'] as $tests) {
@ -437,7 +437,7 @@ class configurationTestGenerator
if (is_string($arg) && strpos($arg, '$') === 0) {
$args[] = $arg;
} else {
$args[] = helper::var_export_min($arg, true);
$args[] = Helper::varExportMin($arg, true);
}
}
$testCode[] = array($test['type'], implode(', ', $args));
@ -449,7 +449,7 @@ class configurationTestGenerator
}
}
$code .= '}' . PHP_EOL;
file_put_contents('configurationCombinations.php', $code);
file_put_contents('ConfigurationCombinationsTest.php', $code);
}
/**
@ -464,26 +464,32 @@ class configurationTestGenerator
/**
* DO NOT EDIT: This file is generated automatically using configGenerator.php
*/
class configurationCombinationsTest extends PHPUnit_Framework_TestCase
use PrivateBin\Data\Filesystem;
class ConfigurationCombinationsTest extends PHPUnit_Framework_TestCase
{
private $_conf;
private $_model;
private $_conf;
private $_path;
public function setUp()
{
/* Setup Routine */
helper::confBackup();
$this->_model = privatebin_data::getInstance(array('dir' => PATH . 'data'));
serversalt::setPath(PATH . 'data');
Helper::confBackup();
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
$this->_model = Filesystem::getInstance(array('dir' => $this->_path));
serversalt::setPath($this->_path);
$this->reset();
}
public function tearDown()
{
/* Tear Down Routine */
helper::confRestore();
Helper::confRestore();
Helper::rmDir($this->_path);
}
public function reset($configuration = array())
@ -493,7 +499,7 @@ class configurationCombinationsTest extends PHPUnit_Framework_TestCase
$_SERVER = array();
if ($this->_model->exists(helper::getPasteId()))
$this->_model->delete(helper::getPasteId());
helper::createIniFile(CONF, $configuration);
Helper::createIniFile(CONF, $configuration);
}
@ -665,7 +671,7 @@ EOT;
array_key_exists($option, $configuration['options'][$section]) &&
$configuration['options'][$section][$option] === $setting['setting']
) {
$val = helper::var_export_min($setting['setting'], true);
$val = Helper::varExportMin($setting['setting'], true);
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'];

View file

@ -1,11 +1,13 @@
<?php
use PrivateBin\data\db;
use PrivateBin\Data\Database;
class privatebin_dbTest extends PHPUnit_Framework_TestCase
class DatabaseTest extends PHPUnit_Framework_TestCase
{
private $_model;
private $_path;
private $_options = array(
'dsn' => 'sqlite::memory:',
'usr' => null,
@ -16,68 +18,69 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
public function setUp()
{
/* Setup Routine */
$this->_model = db::getInstance($this->_options);
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
$this->_model = Database::getInstance($this->_options);
}
public function tearDown()
{
/* Tear Down Routine */
if (is_dir(PATH . 'data')) {
helper::rmdir(PATH . 'data');
if (is_dir($this->_path)) {
Helper::rmDir($this->_path);
}
}
public function testDatabaseBasedDataStoreWorks()
{
$this->_model->delete(helper::getPasteId());
$this->_model->delete(Helper::getPasteId());
// storing pastes
$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()));
$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()));
// storing comments
$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();
$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();
$this->assertEquals(
array($comment->meta->postdate => $comment),
$this->_model->readComments(helper::getPasteId())
$this->_model->readComments(Helper::getPasteId())
);
// deleting pastes
$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');
$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');
}
public function testDatabaseBasedAttachmentStoreWorks()
{
$this->_model->delete(helper::getPasteId());
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$this->_model->delete(Helper::getPasteId());
$original = $paste = Helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true;
$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()));
$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()));
}
public function testPurge()
{
$this->_model->delete(helper::getPasteId());
$expired = helper::getPaste(array('expire_date' => 1344803344));
$paste = helper::getPaste(array('expire_date' => time() + 3600));
$this->_model->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) {
@ -107,7 +110,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
*/
public function testGetIbmInstance()
{
db::getInstance(array(
Database::getInstance(array(
'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
));
@ -118,7 +121,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
*/
public function testGetInformixInstance()
{
db::getInstance(array(
Database::getInstance(array(
'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
));
@ -129,7 +132,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
*/
public function testGetMssqlInstance()
{
db::getInstance(array(
Database::getInstance(array(
'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
));
@ -140,7 +143,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
*/
public function testGetMysqlInstance()
{
db::getInstance(array(
Database::getInstance(array(
'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
));
@ -151,7 +154,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
*/
public function testGetOciInstance()
{
db::getInstance(array(
Database::getInstance(array(
'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
));
@ -162,7 +165,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
*/
public function testGetPgsqlInstance()
{
db::getInstance(array(
Database::getInstance(array(
'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
));
@ -174,7 +177,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
*/
public function testGetFooInstance()
{
db::getInstance(array(
Database::getInstance(array(
'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
));
}
@ -187,7 +190,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
{
$options = $this->_options;
unset($options['dsn']);
db::getInstance($options);
Database::getInstance($options);
}
/**
@ -198,7 +201,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
{
$options = $this->_options;
unset($options['usr']);
db::getInstance($options);
Database::getInstance($options);
}
/**
@ -209,7 +212,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
{
$options = $this->_options;
unset($options['pwd']);
db::getInstance($options);
Database::getInstance($options);
}
/**
@ -220,19 +223,19 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
{
$options = $this->_options;
unset($options['opt']);
db::getInstance($options);
Database::getInstance($options);
}
public function testOldAttachments()
{
mkdir(PATH . 'data');
$path = PATH . 'data' . DIRECTORY_SEPARATOR . 'attachement-test.sq3';
@unlink($path);
mkdir($this->_path);
$path = $this->_path . DIRECTORY_SEPARATOR . 'attachement-test.sq3';
if (is_file($path)) unlink($path);
$this->_options['dsn'] = 'sqlite:' . $path;
$this->_options['tbl'] = 'bar_';
$model = db::getInstance($this->_options);
$model = Database::getInstance($this->_options);
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$original = $paste = Helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$paste['meta']['attachment'] = $paste['attachment'];
$paste['meta']['attachmentname'] = $paste['attachmentname'];
unset($paste['attachment'], $paste['attachmentname']);
@ -247,7 +250,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
$statement = $db->prepare('INSERT INTO bar_paste VALUES(?,?,?,?,?,?,?,?,?)');
$statement->execute(
array(
helper::getPasteId(),
Helper::getPasteId(),
$paste['data'],
$paste['meta']['postdate'],
1344803344,
@ -260,17 +263,17 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
);
$statement->closeCursor();
$this->assertTrue($model->exists(helper::getPasteId()), 'paste exists after storing it');
$this->assertEquals(json_decode(json_encode($original)), $model->read(helper::getPasteId()));
$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');
Helper::rmDir($this->_path);
}
public function testTableUpgrade()
{
mkdir(PATH . 'data');
$path = PATH . 'data' . DIRECTORY_SEPARATOR . 'db-test.sq3';
@unlink($path);
mkdir($this->_path);
$path = $this->_path . DIRECTORY_SEPARATOR . 'db-test.sq3';
if (is_file($path)) unlink($path);
$this->_options['dsn'] = 'sqlite:' . $path;
$this->_options['tbl'] = 'foo_';
$db = new PDO(
@ -298,7 +301,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
'vizhash BLOB, ' .
"postdate INT );"
);
db::getInstance($this->_options);
helper::rmdir(PATH . 'data');
$this->assertInstanceOf(Database::class, Database::getInstance($this->_options));
Helper::rmDir($this->_path);
}
}

View file

@ -1,8 +1,8 @@
<?php
use PrivateBin\data\data;
use PrivateBin\Data\Filesystem;
class privatebin_dataTest extends PHPUnit_Framework_TestCase
class FilesystemTest extends PHPUnit_Framework_TestCase
{
private $_model;
@ -12,65 +12,65 @@ class privatebin_dataTest extends PHPUnit_Framework_TestCase
{
/* Setup Routine */
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
$this->_model = data::getInstance(array('dir' => $this->_path));
$this->_model = Filesystem::getInstance(array('dir' => $this->_path));
}
public function tearDown()
{
/* Tear Down Routine */
helper::rmdir($this->_path);
Helper::rmDir($this->_path);
}
public function testFileBasedDataStoreWorks()
{
$this->_model->delete(helper::getPasteId());
$this->_model->delete(Helper::getPasteId());
// storing pastes
$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()));
$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()));
// storing comments
$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();
$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();
$this->assertEquals(
array($comment->meta->postdate => $comment),
$this->_model->readComments(helper::getPasteId())
$this->_model->readComments(Helper::getPasteId())
);
// deleting pastes
$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');
$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');
}
public function testFileBasedAttachmentStoreWorks()
{
$this->_model->delete(helper::getPasteId());
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$this->_model->delete(Helper::getPasteId());
$original = $paste = Helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$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()));
$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()));
}
public function testPurge()
{
mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
$expired = helper::getPaste(array('expire_date' => 1344803344));
$paste = helper::getPaste(array('expire_date' => time() + 3600));
$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) {

81
tst/FilterTest.php Normal file
View file

@ -0,0 +1,81 @@
<?php
use PrivateBin\Filter;
class FilterTest extends PHPUnit_Framework_TestCase
{
public function testFilterStripsSlashesDeeply()
{
$this->assertEquals(
array("f'oo", "b'ar", array("fo'o", "b'ar")),
Filter::stripslashesDeep(array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")))
);
}
public function testFilterMakesTimesHumanlyReadable()
{
$this->assertEquals('5 minutes', Filter::formatHumanReadableTime('5min'));
$this->assertEquals('90 seconds', Filter::formatHumanReadableTime('90sec'));
$this->assertEquals('1 week', Filter::formatHumanReadableTime('1week'));
$this->assertEquals('6 months', Filter::formatHumanReadableTime('6months'));
}
/**
* @expectedException Exception
* @expectedExceptionCode 30
*/
public function testFilterFailTimesHumanlyReadable()
{
Filter::formatHumanReadableTime('five_minutes');
}
public function testFilterMakesSizesHumanlyReadable()
{
$this->assertEquals('1 B', Filter::formatHumanReadableSize(1));
$this->assertEquals('1 000 B', Filter::formatHumanReadableSize(1000));
$this->assertEquals('1.00 KiB', Filter::formatHumanReadableSize(1024));
$this->assertEquals('1.21 KiB', Filter::formatHumanReadableSize(1234));
$exponent = 1024;
$this->assertEquals('1 000.00 KiB', Filter::formatHumanReadableSize(1000 * $exponent));
$this->assertEquals('1.00 MiB', Filter::formatHumanReadableSize(1024 * $exponent));
$this->assertEquals('1.21 MiB', Filter::formatHumanReadableSize(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 MiB', Filter::formatHumanReadableSize(1000 * $exponent));
$this->assertEquals('1.00 GiB', Filter::formatHumanReadableSize(1024 * $exponent));
$this->assertEquals('1.21 GiB', Filter::formatHumanReadableSize(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 GiB', Filter::formatHumanReadableSize(1000 * $exponent));
$this->assertEquals('1.00 TiB', Filter::formatHumanReadableSize(1024 * $exponent));
$this->assertEquals('1.21 TiB', Filter::formatHumanReadableSize(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 TiB', Filter::formatHumanReadableSize(1000 * $exponent));
$this->assertEquals('1.00 PiB', Filter::formatHumanReadableSize(1024 * $exponent));
$this->assertEquals('1.21 PiB', Filter::formatHumanReadableSize(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 PiB', Filter::formatHumanReadableSize(1000 * $exponent));
$this->assertEquals('1.00 EiB', Filter::formatHumanReadableSize(1024 * $exponent));
$this->assertEquals('1.21 EiB', Filter::formatHumanReadableSize(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 EiB', Filter::formatHumanReadableSize(1000 * $exponent));
$this->assertEquals('1.00 ZiB', Filter::formatHumanReadableSize(1024 * $exponent));
$this->assertEquals('1.21 ZiB', Filter::formatHumanReadableSize(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 ZiB', Filter::formatHumanReadableSize(1000 * $exponent));
$this->assertEquals('1.00 YiB', Filter::formatHumanReadableSize(1024 * $exponent));
$this->assertEquals('1.21 YiB', Filter::formatHumanReadableSize(1234 * $exponent));
}
public function testSlowEquals()
{
$this->assertTrue(Filter::slowEquals('foo', 'foo'), 'same string');
$this->assertFalse(Filter::slowEquals('foo', true), 'string and boolean');
$this->assertFalse(Filter::slowEquals('foo', 0), 'string and integer');
$this->assertFalse(Filter::slowEquals('123foo', 123), 'string and integer');
$this->assertFalse(Filter::slowEquals('123foo', '123'), 'different strings');
$this->assertFalse(Filter::slowEquals('6', ' 6'), 'strings with space');
$this->assertFalse(Filter::slowEquals('4.2', '4.20'), 'floats as strings');
$this->assertFalse(Filter::slowEquals('1e3', '1000'), 'integers as strings');
$this->assertFalse(Filter::slowEquals('9223372036854775807', '9223372036854775808'), 'large integers as strings');
$this->assertFalse(Filter::slowEquals('61529519452809720693702583126814', '61529519452809720000000000000000'), 'larger integers as strings');
}
}

View file

@ -1,8 +1,8 @@
<?php
use PrivateBin\i18n;
use PrivateBin\I18n;
class i18nTest extends PHPUnit_Framework_TestCase
class I18nTest extends PHPUnit_Framework_TestCase
{
private $_translations = array();
@ -24,59 +24,59 @@ class i18nTest extends PHPUnit_Framework_TestCase
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
$messageId = 'It does not matter if the message ID exists';
i18n::loadTranslations();
$this->assertEquals($messageId, i18n::_($messageId), 'fallback to en');
I18n::loadTranslations();
$this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
}
public function testCookieLanguageDeDetection()
{
$_COOKIE['lang'] = 'de';
i18n::loadTranslations();
$this->assertEquals($this->_translations['en'], i18n::_('en'), 'browser language de');
$this->assertEquals('0 Stunden', i18n::_('%d hours', 0), '0 hours in german');
$this->assertEquals('1 Stunde', i18n::_('%d hours', 1), '1 hour in german');
$this->assertEquals('2 Stunden', i18n::_('%d hours', 2), '2 hours in french');
I18n::loadTranslations();
$this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
$this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in german');
$this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in german');
$this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in french');
}
public function testBrowserLanguageDeDetection()
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
i18n::loadTranslations();
$this->assertEquals($this->_translations['en'], i18n::_('en'), 'browser language de');
$this->assertEquals('0 Stunden', i18n::_('%d hours', 0), '0 hours in german');
$this->assertEquals('1 Stunde', i18n::_('%d hours', 1), '1 hour in german');
$this->assertEquals('2 Stunden', i18n::_('%d hours', 2), '2 hours in french');
I18n::loadTranslations();
$this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
$this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in german');
$this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in german');
$this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in french');
}
public function testBrowserLanguageFrDetection()
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
i18n::loadTranslations();
$this->assertEquals('fr', i18n::_('en'), 'browser language fr');
$this->assertEquals('0 heure', i18n::_('%d hours', 0), '0 hours in french');
$this->assertEquals('1 heure', i18n::_('%d hours', 1), '1 hour in french');
$this->assertEquals('2 heures', i18n::_('%d hours', 2), '2 hours in french');
I18n::loadTranslations();
$this->assertEquals('fr', I18n::_('en'), 'browser language fr');
$this->assertEquals('0 heure', I18n::_('%d hours', 0), '0 hours in french');
$this->assertEquals('1 heure', I18n::_('%d hours', 1), '1 hour in french');
$this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in french');
}
public function testBrowserLanguagePlDetection()
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
i18n::loadTranslations();
$this->assertEquals('pl', i18n::_('en'), 'browser language pl');
$this->assertEquals('2 godzina', i18n::_('%d hours', 2), 'hours in polish');
I18n::loadTranslations();
$this->assertEquals('pl', I18n::_('en'), 'browser language pl');
$this->assertEquals('2 godzina', I18n::_('%d hours', 2), 'hours in polish');
}
public function testBrowserLanguageAnyDetection()
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
i18n::loadTranslations();
$this->assertTrue(strlen(i18n::_('en')) == 2, 'browser language any');
I18n::loadTranslations();
$this->assertTrue(strlen(I18n::_('en')) == 2, 'browser language any');
}
public function testVariableInjection()
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
i18n::loadTranslations();
$this->assertEquals('some string + 1', i18n::_('some %s + %d', 'string', 1), 'browser language en');
I18n::loadTranslations();
$this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
}
}

View file

@ -1,26 +1,31 @@
<?php
use PrivateBin\data\data;
use PrivateBin\privatebin;
use PrivateBin\request;
use PrivateBin\serversalt;
use PrivateBin\Data\Filesystem;
use PrivateBin\PrivateBin;
use PrivateBin\Request;
use PrivateBin\Persistence\ServerSalt;
class jsonApiTest extends PHPUnit_Framework_TestCase
class JsonApiTest extends PHPUnit_Framework_TestCase
{
protected $_model;
protected $_path;
public function setUp()
{
/* Setup Routine */
$this->_model = data::getInstance(array('dir' => PATH . 'data'));
serversalt::setPath(PATH . 'data');
Helper::confBackup();
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
$this->_model = Filesystem::getInstance(array('dir' => $this->_path));
ServerSalt::setPath($this->_path);
$this->reset();
}
public function tearDown()
{
/* Tear Down Routine */
helper::confRestore();
Helper::confRestore();
Helper::rmDir($this->_path);
}
public function reset()
@ -28,10 +33,16 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
$_POST = array();
$_GET = array();
$_SERVER = array();
if ($this->_model->exists(helper::getPasteId())) {
$this->_model->delete(helper::getPasteId());
if ($this->_model->exists(Helper::getPasteId())) {
$this->_model->delete(Helper::getPasteId());
}
helper::confRestore();
Helper::confRestore();
$options = parse_ini_file(CONF, true);
$options['purge']['dir'] = $this->_path;
$options['traffic']['dir'] = $this->_path;
$options['model_options']['dir'] = $this->_path;
Helper::confBackup();
Helper::createIniFile(CONF, $options);
}
/**
@ -42,14 +53,14 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -72,24 +83,24 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$paste = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$paste = Helper::getPaste();
unset($paste['meta']);
$file = tempnam(sys_get_temp_dir(), 'FOO');
file_put_contents($file, http_build_query($paste));
request::setInputStream($file);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
Request::setInputStream($file);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'PUT';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertEquals(helper::getPasteId(), $response['id'], 'outputted paste ID matches input');
$this->assertEquals(Helper::getPasteId(), $response['id'], 'outputted paste ID matches input');
$this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
$paste = $this->_model->read($response['id']);
@ -106,24 +117,24 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testDelete()
{
$this->reset();
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
$paste = $this->_model->read(helper::getPasteId());
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
$paste = $this->_model->read(Helper::getPasteId());
$file = tempnam(sys_get_temp_dir(), 'FOO');
file_put_contents($file, http_build_query(array(
'deletetoken' => hash_hmac('sha256', helper::getPasteId(), $paste->meta->salt),
'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste->meta->salt),
)));
request::setInputStream($file);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
Request::setInputStream($file);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'DELETE';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
}
/**
@ -132,23 +143,23 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testDeleteWithPost()
{
$this->reset();
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
$paste = $this->_model->read(helper::getPasteId());
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
$paste = $this->_model->read(Helper::getPasteId());
$_POST = array(
'action' => 'delete',
'deletetoken' => hash_hmac('sha256', helper::getPasteId(), $paste->meta->salt),
'deletetoken' => hash_hmac('sha256', Helper::getPasteId(), $paste->meta->salt),
);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
}
/**
@ -157,21 +168,25 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testRead()
{
$this->reset();
$paste = helper::getPasteWithAttachment();
$this->_model->create(helper::getPasteId(), $paste);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$paste = Helper::getPasteWithAttachment();
$paste['meta']['attachment'] = $paste['attachment'];
$paste['meta']['attachmentname'] = $paste['attachmentname'];
unset($paste['attachment']);
unset($paste['attachmentname']);
$this->_model->create(Helper::getPasteId(), $paste);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs success status');
$this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly');
$this->assertEquals(Helper::getPasteId(), $response['id'], 'outputs data correctly');
$this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
$this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
$this->assertEquals($paste['attachment'], $response['attachment'], 'outputs attachment correctly');
$this->assertEquals($paste['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly');
$this->assertEquals($paste['meta']['attachment'], $response['attachment'], 'outputs attachment correctly');
$this->assertEquals($paste['meta']['attachmentname'], $response['attachmentname'], 'outputs attachmentname correctly');
$this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
$this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
$this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
@ -185,11 +200,11 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testJsonLdPaste()
{
$this->reset();
$paste = helper::getPasteWithAttachment();
$this->_model->create(helper::getPasteId(), $paste);
$paste = Helper::getPasteWithAttachment();
$this->_model->create(Helper::getPasteId(), $paste);
$_GET['jsonld'] = 'paste';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertEquals(str_replace(
@ -205,11 +220,11 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testJsonLdComment()
{
$this->reset();
$paste = helper::getPasteWithAttachment();
$this->_model->create(helper::getPasteId(), $paste);
$paste = Helper::getPasteWithAttachment();
$this->_model->create(Helper::getPasteId(), $paste);
$_GET['jsonld'] = 'comment';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertEquals(str_replace(
@ -225,11 +240,11 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testJsonLdPasteMeta()
{
$this->reset();
$paste = helper::getPasteWithAttachment();
$this->_model->create(helper::getPasteId(), $paste);
$paste = Helper::getPasteWithAttachment();
$this->_model->create(Helper::getPasteId(), $paste);
$_GET['jsonld'] = 'pastemeta';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertEquals(str_replace(
@ -245,11 +260,11 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testJsonLdCommentMeta()
{
$this->reset();
$paste = helper::getPasteWithAttachment();
$this->_model->create(helper::getPasteId(), $paste);
$paste = Helper::getPasteWithAttachment();
$this->_model->create(Helper::getPasteId(), $paste);
$_GET['jsonld'] = 'commentmeta';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertEquals(str_replace(
@ -265,11 +280,11 @@ class jsonApiTest extends PHPUnit_Framework_TestCase
public function testJsonLdInvalid()
{
$this->reset();
$paste = helper::getPasteWithAttachment();
$this->_model->create(helper::getPasteId(), $paste);
$paste = Helper::getPasteWithAttachment();
$this->_model->create(Helper::getPasteId(), $paste);
$_GET['jsonld'] = '../cfg/conf.ini';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertEquals('{}', $content, 'does not output nasty data');

View file

@ -1,12 +1,12 @@
<?php
use PrivateBin\configuration;
use PrivateBin\data\db;
use PrivateBin\model;
use PrivateBin\model\paste;
use PrivateBin\vizhash16x16;
use PrivateBin\Configuration;
use PrivateBin\Data\Database;
use PrivateBin\Model;
use PrivateBin\Model\Paste;
use PrivateBin\Vizhash16x16;
class modelTest extends PHPUnit_Framework_TestCase
class ModelTest extends PHPUnit_Framework_TestCase
{
private $_conf;
@ -15,11 +15,11 @@ class modelTest extends PHPUnit_Framework_TestCase
public function setUp()
{
/* Setup Routine */
helper::confRestore();
Helper::confRestore();
$options = parse_ini_file(CONF, true);
$options['purge']['limit'] = 0;
$options['model'] = array(
'class' => 'privatebin_db',
'class' => 'Database',
);
$options['model_options'] = array(
'dsn' => 'sqlite::memory:',
@ -27,10 +27,10 @@ class modelTest extends PHPUnit_Framework_TestCase
'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
);
helper::confBackup();
helper::createIniFile(CONF, $options);
$this->_conf = new configuration;
$this->_model = new model($this->_conf);
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$this->_conf = new Configuration;
$this->_model = new Model($this->_conf);
$_SERVER['REMOTE_ADDR'] = '::1';
}
@ -42,9 +42,9 @@ class modelTest extends PHPUnit_Framework_TestCase
public function testBasicWorkflow()
{
// storing pastes
$pasteData = helper::getPaste();
$this->_model->getPaste(helper::getPasteId())->delete();
$paste = $this->_model->getPaste(helper::getPasteId());
$pasteData = Helper::getPaste();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste(Helper::getPasteId());
$this->assertFalse($paste->exists(), 'paste does not yet exist');
$paste = $this->_model->getPaste();
@ -53,7 +53,7 @@ class modelTest extends PHPUnit_Framework_TestCase
$paste->setFormatter($pasteData['meta']['formatter']);
$paste->store();
$paste = $this->_model->getPaste(helper::getPasteId());
$paste = $this->_model->getPaste(Helper::getPasteId());
$this->assertTrue($paste->exists(), 'paste exists after storing it');
$paste = $paste->get();
$this->assertEquals($pasteData['data'], $paste->data);
@ -62,25 +62,25 @@ class modelTest extends PHPUnit_Framework_TestCase
}
// storing comments
$commentData = helper::getComment();
$paste = $this->_model->getPaste(helper::getPasteId());
$comment = $paste->getComment(helper::getPasteId(), helper::getCommentId());
$commentData = Helper::getComment();
$paste = $this->_model->getPaste(Helper::getPasteId());
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
$this->assertFalse($comment->exists(), 'comment does not yet exist');
$comment = $paste->getComment(helper::getPasteId());
$comment = $paste->getComment(Helper::getPasteId());
$comment->setData($commentData['data']);
$comment->setNickname($commentData['meta']['nickname']);
$comment->store();
$comment = $paste->getComment(helper::getPasteId(), helper::getCommentId());
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
$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
$this->_model->getPaste(helper::getPasteId())->delete();
$paste = $this->_model->getPaste(helper::getPasteId());
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste(Helper::getPasteId());
$this->assertFalse($paste->exists(), 'paste successfully deleted');
$this->assertEquals(array(), $paste->getComments(), 'comment was deleted with paste');
}
@ -91,9 +91,9 @@ class modelTest extends PHPUnit_Framework_TestCase
*/
public function testPasteDuplicate()
{
$pasteData = helper::getPaste();
$pasteData = Helper::getPaste();
$this->_model->getPaste(helper::getPasteId())->delete();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste();
$paste->setData($pasteData['data']);
$paste->setOpendiscussion();
@ -113,9 +113,9 @@ class modelTest extends PHPUnit_Framework_TestCase
*/
public function testCommentDuplicate()
{
$pasteData = helper::getPaste();
$commentData = helper::getComment();
$this->_model->getPaste(helper::getPasteId())->delete();
$pasteData = Helper::getPaste();
$commentData = Helper::getComment();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste();
$paste->setData($pasteData['data']);
@ -123,12 +123,12 @@ class modelTest extends PHPUnit_Framework_TestCase
$paste->setFormatter($pasteData['meta']['formatter']);
$paste->store();
$comment = $paste->getComment(helper::getPasteId());
$comment = $paste->getComment(Helper::getPasteId());
$comment->setData($commentData['data']);
$comment->setNickname($commentData['meta']['nickname']);
$comment->store();
$comment = $paste->getComment(helper::getPasteId());
$comment = $paste->getComment(Helper::getPasteId());
$comment->setData($commentData['data']);
$comment->setNickname($commentData['meta']['nickname']);
$comment->store();
@ -136,9 +136,9 @@ class modelTest extends PHPUnit_Framework_TestCase
public function testImplicitDefaults()
{
$pasteData = helper::getPaste();
$commentData = helper::getComment();
$this->_model->getPaste(helper::getPasteId())->delete();
$pasteData = Helper::getPaste();
$commentData = Helper::getComment();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste();
$paste->setData($pasteData['data']);
@ -147,34 +147,34 @@ class modelTest extends PHPUnit_Framework_TestCase
// not setting a formatter, should use default one
$paste->store();
$paste = $this->_model->getPaste(helper::getPasteId())->get(); // ID was set based on data
$paste = $this->_model->getPaste(Helper::getPasteId())->get(); // ID was set based on data
$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');
$this->assertEquals($this->_conf->getKey('defaultformatter'), $paste->meta->formatter, 'default formatter is set');
$this->_model->getPaste(helper::getPasteId())->delete();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste();
$paste->setData($pasteData['data']);
$paste->setBurnafterreading('0');
$paste->setOpendiscussion();
$paste->store();
$vz = new vizhash16x16();
$vz = new Vizhash16x16();
$pngdata = 'data:image/png;base64,' . base64_encode($vz->generate($_SERVER['REMOTE_ADDR']));
$comment = $paste->getComment(helper::getPasteId());
$comment = $paste->getComment(Helper::getPasteId());
$comment->setData($commentData['data']);
$comment->setNickname($commentData['meta']['nickname']);
$comment->store();
$comment = $paste->getComment(helper::getPasteId(), helper::getCommentId())->get();
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId())->get();
$this->assertEquals($pngdata, $comment->meta->vizhash, 'nickname triggers vizhash to be set');
}
public function testPasteIdValidation()
{
$this->assertTrue(paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
$this->assertFalse(paste::isValidId('foo'), 'invalid hex values');
$this->assertFalse(paste::isValidId('../bar/baz'), 'path attack');
$this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
$this->assertFalse(Paste::isValidId('foo'), 'invalid hex values');
$this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
}
/**
@ -184,14 +184,14 @@ class modelTest extends PHPUnit_Framework_TestCase
public function testInvalidComment()
{
$paste = $this->_model->getPaste();
$paste->getComment(helper::getPasteId());
$paste->getComment(Helper::getPasteId());
}
public function testExpiration()
{
$pasteData = helper::getPaste();
$this->_model->getPaste(helper::getPasteId())->delete();
$paste = $this->_model->getPaste(helper::getPasteId());
$pasteData = Helper::getPaste();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste(Helper::getPasteId());
$this->assertFalse($paste->exists(), 'paste does not yet exist');
$paste = $this->_model->getPaste();
@ -209,22 +209,22 @@ class modelTest extends PHPUnit_Framework_TestCase
*/
public function testCommentDeletion()
{
$pasteData = helper::getPaste();
$this->_model->getPaste(helper::getPasteId())->delete();
$pasteData = Helper::getPaste();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $this->_model->getPaste();
$paste->setData($pasteData['data']);
$paste->store();
$paste->getComment(helper::getPasteId())->delete();
$paste->getComment(Helper::getPasteId())->delete();
}
public function testPurge()
{
$conf = new configuration;
$store = 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));
$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));
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
$ids = array();
foreach ($keys as $key) {
@ -262,13 +262,13 @@ class modelTest extends PHPUnit_Framework_TestCase
'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
);
helper::confBackup();
helper::createIniFile(CONF, $options);
$model = new model(new configuration);
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$model = new Model(new Configuration);
$pasteData = helper::getPaste();
$this->_model->getPaste(helper::getPasteId())->delete();
$paste = $model->getPaste(helper::getPasteId());
$pasteData = Helper::getPaste();
$this->_model->getPaste(Helper::getPasteId())->delete();
$paste = $model->getPaste(Helper::getPasteId());
$this->assertFalse($paste->exists(), 'paste does not yet exist');
$paste = $model->getPaste();
@ -277,7 +277,7 @@ class modelTest extends PHPUnit_Framework_TestCase
$paste->setFormatter($pasteData['meta']['formatter']);
$paste->store();
$paste = $model->getPaste(helper::getPasteId());
$paste = $model->getPaste(Helper::getPasteId());
$this->assertTrue($paste->exists(), 'paste exists after storing it');
$paste = $paste->get();
$this->assertEquals($pasteData['data'], $paste->data);
@ -286,17 +286,17 @@ class modelTest extends PHPUnit_Framework_TestCase
}
// storing comments
$commentData = helper::getComment();
$paste = $model->getPaste(helper::getPasteId());
$comment = $paste->getComment(helper::getPasteId(), helper::getCommentId());
$commentData = Helper::getComment();
$paste = $model->getPaste(Helper::getPasteId());
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
$this->assertFalse($comment->exists(), 'comment does not yet exist');
$comment = $paste->getComment(helper::getPasteId());
$comment = $paste->getComment(Helper::getPasteId());
$comment->setData($commentData['data']);
$comment->setNickname($commentData['meta']['nickname']);
$comment->store();
$comment = $paste->getComment(helper::getPasteId(), helper::getCommentId());
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
$this->assertTrue($comment->exists(), 'comment exists after storing it');
$comment = $comment->get();
$this->assertEquals($commentData['data'], $comment->data);

View file

@ -0,0 +1,41 @@
<?php
use PrivateBin\Persistence\PurgeLimiter;
class PurgeLimiterTest extends PHPUnit_Framework_TestCase
{
private $_path;
public function setUp()
{
/* Setup Routine */
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
if (!is_dir($this->_path)) {
mkdir($this->_path);
}
PurgeLimiter::setPath($this->_path);
}
public function tearDown()
{
/* Tear Down Routine */
Helper::rmDir($this->_path);
}
public function testLimit()
{
// initialize it
PurgeLimiter::canPurge();
// try setting it
PurgeLimiter::setLimit(1);
$this->assertEquals(false, PurgeLimiter::canPurge());
sleep(2);
$this->assertEquals(true, PurgeLimiter::canPurge());
// disable it
PurgeLimiter::setLimit(0);
PurgeLimiter::canPurge();
$this->assertEquals(true, PurgeLimiter::canPurge());
}
}

View file

@ -1,8 +1,8 @@
<?php
use PrivateBin\serversalt;
use PrivateBin\Persistence\ServerSalt;
class serversaltTest extends PHPUnit_Framework_TestCase
class ServerSaltTest extends PHPUnit_Framework_TestCase
{
private $_path;
@ -15,11 +15,11 @@ class serversaltTest extends PHPUnit_Framework_TestCase
public function setUp()
{
/* Setup Routine */
$this->_path = PATH . 'data';
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
if (!is_dir($this->_path)) {
mkdir($this->_path);
}
serversalt::setPath($this->_path);
ServerSalt::setPath($this->_path);
$this->_otherPath = $this->_path . DIRECTORY_SEPARATOR . 'foo';
@ -34,14 +34,14 @@ class serversaltTest extends PHPUnit_Framework_TestCase
{
/* Tear Down Routine */
chmod($this->_invalidPath, 0700);
helper::rmdir($this->_path);
Helper::rmDir($this->_path);
}
public function testGeneration()
{
// generating new salt
serversalt::setPath($this->_path);
$salt = serversalt::get();
ServerSalt::setPath($this->_path);
$salt = ServerSalt::get();
// mcrypt mock
if (!function_exists('mcrypt_create_iv')) {
@ -60,14 +60,14 @@ class serversaltTest extends PHPUnit_Framework_TestCase
}
return hex2bin($randomSalt);
}
$this->assertNotEquals($salt, serversalt::generate());
$this->assertNotEquals($salt, ServerSalt::generate());
}
// try setting a different path and resetting it
serversalt::setPath($this->_otherPath);
$this->assertNotEquals($salt, serversalt::get());
serversalt::setPath($this->_path);
$this->assertEquals($salt, serversalt::get());
ServerSalt::setPath($this->_otherPath);
$this->assertNotEquals($salt, ServerSalt::get());
ServerSalt::setPath($this->_path);
$this->assertEquals($salt, ServerSalt::get());
}
/**
@ -78,8 +78,8 @@ class serversaltTest extends PHPUnit_Framework_TestCase
{
// try setting an invalid path
chmod($this->_invalidPath, 0000);
serversalt::setPath($this->_invalidPath);
serversalt::get();
ServerSalt::setPath($this->_invalidPath);
ServerSalt::get();
}
/**
@ -92,8 +92,8 @@ class serversaltTest extends PHPUnit_Framework_TestCase
chmod($this->_invalidPath, 0700);
file_put_contents($this->_invalidFile, '');
chmod($this->_invalidFile, 0000);
serversalt::setPath($this->_invalidPath);
serversalt::get();
ServerSalt::setPath($this->_invalidPath);
ServerSalt::get();
}
/**
@ -104,11 +104,14 @@ class serversaltTest extends PHPUnit_Framework_TestCase
{
// try setting an invalid file
chmod($this->_invalidPath, 0700);
@unlink($this->_invalidFile);
if (is_file($this->_invalidFile)) {
chmod($this->_invalidFile, 0600);
unlink($this->_invalidFile);
}
file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
chmod($this->_invalidPath, 0500);
serversalt::setPath($this->_invalidPath);
serversalt::get();
ServerSalt::setPath($this->_invalidPath);
ServerSalt::get();
}
/**
@ -119,7 +122,7 @@ class serversaltTest extends PHPUnit_Framework_TestCase
{
// try creating an invalid path
chmod($this->_invalidPath, 0000);
serversalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
serversalt::get();
ServerSalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
ServerSalt::get();
}
}

View file

@ -1,8 +1,8 @@
<?php
use PrivateBin\trafficlimiter;
use PrivateBin\Persistence\TrafficLimiter;
class trafficlimiterTest extends PHPUnit_Framework_TestCase
class TrafficLimiterTest extends PHPUnit_Framework_TestCase
{
private $_path;
@ -10,30 +10,30 @@ class trafficlimiterTest extends PHPUnit_Framework_TestCase
{
/* Setup Routine */
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'trafficlimit';
trafficlimiter::setPath($this->_path);
TrafficLimiter::setPath($this->_path);
}
public function tearDown()
{
/* Tear Down Routine */
helper::rmdir($this->_path . DIRECTORY_SEPARATOR);
Helper::rmDir($this->_path . DIRECTORY_SEPARATOR);
}
public function testTrafficGetsLimited()
{
$this->assertEquals($this->_path, trafficlimiter::getPath());
$this->assertEquals($this->_path, TrafficLimiter::getPath());
$file = 'baz';
$this->assertEquals($this->_path . DIRECTORY_SEPARATOR . $file, trafficlimiter::getPath($file));
trafficlimiter::setLimit(4);
$this->assertEquals($this->_path . DIRECTORY_SEPARATOR . $file, TrafficLimiter::getPath($file));
TrafficLimiter::setLimit(4);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$this->assertTrue(trafficlimiter::canPass(), 'first request may pass');
$this->assertTrue(TrafficLimiter::canPass(), 'first request may pass');
sleep(1);
$this->assertFalse(trafficlimiter::canPass(), 'second request is to fast, may not pass');
$this->assertFalse(TrafficLimiter::canPass(), 'second request is to fast, may not pass');
sleep(4);
$this->assertTrue(trafficlimiter::canPass(), 'third request waited long enough and may pass');
$this->assertTrue(TrafficLimiter::canPass(), 'third request waited long enough and may pass');
$_SERVER['REMOTE_ADDR'] = '2001:1620:2057:dead:beef::cafe:babe';
$this->assertTrue(trafficlimiter::canPass(), 'fourth request has different ip and may pass');
$this->assertTrue(TrafficLimiter::canPass(), 'fourth request has different ip and may pass');
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$this->assertFalse(trafficlimiter::canPass(), 'fifth request is to fast, may not pass');
$this->assertFalse(TrafficLimiter::canPass(), 'fifth request is to fast, may not pass');
}
}

View file

@ -1,25 +1,30 @@
<?php
use PrivateBin\data\data;
use PrivateBin\privatebin;
use PrivateBin\serversalt;
use PrivateBin\trafficlimiter;
use PrivateBin\Data\Filesystem;
use PrivateBin\PrivateBin;
use PrivateBin\Persistence\ServerSalt;
use PrivateBin\Persistence\TrafficLimiter;
class privatebinTest extends PHPUnit_Framework_TestCase
class PrivateBinTest extends PHPUnit_Framework_TestCase
{
protected $_model;
protected $_path;
public function setUp()
{
/* Setup Routine */
$this->_model = data::getInstance(array('dir' => PATH . 'data'));
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
$this->_model = Filesystem::getInstance(array('dir' => $this->_path));
ServerSalt::setPath($this->_path);
$this->reset();
}
public function tearDown()
{
/* Tear Down Routine */
helper::confRestore();
Helper::confRestore();
Helper::rmDir($this->_path);
}
public function reset()
@ -27,10 +32,16 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$_POST = array();
$_GET = array();
$_SERVER = array();
if ($this->_model->exists(helper::getPasteId())) {
$this->_model->delete(helper::getPasteId());
if ($this->_model->exists(Helper::getPasteId())) {
$this->_model->delete(Helper::getPasteId());
}
helper::confRestore();
Helper::confRestore();
$options = parse_ini_file(CONF, true);
$options['purge']['dir'] = $this->_path;
$options['traffic']['dir'] = $this->_path;
$options['model_options']['dir'] = $this->_path;
Helper::confBackup();
Helper::createIniFile(CONF, $options);
}
/**
@ -40,7 +51,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
{
$this->reset();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertContains(
@ -63,11 +74,11 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['main']['languageselection'] = true;
helper::confBackup();
helper::createIniFile(CONF, $options);
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_COOKIE['lang'] = 'de';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertContains(
@ -86,11 +97,11 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$options = parse_ini_file(CONF, true);
$options['main']['languageselection'] = false;
$options['main']['languagedefault'] = 'fr';
helper::confBackup();
helper::createIniFile(CONF, $options);
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_COOKIE['lang'] = 'de';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertContains(
@ -109,11 +120,11 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['main']['urlshortener'] = $shortener;
helper::confBackup();
helper::createIniFile(CONF, $options);
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_COOKIE['lang'] = 'de';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -135,7 +146,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
@unlink($file);
}
ob_start();
new privatebin;
new PrivateBin;
ob_end_clean();
foreach ($dirs as $dir) {
$file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
@ -153,11 +164,9 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testConf()
{
$this->reset();
helper::confBackup();
Helper::confBackup();
file_put_contents(CONF, '');
ob_start();
new privatebin;
ob_end_clean();
new PrivateBin;
}
/**
@ -168,14 +177,14 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -197,15 +206,15 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste(array('expire' => 25));
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste(array('expire' => 25));
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
trafficlimiter::canPass();
TrafficLimiter::canPass();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -228,19 +237,19 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$options = parse_ini_file(CONF, true);
$options['main']['sizelimit'] = 10;
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$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');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste exists after posting data');
}
/**
@ -251,15 +260,15 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['header'] = 'X_FORWARDED_FOR';
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_SERVER['HTTP_X_FORWARDED_FOR'] = '::2';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -281,20 +290,20 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$this->_model->create(helper::getPasteId(), helper::getPaste());
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$_POST = Helper::getPaste();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after posting data');
}
/**
@ -305,9 +314,9 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_POST['expire'] = '5min';
$_POST['formatter'] = 'foo';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
@ -315,7 +324,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$_SERVER['REMOTE_ADDR'] = '::1';
$time = time();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -338,9 +347,9 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_POST['expire'] = '5min';
$_POST['opendiscussion'] = '1';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
@ -348,7 +357,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$_SERVER['REMOTE_ADDR'] = '::1';
$time = time();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -372,15 +381,15 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_POST['expire'] = 'foo';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -402,20 +411,20 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_POST['burnafterreading'] = 'neither 1 nor 0';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$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');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste exists after posting data');
}
/**
@ -426,20 +435,20 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_POST['opendiscussion'] = 'neither 1 nor 0';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$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');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste exists after posting data');
}
/**
@ -451,15 +460,15 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
$options['main']['fileupload'] = true;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPasteWithAttachment();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPasteWithAttachment();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exists before posting data');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not exists before posting data');
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -489,21 +498,21 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
$options['main']['fileupload'] = true;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPasteWithAttachment();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPasteWithAttachment();
unset($_POST['attachment']);
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exists before posting data');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not exists before posting data');
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$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');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste exists after posting data');
}
/**
@ -512,21 +521,21 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testCreateTooSoon()
{
$this->reset();
$_POST = helper::getPaste();
$_POST = Helper::getPaste();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
ob_end_clean();
$this->_model->delete(helper::getPasteId());
$this->_model->delete(Helper::getPasteId());
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$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');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste exists after posting data');
}
/**
@ -537,15 +546,15 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getPaste();
$_POST['nickname'] = helper::getComment()['meta']['nickname'];
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getPaste();
$_POST['nickname'] = Helper::getComment()['meta']['nickname'];
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -567,23 +576,23 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getCommentPost();
$_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getCommentPost();
$_POST['pasteid'] = Helper::getPasteId();
$_POST['parentid'] = Helper::getPasteId();
$_POST['nickname'] = 'foo';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after posting data');
}
/**
@ -594,22 +603,22 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getCommentPost();
$_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getCommentPost();
$_POST['pasteid'] = Helper::getPasteId();
$_POST['parentid'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), $response['id']), 'paste exists after posting data');
$this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), $response['id']), 'paste exists after posting data');
}
/**
@ -620,22 +629,22 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getCommentPost();
$_POST['pasteid'] = helper::getPasteId();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getCommentPost();
$_POST['pasteid'] = Helper::getPasteId();
$_POST['parentid'] = 'foo';
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'paste exists after posting data');
}
/**
@ -646,23 +655,23 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getCommentPost();
$_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getCommentPost();
$_POST['pasteid'] = Helper::getPasteId();
$_POST['parentid'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
$paste = helper::getPaste(array('opendiscussion' => false));
$this->_model->create(helper::getPasteId(), $paste);
$paste = Helper::getPaste(array('opendiscussion' => false));
$this->_model->create(Helper::getPasteId(), $paste);
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'paste exists after posting data');
}
/**
@ -673,21 +682,21 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$_POST = helper::getCommentPost();
$_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$_POST = Helper::getCommentPost();
$_POST['pasteid'] = Helper::getPasteId();
$_POST['parentid'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'paste exists after posting data');
}
/**
@ -698,24 +707,24 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$options = parse_ini_file(CONF, true);
$options['traffic']['limit'] = 0;
helper::confBackup();
helper::createIniFile(CONF, $options);
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment());
$this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data');
$_POST = helper::getCommentPost();
$_POST['pasteid'] = helper::getPasteId();
$_POST['parentid'] = helper::getPasteId();
Helper::confBackup();
Helper::createIniFile(CONF, $options);
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment());
$this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment exists before posting data');
$_POST = Helper::getCommentPost();
$_POST['pasteid'] = Helper::getPasteId();
$_POST['parentid'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
$this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'paste exists after posting data');
}
/**
@ -724,15 +733,15 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testRead()
{
$this->reset();
$this->_model->create(helper::getPasteId(), helper::getPaste());
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertContains(
'<div id="cipherdata" class="hidden">' .
htmlspecialchars(helper::getPasteAsJson(), ENT_NOQUOTES) .
htmlspecialchars(Helper::getPasteAsJson(), ENT_NOQUOTES) .
'</div>',
$content,
'outputs data correctly'
@ -747,7 +756,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$this->reset();
$_SERVER['QUERY_STRING'] = 'foo';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -763,9 +772,9 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testReadNonexisting()
{
$this->reset();
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -781,11 +790,11 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testReadExpired()
{
$this->reset();
$expiredPaste = helper::getPaste(array('expire_date' => 1344803344));
$this->_model->create(helper::getPasteId(), $expiredPaste);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$expiredPaste = Helper::getPaste(array('expire_date' => 1344803344));
$this->_model->create(Helper::getPasteId(), $expiredPaste);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -801,17 +810,17 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testReadBurn()
{
$this->reset();
$burnPaste = helper::getPaste(array('burnafterreading' => true));
$this->_model->create(helper::getPasteId(), $burnPaste);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$burnPaste = Helper::getPaste(array('burnafterreading' => true));
$this->_model->create(Helper::getPasteId(), $burnPaste);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
unset($burnPaste['meta']['salt']);
$this->assertContains(
'<div id="cipherdata" class="hidden">' .
htmlspecialchars(helper::getPasteAsJson($burnPaste['meta']), ENT_NOQUOTES) .
htmlspecialchars(Helper::getPasteAsJson($burnPaste['meta']), ENT_NOQUOTES) .
'</div>',
$content,
'outputs data correctly'
@ -824,17 +833,17 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testReadJson()
{
$this->reset();
$paste = helper::getPaste();
$this->_model->create(helper::getPasteId(), $paste);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$paste = Helper::getPaste();
$this->_model->create(Helper::getPasteId(), $paste);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs success status');
$this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly');
$this->assertEquals(Helper::getPasteId(), $response['id'], 'outputs data correctly');
$this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
$this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
$this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
@ -850,10 +859,10 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testReadInvalidJson()
{
$this->reset();
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
@ -866,23 +875,23 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testReadOldSyntax()
{
$this->reset();
$oldPaste = helper::getPaste();
$oldPaste = Helper::getPaste();
$meta = array(
'syntaxcoloring' => true,
'postdate' => $oldPaste['meta']['postdate'],
'opendiscussion' => $oldPaste['meta']['opendiscussion'],
);
$oldPaste['meta'] = $meta;
$this->_model->create(helper::getPasteId(), $oldPaste);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$this->_model->create(Helper::getPasteId(), $oldPaste);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$meta['formatter'] = 'syntaxhighlighting';
$this->assertContains(
'<div id="cipherdata" class="hidden">' .
htmlspecialchars(helper::getPasteAsJson($meta), ENT_NOQUOTES) .
htmlspecialchars(Helper::getPasteAsJson($meta), ENT_NOQUOTES) .
'</div>',
$content,
'outputs data correctly'
@ -895,19 +904,19 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testReadOldFormat()
{
$this->reset();
$oldPaste = helper::getPaste();
$oldPaste = Helper::getPaste();
unset($oldPaste['meta']['formatter']);
$this->_model->create(helper::getPasteId(), $oldPaste);
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$this->_model->create(Helper::getPasteId(), $oldPaste);
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$oldPaste['meta']['formatter'] = 'plaintext';
unset($oldPaste['meta']['salt']);
$this->assertContains(
'<div id="cipherdata" class="hidden">' .
htmlspecialchars(helper::getPasteAsJson($oldPaste['meta']), ENT_NOQUOTES) .
htmlspecialchars(Helper::getPasteAsJson($oldPaste['meta']), ENT_NOQUOTES) .
'</div>',
$content,
'outputs data correctly'
@ -920,13 +929,13 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDelete()
{
$this->reset();
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
$paste = $this->_model->read(helper::getPasteId());
$_GET['pasteid'] = helper::getPasteId();
$_GET['deletetoken'] = hash_hmac('sha256', helper::getPasteId(), $paste->meta->salt);
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
$paste = $this->_model->read(Helper::getPasteId());
$_GET['pasteid'] = Helper::getPasteId();
$_GET['deletetoken'] = hash_hmac('sha256', Helper::getPasteId(), $paste->meta->salt);
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -934,7 +943,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$content,
'outputs deleted status correctly'
);
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
}
/**
@ -943,11 +952,11 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDeleteInvalidId()
{
$this->reset();
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$_GET['pasteid'] = 'foo';
$_GET['deletetoken'] = 'bar';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -955,7 +964,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$content,
'outputs delete error correctly'
);
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after failing to delete data');
}
/**
@ -964,10 +973,10 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDeleteInexistantId()
{
$this->reset();
$_GET['pasteid'] = helper::getPasteId();
$_GET['pasteid'] = Helper::getPasteId();
$_GET['deletetoken'] = 'bar';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -983,11 +992,11 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDeleteInvalidToken()
{
$this->reset();
$this->_model->create(helper::getPasteId(), helper::getPaste());
$_GET['pasteid'] = helper::getPasteId();
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$_GET['pasteid'] = Helper::getPasteId();
$_GET['deletetoken'] = 'bar';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -995,7 +1004,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$content,
'outputs delete error correctly'
);
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after failing to delete data');
}
/**
@ -1004,20 +1013,20 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDeleteBurnAfterReading()
{
$this->reset();
$burnPaste = helper::getPaste(array('burnafterreading' => true));
$this->_model->create(helper::getPasteId(), $burnPaste);
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
$burnPaste = Helper::getPaste(array('burnafterreading' => true));
$this->_model->create(Helper::getPasteId(), $burnPaste);
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
$_POST['deletetoken'] = 'burnafterreading';
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(0, $response['status'], 'outputs status');
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
}
/**
@ -1026,19 +1035,19 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDeleteInvalidBurnAfterReading()
{
$this->reset();
$this->_model->create(helper::getPasteId(), helper::getPaste());
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
$_POST['deletetoken'] = 'burnafterreading';
$_SERVER['QUERY_STRING'] = helper::getPasteId();
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['REQUEST_METHOD'] = 'POST';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs status');
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
}
/**
@ -1047,14 +1056,14 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDeleteExpired()
{
$this->reset();
$expiredPaste = helper::getPaste(array('expire_date' => 1000));
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exist before being created');
$this->_model->create(helper::getPasteId(), $expiredPaste);
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
$_GET['pasteid'] = helper::getPasteId();
$expiredPaste = Helper::getPaste(array('expire_date' => 1000));
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not exist before being created');
$this->_model->create(Helper::getPasteId(), $expiredPaste);
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
$_GET['pasteid'] = Helper::getPasteId();
$_GET['deletetoken'] = 'does not matter in this context, but has to be set';
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -1062,7 +1071,7 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$content,
'outputs error correctly'
);
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
}
/**
@ -1071,14 +1080,14 @@ class privatebinTest extends PHPUnit_Framework_TestCase
public function testDeleteMissingPerPasteSalt()
{
$this->reset();
$paste = helper::getPaste();
$paste = Helper::getPaste();
unset($paste['meta']['salt']);
$this->_model->create(helper::getPasteId(), $paste);
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
$_GET['pasteid'] = helper::getPasteId();
$_GET['deletetoken'] = hash_hmac('sha256', helper::getPasteId(), serversalt::get());
$this->_model->create(Helper::getPasteId(), $paste);
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
$_GET['pasteid'] = Helper::getPasteId();
$_GET['deletetoken'] = hash_hmac('sha256', Helper::getPasteId(), ServerSalt::get());
ob_start();
new privatebin;
new PrivateBin;
$content = ob_get_contents();
ob_end_clean();
$this->assertRegExp(
@ -1086,6 +1095,6 @@ class privatebinTest extends PHPUnit_Framework_TestCase
$content,
'outputs deleted status correctly'
);
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
}
}

View file

@ -1,13 +1,13 @@
<?php
use PrivateBin\data\db;
use PrivateBin\privatebin;
use PrivateBin\serversalt;
use PrivateBin\trafficlimiter;
use PrivateBin\Data\Database;
use PrivateBin\PrivateBin;
use PrivateBin\Persistence\ServerSalt;
use PrivateBin\Persistence\TrafficLimiter;
require_once 'privatebin.php';
require_once 'PrivateBinTest.php';
class privatebinWithDbTest extends privatebinTest
class PrivateBinWithDbTest extends PrivateBinTest
{
private $_options = array(
'usr' => null,
@ -18,8 +18,6 @@ class privatebinWithDbTest extends privatebinTest
),
);
private $_path;
public function setUp()
{
/* Setup Routine */
@ -27,28 +25,24 @@ class privatebinWithDbTest extends privatebinTest
if (!is_dir($this->_path)) {
mkdir($this->_path);
}
ServerSalt::setPath($this->_path);
$this->_options['dsn'] = 'sqlite:' . $this->_path . DIRECTORY_SEPARATOR . 'tst.sq3';
$this->_model = db::getInstance($this->_options);
$this->_model = Database::getInstance($this->_options);
$this->reset();
}
public function tearDown()
{
/* Tear Down Routine */
parent::tearDown();
helper::rmdir($this->_path);
}
public function reset()
{
parent::reset();
// but then inject a db config
$options = parse_ini_file(CONF, true);
$options['model'] = array(
'class' => 'privatebin_db',
'class' => 'Database',
);
$options['purge']['dir'] = $this->_path;
$options['traffic']['dir'] = $this->_path;
$options['model_options'] = $this->_options;
helper::confBackup();
helper::createIniFile(CONF, $options);
Helper::confBackup();
Helper::createIniFile(CONF, $options);
}
}

View file

@ -1,8 +1,8 @@
<?php
use PrivateBin\request;
use PrivateBin\Request;
class requestTest extends PHPUnit_Framework_TestCase
class RequestTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
@ -25,7 +25,7 @@ class requestTest extends PHPUnit_Framework_TestCase
{
$this->reset();
$_SERVER['REQUEST_METHOD'] = 'GET';
$request = new request;
$request = new Request;
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
$this->assertEquals('view', $request->getOperation());
}
@ -35,7 +35,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$this->reset();
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['QUERY_STRING'] = 'foo';
$request = new request;
$request = new Request;
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
$this->assertEquals('foo', $request->getParam('pasteid'));
$this->assertEquals('read', $request->getOperation());
@ -47,7 +47,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['pasteid'] = 'foo';
$_GET['deletetoken'] = 'bar';
$request = new request;
$request = new Request;
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
$this->assertEquals('delete', $request->getOperation());
$this->assertEquals('foo', $request->getParam('pasteid'));
@ -61,8 +61,8 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$file = tempnam(sys_get_temp_dir(), 'FOO');
file_put_contents($file, 'data=foo');
request::setInputStream($file);
$request = new request;
Request::setInputStream($file);
$request = new Request;
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
$this->assertEquals('create', $request->getOperation());
$this->assertEquals('foo', $request->getParam('data'));
@ -74,7 +74,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
$_POST['attachment'] = 'foo';
$request = new request;
$request = new Request;
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
$this->assertEquals('create', $request->getOperation());
$this->assertEquals('foo', $request->getParam('attachment'));
@ -86,7 +86,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
$_SERVER['QUERY_STRING'] = 'foo';
$request = new request;
$request = new Request;
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
$this->assertEquals('foo', $request->getParam('pasteid'));
$this->assertEquals('read', $request->getOperation());
@ -99,7 +99,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
$_SERVER['QUERY_STRING'] = 'foo';
$_POST['deletetoken'] = 'bar';
$request = new request;
$request = new Request;
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
$this->assertEquals('delete', $request->getOperation());
$this->assertEquals('foo', $request->getParam('pasteid'));
@ -112,7 +112,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_ACCEPT'] = 'text/html,text/html; charset=UTF-8,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8, text/csv,application/json';
$_SERVER['QUERY_STRING'] = 'foo';
$request = new request;
$request = new Request;
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
$this->assertEquals('foo', $request->getParam('pasteid'));
$this->assertEquals('read', $request->getOperation());
@ -124,7 +124,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_ACCEPT'] = 'application/xhtml+xml,text/html,text/html; charset=UTF-8, application/xml;q=0.9,*/*;q=0.8, text/csv,application/json';
$_SERVER['QUERY_STRING'] = 'foo';
$request = new request;
$request = new Request;
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
$this->assertEquals('foo', $request->getParam('pasteid'));
$this->assertEquals('read', $request->getOperation());
@ -136,7 +136,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, application/json, text/html,text/html; charset=UTF-8,application/xhtml+xml, */*;q=0.8';
$_SERVER['QUERY_STRING'] = 'foo';
$request = new request;
$request = new Request;
$this->assertTrue($request->isJsonApiCall(), 'is JSON Api call');
$this->assertEquals('foo', $request->getParam('pasteid'));
$this->assertEquals('read', $request->getOperation());
@ -148,7 +148,7 @@ class requestTest extends PHPUnit_Framework_TestCase
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_ACCEPT'] = 'text/plain,text/csv, application/xml;q=0.9, */*;q=0.8';
$_SERVER['QUERY_STRING'] = 'foo';
$request = new request;
$request = new Request;
$this->assertFalse($request->isJsonApiCall(), 'is HTML call');
$this->assertEquals('foo', $request->getParam('pasteid'));
$this->assertEquals('read', $request->getOperation());

View file

@ -1,30 +1,31 @@
<?php
use PrivateBin\sjcl;
use PrivateBin\Sjcl;
class sjclTest extends PHPUnit_Framework_TestCase
class SjclTest extends PHPUnit_Framework_TestCase
{
public function testSjclValidatorValidatesCorrectly()
{
$paste = helper::getPasteWithAttachment();
$this->assertTrue(sjcl::isValid($paste['data']), 'valid sjcl');
$this->assertTrue(sjcl::isValid($paste['attachment']), 'valid sjcl');
$this->assertTrue(sjcl::isValid($paste['attachmentname']), 'valid sjcl');
$this->assertTrue(sjcl::isValid(helper::getComment()['data']), 'valid sjcl');
$paste = Helper::getPasteWithAttachment();
$this->assertTrue(Sjcl::isValid($paste['data']), 'valid sjcl');
$this->assertTrue(Sjcl::isValid($paste['attachment']), 'valid sjcl');
$this->assertTrue(Sjcl::isValid($paste['attachmentname']), 'valid sjcl');
$this->assertTrue(Sjcl::isValid(Helper::getComment()['data']), 'valid sjcl');
$this->assertTrue(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'valid sjcl');
$this->assertFalse(sjcl::isValid('{"iv":"$","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of iv');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"$","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of salt');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","salt":"Gx1vA2/gQ3U","ct":"$"}'), 'invalid base64 encoding of ct');
$this->assertFalse(sjcl::isValid('{"iv":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'iv to long');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'salt to long');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA","foo":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA="}'), 'invalid additional key');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":0.9,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'unsupported version');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":100,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'not enough iterations');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":127,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid key size');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":63,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid tag length');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"!#@","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid mode');
$this->assertFalse(sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"!#@","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid cipher');
$this->assertTrue(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'valid sjcl');
$this->assertFalse(Sjcl::isValid('{"iv":"$","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of iv');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"$","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid base64 encoding of salt');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"$"}'), 'invalid base64 encoding of ct');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"bm9kYXRhbm9kYXRhbm9kYXRhbm9kYXRhbm9kYXRhCg=="}'), 'low ct entropy');
$this->assertFalse(Sjcl::isValid('{"iv":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'iv to long');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'salt to long');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA","foo":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTA="}'), 'invalid additional key');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":0.9,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'unsupported version');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":100,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'not enough iterations');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":127,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid key size');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":63,"mode":"ccm","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid tag length');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"!#@","adata":"","cipher":"aes","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid mode');
$this->assertFalse(Sjcl::isValid('{"iv":"83Ax/OdUav3SanDW9dcQPg","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"!#@","salt":"Gx1vA2/gQ3U","ct":"j7ImByuE5xCqD2YXm6aSyA"}'), 'invalid cipher');
// @note adata is not validated, except as part of the total message length
}
}

View file

@ -1,9 +1,9 @@
<?php
use PrivateBin\i18n;
use PrivateBin\view;
use PrivateBin\I18n;
use PrivateBin\View;
class viewTest extends PHPUnit_Framework_TestCase
class ViewTest extends PHPUnit_Framework_TestCase
{
private static $error = 'foo bar';
@ -32,7 +32,7 @@ class viewTest extends PHPUnit_Framework_TestCase
public function setUp()
{
/* Setup Routine */
$page = new view;
$page = new View;
$page->assign('CIPHERDATA', helper::getPaste()['data']);
$page->assign('ERROR', self::$error);
$page->assign('STATUS', self::$status);
@ -50,7 +50,7 @@ class viewTest extends PHPUnit_Framework_TestCase
$page->assign('BASE64JSVERSION', '2.1.9');
$page->assign('NOTICE', 'example');
$page->assign('LANGUAGESELECTION', '');
$page->assign('LANGUAGES', i18n::getLanguageLabels(i18n::getAvailableLanguages()));
$page->assign('LANGUAGES', I18n::getLanguageLabels(i18n::getAvailableLanguages()));
$page->assign('EXPIRE', self::$expire);
$page->assign('EXPIREDEFAULT', self::$expire_default);
$page->assign('EXPIRECLONE', true);
@ -109,7 +109,7 @@ class viewTest extends PHPUnit_Framework_TestCase
*/
public function testMissingTemplate()
{
$test = new view;
$test = new View;
$test->draw('123456789 does not exist!');
}
}

View file

@ -1,9 +1,9 @@
<?php
use PrivateBin\serversalt;
use PrivateBin\vizhash16x16;
use PrivateBin\Persistence\ServerSalt;
use PrivateBin\Vizhash16x16;
class vizhash16x16Test extends PHPUnit_Framework_TestCase
class Vizhash16x16Test extends PHPUnit_Framework_TestCase
{
private $_file;
@ -12,27 +12,24 @@ class vizhash16x16Test extends PHPUnit_Framework_TestCase
public function setUp()
{
/* Setup Routine */
$this->_path = PATH . 'data';
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
if (!is_dir($this->_path)) {
mkdir($this->_path);
}
$this->_file = $this->_path . DIRECTORY_SEPARATOR . 'vizhash.png';
serversalt::setPath($this->_path);
ServerSalt::setPath($this->_path);
}
public function tearDown()
{
/* Tear Down Routine */
chmod($this->_path, 0700);
if (!@unlink($this->_file)) {
throw new Exception('Error deleting file "' . $this->_file . '".');
}
helper::rmdir($this->_path);
Helper::rmDir($this->_path);
}
public function testVizhashGeneratesUniquePngsPerIp()
{
$vz = new vizhash16x16();
$vz = new Vizhash16x16();
$pngdata = $vz->generate('127.0.0.1');
file_put_contents($this->_file, $pngdata);
$finfo = new finfo(FILEINFO_MIME_TYPE);

View file

@ -1,81 +0,0 @@
<?php
use PrivateBin\filter;
class filterTest extends PHPUnit_Framework_TestCase
{
public function testFilterStripsSlashesDeeply()
{
$this->assertEquals(
array("f'oo", "b'ar", array("fo'o", "b'ar")),
filter::stripslashes_deep(array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")))
);
}
public function testFilterMakesTimesHumanlyReadable()
{
$this->assertEquals('5 minutes', filter::time_humanreadable('5min'));
$this->assertEquals('90 seconds', filter::time_humanreadable('90sec'));
$this->assertEquals('1 week', filter::time_humanreadable('1week'));
$this->assertEquals('6 months', filter::time_humanreadable('6months'));
}
/**
* @expectedException Exception
* @expectedExceptionCode 30
*/
public function testFilterFailTimesHumanlyReadable()
{
filter::time_humanreadable('five_minutes');
}
public function testFilterMakesSizesHumanlyReadable()
{
$this->assertEquals('1 B', filter::size_humanreadable(1));
$this->assertEquals('1 000 B', filter::size_humanreadable(1000));
$this->assertEquals('1.00 KiB', filter::size_humanreadable(1024));
$this->assertEquals('1.21 KiB', filter::size_humanreadable(1234));
$exponent = 1024;
$this->assertEquals('1 000.00 KiB', filter::size_humanreadable(1000 * $exponent));
$this->assertEquals('1.00 MiB', filter::size_humanreadable(1024 * $exponent));
$this->assertEquals('1.21 MiB', filter::size_humanreadable(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 MiB', filter::size_humanreadable(1000 * $exponent));
$this->assertEquals('1.00 GiB', filter::size_humanreadable(1024 * $exponent));
$this->assertEquals('1.21 GiB', filter::size_humanreadable(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 GiB', filter::size_humanreadable(1000 * $exponent));
$this->assertEquals('1.00 TiB', filter::size_humanreadable(1024 * $exponent));
$this->assertEquals('1.21 TiB', filter::size_humanreadable(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 TiB', filter::size_humanreadable(1000 * $exponent));
$this->assertEquals('1.00 PiB', filter::size_humanreadable(1024 * $exponent));
$this->assertEquals('1.21 PiB', filter::size_humanreadable(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 PiB', filter::size_humanreadable(1000 * $exponent));
$this->assertEquals('1.00 EiB', filter::size_humanreadable(1024 * $exponent));
$this->assertEquals('1.21 EiB', filter::size_humanreadable(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 EiB', filter::size_humanreadable(1000 * $exponent));
$this->assertEquals('1.00 ZiB', filter::size_humanreadable(1024 * $exponent));
$this->assertEquals('1.21 ZiB', filter::size_humanreadable(1234 * $exponent));
$exponent *= 1024;
$this->assertEquals('1 000.00 ZiB', filter::size_humanreadable(1000 * $exponent));
$this->assertEquals('1.00 YiB', filter::size_humanreadable(1024 * $exponent));
$this->assertEquals('1.21 YiB', filter::size_humanreadable(1234 * $exponent));
}
public function testSlowEquals()
{
$this->assertTrue(filter::slow_equals('foo', 'foo'), 'same string');
$this->assertFalse(filter::slow_equals('foo', true), 'string and boolean');
$this->assertFalse(filter::slow_equals('foo', 0), 'string and integer');
$this->assertFalse(filter::slow_equals('123foo', 123), 'string and integer');
$this->assertFalse(filter::slow_equals('123foo', '123'), 'different strings');
$this->assertFalse(filter::slow_equals('6', ' 6'), 'strings with space');
$this->assertFalse(filter::slow_equals('4.2', '4.20'), 'floats as strings');
$this->assertFalse(filter::slow_equals('1e3', '1000'), 'integers as strings');
$this->assertFalse(filter::slow_equals('9223372036854775807', '9223372036854775808'), 'large integers as strings');
$this->assertFalse(filter::slow_equals('61529519452809720693702583126814', '61529519452809720000000000000000'), 'larger integers as strings');
}
}

View file

@ -1,13 +1,13 @@
<phpunit bootstrap="bootstrap.php" colors="true">
<phpunit bootstrap="Bootstrap.php" colors="true">
<testsuite name="PrivateBin Test Suite">
<directory suffix=".php">./</directory>
<exclude>configGenerator.php</exclude>
<exclude>ConfigurationTestGenerator.php</exclude>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../lib</directory>
<exclude>
<file>../lib/data/AbstractData.php</file>
<file>../lib/Data/AbstractData.php</file>
</exclude>
</whitelist>
</filter>

View file

@ -1,41 +0,0 @@
<?php
use PrivateBin\purgelimiter;
class purgelimiterTest extends PHPUnit_Framework_TestCase
{
private $_path;
public function setUp()
{
/* Setup Routine */
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
if (!is_dir($this->_path)) {
mkdir($this->_path);
}
purgelimiter::setPath($this->_path);
}
public function tearDown()
{
/* Tear Down Routine */
helper::rmdir($this->_path);
}
public function testLimit()
{
// initialize it
purgelimiter::canPurge();
// try setting it
purgelimiter::setLimit(1);
$this->assertEquals(false, purgelimiter::canPurge());
sleep(2);
$this->assertEquals(true, purgelimiter::canPurge());
// disable it
purgelimiter::setLimit(0);
purgelimiter::canPurge();
$this->assertEquals(true, purgelimiter::canPurge());
}
}

View file

@ -1,49 +0,0 @@
[main]
discussion = true
opendiscussion = true
syntaxhighlighting = true
burnafterreadingselected = true
password = true
sizelimit = 10
template = "page"
base64version = "2.1.9"
syntaxhighlightingtheme = "sons-of-obsidian"
[expire]
default = "1week"
[expire_options]
5min = "300"
10min = "600"
1hour = "3600"
1day = "86400"
1week = "604800"
1month = "2592000"
1year = "31536000"
never = "0"
[expire_labels]
5min = "5 minutes"
10min = "10 minutes"
1hour = "1 hour"
1day = "1 day"
1week = "1 week"
1month = "1 month"
1year = "1 year"
never = "Never"
[traffic]
limit = 0
dir = "../data"
[model]
class = "privatebin_db"
[model_options]
dsn = "sqlite:../data/db.sq3"
usr = ""
pwd = ""
opt = array (
12 => '1',
)