FilesystemTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 = Filesystem::getInstance(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(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(json_decode(json_encode($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 = json_decode(json_encode(Helper::getComment()));
  43. $comment->id = Helper::getCommentId();
  44. $comment->parentid = Helper::getPasteId();
  45. $this->assertEquals(
  46. array($comment->meta->postdate => $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(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(json_decode(json_encode($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(array('expire_date' => 1344803344));
  75. $paste = Helper::getPaste(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] = substr(md5($key), 0, 16);
  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. /**
  101. * @expectedException Exception
  102. * @expectedExceptionCode 90
  103. */
  104. public function testErrorDetection()
  105. {
  106. $this->_model->delete(Helper::getPasteId());
  107. $paste = Helper::getPaste(array('formatter' => "Invalid UTF-8 sequence: \xB1\x31"));
  108. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  109. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
  110. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
  111. }
  112. /**
  113. * @expectedException Exception
  114. * @expectedExceptionCode 90
  115. */
  116. public function testCommentErrorDetection()
  117. {
  118. $this->_model->delete(Helper::getPasteId());
  119. $comment = Helper::getComment(array('formatter' => "Invalid UTF-8 sequence: \xB1\x31"));
  120. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  121. $this->assertTrue($this->_model->create(Helper::getPasteId(), Helper::getPaste()), 'store new paste');
  122. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  123. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
  124. $this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
  125. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
  126. }
  127. /**
  128. * @expectedException Exception
  129. * @expectedExceptionCode 10
  130. */
  131. public function testPermissionShenanigans()
  132. {
  133. // try creating an invalid path
  134. chmod($this->_invalidPath, 0000);
  135. Filesystem::getInstance(array('dir' => $this->_invalidPath . DIRECTORY_SEPARATOR . 'baz'));
  136. }
  137. /**
  138. * @expectedException Exception
  139. * @expectedExceptionCode 11
  140. */
  141. public function testPathShenanigans()
  142. {
  143. // try setting an invalid path
  144. chmod($this->_invalidPath, 0000);
  145. Filesystem::getInstance(array('dir' => $this->_invalidPath));
  146. }
  147. }