1
0

DatabaseTest.php 12 KB

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