1
0

FilesystemTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(['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(['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. [$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(['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(['expire_date' => 1344803344]);
  73. $paste = Helper::getPaste(['expire_date' => time() + 3600]);
  74. $keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z'];
  75. $ids = [];
  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, ['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, ['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. $error_log_setting = ini_get('error_log');
  102. $this->_model->delete(Helper::getPasteId());
  103. $paste = Helper::getPaste(['expire' => "Invalid UTF-8 sequence: \xB1\x31"]);
  104. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  105. ini_set('error_log', '/dev/null');
  106. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
  107. ini_set('error_log', $error_log_setting);
  108. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
  109. $this->assertFalse($this->_model->setValue('foo', 'non existing namespace'), 'rejects setting value in non existing namespace');
  110. }
  111. public function testCommentErrorDetection()
  112. {
  113. $error_log_setting = ini_get('error_log');
  114. $this->_model->delete(Helper::getPasteId());
  115. $data = Helper::getPaste();
  116. $comment = Helper::getComment(['icon' => "Invalid UTF-8 sequence: \xB1\x31"]);
  117. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  118. $this->assertTrue($this->_model->create(Helper::getPasteId(), $data), 'store new paste');
  119. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  120. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  121. ini_set('error_log', '/dev/null');
  122. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
  123. ini_set('error_log', $error_log_setting);
  124. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
  125. }
  126. public function testOldFilesGetConverted()
  127. {
  128. // generate 10 (default purge batch size) pastes in the old format
  129. $paste = Helper::getPaste();
  130. $comment = Helper::getComment();
  131. $commentid = Helper::getCommentId();
  132. $ids = [];
  133. for ($i = 0, $max = 10; $i < $max; ++$i) {
  134. $dataid = Helper::getRandomId();
  135. $storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) .
  136. DIRECTORY_SEPARATOR . substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
  137. $ids[$dataid] = $storagedir;
  138. if (!is_dir($storagedir)) {
  139. mkdir($storagedir, 0700, true);
  140. }
  141. file_put_contents($storagedir . $dataid, json_encode($paste));
  142. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  143. if (!is_dir($storagedir)) {
  144. mkdir($storagedir, 0700, true);
  145. }
  146. file_put_contents($storagedir . $dataid . '.' . $commentid . '.' . $dataid, json_encode($comment));
  147. }
  148. // check that all 10 pastes were converted after the purge
  149. $this->_model->purge(10);
  150. foreach ($ids as $dataid => $storagedir) {
  151. $dataid = (string) $dataid; // undue potential key cast, see https://www.php.net/manual/en/language.types.array.php
  152. $this->assertFileExists($storagedir . $dataid . '.php', "paste $dataid exists in new format");
  153. $this->assertFileDoesNotExist($storagedir . $dataid, "old format paste $dataid got removed");
  154. $this->assertTrue($this->_model->exists($dataid), "paste $dataid exists");
  155. $this->assertEquals($this->_model->read($dataid), $paste, "paste $dataid wasn't modified in the conversion");
  156. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  157. $this->assertFileExists($storagedir . $dataid . '.' . $commentid . '.' . $dataid . '.php', "comment of $dataid exists in new format");
  158. $this->assertFileDoesNotExist($storagedir . $dataid . '.' . $commentid . '.' . $dataid, "old format comment of $dataid got removed");
  159. $this->assertTrue($this->_model->existsComment($dataid, $dataid, $commentid), "comment in paste $dataid exists");
  160. $comment = $comment;
  161. $comment['id'] = $commentid;
  162. $comment['parentid'] = $dataid;
  163. $this->assertEquals($this->_model->readComments($dataid), [$comment['meta']['created'] => $comment], "comment of $dataid wasn't modified in the conversion");
  164. }
  165. }
  166. public function testValueFileErrorHandling()
  167. {
  168. define('VALID', 'valid content');
  169. foreach (['purge_limiter', 'salt', 'traffic_limiter'] as $namespace) {
  170. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . $namespace . '.php', 'invalid content');
  171. $model = new Filesystem(['dir' => $this->_invalidPath]);
  172. ob_start(); // hide "invalid content", when file gets included
  173. $this->assertEquals($model->getValue($namespace), '', 'empty default value returned, invalid content ignored');
  174. ob_end_clean();
  175. $this->assertTrue($model->setValue(VALID, $namespace), 'setting valid value');
  176. $this->assertEquals($model->getValue($namespace), VALID, 'valid value returned');
  177. }
  178. }
  179. }