ModelTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. use Identicon\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'] = array(
  27. 'class' => 'Database',
  28. );
  29. $options['model_options'] = array(
  30. 'dsn' => 'sqlite::memory:',
  31. 'usr' => null,
  32. 'pwd' => null,
  33. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  34. );
  35. Helper::confBackup();
  36. Helper::createIniFile(CONF, $options);
  37. ServerSalt::setStore(Database::getInstance($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::getPasteId();
  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(array(), $paste->getComments(), 'comment was deleted with paste');
  91. }
  92. public function testPasteV1()
  93. {
  94. $pasteData = Helper::getPaste(1);
  95. unset($pasteData['meta']['formatter']);
  96. $path = $this->_path . DIRECTORY_SEPARATOR . 'v1-test.sq3';
  97. if (is_file($path)) {
  98. unlink($path);
  99. }
  100. $options = parse_ini_file(CONF_SAMPLE, true);
  101. $options['purge']['limit'] = 0;
  102. $options['model'] = array(
  103. 'class' => 'Database',
  104. );
  105. $options['model_options'] = array(
  106. 'dsn' => 'sqlite:' . $path,
  107. 'usr' => null,
  108. 'pwd' => null,
  109. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  110. );
  111. Helper::createIniFile(CONF, $options);
  112. $model = new Model(new Configuration);
  113. $model->getPaste('0000000000000000')->exists(); // triggers database table creation
  114. $model->getPaste(Helper::getPasteId())->delete(); // deletes the cache
  115. $db = new PDO(
  116. $options['model_options']['dsn'],
  117. $options['model_options']['usr'],
  118. $options['model_options']['pwd'],
  119. $options['model_options']['opt']
  120. );
  121. $statement = $db->prepare('INSERT INTO paste VALUES(?,?,?,?,?,?,?,?,?)');
  122. $statement->execute(
  123. array(
  124. Helper::getPasteId(),
  125. $pasteData['data'],
  126. $pasteData['meta']['postdate'],
  127. 0,
  128. 0,
  129. 0,
  130. json_encode($pasteData['meta']),
  131. null,
  132. null,
  133. )
  134. );
  135. $statement->closeCursor();
  136. $paste = $model->getPaste(Helper::getPasteId());
  137. $this->assertNotEmpty($paste->getDeleteToken(), 'excercise the condition to load the data from storage');
  138. $this->assertEquals('plaintext', $paste->get()['meta']['formatter'], 'paste got created with default formatter');
  139. }
  140. public function testCommentDefaults()
  141. {
  142. $comment = new Comment(
  143. $this->_conf,
  144. forward_static_call(
  145. 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model') . '::getInstance',
  146. $this->_conf->getSection('model_options')
  147. )
  148. );
  149. $comment->setPaste($this->_model->getPaste(Helper::getPasteId()));
  150. $this->assertEquals(Helper::getPasteId(), $comment->getParentId(), 'comment parent ID gets initialized to paste ID');
  151. }
  152. public function testPasteDuplicate()
  153. {
  154. $pasteData = Helper::getPastePost();
  155. $this->_model->getPaste(Helper::getPasteId())->delete();
  156. $paste = $this->_model->getPaste();
  157. $paste->setData($pasteData);
  158. $paste->store();
  159. $paste = $this->_model->getPaste();
  160. $paste->setData($pasteData);
  161. $this->expectException(Exception::class);
  162. $this->expectExceptionCode(75);
  163. $paste->store();
  164. }
  165. /**
  166. * @expectedException Exception
  167. * @expectedExceptionCode 76
  168. */
  169. public function testStoreFail()
  170. {
  171. $path = $this->_path . DIRECTORY_SEPARATOR . 'model-store-test.sq3';
  172. if (is_file($path)) {
  173. unlink($path);
  174. }
  175. $options = parse_ini_file(CONF_SAMPLE, true);
  176. $options['purge']['limit'] = 0;
  177. $options['model'] = array(
  178. 'class' => 'Database',
  179. );
  180. $options['model_options'] = array(
  181. 'dsn' => 'sqlite:' . $path,
  182. 'usr' => null,
  183. 'pwd' => null,
  184. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  185. );
  186. Helper::createIniFile(CONF, $options);
  187. $model = new Model(new Configuration);
  188. $pasteData = Helper::getPastePost();
  189. $model->getPaste(Helper::getPasteId())->delete();
  190. $model->getPaste(Helper::getPasteId())->exists();
  191. $db = new PDO(
  192. $options['model_options']['dsn'],
  193. $options['model_options']['usr'],
  194. $options['model_options']['pwd'],
  195. $options['model_options']['opt']
  196. );
  197. $statement = $db->prepare('DROP TABLE paste');
  198. $statement->execute();
  199. $statement->closeCursor();
  200. $paste = $model->getPaste();
  201. $paste->setData($pasteData);
  202. $paste->store();
  203. }
  204. /**
  205. * @expectedException Exception
  206. * @expectedExceptionCode 70
  207. */
  208. public function testCommentStoreFail()
  209. {
  210. $path = $this->_path . DIRECTORY_SEPARATOR . 'model-test.sq3';
  211. if (is_file($path)) {
  212. unlink($path);
  213. }
  214. $options = parse_ini_file(CONF_SAMPLE, true);
  215. $options['purge']['limit'] = 0;
  216. $options['model'] = array(
  217. 'class' => 'Database',
  218. );
  219. $options['model_options'] = array(
  220. 'dsn' => 'sqlite:' . $path,
  221. 'usr' => null,
  222. 'pwd' => null,
  223. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  224. );
  225. Helper::createIniFile(CONF, $options);
  226. $model = new Model(new Configuration);
  227. $pasteData = Helper::getPastePost();
  228. $commentData = Helper::getCommentPost();
  229. $model->getPaste(Helper::getPasteId())->delete();
  230. $paste = $model->getPaste();
  231. $paste->setData($pasteData);
  232. $paste->store();
  233. $paste->exists();
  234. $db = new PDO(
  235. $options['model_options']['dsn'],
  236. $options['model_options']['usr'],
  237. $options['model_options']['pwd'],
  238. $options['model_options']['opt']
  239. );
  240. $statement = $db->prepare('DROP TABLE comment');
  241. $statement->execute();
  242. $statement->closeCursor();
  243. $comment = $paste->getComment(Helper::getPasteId());
  244. $comment->setData($commentData);
  245. $comment->store();
  246. }
  247. /**
  248. * @expectedException Exception
  249. * @expectedExceptionCode 69
  250. */
  251. public function testCommentDuplicate()
  252. {
  253. $pasteData = Helper::getPastePost();
  254. $commentData = Helper::getCommentPost();
  255. $this->_model->getPaste(Helper::getPasteId())->delete();
  256. $paste = $this->_model->getPaste();
  257. $paste->setData($pasteData);
  258. $paste->store();
  259. $comment = $paste->getComment(Helper::getPasteId());
  260. $comment->setData($commentData);
  261. $comment->store();
  262. $comment = $paste->getComment(Helper::getPasteId());
  263. $comment->setData($commentData);
  264. $this->expectException(Exception::class);
  265. $this->expectExceptionCode(69);
  266. $comment->store();
  267. }
  268. public function testImplicitDefaults()
  269. {
  270. $pasteData = Helper::getPastePost();
  271. $commentData = Helper::getCommentPost();
  272. $this->_model->getPaste(Helper::getPasteId())->delete();
  273. $paste = $this->_model->getPaste();
  274. $paste->setData($pasteData);
  275. $paste->store();
  276. $comment = $paste->getComment(Helper::getPasteId());
  277. $comment->setData($commentData);
  278. $comment->get();
  279. $comment->store();
  280. $identicon = new Identicon();
  281. $pngdata = $identicon->getImageDataUri(TrafficLimiter::getHash(), 16);
  282. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  283. $this->assertEquals($pngdata, $comment['meta']['icon'], 'icon gets set');
  284. }
  285. public function testPasteIdValidation()
  286. {
  287. $this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
  288. $this->assertFalse(Paste::isValidId('foo'), 'invalid hex values');
  289. $this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
  290. }
  291. public function testInvalidPaste()
  292. {
  293. $this->_model->getPaste(Helper::getPasteId())->delete();
  294. $paste = $this->_model->getPaste(Helper::getPasteId());
  295. $this->expectException(Exception::class);
  296. $this->expectExceptionCode(64);
  297. $paste->get();
  298. }
  299. /**
  300. * @expectedException Exception
  301. * @expectedExceptionCode 75
  302. */
  303. public function testInvalidPasteFormat()
  304. {
  305. $pasteData = Helper::getPastePost();
  306. $pasteData['adata'][1] = 'format does not exist';
  307. $paste = $this->_model->getPaste();
  308. $paste->setData($pasteData);
  309. }
  310. /**
  311. * @expectedException Exception
  312. * @expectedExceptionCode 60
  313. */
  314. public function testInvalidPasteId()
  315. {
  316. $this->expectException(Exception::class);
  317. $this->expectExceptionCode(60);
  318. $this->_model->getPaste('');
  319. }
  320. public function testInvalidComment()
  321. {
  322. $paste = $this->_model->getPaste();
  323. $this->expectException(Exception::class);
  324. $this->expectExceptionCode(62);
  325. $paste->getComment(Helper::getPasteId());
  326. }
  327. public function testInvalidCommentDeletedPaste()
  328. {
  329. $pasteData = Helper::getPastePost();
  330. $paste = $this->_model->getPaste(Helper::getPasteId());
  331. $paste->setData($pasteData);
  332. $paste->store();
  333. $comment = $paste->getComment(Helper::getPasteId());
  334. $paste->delete();
  335. $this->expectException(Exception::class);
  336. $this->expectExceptionCode(67);
  337. $comment->store();
  338. }
  339. public function testInvalidCommentData()
  340. {
  341. $pasteData = Helper::getPastePost();
  342. $pasteData['adata'][2] = 0;
  343. $paste = $this->_model->getPaste(Helper::getPasteId());
  344. $paste->setData($pasteData);
  345. $paste->store();
  346. $comment = $paste->getComment(Helper::getPasteId());
  347. $this->expectException(Exception::class);
  348. $this->expectExceptionCode(68);
  349. $comment->store();
  350. }
  351. public function testInvalidCommentParent()
  352. {
  353. $paste = $this->_model->getPaste(Helper::getPasteId());
  354. $this->expectException(Exception::class);
  355. $this->expectExceptionCode(65);
  356. $paste->getComment('');
  357. }
  358. public function testExpiration()
  359. {
  360. $pasteData = Helper::getPastePost();
  361. $this->_model->getPaste(Helper::getPasteId())->delete();
  362. $paste = $this->_model->getPaste(Helper::getPasteId());
  363. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  364. $paste = $this->_model->getPaste();
  365. $paste->setData($pasteData);
  366. $paste->store();
  367. $paste = $paste->get();
  368. $this->assertEquals((float) 300, (float) $paste['meta']['time_to_live'], 'remaining time is set correctly', 1.0);
  369. }
  370. public function testCommentDeletion()
  371. {
  372. $pasteData = Helper::getPastePost();
  373. $this->_model->getPaste(Helper::getPasteId())->delete();
  374. $paste = $this->_model->getPaste();
  375. $paste->setData($pasteData);
  376. $paste->store();
  377. $this->expectException(Exception::class);
  378. $this->expectExceptionCode(64);
  379. $paste->getComment(Helper::getPasteId())->delete();
  380. }
  381. public function testPurge()
  382. {
  383. $conf = new Configuration;
  384. $store = Database::getInstance($conf->getSection('model_options'));
  385. $store->delete(Helper::getPasteId());
  386. $expired = Helper::getPaste(2, array('expire_date' => 1344803344));
  387. $paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
  388. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
  389. $ids = array();
  390. foreach ($keys as $key) {
  391. $ids[$key] = hash('fnv164', $key);
  392. $store->delete($ids[$key]);
  393. $this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
  394. if (in_array($key, array('x', 'y', 'z'))) {
  395. $this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
  396. } else {
  397. $this->assertTrue($store->create($ids[$key], $expired), "store $key paste");
  398. }
  399. $this->assertTrue($store->exists($ids[$key]), "paste $key exists after storing it");
  400. }
  401. $this->_model->purge(10);
  402. foreach ($ids as $key => $id) {
  403. if (in_array($key, array('x', 'y', 'z'))) {
  404. $this->assertTrue($this->_model->getPaste($id)->exists(), "paste $key exists after purge");
  405. $this->_model->getPaste($id)->delete();
  406. } else {
  407. $this->assertFalse($this->_model->getPaste($id)->exists(), "paste $key was purged");
  408. }
  409. }
  410. }
  411. public function testCommentWithDisabledVizhash()
  412. {
  413. $options = parse_ini_file(CONF, true);
  414. $options['main']['icon'] = 'none';
  415. $options['model'] = array(
  416. 'class' => 'Database',
  417. );
  418. $options['model_options'] = array(
  419. 'dsn' => 'sqlite::memory:',
  420. 'usr' => null,
  421. 'pwd' => null,
  422. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  423. );
  424. Helper::createIniFile(CONF, $options);
  425. $model = new Model(new Configuration);
  426. $pasteData = Helper::getPastePost();
  427. $this->_model->getPaste(Helper::getPasteId())->delete();
  428. $paste = $model->getPaste(Helper::getPasteId());
  429. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  430. $paste = $model->getPaste();
  431. $paste->setData($pasteData);
  432. $paste->store();
  433. $paste = $model->getPaste(Helper::getPasteId());
  434. $this->assertTrue($paste->exists(), 'paste exists after storing it');
  435. // storing comments
  436. $commentData = Helper::getCommentPost();
  437. unset($commentData['meta']['icon']);
  438. $paste = $model->getPaste(Helper::getPasteId());
  439. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  440. $this->assertFalse($comment->exists(), 'comment does not yet exist');
  441. $comment = $paste->getComment(Helper::getPasteId());
  442. $comment->setData($commentData);
  443. $comment->store();
  444. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  445. $this->assertTrue($comment->exists(), 'comment exists after storing it');
  446. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  447. $this->assertFalse(array_key_exists('icon', $comment['meta']), 'icon was not generated');
  448. }
  449. public function testCommentVizhash()
  450. {
  451. $options = parse_ini_file(CONF, true);
  452. $options['main']['icon'] = 'vizhash';
  453. $options['model'] = array(
  454. 'class' => 'Database',
  455. );
  456. $options['model_options'] = array(
  457. 'dsn' => 'sqlite::memory:',
  458. 'usr' => null,
  459. 'pwd' => null,
  460. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  461. );
  462. Helper::createIniFile(CONF, $options);
  463. $model = new Model(new Configuration);
  464. $pasteData = Helper::getPastePost();
  465. $commentData = Helper::getCommentPost();
  466. $model->getPaste(Helper::getPasteId())->delete();
  467. $paste = $model->getPaste();
  468. $paste->setData($pasteData);
  469. $paste->store();
  470. $comment = $paste->getComment(Helper::getPasteId());
  471. $comment->setData($commentData);
  472. $comment->store();
  473. $vz = new Vizhash16x16();
  474. $pngdata = 'data:image/png;base64,' . base64_encode($vz->generate(TrafficLimiter::getHash()));
  475. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  476. $this->assertEquals($pngdata, $comment['meta']['icon'], 'nickname triggers vizhash to be set');
  477. }
  478. }