FilesystemTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. public function testCorruptCommentsAreIgnored()
  67. {
  68. $pasteid = Helper::getPasteId();
  69. $commentid = Helper::getCommentId();
  70. $comment = Helper::getComment();
  71. $this->assertTrue($this->_model->createComment($pasteid, $pasteid, $commentid, $comment));
  72. $discussionPath = $this->_path . DIRECTORY_SEPARATOR . substr($pasteid, 0, 2) .
  73. DIRECTORY_SEPARATOR . substr($pasteid, 2, 2) . DIRECTORY_SEPARATOR .
  74. $pasteid . '.discussion' . DIRECTORY_SEPARATOR;
  75. file_put_contents(
  76. $discussionPath . $pasteid . '.ffffffffffffffff.' . $pasteid . '.php',
  77. Filesystem::PROTECTION_LINE . PHP_EOL . '{'
  78. );
  79. file_put_contents(
  80. $discussionPath . $pasteid . '.invalid.invalid.php',
  81. Filesystem::PROTECTION_LINE . PHP_EOL . json_encode($comment)
  82. );
  83. file_put_contents(
  84. $discussionPath . 'ffffffffffffffff.eeeeeeeeeeeeeeee.' . $pasteid . '.php',
  85. Filesystem::PROTECTION_LINE . PHP_EOL . json_encode($comment)
  86. );
  87. file_put_contents(
  88. $discussionPath . $pasteid . '.dddddddddddddddd.' . $pasteid,
  89. Filesystem::PROTECTION_LINE . PHP_EOL . json_encode($comment)
  90. );
  91. $errorLog = ini_get('error_log');
  92. ini_set('error_log', '/dev/null');
  93. $comments = $this->_model->readComments($pasteid);
  94. ini_set('error_log', $errorLog);
  95. $this->assertCount(1, $comments);
  96. $this->assertSame($commentid, current($comments)['id']);
  97. }
  98. /**
  99. * pastes a-g are expired and should get deleted, x never expires and y-z expire in an hour
  100. */
  101. public function testPurge()
  102. {
  103. mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
  104. $expired = Helper::getPaste(['expire_date' => 1344803344]);
  105. $paste = Helper::getPaste(['expire_date' => time() + 3600]);
  106. $keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z'];
  107. $ids = [];
  108. foreach ($keys as $key) {
  109. $ids[$key] = hash('fnv164', $key);
  110. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  111. if (in_array($key, ['x', 'y', 'z'])) {
  112. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  113. } elseif ($key === 'x') {
  114. $data = Helper::getPaste();
  115. $this->assertTrue($this->_model->create($ids[$key], $data), "store $key paste");
  116. } else {
  117. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  118. }
  119. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  120. }
  121. $this->_model->purge(10);
  122. foreach ($ids as $key => $id) {
  123. if (in_array($key, ['x', 'y', 'z'])) {
  124. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  125. $this->_model->delete($id);
  126. } else {
  127. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  128. }
  129. }
  130. }
  131. public function testErrorDetection()
  132. {
  133. $error_log_setting = ini_get('error_log');
  134. $this->_model->delete(Helper::getPasteId());
  135. $paste = Helper::getPaste(['expire' => "Invalid UTF-8 sequence: \xB1\x31"]);
  136. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  137. ini_set('error_log', '/dev/null');
  138. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
  139. ini_set('error_log', $error_log_setting);
  140. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
  141. $this->assertFalse($this->_model->setValue('foo', 'non existing namespace'), 'rejects setting value in non existing namespace');
  142. }
  143. public function testCommentErrorDetection()
  144. {
  145. $error_log_setting = ini_get('error_log');
  146. $this->_model->delete(Helper::getPasteId());
  147. $data = Helper::getPaste();
  148. $comment = Helper::getComment(['icon' => "Invalid UTF-8 sequence: \xB1\x31"]);
  149. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  150. $this->assertTrue($this->_model->create(Helper::getPasteId(), $data), 'store new paste');
  151. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  152. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  153. ini_set('error_log', '/dev/null');
  154. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
  155. ini_set('error_log', $error_log_setting);
  156. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
  157. }
  158. public function testOldFilesGetConverted()
  159. {
  160. // generate 10 (default purge batch size) pastes in the old format
  161. $paste = Helper::getPaste();
  162. $comment = Helper::getComment();
  163. $commentid = Helper::getCommentId();
  164. $ids = [];
  165. for ($i = 0, $max = 10; $i < $max; ++$i) {
  166. $dataid = Helper::getRandomId();
  167. $storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) .
  168. DIRECTORY_SEPARATOR . substr($dataid, 2, 2) . DIRECTORY_SEPARATOR;
  169. $ids[$dataid] = $storagedir;
  170. if (!is_dir($storagedir)) {
  171. mkdir($storagedir, 0700, true);
  172. }
  173. file_put_contents($storagedir . $dataid, json_encode($paste));
  174. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  175. if (!is_dir($storagedir)) {
  176. mkdir($storagedir, 0700, true);
  177. }
  178. file_put_contents($storagedir . $dataid . '.' . $commentid . '.' . $dataid, json_encode($comment));
  179. }
  180. // check that all 10 pastes were converted after the purge
  181. $oldUmask = umask(0000);
  182. try {
  183. $this->_model->purge(10);
  184. } finally {
  185. umask($oldUmask);
  186. }
  187. foreach ($ids as $dataid => $storagedir) {
  188. $dataid = (string) $dataid; // undue potential key cast, see https://www.php.net/manual/en/language.types.array.php
  189. $this->assertFileExists($storagedir . $dataid . '.php', "paste $dataid exists in new format");
  190. $this->assertSame(
  191. 0640,
  192. fileperms($storagedir . $dataid . '.php') & 0777,
  193. "converted paste $dataid has protected permissions"
  194. );
  195. $this->assertFileDoesNotExist($storagedir . $dataid, "old format paste $dataid got removed");
  196. $this->assertTrue($this->_model->exists($dataid), "paste $dataid exists");
  197. $this->assertEquals($this->_model->read($dataid), $paste, "paste $dataid wasn't modified in the conversion");
  198. $storagedir .= $dataid . '.discussion' . DIRECTORY_SEPARATOR;
  199. $this->assertFileExists($storagedir . $dataid . '.' . $commentid . '.' . $dataid . '.php', "comment of $dataid exists in new format");
  200. $this->assertSame(
  201. 0640,
  202. fileperms($storagedir . $dataid . '.' . $commentid . '.' . $dataid . '.php') & 0777,
  203. "converted comment of $dataid has protected permissions"
  204. );
  205. $this->assertFileDoesNotExist($storagedir . $dataid . '.' . $commentid . '.' . $dataid, "old format comment of $dataid got removed");
  206. $this->assertTrue($this->_model->existsComment($dataid, $dataid, $commentid), "comment in paste $dataid exists");
  207. $comment = $comment;
  208. $comment['id'] = $commentid;
  209. $comment['parentid'] = $dataid;
  210. $this->assertEquals($this->_model->readComments($dataid), [$comment['meta']['created'] => $comment], "comment of $dataid wasn't modified in the conversion");
  211. }
  212. }
  213. public function testValueFileErrorHandling()
  214. {
  215. define('VALID', 'valid content');
  216. foreach (['purge_limiter', 'salt', 'traffic_limiter'] as $namespace) {
  217. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . $namespace . '.php', 'invalid content');
  218. $model = new Filesystem(['dir' => $this->_invalidPath]);
  219. ob_start(); // hide "invalid content", when file gets included
  220. $this->assertEquals($model->getValue($namespace), '', 'empty default value returned, invalid content ignored');
  221. ob_end_clean();
  222. $this->assertTrue($model->setValue(VALID, $namespace), 'setting valid value');
  223. $this->assertEquals($model->getValue($namespace), VALID, 'valid value returned');
  224. }
  225. }
  226. }