DatabaseTest.php 14 KB

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