db.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. class privatebin_dbTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_model;
  5. private $_options = array(
  6. 'dsn' => 'sqlite::memory:',
  7. 'usr' => null,
  8. 'pwd' => null,
  9. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  10. );
  11. public function setUp()
  12. {
  13. /* Setup Routine */
  14. $this->_model = privatebin_db::getInstance($this->_options);
  15. }
  16. public function tearDown()
  17. {
  18. /* Tear Down Routine */
  19. if (is_dir(PATH . 'data')) helper::rmdir(PATH . 'data');
  20. }
  21. public function testDatabaseBasedDataStoreWorks()
  22. {
  23. $this->_model->delete(helper::getPasteId());
  24. // storing pastes
  25. $paste = helper::getPaste(array('expire_date' => 1344803344));
  26. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  27. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  28. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  29. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  30. $this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(helper::getPasteId()));
  31. // storing comments
  32. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment does not yet exist');
  33. $this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment');
  34. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
  35. $comment = json_decode(json_encode(helper::getComment()));
  36. $comment->id = helper::getCommentId();
  37. $comment->parentid = helper::getPasteId();
  38. $this->assertEquals(
  39. array($comment->meta->postdate => $comment),
  40. $this->_model->readComments(helper::getPasteId())
  41. );
  42. // deleting pastes
  43. $this->_model->delete(helper::getPasteId());
  44. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  45. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment was deleted with paste');
  46. $this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
  47. }
  48. public function testDatabaseBasedAttachmentStoreWorks()
  49. {
  50. $this->_model->delete(helper::getPasteId());
  51. $original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
  52. $paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true;
  53. $paste['meta']['attachment'] = $paste['attachment'];
  54. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  55. unset($paste['attachment'], $paste['attachmentname']);
  56. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  57. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  58. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  59. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  60. $this->assertEquals(json_decode(json_encode($original)), $this->_model->read(helper::getPasteId()));
  61. }
  62. public function testPurge()
  63. {
  64. $this->_model->delete(helper::getPasteId());
  65. $expired = helper::getPaste(array('expire_date' => 1344803344));
  66. $paste = helper::getPaste(array('expire_date' => time() + 3600));
  67. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
  68. $ids = array();
  69. foreach ($keys as $key)
  70. {
  71. $ids[$key] = substr(md5($key), 0, 16);
  72. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  73. if (in_array($key, array('x', 'y', 'z')))
  74. {
  75. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  76. }
  77. else
  78. {
  79. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  80. }
  81. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  82. }
  83. $this->_model->purge(10);
  84. foreach ($ids as $key => $id)
  85. {
  86. if (in_array($key, array('x', 'y', 'z')))
  87. {
  88. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after purge");
  89. }
  90. else
  91. {
  92. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key was purged");
  93. }
  94. }
  95. }
  96. /**
  97. * @expectedException PDOException
  98. */
  99. public function testGetIbmInstance()
  100. {
  101. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::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. privatebin_db::getInstance($options);
  205. }
  206. public function testTableUpgrade()
  207. {
  208. mkdir(PATH . 'data');
  209. $path = PATH . 'data/db-test.sq3';
  210. @unlink($path);
  211. $this->_options['dsn'] = 'sqlite:' . $path;
  212. $this->_options['tbl'] = 'foo_';
  213. $db = new PDO(
  214. $this->_options['dsn'],
  215. $this->_options['usr'],
  216. $this->_options['pwd'],
  217. $this->_options['opt']
  218. );
  219. $db->exec(
  220. 'CREATE TABLE foo_paste ( ' .
  221. 'dataid CHAR(16), ' .
  222. 'data TEXT, ' .
  223. 'postdate INT, ' .
  224. 'expiredate INT, ' .
  225. 'opendiscussion INT, ' .
  226. 'burnafterreading INT );'
  227. );
  228. privatebin_db::getInstance($this->_options);
  229. @unlink($path);
  230. }
  231. }