2015-08-27 21:41:21 +02:00
|
|
|
<?php
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2016-08-09 11:54:42 +02:00
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2016-08-09 11:54:42 +02:00
|
|
|
class ServerSaltTest extends PHPUnit_Framework_TestCase
|
2015-08-27 21:41:21 +02:00
|
|
|
{
|
|
|
|
private $_path;
|
|
|
|
|
|
|
|
private $_invalidPath;
|
|
|
|
|
|
|
|
private $_otherPath;
|
|
|
|
|
|
|
|
private $_invalidFile;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 11:54:42 +02:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!is_dir($this->_path)) {
|
|
|
|
mkdir($this->_path);
|
|
|
|
}
|
2016-08-09 11:54:42 +02:00
|
|
|
ServerSalt::setPath($this->_path);
|
2015-08-27 21:41:21 +02:00
|
|
|
|
|
|
|
$this->_otherPath = $this->_path . DIRECTORY_SEPARATOR . 'foo';
|
|
|
|
|
|
|
|
$this->_invalidPath = $this->_path . DIRECTORY_SEPARATOR . 'bar';
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!is_dir($this->_invalidPath)) {
|
|
|
|
mkdir($this->_invalidPath);
|
|
|
|
}
|
2015-08-27 21:41:21 +02:00
|
|
|
$this->_invalidFile = $this->_invalidPath . DIRECTORY_SEPARATOR . 'salt.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
chmod($this->_invalidPath, 0700);
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::rmDir($this->_path);
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneration()
|
|
|
|
{
|
|
|
|
// generating new salt
|
2016-08-09 11:54:42 +02:00
|
|
|
ServerSalt::setPath($this->_path);
|
|
|
|
$salt = ServerSalt::get();
|
2015-08-27 23:30:35 +02:00
|
|
|
|
|
|
|
// mcrypt mock
|
2016-07-26 08:19:35 +02:00
|
|
|
if (!function_exists('mcrypt_create_iv')) {
|
|
|
|
if (!defined('MCRYPT_DEV_URANDOM')) {
|
|
|
|
define('MCRYPT_DEV_URANDOM', 1);
|
|
|
|
}
|
2016-05-22 17:17:09 +02:00
|
|
|
function mcrypt_create_iv($int, $flag)
|
2015-08-27 23:30:35 +02:00
|
|
|
{
|
2016-05-22 17:17:09 +02:00
|
|
|
$randomSalt = '';
|
2016-07-26 08:19:35 +02:00
|
|
|
for ($i = 0; $i < $int; ++$i) {
|
2016-05-22 17:17:09 +02:00
|
|
|
$randomSalt .= base_convert(mt_rand(), 10, 16);
|
|
|
|
}
|
|
|
|
// hex2bin requires an even length, pad if necessary
|
2016-07-26 08:19:35 +02:00
|
|
|
if (strlen($randomSalt) % 2) {
|
2016-05-22 17:17:09 +02:00
|
|
|
$randomSalt = '0' . $randomSalt;
|
|
|
|
}
|
|
|
|
return hex2bin($randomSalt);
|
2015-08-27 23:30:35 +02:00
|
|
|
}
|
2016-08-09 11:54:42 +02:00
|
|
|
$this->assertNotEquals($salt, ServerSalt::generate());
|
2015-08-27 23:30:35 +02:00
|
|
|
}
|
2015-08-27 21:41:21 +02:00
|
|
|
|
|
|
|
// try setting a different path and resetting it
|
2016-08-09 11:54:42 +02:00
|
|
|
ServerSalt::setPath($this->_otherPath);
|
|
|
|
$this->assertNotEquals($salt, ServerSalt::get());
|
|
|
|
ServerSalt::setPath($this->_path);
|
|
|
|
$this->assertEquals($salt, ServerSalt::get());
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 11
|
|
|
|
*/
|
|
|
|
public function testPathShenanigans()
|
|
|
|
{
|
|
|
|
// try setting an invalid path
|
|
|
|
chmod($this->_invalidPath, 0000);
|
2016-08-09 11:54:42 +02:00
|
|
|
ServerSalt::setPath($this->_invalidPath);
|
|
|
|
ServerSalt::get();
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 20
|
|
|
|
*/
|
|
|
|
public function testFileRead()
|
|
|
|
{
|
|
|
|
// try setting an invalid file
|
|
|
|
chmod($this->_invalidPath, 0700);
|
|
|
|
file_put_contents($this->_invalidFile, '');
|
|
|
|
chmod($this->_invalidFile, 0000);
|
2016-08-09 11:54:42 +02:00
|
|
|
ServerSalt::setPath($this->_invalidPath);
|
|
|
|
ServerSalt::get();
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 13
|
|
|
|
*/
|
|
|
|
public function testFileWrite()
|
|
|
|
{
|
|
|
|
// try setting an invalid file
|
|
|
|
chmod($this->_invalidPath, 0700);
|
2016-08-09 11:54:42 +02:00
|
|
|
if (is_file($this->_invalidFile)) {
|
|
|
|
chmod($this->_invalidFile, 0600);
|
|
|
|
unlink($this->_invalidFile);
|
|
|
|
}
|
2015-08-27 21:41:21 +02:00
|
|
|
file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
|
|
|
|
chmod($this->_invalidPath, 0500);
|
2016-08-09 11:54:42 +02:00
|
|
|
ServerSalt::setPath($this->_invalidPath);
|
|
|
|
ServerSalt::get();
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 10
|
|
|
|
*/
|
|
|
|
public function testPermissionShenanigans()
|
|
|
|
{
|
|
|
|
// try creating an invalid path
|
|
|
|
chmod($this->_invalidPath, 0000);
|
2016-08-09 11:54:42 +02:00
|
|
|
ServerSalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
|
|
|
|
ServerSalt::get();
|
2015-08-27 21:41:21 +02:00
|
|
|
}
|
|
|
|
}
|