ModelTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php declare(strict_types=1);
  2. use Jdenticon\Identicon;
  3. use PHPUnit\Framework\TestCase;
  4. use PrivateBin\Configuration;
  5. use PrivateBin\Data\Database;
  6. use PrivateBin\Model;
  7. use PrivateBin\Model\Comment;
  8. use PrivateBin\Model\Paste;
  9. use PrivateBin\Persistence\ServerSalt;
  10. use PrivateBin\Persistence\TrafficLimiter;
  11. use PrivateBin\Vizhash16x16;
  12. class ModelTest extends TestCase
  13. {
  14. private $_conf;
  15. private $_model;
  16. protected $_path;
  17. public function setUp(): void
  18. {
  19. /* Setup Routine */
  20. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  21. if (!is_dir($this->_path)) {
  22. mkdir($this->_path);
  23. }
  24. $options = parse_ini_file(CONF_SAMPLE, true);
  25. $options['purge']['limit'] = 0;
  26. $options['model'] = [
  27. 'class' => 'Database',
  28. ];
  29. $options['model_options'] = [
  30. 'dsn' => 'sqlite::memory:',
  31. 'usr' => null,
  32. 'pwd' => null,
  33. 'opt' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
  34. ];
  35. Helper::confBackup();
  36. Helper::createIniFile(CONF, $options);
  37. ServerSalt::setStore(new Database($options['model_options']));
  38. $this->_conf = new Configuration;
  39. $this->_model = new Model($this->_conf);
  40. $_SERVER['REMOTE_ADDR'] = '::1';
  41. }
  42. public function tearDown(): void
  43. {
  44. /* Tear Down Routine */
  45. unlink(CONF);
  46. Helper::confRestore();
  47. Helper::rmDir($this->_path);
  48. }
  49. public function testBasicWorkflow()
  50. {
  51. // storing pastes
  52. $pasteData = Helper::getPastePost();
  53. unset($pasteData['meta']['created'], $pasteData['meta']['salt']);
  54. $this->_model->getPaste(Helper::getPasteId())->delete();
  55. $paste = $this->_model->getPaste(Helper::getPasteId());
  56. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  57. $paste = $this->_model->getPaste();
  58. $paste->setData($pasteData);
  59. $paste->store();
  60. $paste = $this->_model->getPaste(Helper::getPasteId());
  61. $this->assertTrue($paste->exists(), 'paste exists after storing it');
  62. $paste = $paste->get();
  63. unset(
  64. $pasteData['meta'],
  65. $paste['meta'],
  66. $paste['comments'],
  67. $paste['comment_count'],
  68. $paste['comment_offset'],
  69. $paste['@context']
  70. );
  71. $this->assertEquals($pasteData, $paste);
  72. // storing comments
  73. $commentData = Helper::getCommentPost();
  74. $paste = $this->_model->getPaste(Helper::getPasteId());
  75. $comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
  76. $this->assertFalse($comment->exists(), 'comment does not yet exist');
  77. $comment = $paste->getComment(Helper::getPasteId());
  78. $comment->setData($commentData);
  79. $comment->store();
  80. $comments = $this->_model->getPaste(Helper::getPasteId())->get()['comments'];
  81. $this->assertTrue(count($comments) === 1, 'comment exists after storing it');
  82. $commentData['id'] = Helper::getCommentId();
  83. $commentData['meta']['created'] = current($comments)['meta']['created'];
  84. $commentData['meta']['icon'] = current($comments)['meta']['icon'];
  85. $this->assertEquals($commentData, current($comments));
  86. // deleting pastes
  87. $this->_model->getPaste(Helper::getPasteId())->delete();
  88. $paste = $this->_model->getPaste(Helper::getPasteId());
  89. $this->assertFalse($paste->exists(), 'paste successfully deleted');
  90. $this->assertEquals([], $paste->getComments(), 'comment was deleted with paste');
  91. }
  92. public function testCommentDefaults()
  93. {
  94. $class = 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model');
  95. $comment = new Comment(
  96. $this->_conf,
  97. new $class(
  98. $this->_conf->getSection('model_options')
  99. )
  100. );
  101. $paste = $this->_model->getPaste(Helper::getPasteId());
  102. $comment->setPaste($paste);
  103. $this->assertEquals(Helper::getPasteId(), $comment->getParentId(), 'comment parent ID gets initialized to paste ID');
  104. }
  105. public function testPasteDuplicate()
  106. {
  107. $pasteData = Helper::getPastePost();
  108. $this->_model->getPaste(Helper::getPasteId())->delete();
  109. $paste = $this->_model->getPaste();
  110. $paste->setData($pasteData);
  111. $paste->store();
  112. $paste = $this->_model->getPaste();
  113. $paste->setData($pasteData);
  114. $this->expectException(Exception::class);
  115. $this->expectExceptionCode(75);
  116. $paste->store();
  117. }
  118. public function testStoreFail()
  119. {
  120. $path = $this->_path . DIRECTORY_SEPARATOR . 'model-store-test.sq3';
  121. if (is_file($path)) {
  122. unlink($path);
  123. }
  124. $options = parse_ini_file(CONF_SAMPLE, true);
  125. $options['purge']['limit'] = 0;
  126. $options['model'] = [
  127. 'class' => 'Database',
  128. ];
  129. $options['model_options'] = [
  130. 'dsn' => 'sqlite:' . $path,
  131. 'usr' => null,
  132. 'pwd' => null,
  133. 'opt' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
  134. ];
  135. Helper::createIniFile(CONF, $options);
  136. $model = new Model(new Configuration);
  137. $pasteData = Helper::getPastePost();
  138. $model->getPaste(Helper::getPasteId())->delete();
  139. $model->getPaste(Helper::getPasteId())->exists();
  140. $db = new PDO(
  141. $options['model_options']['dsn'],
  142. $options['model_options']['usr'],
  143. $options['model_options']['pwd'],
  144. $options['model_options']['opt']
  145. );
  146. $statement = $db->prepare('DROP TABLE paste');
  147. $statement->execute();
  148. $statement->closeCursor();
  149. $paste = $model->getPaste();
  150. $paste->setData($pasteData);
  151. $this->expectException(Exception::class);
  152. $this->expectExceptionCode(76);
  153. $paste->store();
  154. }
  155. public function testCommentStoreFail()
  156. {
  157. $path = $this->_path . DIRECTORY_SEPARATOR . 'model-test.sq3';
  158. if (is_file($path)) {
  159. unlink($path);
  160. }
  161. $options = parse_ini_file(CONF_SAMPLE, true);
  162. $options['purge']['limit'] = 0;
  163. $options['model'] = [
  164. 'class' => 'Database',
  165. ];
  166. $options['model_options'] = [
  167. 'dsn' => 'sqlite:' . $path,
  168. 'usr' => null,
  169. 'pwd' => null,
  170. 'opt' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
  171. ];
  172. Helper::createIniFile(CONF, $options);
  173. $model = new Model(new Configuration);
  174. $pasteData = Helper::getPastePost();
  175. $commentData = Helper::getCommentPost();
  176. $model->getPaste(Helper::getPasteId())->delete();
  177. $paste = $model->getPaste();
  178. $paste->setData($pasteData);
  179. $paste->store();
  180. $this->assertTrue($paste->exists(), 'paste exists before creating comment');
  181. $comment = $paste->getComment(Helper::getPasteId());
  182. $comment->setData($commentData);
  183. $db = new PDO(
  184. $options['model_options']['dsn'],
  185. $options['model_options']['usr'],
  186. $options['model_options']['pwd'],
  187. $options['model_options']['opt']
  188. );
  189. $statement = $db->prepare('DROP TABLE comment');
  190. $statement->execute();
  191. $statement->closeCursor();
  192. $this->expectException(Exception::class);
  193. $this->expectExceptionCode(70);
  194. $comment->store();
  195. }
  196. public function testCommentRequireIds()
  197. {
  198. $options = parse_ini_file(CONF, true);
  199. $options['main']['discussionrequireids'] = 'true';
  200. Helper::createIniFile(CONF, $options);
  201. $model = new Model(new Configuration);
  202. $pasteData = Helper::getPastePost();
  203. $commentData = Helper::getCommentPost();
  204. $model->getPaste(Helper::getPasteId())->delete();
  205. $paste = $model->getPaste();
  206. $paste->setData($pasteData);
  207. $paste->store();
  208. $commentData['adata'][] = $commentData['pasteid'];
  209. $commentData['adata'][] = $commentData['parentid'];
  210. $comment = $paste->getComment(Helper::getPasteId());
  211. $comment->setData($commentData);
  212. $comment->store();
  213. $this->assertTrue($comment->exists(), 'comment with authenticated IDs can be stored');
  214. $commentData = Helper::getCommentPost();
  215. $commentData['ct'] = base64_encode('other ciphertext');
  216. $comment = $paste->getComment(Helper::getPasteId());
  217. $this->expectException(Exception::class);
  218. $this->expectExceptionCode(77);
  219. $comment->setData($commentData);
  220. }
  221. public function testCommentDuplicate()
  222. {
  223. $pasteData = Helper::getPastePost();
  224. $commentData = Helper::getCommentPost();
  225. $this->_model->getPaste(Helper::getPasteId())->delete();
  226. $paste = $this->_model->getPaste();
  227. $paste->setData($pasteData);
  228. $paste->store();
  229. $comment = $paste->getComment(Helper::getPasteId());
  230. $comment->setData($commentData);
  231. $comment->store();
  232. $comment = $paste->getComment(Helper::getPasteId());
  233. $comment->setData($commentData);
  234. $this->expectException(Exception::class);
  235. $this->expectExceptionCode(69);
  236. $comment->store();
  237. }
  238. public function testCommentDuplicateOtherParent()
  239. {
  240. $pasteData = Helper::getPastePost();
  241. $commentData = Helper::getCommentPost();
  242. $this->_model->getPaste(Helper::getPasteId())->delete();
  243. $paste = $this->_model->getPaste();
  244. $paste->setData($pasteData);
  245. $paste->store();
  246. $comment = $paste->getComment(Helper::getPasteId());
  247. $comment->setData($commentData);
  248. $comment->store();
  249. $firstCommentId = $comment->getId();
  250. // reply to the first comment, using a different ciphertext
  251. $replyData = $commentData;
  252. $replyData['ct'] = base64_encode('reply ciphertext');
  253. $replyData['parentid'] = $firstCommentId;
  254. $reply = $paste->getComment($firstCommentId);
  255. $reply->setData($replyData);
  256. $reply->store();
  257. $this->assertTrue($reply->exists(), 'reply to existing comment can be stored');
  258. // replay of the first comment under the reply as parent
  259. $commentData['parentid'] = $reply->getId();
  260. $comment = $paste->getComment($reply->getId());
  261. $comment->setData($commentData);
  262. $this->expectException(Exception::class);
  263. $this->expectExceptionCode(69);
  264. $comment->store();
  265. }
  266. public function testCommentNonexistentParent()
  267. {
  268. $pasteData = Helper::getPastePost();
  269. $commentData = Helper::getCommentPost();
  270. $commentData['parentid'] = '1111111111111111';
  271. $this->_model->getPaste(Helper::getPasteId())->delete();
  272. $paste = $this->_model->getPaste();
  273. $paste->setData($pasteData);
  274. $paste->store();
  275. $comment = $paste->getComment('1111111111111111');
  276. $comment->setData($commentData);
  277. $this->expectException(Exception::class);
  278. $this->expectExceptionCode(71);
  279. $comment->store();
  280. }
  281. public function testCommentEqualsPaste()
  282. {
  283. $pasteData = Helper::getPastePost();
  284. $commentData = Helper::getCommentPost();
  285. $commentData['ct'] = $pasteData['ct'];
  286. $this->_model->getPaste(Helper::getPasteId())->delete();
  287. $paste = $this->_model->getPaste();
  288. $paste->setData($pasteData);
  289. $paste->store();
  290. $comment = $paste->getComment(Helper::getPasteId());
  291. $comment->setData($commentData);
  292. $this->expectException(Exception::class);
  293. $this->expectExceptionCode(72);
  294. $comment->store();
  295. }
  296. public function testImplicitDefaults()
  297. {
  298. $pasteData = Helper::getPastePost();
  299. $commentData = Helper::getCommentPost();
  300. $this->_model->getPaste(Helper::getPasteId())->delete();
  301. $paste = $this->_model->getPaste();
  302. $paste->setData($pasteData);
  303. $paste->store();
  304. $comment = $paste->getComment(Helper::getPasteId());
  305. $comment->setData($commentData);
  306. $comment->get();
  307. $comment->store();
  308. $identicon = new Identicon([
  309. 'hash' => TrafficLimiter::getHash(),
  310. 'size' => 16,
  311. 'style' => [
  312. 'backgroundColor' => '#fff0', // fully transparent, for dark mode
  313. 'padding' => 0,
  314. ],
  315. ]);
  316. $pngdata = $identicon->getImageDataUri('png');
  317. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  318. $this->assertEquals($pngdata, $comment['meta']['icon'], 'icon gets set');
  319. }
  320. public function testPasteIdValidation()
  321. {
  322. $this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
  323. $this->assertFalse(Paste::isValidId('foo'), 'invalid hex values & length');
  324. $this->assertFalse(Paste::isValidId('f00'), 'invalid length');
  325. $this->assertFalse(Paste::isValidId('foo bar baz quux'), 'invalid hex values');
  326. $this->assertFalse(Paste::isValidId("\n01234567feedcafe"), 'invalid line breaks');
  327. $this->assertFalse(Paste::isValidId("deadbeef01234567\n"), 'invalid line breaks');
  328. $this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
  329. }
  330. public function testInvalidPaste()
  331. {
  332. $this->_model->getPaste(Helper::getPasteId())->delete();
  333. $paste = $this->_model->getPaste(Helper::getPasteId());
  334. $this->expectException(Exception::class);
  335. $this->expectExceptionCode(64);
  336. $paste->get();
  337. }
  338. public function testInvalidPasteFormat()
  339. {
  340. $pasteData = Helper::getPastePost();
  341. $pasteData['adata'][1] = 'format does not exist';
  342. $paste = $this->_model->getPaste();
  343. $this->expectException(Exception::class);
  344. $this->expectExceptionCode(75);
  345. $paste->setData($pasteData);
  346. }
  347. public function testInvalidPasteId()
  348. {
  349. $this->expectException(Exception::class);
  350. $this->expectExceptionCode(60);
  351. $this->_model->getPaste('');
  352. }
  353. public function testInvalidComment()
  354. {
  355. $paste = $this->_model->getPaste();
  356. $this->expectException(Exception::class);
  357. $this->expectExceptionCode(62);
  358. $paste->getComment(Helper::getPasteId());
  359. }
  360. public function testInvalidCommentDeletedPaste()
  361. {
  362. $pasteData = Helper::getPastePost();
  363. $paste = $this->_model->getPaste(Helper::getPasteId());
  364. $paste->setData($pasteData);
  365. $paste->store();
  366. $comment = $paste->getComment(Helper::getPasteId());
  367. $paste->delete();
  368. $this->expectException(Exception::class);
  369. $this->expectExceptionCode(67);
  370. $comment->store();
  371. }
  372. public function testInvalidCommentData()
  373. {
  374. $pasteData = Helper::getPastePost();
  375. $pasteData['adata'][2] = 0;
  376. $paste = $this->_model->getPaste(Helper::getPasteId());
  377. $paste->setData($pasteData);
  378. $paste->store();
  379. $comment = $paste->getComment(Helper::getPasteId());
  380. $this->expectException(Exception::class);
  381. $this->expectExceptionCode(68);
  382. $comment->store();
  383. }
  384. public function testInvalidCommentParent()
  385. {
  386. $paste = $this->_model->getPaste(Helper::getPasteId());
  387. $this->expectException(Exception::class);
  388. $this->expectExceptionCode(65);
  389. $paste->getComment('');
  390. }
  391. public function testExpiration()
  392. {
  393. $pasteData = Helper::getPastePost();
  394. $this->_model->getPaste(Helper::getPasteId())->delete();
  395. $paste = $this->_model->getPaste(Helper::getPasteId());
  396. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  397. $paste = $this->_model->getPaste();
  398. $paste->setData($pasteData);
  399. $paste->store();
  400. $paste = $paste->get();
  401. $this->assertEquals((float) 300, (float) $paste['meta']['time_to_live'], 'remaining time is set correctly', 1.0);
  402. }
  403. public function testPurge()
  404. {
  405. $conf = new Configuration;
  406. $store = new Database($conf->getSection('model_options'));
  407. $store->delete(Helper::getPasteId());
  408. $expired = Helper::getPaste(['expire_date' => 1344803344]);
  409. $paste = Helper::getPaste(['expire_date' => time() + 3600]);
  410. $keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z'];
  411. $ids = [];
  412. foreach ($keys as $key) {
  413. $ids[$key] = hash('fnv164', $key);
  414. $store->delete($ids[$key]);
  415. $this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
  416. if (in_array($key, ['x', 'y', 'z'])) {
  417. $this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
  418. } else {
  419. $this->assertTrue($store->create($ids[$key], $expired), "store $key paste");
  420. }
  421. $this->assertTrue($store->exists($ids[$key]), "paste $key exists after storing it");
  422. }
  423. $this->_model->purge(10);
  424. foreach ($ids as $key => $id) {
  425. if (in_array($key, ['x', 'y', 'z'])) {
  426. $this->assertTrue($this->_model->getPaste($id)->exists(), "paste $key exists after purge");
  427. $this->_model->getPaste($id)->delete();
  428. } else {
  429. $this->assertFalse($this->_model->getPaste($id)->exists(), "paste $key was purged");
  430. }
  431. }
  432. }
  433. public function testCommentWithDisabledVizhash()
  434. {
  435. $options = parse_ini_file(CONF, true);
  436. $options['main']['discussiondatedisplay'] = 'false';
  437. $options['main']['icon'] = 'none';
  438. $options['model'] = [
  439. 'class' => 'Database',
  440. ];
  441. $options['model_options'] = [
  442. 'dsn' => 'sqlite::memory:',
  443. 'usr' => null,
  444. 'pwd' => null,
  445. 'opt' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
  446. ];
  447. Helper::createIniFile(CONF, $options);
  448. $model = new Model(new Configuration);
  449. $pasteData = Helper::getPastePost();
  450. $this->_model->getPaste(Helper::getPasteId())->delete();
  451. $paste = $model->getPaste(Helper::getPasteId());
  452. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  453. $paste = $model->getPaste();
  454. $paste->setData($pasteData);
  455. $paste->store();
  456. $paste = $model->getPaste(Helper::getPasteId());
  457. $this->assertTrue($paste->exists(), 'paste exists after storing it');
  458. // storing comments
  459. $commentData = Helper::getCommentPost();
  460. unset($commentData['meta']['icon']);
  461. $paste = $model->getPaste(Helper::getPasteId());
  462. $comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
  463. $this->assertFalse($comment->exists(), 'comment does not yet exist');
  464. $comment = $paste->getComment(Helper::getPasteId());
  465. $comment->setData($commentData);
  466. $comment->store();
  467. $comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
  468. $this->assertTrue($comment->exists(), 'comment exists after storing it');
  469. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  470. $this->assertFalse(array_key_exists('icon', $comment['meta']), 'icon was not generated');
  471. $this->assertTrue(array_key_exists('created', $comment['meta']), 'creation is set, when using default configuration');
  472. $comment = current($paste->get()['comments']);
  473. $this->assertFalse(array_key_exists('created', $comment['meta']), 'creation is not set, if using disabled configuration');
  474. }
  475. public function testCommentVizhash()
  476. {
  477. $options = parse_ini_file(CONF, true);
  478. $options['main']['icon'] = 'vizhash';
  479. $options['model'] = [
  480. 'class' => 'Database',
  481. ];
  482. $options['model_options'] = [
  483. 'dsn' => 'sqlite::memory:',
  484. 'usr' => null,
  485. 'pwd' => null,
  486. 'opt' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
  487. ];
  488. Helper::createIniFile(CONF, $options);
  489. $model = new Model(new Configuration);
  490. $pasteData = Helper::getPastePost();
  491. $commentData = Helper::getCommentPost();
  492. $model->getPaste(Helper::getPasteId())->delete();
  493. $paste = $model->getPaste();
  494. $paste->setData($pasteData);
  495. $paste->store();
  496. $comment = $paste->getComment(Helper::getPasteId());
  497. $comment->setData($commentData);
  498. $comment->store();
  499. $vz = new Vizhash16x16();
  500. $pngdata = 'data:image/png;base64,' . base64_encode($vz->generate(TrafficLimiter::getHash()));
  501. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  502. $this->assertEquals($pngdata, $comment['meta']['icon'], 'vizhash was generated');
  503. }
  504. }