ModelTest.php 19 KB

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