db.php 10 KB

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