ModelTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <?php declare(strict_types=1);
  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(new Database($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. 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. $paste = $this->_model->getPaste(Helper::getPasteId());
  149. $comment->setPaste($paste);
  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. public function testStoreFail()
  166. {
  167. $path = $this->_path . DIRECTORY_SEPARATOR . 'model-store-test.sq3';
  168. if (is_file($path)) {
  169. unlink($path);
  170. }
  171. $options = parse_ini_file(CONF_SAMPLE, true);
  172. $options['purge']['limit'] = 0;
  173. $options['model'] = array(
  174. 'class' => 'Database',
  175. );
  176. $options['model_options'] = array(
  177. 'dsn' => 'sqlite:' . $path,
  178. 'usr' => null,
  179. 'pwd' => null,
  180. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  181. );
  182. Helper::createIniFile(CONF, $options);
  183. $model = new Model(new Configuration);
  184. $pasteData = Helper::getPastePost();
  185. $model->getPaste(Helper::getPasteId())->delete();
  186. $model->getPaste(Helper::getPasteId())->exists();
  187. $db = new PDO(
  188. $options['model_options']['dsn'],
  189. $options['model_options']['usr'],
  190. $options['model_options']['pwd'],
  191. $options['model_options']['opt']
  192. );
  193. $statement = $db->prepare('DROP TABLE paste');
  194. $statement->execute();
  195. $statement->closeCursor();
  196. $paste = $model->getPaste();
  197. $paste->setData($pasteData);
  198. $this->expectException(Exception::class);
  199. $this->expectExceptionCode(76);
  200. $paste->store();
  201. }
  202. public function testCommentStoreFail()
  203. {
  204. $path = $this->_path . DIRECTORY_SEPARATOR . 'model-test.sq3';
  205. if (is_file($path)) {
  206. unlink($path);
  207. }
  208. $options = parse_ini_file(CONF_SAMPLE, true);
  209. $options['purge']['limit'] = 0;
  210. $options['model'] = array(
  211. 'class' => 'Database',
  212. );
  213. $options['model_options'] = array(
  214. 'dsn' => 'sqlite:' . $path,
  215. 'usr' => null,
  216. 'pwd' => null,
  217. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  218. );
  219. Helper::createIniFile(CONF, $options);
  220. $model = new Model(new Configuration);
  221. $pasteData = Helper::getPastePost();
  222. $commentData = Helper::getCommentPost();
  223. $model->getPaste(Helper::getPasteId())->delete();
  224. $paste = $model->getPaste();
  225. $paste->setData($pasteData);
  226. $paste->store();
  227. $this->assertTrue($paste->exists(), 'paste exists before creating comment');
  228. $comment = $paste->getComment(Helper::getPasteId());
  229. $comment->setData($commentData);
  230. $db = new PDO(
  231. $options['model_options']['dsn'],
  232. $options['model_options']['usr'],
  233. $options['model_options']['pwd'],
  234. $options['model_options']['opt']
  235. );
  236. $statement = $db->prepare('DROP TABLE comment');
  237. $statement->execute();
  238. $statement->closeCursor();
  239. $this->expectException(Exception::class);
  240. $this->expectExceptionCode(70);
  241. $comment->store();
  242. }
  243. public function testCommentDuplicate()
  244. {
  245. $pasteData = Helper::getPastePost();
  246. $commentData = Helper::getCommentPost();
  247. $this->_model->getPaste(Helper::getPasteId())->delete();
  248. $paste = $this->_model->getPaste();
  249. $paste->setData($pasteData);
  250. $paste->store();
  251. $comment = $paste->getComment(Helper::getPasteId());
  252. $comment->setData($commentData);
  253. $comment->store();
  254. $comment = $paste->getComment(Helper::getPasteId());
  255. $comment->setData($commentData);
  256. $this->expectException(Exception::class);
  257. $this->expectExceptionCode(69);
  258. $comment->store();
  259. }
  260. public function testImplicitDefaults()
  261. {
  262. $pasteData = Helper::getPastePost();
  263. $commentData = Helper::getCommentPost();
  264. $this->_model->getPaste(Helper::getPasteId())->delete();
  265. $paste = $this->_model->getPaste();
  266. $paste->setData($pasteData);
  267. $paste->store();
  268. $comment = $paste->getComment(Helper::getPasteId());
  269. $comment->setData($commentData);
  270. $comment->get();
  271. $comment->store();
  272. $identicon = new Identicon();
  273. $pngdata = $identicon->getImageDataUri(TrafficLimiter::getHash(), 16);
  274. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  275. $this->assertEquals($pngdata, $comment['meta']['icon'], 'icon gets set');
  276. }
  277. public function testPasteIdValidation()
  278. {
  279. $this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
  280. $this->assertFalse(Paste::isValidId('foo'), 'invalid hex values & length');
  281. $this->assertFalse(Paste::isValidId('f00'), 'invalid length');
  282. $this->assertFalse(Paste::isValidId('foo bar baz quux'), 'invalid hex values');
  283. $this->assertFalse(Paste::isValidId("\n01234567feedcafe"), 'invalid line breaks');
  284. $this->assertFalse(Paste::isValidId("deadbeef01234567\n"), 'invalid line breaks');
  285. $this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
  286. }
  287. public function testInvalidPaste()
  288. {
  289. $this->_model->getPaste(Helper::getPasteId())->delete();
  290. $paste = $this->_model->getPaste(Helper::getPasteId());
  291. $this->expectException(Exception::class);
  292. $this->expectExceptionCode(64);
  293. $paste->get();
  294. }
  295. public function testInvalidPasteFormat()
  296. {
  297. $pasteData = Helper::getPastePost();
  298. $pasteData['adata'][1] = 'format does not exist';
  299. $paste = $this->_model->getPaste();
  300. $this->expectException(Exception::class);
  301. $this->expectExceptionCode(75);
  302. $paste->setData($pasteData);
  303. }
  304. public function testInvalidPasteId()
  305. {
  306. $this->expectException(Exception::class);
  307. $this->expectExceptionCode(60);
  308. $this->_model->getPaste('');
  309. }
  310. public function testInvalidComment()
  311. {
  312. $paste = $this->_model->getPaste();
  313. $this->expectException(Exception::class);
  314. $this->expectExceptionCode(62);
  315. $paste->getComment(Helper::getPasteId());
  316. }
  317. public function testInvalidCommentDeletedPaste()
  318. {
  319. $pasteData = Helper::getPastePost();
  320. $paste = $this->_model->getPaste(Helper::getPasteId());
  321. $paste->setData($pasteData);
  322. $paste->store();
  323. $comment = $paste->getComment(Helper::getPasteId());
  324. $paste->delete();
  325. $this->expectException(Exception::class);
  326. $this->expectExceptionCode(67);
  327. $comment->store();
  328. }
  329. public function testInvalidCommentData()
  330. {
  331. $pasteData = Helper::getPastePost();
  332. $pasteData['adata'][2] = 0;
  333. $paste = $this->_model->getPaste(Helper::getPasteId());
  334. $paste->setData($pasteData);
  335. $paste->store();
  336. $comment = $paste->getComment(Helper::getPasteId());
  337. $this->expectException(Exception::class);
  338. $this->expectExceptionCode(68);
  339. $comment->store();
  340. }
  341. public function testInvalidCommentParent()
  342. {
  343. $paste = $this->_model->getPaste(Helper::getPasteId());
  344. $this->expectException(Exception::class);
  345. $this->expectExceptionCode(65);
  346. $paste->getComment('');
  347. }
  348. public function testExpiration()
  349. {
  350. $pasteData = Helper::getPastePost();
  351. $this->_model->getPaste(Helper::getPasteId())->delete();
  352. $paste = $this->_model->getPaste(Helper::getPasteId());
  353. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  354. $paste = $this->_model->getPaste();
  355. $paste->setData($pasteData);
  356. $paste->store();
  357. $paste = $paste->get();
  358. $this->assertEquals((float) 300, (float) $paste['meta']['time_to_live'], 'remaining time is set correctly', 1.0);
  359. }
  360. public function testCommentDeletion()
  361. {
  362. $pasteData = Helper::getPastePost();
  363. $this->_model->getPaste(Helper::getPasteId())->delete();
  364. $paste = $this->_model->getPaste();
  365. $paste->setData($pasteData);
  366. $paste->store();
  367. $this->expectException(Exception::class);
  368. $this->expectExceptionCode(64);
  369. $paste->getComment(Helper::getPasteId())->delete();
  370. }
  371. public function testPurge()
  372. {
  373. $conf = new Configuration;
  374. $store = new Database($conf->getSection('model_options'));
  375. $store->delete(Helper::getPasteId());
  376. $expired = Helper::getPaste(2, array('expire_date' => 1344803344));
  377. $paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
  378. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
  379. $ids = array();
  380. foreach ($keys as $key) {
  381. $ids[$key] = hash('fnv164', $key);
  382. $store->delete($ids[$key]);
  383. $this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
  384. if (in_array($key, array('x', 'y', 'z'))) {
  385. $this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
  386. } else {
  387. $this->assertTrue($store->create($ids[$key], $expired), "store $key paste");
  388. }
  389. $this->assertTrue($store->exists($ids[$key]), "paste $key exists after storing it");
  390. }
  391. $this->_model->purge(10);
  392. foreach ($ids as $key => $id) {
  393. if (in_array($key, array('x', 'y', 'z'))) {
  394. $this->assertTrue($this->_model->getPaste($id)->exists(), "paste $key exists after purge");
  395. $this->_model->getPaste($id)->delete();
  396. } else {
  397. $this->assertFalse($this->_model->getPaste($id)->exists(), "paste $key was purged");
  398. }
  399. }
  400. }
  401. public function testCommentWithDisabledVizhash()
  402. {
  403. $options = parse_ini_file(CONF, true);
  404. $options['main']['discussiondatedisplay'] = 'false';
  405. $options['main']['icon'] = 'none';
  406. $options['model'] = array(
  407. 'class' => 'Database',
  408. );
  409. $options['model_options'] = array(
  410. 'dsn' => 'sqlite::memory:',
  411. 'usr' => null,
  412. 'pwd' => null,
  413. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  414. );
  415. Helper::createIniFile(CONF, $options);
  416. $model = new Model(new Configuration);
  417. $pasteData = Helper::getPastePost();
  418. $this->_model->getPaste(Helper::getPasteId())->delete();
  419. $paste = $model->getPaste(Helper::getPasteId());
  420. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  421. $paste = $model->getPaste();
  422. $paste->setData($pasteData);
  423. $paste->store();
  424. $paste = $model->getPaste(Helper::getPasteId());
  425. $this->assertTrue($paste->exists(), 'paste exists after storing it');
  426. // storing comments
  427. $commentData = Helper::getCommentPost();
  428. unset($commentData['meta']['icon']);
  429. $paste = $model->getPaste(Helper::getPasteId());
  430. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  431. $this->assertFalse($comment->exists(), 'comment does not yet exist');
  432. $comment = $paste->getComment(Helper::getPasteId());
  433. $comment->setData($commentData);
  434. $comment->store();
  435. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  436. $this->assertTrue($comment->exists(), 'comment exists after storing it');
  437. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  438. $this->assertFalse(array_key_exists('icon', $comment['meta']), 'icon was not generated');
  439. $this->assertTrue(array_key_exists('created', $comment['meta']), 'creation is set, when using default configuration');
  440. $comment = current($paste->get()['comments']);
  441. $this->assertFalse(array_key_exists('created', $comment['meta']), 'creation is not set, if using disabled configuration');
  442. }
  443. public function testCommentVizhash()
  444. {
  445. $options = parse_ini_file(CONF, true);
  446. $options['main']['icon'] = 'vizhash';
  447. $options['model'] = array(
  448. 'class' => 'Database',
  449. );
  450. $options['model_options'] = array(
  451. 'dsn' => 'sqlite::memory:',
  452. 'usr' => null,
  453. 'pwd' => null,
  454. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  455. );
  456. Helper::createIniFile(CONF, $options);
  457. $model = new Model(new Configuration);
  458. $pasteData = Helper::getPastePost();
  459. $commentData = Helper::getCommentPost();
  460. $model->getPaste(Helper::getPasteId())->delete();
  461. $paste = $model->getPaste();
  462. $paste->setData($pasteData);
  463. $paste->store();
  464. $comment = $paste->getComment(Helper::getPasteId());
  465. $comment->setData($commentData);
  466. $comment->store();
  467. $vz = new Vizhash16x16();
  468. $pngdata = 'data:image/png;base64,' . base64_encode($vz->generate(TrafficLimiter::getHash()));
  469. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  470. $this->assertEquals($pngdata, $comment['meta']['icon'], 'nickname triggers vizhash to be set');
  471. }
  472. }