DatabaseTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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)) {
  211. unlink($path);
  212. }
  213. $this->_options['dsn'] = 'sqlite:' . $path;
  214. $this->_options['tbl'] = 'bar_';
  215. $model = Database::getInstance($this->_options);
  216. $original = $paste = Helper::getPasteWithAttachment(array('expire_date' => 1344803344));
  217. $paste['meta']['attachment'] = $paste['attachment'];
  218. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  219. unset($paste['attachment'], $paste['attachmentname']);
  220. $meta = $paste['meta'];
  221. $db = new PDO(
  222. $this->_options['dsn'],
  223. $this->_options['usr'],
  224. $this->_options['pwd'],
  225. $this->_options['opt']
  226. );
  227. $statement = $db->prepare('INSERT INTO bar_paste VALUES(?,?,?,?,?,?,?,?,?)');
  228. $statement->execute(
  229. array(
  230. Helper::getPasteId(),
  231. $paste['data'],
  232. $paste['meta']['postdate'],
  233. 1344803344,
  234. 0,
  235. 0,
  236. json_encode($meta),
  237. null,
  238. null,
  239. )
  240. );
  241. $statement->closeCursor();
  242. $this->assertTrue($model->exists(Helper::getPasteId()), 'paste exists after storing it');
  243. $this->assertEquals(json_decode(json_encode($original)), $model->read(Helper::getPasteId()));
  244. Helper::rmDir($this->_path);
  245. }
  246. public function testTableUpgrade()
  247. {
  248. mkdir($this->_path);
  249. $path = $this->_path . DIRECTORY_SEPARATOR . 'db-test.sq3';
  250. if (is_file($path)) {
  251. unlink($path);
  252. }
  253. $this->_options['dsn'] = 'sqlite:' . $path;
  254. $this->_options['tbl'] = 'foo_';
  255. $db = new PDO(
  256. $this->_options['dsn'],
  257. $this->_options['usr'],
  258. $this->_options['pwd'],
  259. $this->_options['opt']
  260. );
  261. $db->exec(
  262. 'CREATE TABLE foo_paste ( ' .
  263. 'dataid CHAR(16), ' .
  264. 'data TEXT, ' .
  265. 'postdate INT, ' .
  266. 'expiredate INT, ' .
  267. 'opendiscussion INT, ' .
  268. 'burnafterreading INT );'
  269. );
  270. $db->exec(
  271. 'CREATE TABLE foo_comment ( ' .
  272. 'dataid CHAR(16) NOT NULL, ' .
  273. 'pasteid CHAR(16), ' .
  274. 'parentid CHAR(16), ' .
  275. 'data BLOB, ' .
  276. 'nickname BLOB, ' .
  277. 'vizhash BLOB, ' .
  278. 'postdate INT );'
  279. );
  280. $this->assertInstanceOf(Database::class, Database::getInstance($this->_options));
  281. // check if version number was upgraded in created configuration table
  282. $statement = $db->prepare('SELECT value FROM foo_config WHERE id LIKE ?');
  283. $statement->execute(array('VERSION'));
  284. $result = $statement->fetch(PDO::FETCH_ASSOC);
  285. $statement->closeCursor();
  286. $this->assertEquals(PrivateBin::VERSION, $result['value']);
  287. Helper::rmDir($this->_path);
  288. }
  289. }