ModelTest.php 19 KB

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