DatabaseTest.php 11 KB

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