FilesystemTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php declare(strict_types=1);
  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(): void
  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 = new Filesystem(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(): void
  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(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. $comment = Helper::getComment();
  40. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  41. $this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'store comment');
  42. $this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment exists after storing it');
  43. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store the same comment twice');
  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::getPaste(array('expire_date' => 1344803344));
  60. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  61. $this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
  62. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  63. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
  64. $this->assertEquals($original, $this->_model->read(Helper::getPasteId()));
  65. }
  66. /**
  67. * pastes a-g are expired and should get deleted, x never expires and y-z expire in an hour
  68. */
  69. public function testPurge()
  70. {
  71. mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
  72. $expired = Helper::getPaste(array('expire_date' => 1344803344));
  73. $paste = Helper::getPaste(array('expire_date' => time() + 3600));
  74. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z');
  75. $ids = array();
  76. foreach ($keys as $key) {
  77. $ids[$key] = hash('fnv164', $key);
  78. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  79. if (in_array($key, array('x', 'y', 'z'))) {
  80. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  81. } elseif ($key === 'x') {
  82. $data = Helper::getPaste();
  83. $this->assertTrue($this->_model->create($ids[$key], $data), "store $key paste");
  84. } else {
  85. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  86. }
  87. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  88. }
  89. $this->_model->purge(10);
  90. foreach ($ids as $key => $id) {
  91. if (in_array($key, array('x', 'y', 'z'))) {
  92. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  93. $this->_model->delete($id);
  94. } else {
  95. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  96. }
  97. }
  98. }
  99. public function testErrorDetection()
  100. {
  101. $this->_model->delete(Helper::getPasteId());
  102. $paste = Helper::getPaste(array('expire' => "Invalid UTF-8 sequence: \xB1\x31"));
  103. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  104. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
  105. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
  106. $this->assertFalse($this->_model->setValue('foo', 'non existing namespace'), 'rejects setting value in non existing namespace');
  107. }
  108. public function testCommentErrorDetection()
  109. {
  110. $this->_model->delete(Helper::getPasteId());
  111. $data = Helper::getPaste();
  112. $comment = Helper::getComment(array('icon' => "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(), $data), '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. $dataid = Helper::getRandomId();
  129. $storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) .
  130. DIRECTORY_SEPARATOR . substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
  131. $ids[$dataid] = $storagedir;
  132. if (!is_dir($storagedir)) {
  133. mkdir($storagedir, 0700, true);
  134. }
  135. file_put_contents($storagedir . $dataid, json_encode($paste));
  136. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  137. if (!is_dir($storagedir)) {
  138. mkdir($storagedir, 0700, true);
  139. }
  140. file_put_contents($storagedir . $dataid . '.' . $commentid . '.' . $dataid, json_encode($comment));
  141. }
  142. // check that all 10 pastes were converted after the purge
  143. $this->_model->purge(10);
  144. foreach ($ids as $dataid => $storagedir) {
  145. $dataid = (string) $dataid; // undue potential key cast, see https://www.php.net/manual/en/language.types.array.php
  146. $this->assertFileExists($storagedir . $dataid . '.php', "paste $dataid exists in new format");
  147. $this->assertFileDoesNotExist($storagedir . $dataid, "old format paste $dataid got removed");
  148. $this->assertTrue($this->_model->exists($dataid), "paste $dataid exists");
  149. $this->assertEquals($this->_model->read($dataid), $paste, "paste $dataid wasn't modified in the conversion");
  150. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  151. $this->assertFileExists($storagedir . $dataid . '.' . $commentid . '.' . $dataid . '.php', "comment of $dataid exists in new format");
  152. $this->assertFileDoesNotExist($storagedir . $dataid . '.' . $commentid . '.' . $dataid, "old format comment of $dataid got removed");
  153. $this->assertTrue($this->_model->existsComment($dataid, $dataid, $commentid), "comment in paste $dataid exists");
  154. $comment = $comment;
  155. $comment['id'] = $commentid;
  156. $comment['parentid'] = $dataid;
  157. $this->assertEquals($this->_model->readComments($dataid), array($comment['meta']['created'] => $comment), "comment of $dataid wasn't modified in the conversion");
  158. }
  159. }
  160. public function testValueFileErrorHandling()
  161. {
  162. define('VALID', 'valid content');
  163. foreach (array('purge_limiter', 'salt', 'traffic_limiter') as $namespace) {
  164. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . $namespace . '.php', 'invalid content');
  165. $model = new Filesystem(array('dir' => $this->_invalidPath));
  166. ob_start(); // hide "invalid content", when file gets included
  167. $this->assertEquals($model->getValue($namespace), '', 'empty default value returned, invalid content ignored');
  168. ob_end_clean();
  169. $this->assertTrue($model->setValue(VALID, $namespace), 'setting valid value');
  170. $this->assertEquals($model->getValue($namespace), VALID, 'valid value returned');
  171. }
  172. }
  173. }