ModelTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  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. ServerSalt::setPath($this->_path);
  25. $options = parse_ini_file(CONF_SAMPLE, true);
  26. $options['purge']['limit'] = 0;
  27. $options['model'] = array(
  28. 'class' => 'Database',
  29. );
  30. $options['model_options'] = array(
  31. 'dsn' => 'sqlite::memory:',
  32. 'usr' => null,
  33. 'pwd' => null,
  34. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  35. );
  36. Helper::confBackup();
  37. Helper::createIniFile(CONF, $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 testCommentDefaults()
  93. {
  94. $comment = new Comment(
  95. $this->_conf,
  96. forward_static_call(
  97. 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model') . '::getInstance',
  98. $this->_conf->getSection('model_options')
  99. )
  100. );
  101. $comment->setPaste($this->_model->getPaste(Helper::getPasteId()));
  102. $this->assertEquals(Helper::getPasteId(), $comment->getParentId(), 'comment parent ID gets initialized to paste ID');
  103. }
  104. public function testPasteDuplicate()
  105. {
  106. $pasteData = Helper::getPastePost();
  107. $this->_model->getPaste(Helper::getPasteId())->delete();
  108. $paste = $this->_model->getPaste();
  109. $paste->setData($pasteData);
  110. $paste->store();
  111. $paste = $this->_model->getPaste();
  112. $paste->setData($pasteData);
  113. $this->expectException(Exception::class);
  114. $this->expectExceptionCode(75);
  115. $paste->store();
  116. }
  117. public function testCommentDuplicate()
  118. {
  119. $pasteData = Helper::getPastePost();
  120. $commentData = Helper::getCommentPost();
  121. $this->_model->getPaste(Helper::getPasteId())->delete();
  122. $paste = $this->_model->getPaste();
  123. $paste->setData($pasteData);
  124. $paste->store();
  125. $comment = $paste->getComment(Helper::getPasteId());
  126. $comment->setData($commentData);
  127. $comment->store();
  128. $comment = $paste->getComment(Helper::getPasteId());
  129. $comment->setData($commentData);
  130. $this->expectException(Exception::class);
  131. $this->expectExceptionCode(69);
  132. $comment->store();
  133. }
  134. public function testImplicitDefaults()
  135. {
  136. $pasteData = Helper::getPastePost();
  137. $commentData = Helper::getCommentPost();
  138. $this->_model->getPaste(Helper::getPasteId())->delete();
  139. $paste = $this->_model->getPaste();
  140. $paste->setData($pasteData);
  141. $paste->store();
  142. $comment = $paste->getComment(Helper::getPasteId());
  143. $comment->setData($commentData);
  144. $comment->get();
  145. $comment->store();
  146. $identicon = new Identicon();
  147. $pngdata = $identicon->getImageDataUri(TrafficLimiter::getHash(), 16);
  148. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  149. $this->assertEquals($pngdata, $comment['meta']['icon'], 'icon gets set');
  150. }
  151. public function testPasteIdValidation()
  152. {
  153. $this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
  154. $this->assertFalse(Paste::isValidId('foo'), 'invalid hex values');
  155. $this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
  156. }
  157. public function testInvalidPaste()
  158. {
  159. $this->_model->getPaste(Helper::getPasteId())->delete();
  160. $paste = $this->_model->getPaste(Helper::getPasteId());
  161. $this->expectException(Exception::class);
  162. $this->expectExceptionCode(64);
  163. $paste->get();
  164. }
  165. public function testInvalidPasteId()
  166. {
  167. $this->expectException(Exception::class);
  168. $this->expectExceptionCode(60);
  169. $this->_model->getPaste('');
  170. }
  171. public function testInvalidComment()
  172. {
  173. $paste = $this->_model->getPaste();
  174. $this->expectException(Exception::class);
  175. $this->expectExceptionCode(62);
  176. $paste->getComment(Helper::getPasteId());
  177. }
  178. public function testInvalidCommentDeletedPaste()
  179. {
  180. $pasteData = Helper::getPastePost();
  181. $paste = $this->_model->getPaste(Helper::getPasteId());
  182. $paste->setData($pasteData);
  183. $paste->store();
  184. $comment = $paste->getComment(Helper::getPasteId());
  185. $paste->delete();
  186. $this->expectException(Exception::class);
  187. $this->expectExceptionCode(67);
  188. $comment->store();
  189. }
  190. public function testInvalidCommentData()
  191. {
  192. $pasteData = Helper::getPastePost();
  193. $pasteData['adata'][2] = 0;
  194. $paste = $this->_model->getPaste(Helper::getPasteId());
  195. $paste->setData($pasteData);
  196. $paste->store();
  197. $comment = $paste->getComment(Helper::getPasteId());
  198. $this->expectException(Exception::class);
  199. $this->expectExceptionCode(68);
  200. $comment->store();
  201. }
  202. public function testInvalidCommentParent()
  203. {
  204. $paste = $this->_model->getPaste(Helper::getPasteId());
  205. $this->expectException(Exception::class);
  206. $this->expectExceptionCode(65);
  207. $paste->getComment('');
  208. }
  209. public function testExpiration()
  210. {
  211. $pasteData = Helper::getPastePost();
  212. $this->_model->getPaste(Helper::getPasteId())->delete();
  213. $paste = $this->_model->getPaste(Helper::getPasteId());
  214. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  215. $paste = $this->_model->getPaste();
  216. $paste->setData($pasteData);
  217. $paste->store();
  218. $paste = $paste->get();
  219. $this->assertEquals((float) 300, (float) $paste['meta']['time_to_live'], 'remaining time is set correctly', 1.0);
  220. }
  221. public function testCommentDeletion()
  222. {
  223. $pasteData = Helper::getPastePost();
  224. $this->_model->getPaste(Helper::getPasteId())->delete();
  225. $paste = $this->_model->getPaste();
  226. $paste->setData($pasteData);
  227. $paste->store();
  228. $this->expectException(Exception::class);
  229. $this->expectExceptionCode(64);
  230. $paste->getComment(Helper::getPasteId())->delete();
  231. }
  232. public function testPurge()
  233. {
  234. $conf = new Configuration;
  235. $store = Database::getInstance($conf->getSection('model_options'));
  236. $store->delete(Helper::getPasteId());
  237. $expired = Helper::getPaste(2, array('expire_date' => 1344803344));
  238. $paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
  239. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
  240. $ids = array();
  241. foreach ($keys as $key) {
  242. $ids[$key] = hash('fnv164', $key);
  243. $store->delete($ids[$key]);
  244. $this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
  245. if (in_array($key, array('x', 'y', 'z'))) {
  246. $this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
  247. } else {
  248. $this->assertTrue($store->create($ids[$key], $expired), "store $key paste");
  249. }
  250. $this->assertTrue($store->exists($ids[$key]), "paste $key exists after storing it");
  251. }
  252. $this->_model->purge(10);
  253. foreach ($ids as $key => $id) {
  254. if (in_array($key, array('x', 'y', 'z'))) {
  255. $this->assertTrue($this->_model->getPaste($id)->exists(), "paste $key exists after purge");
  256. $this->_model->getPaste($id)->delete();
  257. } else {
  258. $this->assertFalse($this->_model->getPaste($id)->exists(), "paste $key was purged");
  259. }
  260. }
  261. }
  262. public function testCommentWithDisabledVizhash()
  263. {
  264. $options = parse_ini_file(CONF, true);
  265. $options['main']['icon'] = 'none';
  266. $options['model'] = array(
  267. 'class' => 'Database',
  268. );
  269. $options['model_options'] = array(
  270. 'dsn' => 'sqlite::memory:',
  271. 'usr' => null,
  272. 'pwd' => null,
  273. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  274. );
  275. Helper::createIniFile(CONF, $options);
  276. $model = new Model(new Configuration);
  277. $pasteData = Helper::getPastePost();
  278. $this->_model->getPaste(Helper::getPasteId())->delete();
  279. $paste = $model->getPaste(Helper::getPasteId());
  280. $this->assertFalse($paste->exists(), 'paste does not yet exist');
  281. $paste = $model->getPaste();
  282. $paste->setData($pasteData);
  283. $paste->store();
  284. $paste = $model->getPaste(Helper::getPasteId());
  285. $this->assertTrue($paste->exists(), 'paste exists after storing it');
  286. // storing comments
  287. $commentData = Helper::getCommentPost();
  288. unset($commentData['meta']['icon']);
  289. $paste = $model->getPaste(Helper::getPasteId());
  290. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  291. $this->assertFalse($comment->exists(), 'comment does not yet exist');
  292. $comment = $paste->getComment(Helper::getPasteId());
  293. $comment->setData($commentData);
  294. $comment->store();
  295. $comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
  296. $this->assertTrue($comment->exists(), 'comment exists after storing it');
  297. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  298. $this->assertFalse(array_key_exists('icon', $comment['meta']), 'icon was not generated');
  299. }
  300. public function testCommentVizhash()
  301. {
  302. $options = parse_ini_file(CONF, true);
  303. $options['main']['icon'] = 'vizhash';
  304. $options['model'] = array(
  305. 'class' => 'Database',
  306. );
  307. $options['model_options'] = array(
  308. 'dsn' => 'sqlite::memory:',
  309. 'usr' => null,
  310. 'pwd' => null,
  311. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  312. );
  313. Helper::createIniFile(CONF, $options);
  314. $model = new Model(new Configuration);
  315. $pasteData = Helper::getPastePost();
  316. $commentData = Helper::getCommentPost();
  317. $model->getPaste(Helper::getPasteId())->delete();
  318. $paste = $model->getPaste();
  319. $paste->setData($pasteData);
  320. $paste->store();
  321. $comment = $paste->getComment(Helper::getPasteId());
  322. $comment->setData($commentData);
  323. $comment->store();
  324. $vz = new Vizhash16x16();
  325. $pngdata = 'data:image/png;base64,' . base64_encode($vz->generate(TrafficLimiter::getHash()));
  326. $comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
  327. $this->assertEquals($pngdata, $comment['meta']['icon'], 'nickname triggers vizhash to be set');
  328. }
  329. }