DatabaseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Controller;
  4. use PrivateBin\Data\Database;
  5. class DatabaseTest extends TestCase
  6. {
  7. private $_model;
  8. private $_path;
  9. private $_options = array(
  10. 'dsn' => 'sqlite::memory:',
  11. 'usr' => null,
  12. 'pwd' => null,
  13. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  14. );
  15. public function setUp(): void
  16. {
  17. /* Setup Routine */
  18. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  19. $this->_model = Database::getInstance($this->_options);
  20. }
  21. public function tearDown(): void
  22. {
  23. /* Tear Down Routine */
  24. if (is_dir($this->_path)) {
  25. Helper::rmDir($this->_path);
  26. }
  27. }
  28. public function testDatabaseBasedDataStoreWorks()
  29. {
  30. $this->_model->delete(Helper::getPasteId());
  31. // storing pastes
  32. $paste = Helper::getPaste();
  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. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'v1 comment does not yet exist');
  40. $this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment(1)) !== false, 'store v1 comment');
  41. $this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'v1 comment exists after storing it');
  42. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId()), 'v2 comment does not yet exist');
  43. $this->assertTrue($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId(), Helper::getComment(2)) !== false, 'store v2 comment');
  44. $this->assertTrue($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getPasteId()), 'v2 comment exists after storing it');
  45. $comment1 = Helper::getComment(1);
  46. $comment1['id'] = Helper::getCommentId();
  47. $comment1['parentid'] = Helper::getPasteId();
  48. $comment2 = Helper::getComment(2);
  49. $comment2['id'] = Helper::getPasteId();
  50. $comment2['parentid'] = Helper::getPasteId();
  51. $this->assertEquals(
  52. array(
  53. $comment1['meta']['postdate'] => $comment1,
  54. $comment2['meta']['created'] . '.1' => $comment2,
  55. ),
  56. $this->_model->readComments(Helper::getPasteId())
  57. );
  58. // deleting pastes
  59. $this->_model->delete(Helper::getPasteId());
  60. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
  61. $this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment was deleted with paste');
  62. $this->assertFalse($this->_model->read(Helper::getPasteId()), 'paste can no longer be found');
  63. }
  64. public function testDatabaseBasedAttachmentStoreWorks()
  65. {
  66. // this assumes a version 1 formatted paste
  67. $this->_model->delete(Helper::getPasteId());
  68. $original = $paste = Helper::getPasteWithAttachment(1, array('expire_date' => 1344803344));
  69. $paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true;
  70. $paste['meta']['attachment'] = $paste['attachment'];
  71. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  72. unset($paste['attachment'], $paste['attachmentname']);
  73. $this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
  74. $this->assertTrue($this->_model->create(Helper::getPasteId(), $paste), 'store new paste');
  75. $this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
  76. $this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store the same paste twice');
  77. $this->assertEquals($original, $this->_model->read(Helper::getPasteId()));
  78. }
  79. /**
  80. * pastes a-g are expired and should get deleted, x never expires and y-z expire in an hour
  81. */
  82. public function testPurge()
  83. {
  84. $this->_model->delete(Helper::getPasteId());
  85. $expired = Helper::getPaste(2, array('expire_date' => 1344803344));
  86. $paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
  87. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'x', 'y', 'z');
  88. $ids = array();
  89. foreach ($keys as $key) {
  90. $ids[$key] = hash('fnv164', $key);
  91. $this->_model->delete($ids[$key]);
  92. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  93. if (in_array($key, array('y', 'z'))) {
  94. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  95. } elseif ($key === 'x') {
  96. $this->assertTrue($this->_model->create($ids[$key], Helper::getPaste()), "store $key paste");
  97. } else {
  98. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  99. }
  100. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  101. }
  102. $this->_model->purge(10);
  103. foreach ($ids as $key => $id) {
  104. if (in_array($key, array('x', 'y', 'z'))) {
  105. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  106. $this->_model->delete($id);
  107. } else {
  108. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  109. }
  110. }
  111. }
  112. public function testGetIbmInstance()
  113. {
  114. $this->expectException(PDOException::class);
  115. Database::getInstance(array(
  116. 'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
  117. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  118. ));
  119. }
  120. public function testGetInformixInstance()
  121. {
  122. $this->expectException(PDOException::class);
  123. Database::getInstance(array(
  124. 'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
  125. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  126. ));
  127. }
  128. public function testGetMssqlInstance()
  129. {
  130. $this->expectException(PDOException::class);
  131. Database::getInstance(array(
  132. 'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
  133. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  134. ));
  135. }
  136. public function testGetMysqlInstance()
  137. {
  138. $this->expectException(PDOException::class);
  139. Database::getInstance(array(
  140. 'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
  141. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  142. ));
  143. }
  144. public function testGetOciInstance()
  145. {
  146. $this->expectException(PDOException::class);
  147. Database::getInstance(array(
  148. 'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
  149. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  150. ));
  151. }
  152. public function testGetPgsqlInstance()
  153. {
  154. $this->expectException(PDOException::class);
  155. Database::getInstance(array(
  156. 'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
  157. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  158. ));
  159. }
  160. public function testGetFooInstance()
  161. {
  162. $this->expectException(Exception::class);
  163. $this->expectExceptionCode(5);
  164. Database::getInstance(array(
  165. 'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null,
  166. ));
  167. }
  168. public function testMissingDsn()
  169. {
  170. $options = $this->_options;
  171. unset($options['dsn']);
  172. $this->expectException(Exception::class);
  173. $this->expectExceptionCode(6);
  174. Database::getInstance($options);
  175. }
  176. public function testMissingUsr()
  177. {
  178. $options = $this->_options;
  179. unset($options['usr']);
  180. $this->expectException(Exception::class);
  181. $this->expectExceptionCode(6);
  182. Database::getInstance($options);
  183. }
  184. public function testMissingPwd()
  185. {
  186. $options = $this->_options;
  187. unset($options['pwd']);
  188. $this->expectException(Exception::class);
  189. $this->expectExceptionCode(6);
  190. Database::getInstance($options);
  191. }
  192. public function testMissingOpt()
  193. {
  194. $options = $this->_options;
  195. unset($options['opt']);
  196. $this->expectException(Exception::class);
  197. $this->expectExceptionCode(6);
  198. Database::getInstance($options);
  199. }
  200. public function testOldAttachments()
  201. {
  202. mkdir($this->_path);
  203. $path = $this->_path . DIRECTORY_SEPARATOR . 'attachement-test.sq3';
  204. if (is_file($path)) {
  205. unlink($path);
  206. }
  207. $this->_options['dsn'] = 'sqlite:' . $path;
  208. $this->_options['tbl'] = 'bar_';
  209. $model = Database::getInstance($this->_options);
  210. $original = $paste = Helper::getPasteWithAttachment(1, array('expire_date' => 1344803344));
  211. $meta = $paste['meta'];
  212. $meta['attachment'] = $paste['attachment'];
  213. $meta['attachmentname'] = $paste['attachmentname'];
  214. unset($paste['attachment'], $paste['attachmentname']);
  215. $db = new PDO(
  216. $this->_options['dsn'],
  217. $this->_options['usr'],
  218. $this->_options['pwd'],
  219. $this->_options['opt']
  220. );
  221. $statement = $db->prepare('INSERT INTO bar_paste VALUES(?,?,?,?,?,?,?,?,?)');
  222. $statement->execute(
  223. array(
  224. Helper::getPasteId(),
  225. $paste['data'],
  226. $paste['meta']['postdate'],
  227. $paste['meta']['expire_date'],
  228. 0,
  229. 0,
  230. json_encode($meta),
  231. null,
  232. null,
  233. )
  234. );
  235. $statement->closeCursor();
  236. $this->assertTrue($model->exists(Helper::getPasteId()), 'paste exists after storing it');
  237. $this->assertEquals($original, $model->read(Helper::getPasteId()));
  238. Helper::rmDir($this->_path);
  239. }
  240. public function testTableUpgrade()
  241. {
  242. mkdir($this->_path);
  243. $path = $this->_path . DIRECTORY_SEPARATOR . 'db-test.sq3';
  244. if (is_file($path)) {
  245. unlink($path);
  246. }
  247. $this->_options['dsn'] = 'sqlite:' . $path;
  248. $this->_options['tbl'] = 'foo_';
  249. $db = new PDO(
  250. $this->_options['dsn'],
  251. $this->_options['usr'],
  252. $this->_options['pwd'],
  253. $this->_options['opt']
  254. );
  255. $db->exec(
  256. 'CREATE TABLE foo_paste ( ' .
  257. 'dataid CHAR(16), ' .
  258. 'data TEXT, ' .
  259. 'postdate INT, ' .
  260. 'expiredate INT, ' .
  261. 'opendiscussion INT, ' .
  262. 'burnafterreading INT );'
  263. );
  264. $db->exec(
  265. 'CREATE TABLE foo_comment ( ' .
  266. 'dataid CHAR(16) NOT NULL, ' .
  267. 'pasteid CHAR(16), ' .
  268. 'parentid CHAR(16), ' .
  269. 'data BLOB, ' .
  270. 'nickname BLOB, ' .
  271. 'vizhash BLOB, ' .
  272. 'postdate INT );'
  273. );
  274. $this->assertInstanceOf('PrivateBin\\Data\\Database', Database::getInstance($this->_options));
  275. // check if version number was upgraded in created configuration table
  276. $statement = $db->prepare('SELECT value FROM foo_config WHERE id LIKE ?');
  277. $statement->execute(array('VERSION'));
  278. $result = $statement->fetch(PDO::FETCH_ASSOC);
  279. $statement->closeCursor();
  280. $this->assertEquals(Controller::VERSION, $result['value']);
  281. Helper::rmDir($this->_path);
  282. }
  283. }