FilesystemTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. use PrivateBin\Data\Filesystem;
  3. class FilesystemTest extends PHPUnit_Framework_TestCase
  4. {
  5. private $_model;
  6. private $_path;
  7. private $_invalidPath;
  8. public function setUp()
  9. {
  10. /* Setup Routine */
  11. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  12. $this->_invalidPath = $this->_path . DIRECTORY_SEPARATOR . 'bar';
  13. $this->_model = new Filesystem(array('dir' => $this->_path));
  14. if (!is_dir($this->_path)) {
  15. mkdir($this->_path);
  16. }
  17. if (!is_dir($this->_invalidPath)) {
  18. mkdir($this->_invalidPath);
  19. }
  20. }
  21. public function tearDown()
  22. {
  23. /* Tear Down Routine */
  24. chmod($this->_invalidPath, 0700);
  25. Helper::rmDir($this->_path);
  26. }
  27. public function testFileBasedDataStoreWorks()
  28. {
  29. $this->_model->delete(Helper::getPasteId());
  30. // storing pastes
  31. $paste = Helper::getPaste(2, array('expire_date' => 1344803344));
  32. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  33. $this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
  34. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  35. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
  36. $this->assertEquals($paste, $this->_model->read(Helper::getPasteId()));
  37. // storing comments
  38. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  39. $this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()), 'store comment');
  40. $this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment exists after storing it');
  41. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()), 'unable to store the same comment twice');
  42. $comment = Helper::getComment();
  43. $comment['id'] = Helper::getCommentId();
  44. $comment['parentid'] = Helper::getPasteId();
  45. $this->assertEquals(
  46. array($comment['meta']['created'] => $comment),
  47. $this->_model->readComments(Helper::getPasteId())
  48. );
  49. // deleting pastes
  50. $this->_model->delete(Helper::getPasteId());
  51. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  52. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment was deleted with paste');
  53. $this->assertFalse($this->_model->read(Helper::getPasteId()), 'paste can no longer be found');
  54. }
  55. public function testFileBasedAttachmentStoreWorks()
  56. {
  57. $this->_model->delete(Helper::getPasteId());
  58. $original = $paste = Helper::getPasteWithAttachment(1, array('expire_date' => 1344803344));
  59. $paste['meta']['attachment'] = $paste['attachment'];
  60. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  61. unset($paste['attachment'], $paste['attachmentname']);
  62. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  63. $this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
  64. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  65. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
  66. $this->assertEquals($original, $this->_model->read(Helper::getPasteId()));
  67. }
  68. /**
  69. * pastes a-g are expired and should get deleted, x never expires and y-z expire in an hour
  70. */
  71. public function testPurge()
  72. {
  73. mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
  74. $expired = Helper::getPaste(2, array('expire_date' => 1344803344));
  75. $paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
  76. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z');
  77. $ids = array();
  78. foreach ($keys as $key) {
  79. $ids[$key] = hash('fnv164', $key);
  80. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  81. if (in_array($key, array('x', 'y', 'z'))) {
  82. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  83. } elseif ($key === 'x') {
  84. $this->assertTrue($this->_model->create($ids[$key], Helper::getPaste()), "store $key paste");
  85. } else {
  86. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  87. }
  88. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  89. }
  90. $this->_model->purge(10);
  91. foreach ($ids as $key => $id) {
  92. if (in_array($key, array('x', 'y', 'z'))) {
  93. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  94. $this->_model->delete($id);
  95. } else {
  96. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  97. }
  98. }
  99. }
  100. public function testErrorDetection()
  101. {
  102. $this->_model->delete(Helper::getPasteId());
  103. $paste = Helper::getPaste(2, array('expire' => "Invalid UTF-8 sequence: \xB1\x31"));
  104. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  105. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
  106. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
  107. $this->assertFalse($this->_model->setValue('foo', 'non existing namespace'), 'rejects setting value in non existing namespace');
  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. }