zerobin.php 25 KB

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