FilesystemTest.php 11 KB

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