db.php 10 KB

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