db.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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->_model->delete($ids[$key]);
  73. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  74. if (in_array($key, array('x', 'y', 'z')))
  75. {
  76. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  77. }
  78. else
  79. {
  80. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  81. }
  82. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  83. }
  84. $this->_model->purge(10);
  85. foreach ($ids as $key => $id)
  86. {
  87. if (in_array($key, array('x', 'y', 'z')))
  88. {
  89. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  90. $this->_model->delete($id);
  91. }
  92. else
  93. {
  94. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  95. }
  96. }
  97. }
  98. /**
  99. * @expectedException PDOException
  100. */
  101. public function testGetIbmInstance()
  102. {
  103. privatebin_db::getInstance(array(
  104. 'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
  105. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  106. ));
  107. }
  108. /**
  109. * @expectedException PDOException
  110. */
  111. public function testGetInformixInstance()
  112. {
  113. privatebin_db::getInstance(array(
  114. 'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
  115. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  116. ));
  117. }
  118. /**
  119. * @expectedException PDOException
  120. */
  121. public function testGetMssqlInstance()
  122. {
  123. privatebin_db::getInstance(array(
  124. 'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
  125. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  126. ));
  127. }
  128. /**
  129. * @expectedException PDOException
  130. */
  131. public function testGetMysqlInstance()
  132. {
  133. privatebin_db::getInstance(array(
  134. 'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
  135. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  136. ));
  137. }
  138. /**
  139. * @expectedException PDOException
  140. */
  141. public function testGetOciInstance()
  142. {
  143. privatebin_db::getInstance(array(
  144. 'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
  145. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  146. ));
  147. }
  148. /**
  149. * @expectedException PDOException
  150. */
  151. public function testGetPgsqlInstance()
  152. {
  153. privatebin_db::getInstance(array(
  154. 'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
  155. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  156. ));
  157. }
  158. /**
  159. * @expectedException Exception
  160. * @expectedExceptionCode 5
  161. */
  162. public function testGetFooInstance()
  163. {
  164. privatebin_db::getInstance(array(
  165. 'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
  166. ));
  167. }
  168. /**
  169. * @expectedException Exception
  170. * @expectedExceptionCode 6
  171. */
  172. public function testMissingDsn()
  173. {
  174. $options = $this->_options;
  175. unset($options['dsn']);
  176. privatebin_db::getInstance($options);
  177. }
  178. /**
  179. * @expectedException Exception
  180. * @expectedExceptionCode 6
  181. */
  182. public function testMissingUsr()
  183. {
  184. $options = $this->_options;
  185. unset($options['usr']);
  186. privatebin_db::getInstance($options);
  187. }
  188. /**
  189. * @expectedException Exception
  190. * @expectedExceptionCode 6
  191. */
  192. public function testMissingPwd()
  193. {
  194. $options = $this->_options;
  195. unset($options['pwd']);
  196. privatebin_db::getInstance($options);
  197. }
  198. /**
  199. * @expectedException Exception
  200. * @expectedExceptionCode 6
  201. */
  202. public function testMissingOpt()
  203. {
  204. $options = $this->_options;
  205. unset($options['opt']);
  206. privatebin_db::getInstance($options);
  207. }
  208. public function testOldAttachments()
  209. {
  210. mkdir(PATH . 'data');
  211. $path = PATH . 'data' . DIRECTORY_SEPARATOR . 'attachement-test.sq3';
  212. @unlink($path);
  213. $this->_options['dsn'] = 'sqlite:' . $path;
  214. $this->_options['tbl'] = 'bar_';
  215. $model = privatebin_db::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(PATH . 'data');
  245. }
  246. public function testTableUpgrade()
  247. {
  248. mkdir(PATH . 'data');
  249. $path = PATH . 'data' . DIRECTORY_SEPARATOR . 'db-test.sq3';
  250. @unlink($path);
  251. $this->_options['dsn'] = 'sqlite:' . $path;
  252. $this->_options['tbl'] = 'foo_';
  253. $db = new PDO(
  254. $this->_options['dsn'],
  255. $this->_options['usr'],
  256. $this->_options['pwd'],
  257. $this->_options['opt']
  258. );
  259. $db->exec(
  260. 'CREATE TABLE foo_paste ( ' .
  261. 'dataid CHAR(16), ' .
  262. 'data TEXT, ' .
  263. 'postdate INT, ' .
  264. 'expiredate INT, ' .
  265. 'opendiscussion INT, ' .
  266. 'burnafterreading INT );'
  267. );
  268. $db->exec(
  269. 'CREATE TABLE foo_comment ( ' .
  270. "dataid CHAR(16) NOT NULL, " .
  271. 'pasteid CHAR(16), ' .
  272. 'parentid CHAR(16), ' .
  273. 'data BLOB, ' .
  274. 'nickname BLOB, ' .
  275. 'vizhash BLOB, ' .
  276. "postdate INT );"
  277. );
  278. privatebin_db::getInstance($this->_options);
  279. helper::rmdir(PATH . 'data');
  280. }
  281. }