DatabaseTest.php 13 KB

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