2015-08-28 03:41:21 +08:00
|
|
|
<?php
|
2016-07-21 23:09:48 +08:00
|
|
|
|
2020-10-10 18:08:58 +08:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-06-09 04:01:29 +08:00
|
|
|
use PrivateBin\Data\Filesystem;
|
2016-08-09 17:54:42 +08:00
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
2016-07-21 23:09:48 +08:00
|
|
|
|
2020-10-10 18:08:58 +08:00
|
|
|
class ServerSaltTest extends TestCase
|
2015-08-28 03:41:21 +08:00
|
|
|
{
|
|
|
|
private $_path;
|
|
|
|
|
|
|
|
private $_invalidPath;
|
|
|
|
|
|
|
|
private $_otherPath;
|
|
|
|
|
|
|
|
private $_invalidFile;
|
|
|
|
|
2020-10-10 18:22:20 +08:00
|
|
|
public function setUp(): void
|
2015-08-28 03:41:21 +08:00
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
2016-07-26 14:19:35 +08:00
|
|
|
if (!is_dir($this->_path)) {
|
|
|
|
mkdir($this->_path);
|
|
|
|
}
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore(
|
2022-10-28 07:01:02 +08:00
|
|
|
new Filesystem(array('dir' => $this->_path))
|
2021-06-09 04:01:29 +08:00
|
|
|
);
|
2015-08-28 03:41:21 +08:00
|
|
|
|
|
|
|
$this->_otherPath = $this->_path . DIRECTORY_SEPARATOR . 'foo';
|
|
|
|
|
|
|
|
$this->_invalidPath = $this->_path . DIRECTORY_SEPARATOR . 'bar';
|
2016-07-26 14:19:35 +08:00
|
|
|
if (!is_dir($this->_invalidPath)) {
|
|
|
|
mkdir($this->_invalidPath);
|
|
|
|
}
|
2015-08-28 03:41:21 +08:00
|
|
|
$this->_invalidFile = $this->_invalidPath . DIRECTORY_SEPARATOR . 'salt.php';
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:22:20 +08:00
|
|
|
public function tearDown(): void
|
2015-08-28 03:41:21 +08:00
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
chmod($this->_invalidPath, 0700);
|
2016-08-09 17:54:42 +08:00
|
|
|
Helper::rmDir($this->_path);
|
2015-08-28 03:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneration()
|
|
|
|
{
|
|
|
|
// generating new salt
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore(
|
2022-10-28 07:01:02 +08:00
|
|
|
new Filesystem(array('dir' => $this->_path))
|
2021-06-09 04:01:29 +08:00
|
|
|
);
|
2016-08-09 17:54:42 +08:00
|
|
|
$salt = ServerSalt::get();
|
2015-08-28 05:30:35 +08:00
|
|
|
|
2015-08-28 03:41:21 +08:00
|
|
|
// try setting a different path and resetting it
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore(
|
2022-10-28 07:01:02 +08:00
|
|
|
new Filesystem(array('dir' => $this->_otherPath))
|
2021-06-09 04:01:29 +08:00
|
|
|
);
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertNotEquals($salt, ServerSalt::get());
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore(
|
2022-10-28 07:01:02 +08:00
|
|
|
new Filesystem(array('dir' => $this->_path))
|
2021-06-09 04:01:29 +08:00
|
|
|
);
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertEquals($salt, ServerSalt::get());
|
2015-08-28 03:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPathShenanigans()
|
|
|
|
{
|
|
|
|
// try setting an invalid path
|
|
|
|
chmod($this->_invalidPath, 0000);
|
2022-10-28 07:01:02 +08:00
|
|
|
$store = new Filesystem(array('dir' => $this->_invalidPath));
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$salt = ServerSalt::get();
|
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$this->assertNotEquals($salt, ServerSalt::get());
|
2015-08-28 03:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileRead()
|
|
|
|
{
|
|
|
|
// try setting an invalid file
|
|
|
|
chmod($this->_invalidPath, 0700);
|
|
|
|
file_put_contents($this->_invalidFile, '');
|
|
|
|
chmod($this->_invalidFile, 0000);
|
2022-10-28 07:01:02 +08:00
|
|
|
$store = new Filesystem(array('dir' => $this->_invalidPath));
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$salt = ServerSalt::get();
|
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$this->assertNotEquals($salt, ServerSalt::get());
|
2015-08-28 03:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileWrite()
|
|
|
|
{
|
|
|
|
// try setting an invalid file
|
|
|
|
chmod($this->_invalidPath, 0700);
|
2016-08-09 17:54:42 +08:00
|
|
|
if (is_file($this->_invalidFile)) {
|
|
|
|
chmod($this->_invalidFile, 0600);
|
|
|
|
unlink($this->_invalidFile);
|
|
|
|
}
|
2015-08-28 03:41:21 +08:00
|
|
|
file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
|
|
|
|
chmod($this->_invalidPath, 0500);
|
2022-10-28 07:01:02 +08:00
|
|
|
$store = new Filesystem(array('dir' => $this->_invalidPath));
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$salt = ServerSalt::get();
|
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$this->assertNotEquals($salt, ServerSalt::get());
|
2015-08-28 03:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPermissionShenanigans()
|
|
|
|
{
|
|
|
|
// try creating an invalid path
|
|
|
|
chmod($this->_invalidPath, 0000);
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore(
|
2022-10-28 07:01:02 +08:00
|
|
|
new Filesystem(array('dir' => $this->_invalidPath . DIRECTORY_SEPARATOR . 'baz'))
|
2021-06-09 04:01:29 +08:00
|
|
|
);
|
2022-10-28 07:01:02 +08:00
|
|
|
$store = new Filesystem(array('dir' => $this->_invalidPath));
|
2021-06-09 04:01:29 +08:00
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$salt = ServerSalt::get();
|
|
|
|
ServerSalt::setStore($store);
|
|
|
|
$this->assertNotEquals($salt, ServerSalt::get());
|
2015-08-28 03:41:21 +08:00
|
|
|
}
|
|
|
|
}
|