DatabaseTest.php 13 KB

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