introducing automatic purging of expired pastes, triggered by default at least 5 minutes apart, deleting a maximum of 10 pastes - resolves #3
This commit is contained in:
@@ -12,6 +12,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
|
||||
$this->_options = configuration::getDefaults();
|
||||
$this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
|
||||
$this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
|
||||
$this->_options['purge']['dir'] = PATH . $this->_options['purge']['dir'];
|
||||
$this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
|
||||
}
|
||||
|
||||
|
||||
@@ -184,7 +184,11 @@ class privatebinTest extends PHPUnit_Framework_TestCase
|
||||
public function testCreateInvalidTimelimit()
|
||||
{
|
||||
$this->reset();
|
||||
$_POST = helper::getPaste();
|
||||
$options = parse_ini_file(CONF, true);
|
||||
$options['traffic']['limit'] = 0;
|
||||
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';
|
||||
@@ -193,8 +197,14 @@ class privatebinTest extends PHPUnit_Framework_TestCase
|
||||
new privatebin;
|
||||
$content = ob_get_contents();
|
||||
$response = json_decode($content, true);
|
||||
$this->assertEquals(1, $response['status'], 'outputs error status');
|
||||
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
|
||||
$this->assertEquals(0, $response['status'], 'outputs status');
|
||||
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
|
||||
$paste = $this->_model->read($response['id']);
|
||||
$this->assertEquals(
|
||||
hash_hmac('sha256', $response['id'], $paste->meta->salt),
|
||||
$response['deletetoken'],
|
||||
'outputs valid delete token'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,11 +238,10 @@ class privatebinTest extends PHPUnit_Framework_TestCase
|
||||
$this->reset();
|
||||
$options = parse_ini_file(CONF, true);
|
||||
$options['traffic']['header'] = 'X_FORWARDED_FOR';
|
||||
$options['traffic']['limit'] = 100;
|
||||
helper::confBackup();
|
||||
helper::createIniFile(CONF, $options);
|
||||
$_POST = helper::getPaste();
|
||||
$_SERVER['HTTP_X_FORWARDED_FOR'] = '::1';
|
||||
$_SERVER['HTTP_X_FORWARDED_FOR'] = '::2';
|
||||
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
||||
$_SERVER['REQUEST_METHOD'] = 'POST';
|
||||
$_SERVER['REMOTE_ADDR'] = '::1';
|
||||
@@ -240,8 +249,14 @@ class privatebinTest extends PHPUnit_Framework_TestCase
|
||||
new privatebin;
|
||||
$content = ob_get_contents();
|
||||
$response = json_decode($content, true);
|
||||
$this->assertEquals(1, $response['status'], 'outputs error status');
|
||||
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
|
||||
$this->assertEquals(0, $response['status'], 'outputs status');
|
||||
$this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
|
||||
$paste = $this->_model->read($response['id']);
|
||||
$this->assertEquals(
|
||||
hash_hmac('sha256', $response['id'], $paste->meta->salt),
|
||||
$response['deletetoken'],
|
||||
'outputs valid delete token'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -63,4 +63,37 @@ class privatebin_dataTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(json_decode(json_encode($original)), $this->_model->read(helper::getPasteId()));
|
||||
}
|
||||
|
||||
public function testPurge()
|
||||
{
|
||||
$expired = helper::getPaste(array('expire_date' => 1344803344));
|
||||
$paste = helper::getPaste(array('expire_date' => time() + 3600));
|
||||
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
||||
$ids = array();
|
||||
foreach ($keys as $key)
|
||||
{
|
||||
$ids[$key] = substr(md5($key), 0, 16);
|
||||
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
|
||||
if (in_array($key, array('x', 'y', 'z')))
|
||||
{
|
||||
$this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
|
||||
}
|
||||
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
|
||||
}
|
||||
$this->_model->purge(10);
|
||||
foreach ($ids as $key => $id)
|
||||
{
|
||||
if (in_array($key, array('x', 'y', 'z')))
|
||||
{
|
||||
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after purge");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key was purged");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,12 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
||||
$this->_model = privatebin_db::getInstance($this->_options);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
/* Tear Down Routine */
|
||||
if (is_dir(PATH . 'data')) helper::rmdir(PATH . 'data');
|
||||
}
|
||||
|
||||
public function testDatabaseBasedDataStoreWorks()
|
||||
{
|
||||
$this->_model->delete(helper::getPasteId());
|
||||
@@ -62,6 +68,41 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
||||
$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));
|
||||
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
||||
$ids = array();
|
||||
foreach ($keys as $key)
|
||||
{
|
||||
$ids[$key] = substr(md5($key), 0, 16);
|
||||
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
|
||||
if (in_array($key, array('x', 'y', 'z')))
|
||||
{
|
||||
$this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
|
||||
}
|
||||
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
|
||||
}
|
||||
$this->_model->purge(10);
|
||||
foreach ($ids as $key => $id)
|
||||
{
|
||||
if (in_array($key, array('x', 'y', 'z')))
|
||||
{
|
||||
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after purge");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key was purged");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException PDOException
|
||||
*/
|
||||
@@ -185,6 +226,7 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testTableUpgrade()
|
||||
{
|
||||
mkdir(PATH . 'data');
|
||||
$path = PATH . 'data/db-test.sq3';
|
||||
@unlink($path);
|
||||
$this->_options['dsn'] = 'sqlite:' . $path;
|
||||
|
||||
@@ -4,7 +4,6 @@ require_once 'privatebin.php';
|
||||
class privatebinWithDbTest extends privatebinTest
|
||||
{
|
||||
private $_options = array(
|
||||
'dsn' => 'sqlite:../data/tst.sq3',
|
||||
'usr' => null,
|
||||
'pwd' => null,
|
||||
'opt' => array(
|
||||
@@ -13,11 +12,15 @@ class privatebinWithDbTest extends privatebinTest
|
||||
),
|
||||
);
|
||||
|
||||
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);
|
||||
$this->_options['dsn'] = 'sqlite:' . $this->_path . '/tst.sq3';
|
||||
$this->_model = privatebin_db::getInstance($this->_options);
|
||||
serversalt::setPath(PATH . 'data');
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
@@ -25,7 +28,7 @@ class privatebinWithDbTest extends privatebinTest
|
||||
{
|
||||
/* Tear Down Routine */
|
||||
parent::tearDown();
|
||||
@unlink('../data/tst.sq3');
|
||||
helper::rmdir($this->_path);
|
||||
}
|
||||
|
||||
public function reset()
|
||||
|
||||
36
tst/purgelimiter.php
Normal file
36
tst/purgelimiter.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user