zerobin.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. <?php
  2. class zerobinTest extends PHPUnit_Framework_TestCase
  3. {
  4. private static $pasteid = '5e9bc25c89fb3bf9';
  5. private static $paste = array(
  6. 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
  7. 'meta' => array(
  8. 'postdate' => 1344803344,
  9. 'opendiscussion' => true,
  10. 'formatter' => 'syntaxhighlighting',
  11. ),
  12. );
  13. private static $commentid = '5a52eebf11c4c94b';
  14. private static $comment = array(
  15. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  16. 'meta' => array(
  17. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  18. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  19. 'postdate' => 1344803528,
  20. ),
  21. );
  22. private $_conf;
  23. private $_model;
  24. public function setUp()
  25. {
  26. /* Setup Routine */
  27. $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
  28. serversalt::setPath(PATH . 'data');
  29. $this->_conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  30. $this->reset();
  31. }
  32. public function tearDown()
  33. {
  34. /* Tear Down Routine */
  35. }
  36. public function reset()
  37. {
  38. $_POST = array();
  39. $_GET = array();
  40. $_SERVER = array();
  41. if ($this->_model->exists(self::$pasteid))
  42. $this->_model->delete(self::$pasteid);
  43. if (is_file($this->_conf . '.bak'))
  44. rename($this->_conf . '.bak', $this->_conf);
  45. }
  46. /**
  47. * @runInSeparateProcess
  48. */
  49. public function testView()
  50. {
  51. $this->reset();
  52. ob_start();
  53. new zerobin;
  54. $content = ob_get_contents();
  55. $this->assertTag(
  56. array(
  57. 'tag' => 'title',
  58. 'content' => 'ZeroBin'
  59. ),
  60. $content,
  61. 'outputs title correctly'
  62. );
  63. }
  64. /**
  65. * @runInSeparateProcess
  66. */
  67. public function testHtaccess()
  68. {
  69. $this->reset();
  70. $dirs = array('cfg', 'lib');
  71. foreach ($dirs as $dir) {
  72. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  73. @unlink($file);
  74. }
  75. ob_start();
  76. new zerobin;
  77. $content = ob_get_contents();
  78. foreach ($dirs as $dir) {
  79. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  80. $this->assertFileExists(
  81. $file,
  82. "$dir htaccess recreated"
  83. );
  84. }
  85. }
  86. /**
  87. * @expectedException Exception
  88. * @expectedExceptionCode 2
  89. */
  90. public function testConf()
  91. {
  92. $this->reset();
  93. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  94. rename($this->_conf, $this->_conf . '.bak');
  95. file_put_contents($this->_conf, '');
  96. ob_start();
  97. new zerobin;
  98. $content = ob_get_contents();
  99. }
  100. /**
  101. * @runInSeparateProcess
  102. */
  103. public function testCreate()
  104. {
  105. $this->reset();
  106. $_POST = self::$paste;
  107. $_SERVER['REMOTE_ADDR'] = '::1';
  108. ob_start();
  109. new zerobin;
  110. $content = ob_get_contents();
  111. $response = json_decode($content, true);
  112. $this->assertEquals(0, $response['status'], 'outputs status');
  113. $this->assertEquals(
  114. hash_hmac('sha1', $response['id'], serversalt::get()),
  115. $response['deletetoken'],
  116. 'outputs valid delete token'
  117. );
  118. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  119. }
  120. /**
  121. * @runInSeparateProcess
  122. */
  123. public function testCreateInvalidTimelimit()
  124. {
  125. $this->reset();
  126. $_POST = self::$paste;
  127. $_SERVER['REMOTE_ADDR'] = '::1';
  128. ob_start();
  129. new zerobin;
  130. $content = ob_get_contents();
  131. $response = json_decode($content, true);
  132. $this->assertEquals(1, $response['status'], 'outputs error status');
  133. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  134. }
  135. /**
  136. * @runInSeparateProcess
  137. */
  138. public function testCreateInvalidSize()
  139. {
  140. $this->reset();
  141. $options = parse_ini_file($this->_conf, true);
  142. $options['main']['sizelimit'] = 10;
  143. $options['traffic']['limit'] = 0;
  144. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  145. rename($this->_conf, $this->_conf . '.bak');
  146. helper::createIniFile($this->_conf, $options);
  147. $_POST = self::$paste;
  148. $_SERVER['REMOTE_ADDR'] = '::1';
  149. ob_start();
  150. new zerobin;
  151. $content = ob_get_contents();
  152. $response = json_decode($content, true);
  153. $this->assertEquals(1, $response['status'], 'outputs error status');
  154. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  155. }
  156. /**
  157. * @runInSeparateProcess
  158. */
  159. public function testCreateDuplicateId()
  160. {
  161. $this->reset();
  162. $options = parse_ini_file($this->_conf, true);
  163. $options['traffic']['limit'] = 0;
  164. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  165. rename($this->_conf, $this->_conf . '.bak');
  166. helper::createIniFile($this->_conf, $options);
  167. $this->_model->create(self::$pasteid, self::$paste);
  168. $_POST = self::$paste;
  169. $_SERVER['REMOTE_ADDR'] = '::1';
  170. ob_start();
  171. new zerobin;
  172. $content = ob_get_contents();
  173. $response = json_decode($content, true);
  174. $this->assertEquals(1, $response['status'], 'outputs error status');
  175. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  176. }
  177. /**
  178. * @runInSeparateProcess
  179. */
  180. public function testCreateValidExpire()
  181. {
  182. $this->reset();
  183. $options = parse_ini_file($this->_conf, true);
  184. $options['traffic']['limit'] = 0;
  185. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  186. rename($this->_conf, $this->_conf . '.bak');
  187. helper::createIniFile($this->_conf, $options);
  188. $_POST = self::$paste;
  189. $_POST['expire'] = '5min';
  190. $_POST['formatter'] = 'foo';
  191. $_SERVER['REMOTE_ADDR'] = '::1';
  192. ob_start();
  193. new zerobin;
  194. $content = ob_get_contents();
  195. $response = json_decode($content, true);
  196. $this->assertEquals(0, $response['status'], 'outputs status');
  197. $this->assertEquals(
  198. hash_hmac('sha1', $response['id'], serversalt::get()),
  199. $response['deletetoken'],
  200. 'outputs valid delete token'
  201. );
  202. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  203. }
  204. /**
  205. * @runInSeparateProcess
  206. */
  207. public function testCreateInvalidExpire()
  208. {
  209. $this->reset();
  210. $options = parse_ini_file($this->_conf, true);
  211. $options['traffic']['limit'] = 0;
  212. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  213. rename($this->_conf, $this->_conf . '.bak');
  214. helper::createIniFile($this->_conf, $options);
  215. $_POST = self::$paste;
  216. $_POST['expire'] = 'foo';
  217. $_SERVER['REMOTE_ADDR'] = '::1';
  218. ob_start();
  219. new zerobin;
  220. $content = ob_get_contents();
  221. $response = json_decode($content, true);
  222. $this->assertEquals(0, $response['status'], 'outputs status');
  223. $this->assertEquals(
  224. hash_hmac('sha1', $response['id'], serversalt::get()),
  225. $response['deletetoken'],
  226. 'outputs valid delete token'
  227. );
  228. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  229. }
  230. /**
  231. * @runInSeparateProcess
  232. */
  233. public function testCreateInvalidBurn()
  234. {
  235. $this->reset();
  236. $options = parse_ini_file($this->_conf, true);
  237. $options['traffic']['limit'] = 0;
  238. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  239. rename($this->_conf, $this->_conf . '.bak');
  240. helper::createIniFile($this->_conf, $options);
  241. $_POST = self::$paste;
  242. $_POST['burnafterreading'] = 'neither 1 nor 0';
  243. $_SERVER['REMOTE_ADDR'] = '::1';
  244. ob_start();
  245. new zerobin;
  246. $content = ob_get_contents();
  247. $response = json_decode($content, true);
  248. $this->assertEquals(1, $response['status'], 'outputs error status');
  249. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  250. }
  251. /**
  252. * @runInSeparateProcess
  253. */
  254. public function testCreateInvalidOpenDiscussion()
  255. {
  256. $this->reset();
  257. $options = parse_ini_file($this->_conf, true);
  258. $options['traffic']['limit'] = 0;
  259. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  260. rename($this->_conf, $this->_conf . '.bak');
  261. helper::createIniFile($this->_conf, $options);
  262. $_POST = self::$paste;
  263. $_POST['opendiscussion'] = 'neither 1 nor 0';
  264. $_SERVER['REMOTE_ADDR'] = '::1';
  265. ob_start();
  266. new zerobin;
  267. $content = ob_get_contents();
  268. $response = json_decode($content, true);
  269. $this->assertEquals(1, $response['status'], 'outputs error status');
  270. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  271. }
  272. /**
  273. * @runInSeparateProcess
  274. */
  275. public function testCreateValidNick()
  276. {
  277. $this->reset();
  278. $options = parse_ini_file($this->_conf, true);
  279. $options['traffic']['limit'] = 0;
  280. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  281. rename($this->_conf, $this->_conf . '.bak');
  282. helper::createIniFile($this->_conf, $options);
  283. $_POST = self::$paste;
  284. $_POST['nickname'] = self::$comment['meta']['nickname'];
  285. $_SERVER['REMOTE_ADDR'] = '::1';
  286. ob_start();
  287. new zerobin;
  288. $content = ob_get_contents();
  289. $response = json_decode($content, true);
  290. $this->assertEquals(0, $response['status'], 'outputs status');
  291. $this->assertEquals(
  292. hash_hmac('sha1', $response['id'], serversalt::get()),
  293. $response['deletetoken'],
  294. 'outputs valid delete token'
  295. );
  296. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  297. }
  298. /**
  299. * @runInSeparateProcess
  300. */
  301. public function testCreateInvalidNick()
  302. {
  303. $this->reset();
  304. $options = parse_ini_file($this->_conf, true);
  305. $options['traffic']['limit'] = 0;
  306. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  307. rename($this->_conf, $this->_conf . '.bak');
  308. helper::createIniFile($this->_conf, $options);
  309. $_POST = self::$paste;
  310. $_POST['nickname'] = 'foo';
  311. $_SERVER['REMOTE_ADDR'] = '::1';
  312. ob_start();
  313. new zerobin;
  314. $content = ob_get_contents();
  315. $response = json_decode($content, true);
  316. $this->assertEquals(1, $response['status'], 'outputs error status');
  317. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  318. }
  319. /**
  320. * @runInSeparateProcess
  321. */
  322. public function testCreateComment()
  323. {
  324. $this->reset();
  325. $options = parse_ini_file($this->_conf, true);
  326. $options['traffic']['limit'] = 0;
  327. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  328. rename($this->_conf, $this->_conf . '.bak');
  329. helper::createIniFile($this->_conf, $options);
  330. $_POST = self::$comment;
  331. $_POST['pasteid'] = self::$pasteid;
  332. $_POST['parentid'] = self::$pasteid;
  333. $_SERVER['REMOTE_ADDR'] = '::1';
  334. $this->_model->create(self::$pasteid, self::$paste);
  335. ob_start();
  336. new zerobin;
  337. $content = ob_get_contents();
  338. $response = json_decode($content, true);
  339. $this->assertEquals(0, $response['status'], 'outputs status');
  340. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, $response['id']), 'paste exists after posting data');
  341. }
  342. /**
  343. * @runInSeparateProcess
  344. */
  345. public function testCreateInvalidComment()
  346. {
  347. $this->reset();
  348. $options = parse_ini_file($this->_conf, true);
  349. $options['traffic']['limit'] = 0;
  350. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  351. rename($this->_conf, $this->_conf . '.bak');
  352. helper::createIniFile($this->_conf, $options);
  353. $_POST = self::$comment;
  354. $_POST['pasteid'] = self::$pasteid;
  355. $_POST['parentid'] = 'foo';
  356. $_SERVER['REMOTE_ADDR'] = '::1';
  357. $this->_model->create(self::$pasteid, self::$paste);
  358. ob_start();
  359. new zerobin;
  360. $content = ob_get_contents();
  361. $response = json_decode($content, true);
  362. $this->assertEquals(1, $response['status'], 'outputs error status');
  363. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  364. }
  365. /**
  366. * @runInSeparateProcess
  367. */
  368. public function testCreateCommentDiscussionDisabled()
  369. {
  370. $this->reset();
  371. $options = parse_ini_file($this->_conf, true);
  372. $options['traffic']['limit'] = 0;
  373. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  374. rename($this->_conf, $this->_conf . '.bak');
  375. helper::createIniFile($this->_conf, $options);
  376. $_POST = self::$comment;
  377. $_POST['pasteid'] = self::$pasteid;
  378. $_POST['parentid'] = self::$pasteid;
  379. $_SERVER['REMOTE_ADDR'] = '::1';
  380. $paste = self::$paste;
  381. $paste['meta']['opendiscussion'] = false;
  382. $this->_model->create(self::$pasteid, $paste);
  383. ob_start();
  384. new zerobin;
  385. $content = ob_get_contents();
  386. $response = json_decode($content, true);
  387. $this->assertEquals(1, $response['status'], 'outputs error status');
  388. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  389. }
  390. /**
  391. * @runInSeparateProcess
  392. */
  393. public function testCreateCommentInvalidPaste()
  394. {
  395. $this->reset();
  396. $options = parse_ini_file($this->_conf, true);
  397. $options['traffic']['limit'] = 0;
  398. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  399. rename($this->_conf, $this->_conf . '.bak');
  400. helper::createIniFile($this->_conf, $options);
  401. $_POST = self::$comment;
  402. $_POST['pasteid'] = self::$pasteid;
  403. $_POST['parentid'] = self::$pasteid;
  404. $_SERVER['REMOTE_ADDR'] = '::1';
  405. ob_start();
  406. new zerobin;
  407. $content = ob_get_contents();
  408. $response = json_decode($content, true);
  409. $this->assertEquals(1, $response['status'], 'outputs error status');
  410. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  411. }
  412. /**
  413. * @runInSeparateProcess
  414. */
  415. public function testCreateDuplicateComment()
  416. {
  417. $this->reset();
  418. $options = parse_ini_file($this->_conf, true);
  419. $options['traffic']['limit'] = 0;
  420. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  421. rename($this->_conf, $this->_conf . '.bak');
  422. helper::createIniFile($this->_conf, $options);
  423. $this->_model->create(self::$pasteid, self::$paste);
  424. $this->_model->createComment(self::$pasteid, self::$pasteid, self::$commentid, self::$comment);
  425. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment exists before posting data');
  426. $_POST = self::$comment;
  427. $_POST['pasteid'] = self::$pasteid;
  428. $_POST['parentid'] = self::$pasteid;
  429. $_SERVER['REMOTE_ADDR'] = '::1';
  430. ob_start();
  431. new zerobin;
  432. $content = ob_get_contents();
  433. $response = json_decode($content, true);
  434. $this->assertEquals(1, $response['status'], 'outputs error status');
  435. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  436. }
  437. /**
  438. * @runInSeparateProcess
  439. */
  440. public function testRead()
  441. {
  442. $this->reset();
  443. $this->_model->create(self::$pasteid, self::$paste);
  444. $_SERVER['QUERY_STRING'] = self::$pasteid;
  445. ob_start();
  446. new zerobin;
  447. $content = ob_get_contents();
  448. $this->assertTag(
  449. array(
  450. 'id' => 'cipherdata',
  451. 'content' => htmlspecialchars(json_encode(self::$paste), ENT_NOQUOTES)
  452. ),
  453. $content,
  454. 'outputs data correctly'
  455. );
  456. }
  457. /**
  458. * @runInSeparateProcess
  459. */
  460. public function testReadInvalidId()
  461. {
  462. $this->reset();
  463. $_SERVER['QUERY_STRING'] = 'foo';
  464. ob_start();
  465. new zerobin;
  466. $content = ob_get_contents();
  467. $this->assertTag(
  468. array(
  469. 'id' => 'errormessage',
  470. 'content' => 'Invalid paste ID'
  471. ),
  472. $content,
  473. 'outputs error correctly'
  474. );
  475. }
  476. /**
  477. * @runInSeparateProcess
  478. */
  479. public function testReadNonexisting()
  480. {
  481. $this->reset();
  482. $_SERVER['QUERY_STRING'] = self::$pasteid;
  483. ob_start();
  484. new zerobin;
  485. $content = ob_get_contents();
  486. $this->assertTag(
  487. array(
  488. 'id' => 'errormessage',
  489. 'content' => 'Paste does not exist'
  490. ),
  491. $content,
  492. 'outputs error correctly'
  493. );
  494. }
  495. /**
  496. * @runInSeparateProcess
  497. */
  498. public function testReadExpired()
  499. {
  500. $this->reset();
  501. $expiredPaste = self::$paste;
  502. $expiredPaste['meta']['expire_date'] = $expiredPaste['meta']['postdate'];
  503. $this->_model->create(self::$pasteid, $expiredPaste);
  504. $_SERVER['QUERY_STRING'] = self::$pasteid;
  505. ob_start();
  506. new zerobin;
  507. $content = ob_get_contents();
  508. $this->assertTag(
  509. array(
  510. 'id' => 'errormessage',
  511. 'content' => 'Paste does not exist'
  512. ),
  513. $content,
  514. 'outputs error correctly'
  515. );
  516. }
  517. /**
  518. * @runInSeparateProcess
  519. */
  520. public function testReadBurn()
  521. {
  522. $this->reset();
  523. $burnPaste = self::$paste;
  524. $burnPaste['meta']['burnafterreading'] = true;
  525. $this->_model->create(self::$pasteid, $burnPaste);
  526. $_SERVER['QUERY_STRING'] = self::$pasteid;
  527. ob_start();
  528. new zerobin;
  529. $content = ob_get_contents();
  530. $this->assertTag(
  531. array(
  532. 'id' => 'cipherdata',
  533. 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
  534. ),
  535. $content,
  536. 'outputs data correctly'
  537. );
  538. }
  539. /**
  540. * @runInSeparateProcess
  541. */
  542. public function testReadJson()
  543. {
  544. $this->reset();
  545. $this->_model->create(self::$pasteid, self::$paste);
  546. $_SERVER['QUERY_STRING'] = self::$pasteid . '&json';
  547. ob_start();
  548. new zerobin;
  549. $content = ob_get_contents();
  550. $response = json_decode($content, true);
  551. $this->assertEquals(0, $response['status'], 'outputs success status');
  552. $this->assertEquals(array(self::$paste), $response['messages'], 'outputs data correctly');
  553. }
  554. /**
  555. * @runInSeparateProcess
  556. */
  557. public function testReadInvalidJson()
  558. {
  559. $this->reset();
  560. $_SERVER['QUERY_STRING'] = self::$pasteid . '&json';
  561. ob_start();
  562. new zerobin;
  563. $content = ob_get_contents();
  564. $response = json_decode($content, true);
  565. $this->assertEquals(1, $response['status'], 'outputs error status');
  566. }
  567. /**
  568. * @runInSeparateProcess
  569. */
  570. public function testDelete()
  571. {
  572. $this->reset();
  573. $this->_model->create(self::$pasteid, self::$paste);
  574. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  575. $_GET['pasteid'] = self::$pasteid;
  576. $_GET['deletetoken'] = hash_hmac('sha1', self::$pasteid, serversalt::get());
  577. ob_start();
  578. new zerobin;
  579. $content = ob_get_contents();
  580. $this->assertTag(
  581. array(
  582. 'id' => 'status',
  583. 'content' => 'Paste was properly deleted'
  584. ),
  585. $content,
  586. 'outputs deleted status correctly'
  587. );
  588. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  589. }
  590. /**
  591. * @runInSeparateProcess
  592. */
  593. public function testDeleteInvalidId()
  594. {
  595. $this->reset();
  596. $this->_model->create(self::$pasteid, self::$paste);
  597. $_GET['pasteid'] = 'foo';
  598. $_GET['deletetoken'] = 'bar';
  599. ob_start();
  600. new zerobin;
  601. $content = ob_get_contents();
  602. $this->assertTag(
  603. array(
  604. 'id' => 'errormessage',
  605. 'content' => 'Invalid paste ID'
  606. ),
  607. $content,
  608. 'outputs delete error correctly'
  609. );
  610. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  611. }
  612. /**
  613. * @runInSeparateProcess
  614. */
  615. public function testDeleteInexistantId()
  616. {
  617. $this->reset();
  618. $_GET['pasteid'] = self::$pasteid;
  619. $_GET['deletetoken'] = 'bar';
  620. ob_start();
  621. new zerobin;
  622. $content = ob_get_contents();
  623. $this->assertTag(
  624. array(
  625. 'id' => 'errormessage',
  626. 'content' => 'Paste does not exist'
  627. ),
  628. $content,
  629. 'outputs delete error correctly'
  630. );
  631. }
  632. /**
  633. * @runInSeparateProcess
  634. */
  635. public function testDeleteInvalidToken()
  636. {
  637. $this->reset();
  638. $this->_model->create(self::$pasteid, self::$paste);
  639. $_GET['pasteid'] = self::$pasteid;
  640. $_GET['deletetoken'] = 'bar';
  641. ob_start();
  642. new zerobin;
  643. $content = ob_get_contents();
  644. $this->assertTag(
  645. array(
  646. 'id' => 'errormessage',
  647. 'content' => 'Wrong deletion token'
  648. ),
  649. $content,
  650. 'outputs delete error correctly'
  651. );
  652. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  653. }
  654. /**
  655. * @runInSeparateProcess
  656. */
  657. public function testDeleteBurnAfterReading()
  658. {
  659. $this->reset();
  660. $burnPaste = self::$paste;
  661. $burnPaste['meta']['burnafterreading'] = true;
  662. $this->_model->create(self::$pasteid, $burnPaste);
  663. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  664. $_GET['pasteid'] = self::$pasteid;
  665. $_GET['deletetoken'] = 'burnafterreading';
  666. ob_start();
  667. new zerobin;
  668. $content = ob_get_contents();
  669. $response = json_decode($content, true);
  670. $this->assertEquals(0, $response['status'], 'outputs status');
  671. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  672. }
  673. /**
  674. * @runInSeparateProcess
  675. */
  676. public function testDeleteInvalidBurnAfterReading()
  677. {
  678. $this->reset();
  679. $this->_model->create(self::$pasteid, self::$paste);
  680. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  681. $_GET['pasteid'] = self::$pasteid;
  682. $_GET['deletetoken'] = 'burnafterreading';
  683. ob_start();
  684. new zerobin;
  685. $content = ob_get_contents();
  686. $response = json_decode($content, true);
  687. $this->assertEquals(1, $response['status'], 'outputs status');
  688. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  689. }
  690. /**
  691. * @runInSeparateProcess
  692. */
  693. public function testDeleteExpired()
  694. {
  695. $this->reset();
  696. $expiredPaste = self::$paste;
  697. $expiredPaste['meta']['expire_date'] = 1000;
  698. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste does not exist before being created');
  699. $this->_model->create(self::$pasteid, $expiredPaste);
  700. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  701. $_GET['pasteid'] = self::$pasteid;
  702. $_GET['deletetoken'] = 'does not matter in this context, but has to be set';
  703. ob_start();
  704. new zerobin;
  705. $content = ob_get_contents();
  706. $this->assertTag(
  707. array(
  708. 'id' => 'errormessage',
  709. 'content' => 'Paste does not exist'
  710. ),
  711. $content,
  712. 'outputs error correctly'
  713. );
  714. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  715. }
  716. }