ModelTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. use Identicon\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();
  283. $pngdata = $identicon->getImageDataUri(TrafficLimiter::getHash(), 16);
  284. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  285. $this->assertEquals($pngdata, $comment['meta']['icon'], 'icon gets set');
  286. }
  287. public function testPasteIdValidation()
  288. {
  289. $this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
  290. $this->assertFalse(Paste::isValidId('foo'), 'invalid hex values');
  291. $this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
  292. }
  293. /**
  294. * @expectedException Exception
  295. * @expectedExceptionCode 64
  296. */
  297. public function testInvalidPaste()
  298. {
  299. $this->_model->getPaste(Helper::getPasteId())->delete();
  300. $paste = $this->_model->getPaste(Helper::getPasteId());
  301. $paste->get();
  302. }
  303. /**
  304. * @expectedException Exception
  305. * @expectedExceptionCode 75
  306. */
  307. public function testInvalidPasteFormat()
  308. {
  309. $pasteData = Helper::getPastePost();
  310. $pasteData['adata'][1] = 'format does not exist';
  311. $paste = $this->_model->getPaste();
  312. $paste->setData($pasteData);
  313. }
  314. /**
  315. * @expectedException Exception
  316. * @expectedExceptionCode 60
  317. */
  318. public function testInvalidPasteId()
  319. {
  320. $this->_model->getPaste('');
  321. }
  322. /**
  323. * @expectedException Exception
  324. * @expectedExceptionCode 62
  325. */
  326. public function testInvalidComment()
  327. {
  328. $paste = $this->_model->getPaste();
  329. $paste->getComment(Helper::getPasteId());
  330. }
  331. /**
  332. * @expectedException Exception
  333. * @expectedExceptionCode 67
  334. */
  335. public function testInvalidCommentDeletedPaste()
  336. {
  337. $pasteData = Helper::getPastePost();
  338. $paste = $this->_model->getPaste(Helper::getPasteId());
  339. $paste->setData($pasteData);
  340. $paste->store();
  341. $comment = $paste->getComment(Helper::getPasteId());
  342. $paste->delete();
  343. $comment->store();
  344. }
  345. /**
  346. * @expectedException Exception
  347. * @expectedExceptionCode 68
  348. */
  349. public function testInvalidCommentData()
  350. {
  351. $pasteData = Helper::getPastePost();
  352. $pasteData['adata'][2] = 0;
  353. $paste = $this->_model->getPaste(Helper::getPasteId());
  354. $paste->setData($pasteData);
  355. $paste->store();
  356. $comment = $paste->getComment(Helper::getPasteId());
  357. $comment->store();
  358. }
  359. /**
  360. * @expectedException Exception
  361. * @expectedExceptionCode 65
  362. */
  363. public function testInvalidCommentParent()
  364. {
  365. $paste = $this->_model->getPaste(Helper::getPasteId());
  366. $comment = $paste->getComment('');
  367. $comment->store();
  368. }
  369. public function testExpiration()
  370. {
  371. $pasteData = Helper::getPastePost();
  372. $this->_model->getPaste(Helper::getPasteId())->delete();
  373. $paste = $this->_model->getPaste(Helper::getPasteId());
  374. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  375. $paste = $this->_model->getPaste();
  376. $paste->setData($pasteData);
  377. $paste->store();
  378. $paste = $paste->get();
  379. $this->assertEquals((float) 300, (float) $paste['meta']['time_to_live'], 'remaining time is set correctly', 1.0);
  380. }
  381. /**
  382. * @expectedException Exception
  383. * @expectedExceptionCode 64
  384. */
  385. public function testCommentDeletion()
  386. {
  387. $pasteData = Helper::getPastePost();
  388. $this->_model->getPaste(Helper::getPasteId())->delete();
  389. $paste = $this->_model->getPaste();
  390. $paste->setData($pasteData);
  391. $paste->store();
  392. $paste->getComment(Helper::getPasteId())->delete();
  393. }
  394. public function testPurge()
  395. {
  396. $conf = new Configuration;
  397. $store = new Database($conf->getSection('model_options'));
  398. $store->delete(Helper::getPasteId());
  399. $expired = Helper::getPaste(2, array('expire_date' => 1344803344));
  400. $paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
  401. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
  402. $ids = array();
  403. foreach ($keys as $key) {
  404. $ids[$key] = hash('fnv164', $key);
  405. $store->delete($ids[$key]);
  406. $this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
  407. if (in_array($key, array('x', 'y', 'z'))) {
  408. $this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
  409. } else {
  410. $this->assertTrue($store->create($ids[$key], $expired), "store $key paste");
  411. }
  412. $this->assertTrue($store->exists($ids[$key]), "paste $key exists after storing it");
  413. }
  414. $this->_model->purge(10);
  415. foreach ($ids as $key => $id) {
  416. if (in_array($key, array('x', 'y', 'z'))) {
  417. $this->assertTrue($this->_model->getPaste($id)->exists(), "paste $key exists after purge");
  418. $this->_model->getPaste($id)->delete();
  419. } else {
  420. $this->assertFalse($this->_model->getPaste($id)->exists(), "paste $key was purged");
  421. }
  422. }
  423. }
  424. public function testCommentWithDisabledVizhash()
  425. {
  426. $options = parse_ini_file(CONF, true);
  427. $options['main']['icon'] = 'none';
  428. $options['model'] = array(
  429. 'class' => 'Database',
  430. );
  431. $options['model_options'] = array(
  432. 'dsn' => 'sqlite::memory:',
  433. 'usr' => null,
  434. 'pwd' => null,
  435. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  436. );
  437. Helper::createIniFile(CONF, $options);
  438. $model = new Model(new Configuration);
  439. $pasteData = Helper::getPastePost();
  440. $this->_model->getPaste(Helper::getPasteId())->delete();
  441. $paste = $model->getPaste(Helper::getPasteId());
  442. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  443. $paste = $model->getPaste();
  444. $paste->setData($pasteData);
  445. $paste->store();
  446. $paste = $model->getPaste(Helper::getPasteId());
  447. $this->assertTrue($paste->exists(), 'paste exists after storing it');
  448. // storing comments
  449. $commentData = Helper::getCommentPost();
  450. unset($commentData['meta']['icon']);
  451. $paste = $model->getPaste(Helper::getPasteId());
  452. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  453. $this->assertFalse($comment->exists(), 'comment does not yet exist');
  454. $comment = $paste->getComment(Helper::getPasteId());
  455. $comment->setData($commentData);
  456. $comment->store();
  457. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  458. $this->assertTrue($comment->exists(), 'comment exists after storing it');
  459. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  460. $this->assertFalse(array_key_exists('icon', $comment['meta']), 'icon was not generated');
  461. }
  462. public function testCommentVizhash()
  463. {
  464. $options = parse_ini_file(CONF, true);
  465. $options['main']['icon'] = 'vizhash';
  466. $options['model'] = array(
  467. 'class' => 'Database',
  468. );
  469. $options['model_options'] = array(
  470. 'dsn' => 'sqlite::memory:',
  471. 'usr' => null,
  472. 'pwd' => null,
  473. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  474. );
  475. Helper::createIniFile(CONF, $options);
  476. $model = new Model(new Configuration);
  477. $pasteData = Helper::getPastePost();
  478. $commentData = Helper::getCommentPost();
  479. $model->getPaste(Helper::getPasteId())->delete();
  480. $paste = $model->getPaste();
  481. $paste->setData($pasteData);
  482. $paste->store();
  483. $comment = $paste->getComment(Helper::getPasteId());
  484. $comment->setData($commentData);
  485. $comment->store();
  486. $vz = new Vizhash16x16();
  487. $pngdata = 'data:image/png;base64,' . base64_encode($vz->generate(TrafficLimiter::getHash()));
  488. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  489. $this->assertEquals($pngdata, $comment['meta']['icon'], 'nickname triggers vizhash to be set');
  490. }
  491. }