1
0

FilesystemTest.php 9.6 KB

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