2015-08-16 00:32:31 +08:00
< ? php
2016-07-21 23:09:48 +08:00
2016-08-09 17:54:42 +08:00
use PrivateBin\Data\Filesystem ;
2016-07-21 23:09:48 +08:00
2016-08-09 17:54:42 +08:00
class FilesystemTest extends PHPUnit_Framework_TestCase
2015-08-16 00:32:31 +08:00
{
private $_model ;
private $_path ;
2017-03-25 04:30:08 +08:00
private $_invalidPath ;
2015-08-16 00:32:31 +08:00
public function setUp ()
{
/* Setup Routine */
2017-03-25 04:35:50 +08:00
$this -> _path = sys_get_temp_dir () . DIRECTORY_SEPARATOR . 'privatebin_data' ;
2017-03-25 04:30:08 +08:00
$this -> _invalidPath = $this -> _path . DIRECTORY_SEPARATOR . 'bar' ;
2017-03-25 04:35:50 +08:00
$this -> _model = Filesystem :: getInstance ( array ( 'dir' => $this -> _path ));
2017-03-25 04:30:08 +08:00
if ( ! is_dir ( $this -> _path )) {
mkdir ( $this -> _path );
}
if ( ! is_dir ( $this -> _invalidPath )) {
mkdir ( $this -> _invalidPath );
}
2015-08-16 00:32:31 +08:00
}
public function tearDown ()
{
/* Tear Down Routine */
2017-03-25 04:30:08 +08:00
chmod ( $this -> _invalidPath , 0700 );
2016-08-09 17:54:42 +08:00
Helper :: rmDir ( $this -> _path );
2015-08-16 00:32:31 +08:00
}
public function testFileBasedDataStoreWorks ()
{
2016-08-09 17:54:42 +08:00
$this -> _model -> delete ( Helper :: getPasteId ());
2015-09-26 18:29:27 +08:00
2015-08-16 00:32:31 +08:00
// storing pastes
2016-08-09 17:54:42 +08:00
$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 ()));
2015-08-16 00:32:31 +08:00
// storing comments
2016-08-09 17:54:42 +08:00
$this -> assertFalse ( $this -> _model -> existsComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId ()), 'comment does not yet exist' );
2016-08-11 20:41:52 +08:00
$this -> assertTrue ( $this -> _model -> createComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId (), Helper :: getComment ()), 'store comment' );
2016-08-09 17:54:42 +08:00
$this -> assertTrue ( $this -> _model -> existsComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId ()), 'comment exists after storing it' );
2017-03-25 04:30:08 +08:00
$this -> assertFalse ( $this -> _model -> createComment ( Helper :: getPasteId (), Helper :: getPasteId (), Helper :: getCommentId (), Helper :: getComment ()), 'unable to store the same comment twice' );
2016-10-29 16:24:08 +08:00
$comment = json_decode ( json_encode ( Helper :: getComment ()));
$comment -> id = Helper :: getCommentId ();
2016-08-09 17:54:42 +08:00
$comment -> parentid = Helper :: getPasteId ();
2015-08-16 00:32:31 +08:00
$this -> assertEquals (
array ( $comment -> meta -> postdate => $comment ),
2016-08-09 17:54:42 +08:00
$this -> _model -> readComments ( Helper :: getPasteId ())
2015-08-16 00:32:31 +08:00
);
// deleting pastes
2016-08-09 17:54:42 +08:00
$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' );
2015-08-16 00:32:31 +08:00
}
2015-09-26 18:29:27 +08:00
public function testFileBasedAttachmentStoreWorks ()
{
2016-08-09 17:54:42 +08:00
$this -> _model -> delete ( Helper :: getPasteId ());
2016-10-29 16:24:08 +08:00
$original = $paste = Helper :: getPasteWithAttachment ( array ( 'expire_date' => 1344803344 ));
$paste [ 'meta' ][ 'attachment' ] = $paste [ 'attachment' ];
2015-09-26 18:29:27 +08:00
$paste [ 'meta' ][ 'attachmentname' ] = $paste [ 'attachmentname' ];
unset ( $paste [ 'attachment' ], $paste [ 'attachmentname' ]);
2016-08-09 17:54:42 +08:00
$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 ()));
2015-09-26 18:29:27 +08:00
}
2016-12-25 19:04:47 +08:00
/**
* pastes a - g are expired and should get deleted , x never expires and y - z expire in an hour
*/
2016-07-15 23:02:59 +08:00
public function testPurge ()
{
2016-07-18 20:47:32 +08:00
mkdir ( $this -> _path . DIRECTORY_SEPARATOR . '00' , 0777 , true );
2016-08-09 17:54:42 +08:00
$expired = Helper :: getPaste ( array ( 'expire_date' => 1344803344 ));
2016-10-29 16:24:08 +08:00
$paste = Helper :: getPaste ( array ( 'expire_date' => time () + 3600 ));
2016-12-25 19:04:47 +08:00
$keys = array ( 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'x' , 'y' , 'z' );
2016-10-29 16:24:08 +08:00
$ids = array ();
2016-07-26 14:19:35 +08:00
foreach ( $keys as $key ) {
2016-07-15 23:02:59 +08:00
$ids [ $key ] = substr ( md5 ( $key ), 0 , 16 );
$this -> assertFalse ( $this -> _model -> exists ( $ids [ $key ]), " paste $key does not yet exist " );
2016-07-26 14:19:35 +08:00
if ( in_array ( $key , array ( 'x' , 'y' , 'z' ))) {
2016-07-15 23:02:59 +08:00
$this -> assertTrue ( $this -> _model -> create ( $ids [ $key ], $paste ), " store $key paste " );
2016-12-25 19:04:47 +08:00
} elseif ( $key === 'x' ) {
$this -> assertTrue ( $this -> _model -> create ( $ids [ $key ], Helper :: getPaste ()), " store $key paste " );
2016-07-26 14:19:35 +08:00
} else {
2016-07-15 23:02:59 +08:00
$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 );
2016-07-26 14:19:35 +08:00
foreach ( $ids as $key => $id ) {
if ( in_array ( $key , array ( 'x' , 'y' , 'z' ))) {
2016-07-19 14:40:33 +08:00
$this -> assertTrue ( $this -> _model -> exists ( $id ), " paste $key exists after purge " );
$this -> _model -> delete ( $id );
2016-07-26 14:19:35 +08:00
} else {
2016-07-19 14:40:33 +08:00
$this -> assertFalse ( $this -> _model -> exists ( $id ), " paste $key was purged " );
2016-07-15 23:02:59 +08:00
}
}
}
2016-08-11 20:41:52 +08:00
public function testErrorDetection ()
{
$this -> _model -> delete ( Helper :: getPasteId ());
$paste = Helper :: getPaste ( array ( 'formatter' => " 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 ( array ( 'formatter' => " Invalid UTF-8 sequence: \xB1 \x31 " ));
$this -> assertFalse ( $this -> _model -> exists ( Helper :: getPasteId ()), 'paste does not yet exist' );
2016-08-11 21:03:48 +08:00
$this -> assertTrue ( $this -> _model -> create ( Helper :: getPasteId (), Helper :: getPaste ()), 'store new paste' );
2016-08-11 20:41:52 +08:00
$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' );
}
2015-08-16 00:32:31 +08:00
}