ModelTest.php 19 KB

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