ModelTest.php 19 KB

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