DatabaseTest.php 11 KB

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