FilesystemTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. use PrivateBin\Data\Filesystem;
  3. class FilesystemTest extends PHPUnit_Framework_TestCase
  4. {
  5. private $_model;
  6. private $_path;
  7. public function setUp()
  8. {
  9. /* Setup Routine */
  10. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  11. $this->_model = Filesystem::getInstance(array('dir' => $this->_path));
  12. }
  13. public function tearDown()
  14. {
  15. /* Tear Down Routine */
  16. Helper::rmDir($this->_path);
  17. }
  18. public function testFileBasedDataStoreWorks()
  19. {
  20. $this->_model->delete(Helper::getPasteId());
  21. // storing pastes
  22. $paste = Helper::getPaste(array('expire_date' => 1344803344));
  23. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  24. $this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
  25. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  26. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
  27. $this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(Helper::getPasteId()));
  28. // storing comments
  29. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  30. $this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()), 'store comment');
  31. $this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment exists after storing it');
  32. $comment = json_decode(json_encode(Helper::getComment()));
  33. $comment->id = Helper::getCommentId();
  34. $comment->parentid = Helper::getPasteId();
  35. $this->assertEquals(
  36. array($comment->meta->postdate => $comment),
  37. $this->_model->readComments(Helper::getPasteId())
  38. );
  39. // deleting pastes
  40. $this->_model->delete(Helper::getPasteId());
  41. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  42. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment was deleted with paste');
  43. $this->assertFalse($this->_model->read(Helper::getPasteId()), 'paste can no longer be found');
  44. }
  45. public function testFileBasedAttachmentStoreWorks()
  46. {
  47. $this->_model->delete(Helper::getPasteId());
  48. $original = $paste = Helper::getPasteWithAttachment(array('expire_date' => 1344803344));
  49. $paste['meta']['attachment'] = $paste['attachment'];
  50. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  51. unset($paste['attachment'], $paste['attachmentname']);
  52. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  53. $this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
  54. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  55. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
  56. $this->assertEquals(json_decode(json_encode($original)), $this->_model->read(Helper::getPasteId()));
  57. }
  58. /**
  59. * pastes a-g are expired and should get deleted, x never expires and y-z expire in an hour
  60. */
  61. public function testPurge()
  62. {
  63. mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
  64. $expired = Helper::getPaste(array('expire_date' => 1344803344));
  65. $paste = Helper::getPaste(array('expire_date' => time() + 3600));
  66. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z');
  67. $ids = array();
  68. foreach ($keys as $key) {
  69. $ids[$key] = substr(md5($key), 0, 16);
  70. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  71. if (in_array($key, array('x', 'y', 'z'))) {
  72. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  73. } elseif ($key === 'x') {
  74. $this->assertTrue($this->_model->create($ids[$key], Helper::getPaste()), "store $key paste");
  75. } else {
  76. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  77. }
  78. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  79. }
  80. $this->_model->purge(10);
  81. foreach ($ids as $key => $id) {
  82. if (in_array($key, array('x', 'y', 'z'))) {
  83. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  84. $this->_model->delete($id);
  85. } else {
  86. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  87. }
  88. }
  89. }
  90. /**
  91. * @expectedException Exception
  92. * @expectedExceptionCode 90
  93. */
  94. public function testErrorDetection()
  95. {
  96. $this->_model->delete(Helper::getPasteId());
  97. $paste = Helper::getPaste(array('formatter' => "Invalid UTF-8 sequence: \xB1\x31"));
  98. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  99. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
  100. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
  101. }
  102. /**
  103. * @expectedException Exception
  104. * @expectedExceptionCode 90
  105. */
  106. public function testCommentErrorDetection()
  107. {
  108. $this->_model->delete(Helper::getPasteId());
  109. $comment = Helper::getComment(array('formatter' => "Invalid UTF-8 sequence: \xB1\x31"));
  110. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  111. $this->assertTrue($this->_model->create(Helper::getPasteId(), Helper::getPaste()), 'store new paste');
  112. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  113. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  114. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
  115. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
  116. }
  117. public function testOldFilesGetConverted()
  118. {
  119. // generate 10 (default purge batch size) pastes in the old format
  120. $paste = Helper::getPaste();
  121. $comment = Helper::getComment();
  122. $commentid = Helper::getCommentId();
  123. $ids = array();
  124. for ($i = 0, $max = 10; $i < $max; ++$i) {
  125. // PHPs mt_rand only supports 32 bit or up 0x7fffffff on 64 bit systems to be precise :-/
  126. $dataid = str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT) .
  127. str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT);
  128. $storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) .
  129. DIRECTORY_SEPARATOR . substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
  130. $ids[$dataid] = $storagedir;
  131. if (!is_dir($storagedir)) {
  132. mkdir($storagedir, 0700, true);
  133. }
  134. file_put_contents($storagedir . $dataid, json_encode($paste));
  135. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  136. if (!is_dir($storagedir)) {
  137. mkdir($storagedir, 0700, true);
  138. }
  139. file_put_contents($storagedir . $dataid . '.' . $commentid . '.' . $dataid, json_encode($comment));
  140. }
  141. // check that all 10 pastes were converted after the purge
  142. $this->_model->purge(10);
  143. foreach ($ids as $dataid => $storagedir) {
  144. $this->assertFileExists($storagedir . $dataid . '.php', "paste $dataid exists in new format");
  145. $this->assertFileNotExists($storagedir . $dataid, "old format paste $dataid got removed");
  146. $this->assertTrue($this->_model->exists($dataid), "paste $dataid exists");
  147. $this->assertEquals($this->_model->read($dataid), json_decode(json_encode($paste)), "paste $dataid wasn't modified in the conversion");
  148. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  149. $this->assertFileExists($storagedir . $dataid . '.' . $commentid . '.' . $dataid . '.php', "comment of $dataid exists in new format");
  150. $this->assertFileNotExists($storagedir . $dataid . '.' . $commentid . '.' . $dataid, "old format comment of $dataid got removed");
  151. $this->assertTrue($this->_model->existsComment($dataid, $dataid, $commentid), "comment in paste $dataid exists");
  152. $comment = json_decode(json_encode($comment));
  153. $comment->id = $commentid;
  154. $comment->parentid = $dataid;
  155. $this->assertEquals($this->_model->readComments($dataid), array($comment->meta->postdate => $comment), "comment of $dataid wasn't modified in the conversion");
  156. }
  157. }
  158. }