paste.chapril.org-privatebin/tst/Data/GoogleCloudStorageTest.php

184 lines
8.6 KiB
PHP
Raw Normal View History

2021-05-28 22:39:50 +02:00
<?php
use Google\Auth\HttpHandler\HttpHandlerFactory;
use GuzzleHttp\Client;
2021-10-07 22:20:25 +02:00
use PHPUnit\Framework\TestCase;
2021-05-28 22:39:50 +02:00
use PrivateBin\Data\GoogleCloudStorage;
2021-10-07 22:20:25 +02:00
class GoogleCloudStorageTest extends TestCase
2021-05-28 22:39:50 +02:00
{
private static $_client;
private static $_bucket;
2021-10-07 22:22:32 +02:00
public static function setUpBeforeClass(): void
2021-05-28 22:39:50 +02:00
{
$httpClient = new Client(array('debug'=>false));
$handler = HttpHandlerFactory::build($httpClient);
$name = 'pb-';
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
for ($i = 0; $i < 29; ++$i) {
$name .= $alphabet[rand(0, strlen($alphabet) - 1)];
}
self::$_client = new StorageClientStub(array());
self::$_bucket = self::$_client->createBucket($name);
}
2021-10-07 22:22:32 +02:00
public function setUp(): void
2021-05-28 22:39:50 +02:00
{
ini_set('error_log', stream_get_meta_data(tmpfile())['uri']);
$this->_model = new GoogleCloudStorage(array(
2021-05-28 22:39:50 +02:00
'bucket' => self::$_bucket->name(),
'prefix' => 'pastes',
2021-06-13 10:44:26 +02:00
));
2021-05-28 22:39:50 +02:00
}
2021-10-07 22:22:32 +02:00
public function tearDown(): void
2021-05-28 22:39:50 +02:00
{
foreach (self::$_bucket->objects() as $object) {
$object->delete();
}
}
2021-10-07 22:25:51 +02:00
public static function tearDownAfterClass(): void
2021-05-28 22:39:50 +02:00
{
self::$_bucket->delete();
}
public function testFileBasedDataStoreWorks()
{
$this->_model->delete(Helper::getPasteId());
// storing pastes
$paste = Helper::getPaste(2, 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($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()), 'store comment');
$this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment exists after storing it');
$this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()), 'unable to store the same comment twice');
$comment = Helper::getComment();
$comment['id'] = Helper::getCommentId();
$comment['parentid'] = Helper::getPasteId();
$this->assertEquals(
array($comment['meta']['created'] => $comment),
$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');
}
/**
* pastes a-g are expired and should get deleted, x never expires and y-z expire in an hour
*/
public function testPurge()
{
$expired = Helper::getPaste(2, array('expire_date' => 1344803344));
$paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z');
$ids = array();
foreach ($keys as $key) {
$ids[$key] = hash('fnv164', $key);
$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");
} elseif ($key === 'x') {
$this->assertTrue($this->_model->create($ids[$key], Helper::getPaste()), "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($id), "paste $key exists after purge");
$this->_model->delete($id);
} else {
$this->assertFalse($this->_model->exists($id), "paste $key was purged");
}
}
}
public function testErrorDetection()
{
$this->_model->delete(Helper::getPasteId());
$paste = Helper::getPaste(2, array('expire' => "Invalid UTF-8 sequence: \xB1\x31"));
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
}
public function testCommentErrorDetection()
{
$this->_model->delete(Helper::getPasteId());
$comment = Helper::getComment(1, array('nickname' => "Invalid UTF-8 sequence: \xB1\x31"));
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
$this->assertTrue($this->_model->create(Helper::getPasteId(), Helper::getPaste()), 'store new paste');
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
$this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
}
/**
* @throws Exception
*/
public function testKeyValueStore()
{
$salt = bin2hex(random_bytes(256));
$this->_model->setValue($salt, 'salt', '');
$storedSalt = $this->_model->getValue('salt', '');
$this->assertEquals($salt, $storedSalt);
2021-06-09 22:27:34 +02:00
$this->_model->purgeValues('salt', time() + 60);
2021-06-13 11:08:38 +02:00
$this->assertEquals('', $this->_model->getValue('salt', 'master'));
$client = hash_hmac('sha512', '127.0.0.1', $salt);
$expire = time();
$this->_model->setValue(strval($expire), 'traffic_limiter', $client);
$storedExpired = $this->_model->getValue('traffic_limiter', $client);
$this->assertEquals(strval($expire), $storedExpired);
2021-06-09 22:27:34 +02:00
$this->_model->purgeValues('traffic_limiter', time() - 60);
$this->assertEquals($storedExpired, $this->_model->getValue('traffic_limiter', $client));
$this->_model->purgeValues('traffic_limiter', time() + 60);
2021-06-13 11:08:38 +02:00
$this->assertEquals('', $this->_model->getValue('traffic_limiter', $client));
$purgeAt = $expire + (15 * 60);
$this->_model->setValue(strval($purgeAt), 'purge_limiter', '');
$storedPurgedAt = $this->_model->getValue('purge_limiter', '');
$this->assertEquals(strval($purgeAt), $storedPurgedAt);
$this->_model->purgeValues('purge_limiter', $purgeAt + 60);
$this->assertEquals('', $this->_model->getValue('purge_limiter', ''));
2021-06-13 11:08:38 +02:00
$this->assertEquals('', $this->_model->getValue('purge_limiter', 'at'));
}
/**
* @throws Exception
*/
public function testKeyValuePurgeTrafficLimiter()
{
$salt = bin2hex(random_bytes(256));
$client = hash_hmac('sha512', '127.0.0.1', $salt);
$expire = time();
$this->_model->setValue(strval($expire), 'traffic_limiter', $client);
$storedExpired = $this->_model->getValue('traffic_limiter', $client);
$this->assertEquals(strval($expire), $storedExpired);
$this->_model->purgeValues('traffic_limiter', time() - 60);
$this->assertEquals($storedExpired, $this->_model->getValue('traffic_limiter', $client));
$this->_model->purgeValues('traffic_limiter', time() + 60);
2021-06-13 11:12:19 +02:00
$this->assertEquals('', $this->_model->getValue('traffic_limiter', $client));
}
2021-05-28 22:39:50 +02:00
}