zerobin.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 testConfMissingExpireLabel()
  103. {
  104. $this->reset();
  105. $options = parse_ini_file($this->_conf, true);
  106. $options['expire_options']['foobar123'] = 10;
  107. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  108. rename($this->_conf, $this->_conf . '.bak');
  109. helper::createIniFile($this->_conf, $options);
  110. ini_set('magic_quotes_gpc', 1);
  111. ob_start();
  112. new zerobin;
  113. $content = ob_get_contents();
  114. }
  115. /**
  116. * @runInSeparateProcess
  117. */
  118. public function testCreate()
  119. {
  120. $this->reset();
  121. $_POST = self::$paste;
  122. $_SERVER['REMOTE_ADDR'] = '::1';
  123. ob_start();
  124. new zerobin;
  125. $content = ob_get_contents();
  126. $response = json_decode($content, true);
  127. $this->assertEquals(0, $response['status'], 'outputs status');
  128. $this->assertEquals(
  129. hash_hmac('sha1', $response['id'], serversalt::get()),
  130. $response['deletetoken'],
  131. 'outputs valid delete token'
  132. );
  133. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  134. }
  135. /**
  136. * @runInSeparateProcess
  137. */
  138. public function testCreateInvalidTimelimit()
  139. {
  140. $this->reset();
  141. $_POST = self::$paste;
  142. $_SERVER['REMOTE_ADDR'] = '::1';
  143. ob_start();
  144. new zerobin;
  145. $content = ob_get_contents();
  146. $response = json_decode($content, true);
  147. $this->assertEquals(1, $response['status'], 'outputs error status');
  148. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  149. }
  150. /**
  151. * @runInSeparateProcess
  152. */
  153. public function testCreateInvalidSize()
  154. {
  155. $this->reset();
  156. $options = parse_ini_file($this->_conf, true);
  157. $options['main']['sizelimit'] = 10;
  158. $options['traffic']['limit'] = 0;
  159. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  160. rename($this->_conf, $this->_conf . '.bak');
  161. helper::createIniFile($this->_conf, $options);
  162. $_POST = self::$paste;
  163. $_SERVER['REMOTE_ADDR'] = '::1';
  164. ob_start();
  165. new zerobin;
  166. $content = ob_get_contents();
  167. $response = json_decode($content, true);
  168. $this->assertEquals(1, $response['status'], 'outputs error status');
  169. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  170. }
  171. /**
  172. * @runInSeparateProcess
  173. */
  174. public function testCreateDuplicateId()
  175. {
  176. $this->reset();
  177. $options = parse_ini_file($this->_conf, true);
  178. $options['traffic']['limit'] = 0;
  179. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  180. rename($this->_conf, $this->_conf . '.bak');
  181. helper::createIniFile($this->_conf, $options);
  182. $this->_model->create(self::$pasteid, self::$paste);
  183. $_POST = self::$paste;
  184. $_SERVER['REMOTE_ADDR'] = '::1';
  185. ob_start();
  186. new zerobin;
  187. $content = ob_get_contents();
  188. $response = json_decode($content, true);
  189. $this->assertEquals(1, $response['status'], 'outputs error status');
  190. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  191. }
  192. /**
  193. * @runInSeparateProcess
  194. */
  195. public function testCreateValidExpire()
  196. {
  197. $this->reset();
  198. $options = parse_ini_file($this->_conf, true);
  199. $options['traffic']['limit'] = 0;
  200. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  201. rename($this->_conf, $this->_conf . '.bak');
  202. helper::createIniFile($this->_conf, $options);
  203. $_POST = self::$paste;
  204. $_POST['expire'] = '5min';
  205. $_SERVER['REMOTE_ADDR'] = '::1';
  206. ob_start();
  207. new zerobin;
  208. $content = ob_get_contents();
  209. $response = json_decode($content, true);
  210. $this->assertEquals(0, $response['status'], 'outputs status');
  211. $this->assertEquals(
  212. hash_hmac('sha1', $response['id'], serversalt::get()),
  213. $response['deletetoken'],
  214. 'outputs valid delete token'
  215. );
  216. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  217. }
  218. /**
  219. * @runInSeparateProcess
  220. */
  221. public function testCreateInvalidExpire()
  222. {
  223. $this->reset();
  224. $options = parse_ini_file($this->_conf, true);
  225. $options['traffic']['limit'] = 0;
  226. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  227. rename($this->_conf, $this->_conf . '.bak');
  228. helper::createIniFile($this->_conf, $options);
  229. $_POST = self::$paste;
  230. $_POST['expire'] = 'foo';
  231. $_SERVER['REMOTE_ADDR'] = '::1';
  232. ob_start();
  233. new zerobin;
  234. $content = ob_get_contents();
  235. $response = json_decode($content, true);
  236. $this->assertEquals(0, $response['status'], 'outputs status');
  237. $this->assertEquals(
  238. hash_hmac('sha1', $response['id'], serversalt::get()),
  239. $response['deletetoken'],
  240. 'outputs valid delete token'
  241. );
  242. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  243. }
  244. /**
  245. * @runInSeparateProcess
  246. */
  247. public function testCreateInvalidBurn()
  248. {
  249. $this->reset();
  250. $options = parse_ini_file($this->_conf, true);
  251. $options['traffic']['limit'] = 0;
  252. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  253. rename($this->_conf, $this->_conf . '.bak');
  254. helper::createIniFile($this->_conf, $options);
  255. $_POST = self::$paste;
  256. $_POST['burnafterreading'] = 'neither 1 nor 0';
  257. $_SERVER['REMOTE_ADDR'] = '::1';
  258. ob_start();
  259. new zerobin;
  260. $content = ob_get_contents();
  261. $response = json_decode($content, true);
  262. $this->assertEquals(1, $response['status'], 'outputs error status');
  263. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  264. }
  265. /**
  266. * @runInSeparateProcess
  267. */
  268. public function testCreateInvalidOpenDiscussion()
  269. {
  270. $this->reset();
  271. $options = parse_ini_file($this->_conf, true);
  272. $options['traffic']['limit'] = 0;
  273. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  274. rename($this->_conf, $this->_conf . '.bak');
  275. helper::createIniFile($this->_conf, $options);
  276. $_POST = self::$paste;
  277. $_POST['opendiscussion'] = 'neither 1 nor 0';
  278. $_SERVER['REMOTE_ADDR'] = '::1';
  279. ob_start();
  280. new zerobin;
  281. $content = ob_get_contents();
  282. $response = json_decode($content, true);
  283. $this->assertEquals(1, $response['status'], 'outputs error status');
  284. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  285. }
  286. /**
  287. * @runInSeparateProcess
  288. */
  289. public function testCreateValidNick()
  290. {
  291. $this->reset();
  292. $options = parse_ini_file($this->_conf, true);
  293. $options['traffic']['limit'] = 0;
  294. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  295. rename($this->_conf, $this->_conf . '.bak');
  296. helper::createIniFile($this->_conf, $options);
  297. $_POST = self::$paste;
  298. $_POST['nickname'] = self::$comment['meta']['nickname'];
  299. $_SERVER['REMOTE_ADDR'] = '::1';
  300. ob_start();
  301. new zerobin;
  302. $content = ob_get_contents();
  303. $response = json_decode($content, true);
  304. $this->assertEquals(0, $response['status'], 'outputs status');
  305. $this->assertEquals(
  306. hash_hmac('sha1', $response['id'], serversalt::get()),
  307. $response['deletetoken'],
  308. 'outputs valid delete token'
  309. );
  310. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  311. }
  312. /**
  313. * @runInSeparateProcess
  314. */
  315. public function testCreateInvalidNick()
  316. {
  317. $this->reset();
  318. $options = parse_ini_file($this->_conf, true);
  319. $options['traffic']['limit'] = 0;
  320. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  321. rename($this->_conf, $this->_conf . '.bak');
  322. helper::createIniFile($this->_conf, $options);
  323. $_POST = self::$paste;
  324. $_POST['nickname'] = 'foo';
  325. $_SERVER['REMOTE_ADDR'] = '::1';
  326. ob_start();
  327. new zerobin;
  328. $content = ob_get_contents();
  329. $response = json_decode($content, true);
  330. $this->assertEquals(1, $response['status'], 'outputs error status');
  331. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  332. }
  333. /**
  334. * @runInSeparateProcess
  335. */
  336. public function testCreateComment()
  337. {
  338. $this->reset();
  339. $options = parse_ini_file($this->_conf, true);
  340. $options['traffic']['limit'] = 0;
  341. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  342. rename($this->_conf, $this->_conf . '.bak');
  343. helper::createIniFile($this->_conf, $options);
  344. $_POST = self::$comment;
  345. $_POST['pasteid'] = self::$pasteid;
  346. $_POST['parentid'] = self::$pasteid;
  347. $_SERVER['REMOTE_ADDR'] = '::1';
  348. $this->_model->create(self::$pasteid, self::$paste);
  349. ob_start();
  350. new zerobin;
  351. $content = ob_get_contents();
  352. $response = json_decode($content, true);
  353. $this->assertEquals(0, $response['status'], 'outputs status');
  354. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, $response['id']), 'paste exists after posting data');
  355. }
  356. /**
  357. * @runInSeparateProcess
  358. */
  359. public function testCreateInvalidComment()
  360. {
  361. $this->reset();
  362. $options = parse_ini_file($this->_conf, true);
  363. $options['traffic']['limit'] = 0;
  364. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  365. rename($this->_conf, $this->_conf . '.bak');
  366. helper::createIniFile($this->_conf, $options);
  367. $_POST = self::$comment;
  368. $_POST['pasteid'] = self::$pasteid;
  369. $_POST['parentid'] = 'foo';
  370. $_SERVER['REMOTE_ADDR'] = '::1';
  371. $this->_model->create(self::$pasteid, self::$paste);
  372. ob_start();
  373. new zerobin;
  374. $content = ob_get_contents();
  375. $response = json_decode($content, true);
  376. $this->assertEquals(1, $response['status'], 'outputs error status');
  377. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  378. }
  379. /**
  380. * @runInSeparateProcess
  381. */
  382. public function testCreateCommentDiscussionDisabled()
  383. {
  384. $this->reset();
  385. $options = parse_ini_file($this->_conf, true);
  386. $options['traffic']['limit'] = 0;
  387. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  388. rename($this->_conf, $this->_conf . '.bak');
  389. helper::createIniFile($this->_conf, $options);
  390. $_POST = self::$comment;
  391. $_POST['pasteid'] = self::$pasteid;
  392. $_POST['parentid'] = self::$pasteid;
  393. $_SERVER['REMOTE_ADDR'] = '::1';
  394. $paste = self::$paste;
  395. $paste['meta']['opendiscussion'] = false;
  396. $this->_model->create(self::$pasteid, $paste);
  397. ob_start();
  398. new zerobin;
  399. $content = ob_get_contents();
  400. $response = json_decode($content, true);
  401. $this->assertEquals(1, $response['status'], 'outputs error status');
  402. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  403. }
  404. /**
  405. * @runInSeparateProcess
  406. */
  407. public function testCreateCommentInvalidPaste()
  408. {
  409. $this->reset();
  410. $options = parse_ini_file($this->_conf, true);
  411. $options['traffic']['limit'] = 0;
  412. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  413. rename($this->_conf, $this->_conf . '.bak');
  414. helper::createIniFile($this->_conf, $options);
  415. $_POST = self::$comment;
  416. $_POST['pasteid'] = self::$pasteid;
  417. $_POST['parentid'] = self::$pasteid;
  418. $_SERVER['REMOTE_ADDR'] = '::1';
  419. ob_start();
  420. new zerobin;
  421. $content = ob_get_contents();
  422. $response = json_decode($content, true);
  423. $this->assertEquals(1, $response['status'], 'outputs error status');
  424. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  425. }
  426. /**
  427. * @runInSeparateProcess
  428. */
  429. public function testCreateDuplicateComment()
  430. {
  431. $this->reset();
  432. $options = parse_ini_file($this->_conf, true);
  433. $options['traffic']['limit'] = 0;
  434. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  435. rename($this->_conf, $this->_conf . '.bak');
  436. helper::createIniFile($this->_conf, $options);
  437. $this->_model->createComment(self::$pasteid, self::$pasteid, self::$commentid, self::$comment);
  438. $_POST = self::$comment;
  439. $_POST['pasteid'] = self::$pasteid;
  440. $_POST['parentid'] = self::$pasteid;
  441. $_SERVER['REMOTE_ADDR'] = '::1';
  442. ob_start();
  443. new zerobin;
  444. $content = ob_get_contents();
  445. $response = json_decode($content, true);
  446. $this->assertEquals(1, $response['status'], 'outputs error status');
  447. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  448. }
  449. /**
  450. * @runInSeparateProcess
  451. */
  452. public function testRead()
  453. {
  454. $this->reset();
  455. $this->_model->create(self::$pasteid, self::$paste);
  456. $_SERVER['QUERY_STRING'] = self::$pasteid;
  457. ob_start();
  458. new zerobin;
  459. $content = ob_get_contents();
  460. $this->assertTag(
  461. array(
  462. 'id' => 'cipherdata',
  463. 'content' => htmlspecialchars(json_encode(self::$paste), ENT_NOQUOTES)
  464. ),
  465. $content,
  466. 'outputs data correctly'
  467. );
  468. }
  469. /**
  470. * @runInSeparateProcess
  471. */
  472. public function testReadInvalidId()
  473. {
  474. $this->reset();
  475. $_SERVER['QUERY_STRING'] = 'foo';
  476. ob_start();
  477. new zerobin;
  478. $content = ob_get_contents();
  479. $this->assertTag(
  480. array(
  481. 'id' => 'errormessage',
  482. 'content' => 'Invalid paste ID'
  483. ),
  484. $content,
  485. 'outputs error correctly'
  486. );
  487. }
  488. /**
  489. * @runInSeparateProcess
  490. */
  491. public function testReadNonexisting()
  492. {
  493. $this->reset();
  494. $_SERVER['QUERY_STRING'] = self::$pasteid;
  495. ob_start();
  496. new zerobin;
  497. $content = ob_get_contents();
  498. $this->assertTag(
  499. array(
  500. 'id' => 'errormessage',
  501. 'content' => 'Paste does not exist'
  502. ),
  503. $content,
  504. 'outputs error correctly'
  505. );
  506. }
  507. /**
  508. * @runInSeparateProcess
  509. */
  510. public function testReadExpired()
  511. {
  512. $this->reset();
  513. $expiredPaste = self::$paste;
  514. $expiredPaste['meta']['expire_date'] = $expiredPaste['meta']['postdate'];
  515. $this->_model->create(self::$pasteid, $expiredPaste);
  516. $_SERVER['QUERY_STRING'] = self::$pasteid;
  517. ob_start();
  518. new zerobin;
  519. $content = ob_get_contents();
  520. $this->assertTag(
  521. array(
  522. 'id' => 'errormessage',
  523. 'content' => 'Paste does not exist'
  524. ),
  525. $content,
  526. 'outputs error correctly'
  527. );
  528. }
  529. /**
  530. * @runInSeparateProcess
  531. */
  532. public function testReadBurn()
  533. {
  534. $this->reset();
  535. $burnPaste = self::$paste;
  536. $burnPaste['meta']['burnafterreading'] = true;
  537. $this->_model->create(self::$pasteid, $burnPaste);
  538. $_SERVER['QUERY_STRING'] = self::$pasteid;
  539. ob_start();
  540. new zerobin;
  541. $content = ob_get_contents();
  542. $this->assertTag(
  543. array(
  544. 'id' => 'cipherdata',
  545. 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
  546. ),
  547. $content,
  548. 'outputs data correctly'
  549. );
  550. }
  551. /**
  552. * @runInSeparateProcess
  553. */
  554. public function testReadJson()
  555. {
  556. $this->reset();
  557. $this->_model->create(self::$pasteid, self::$paste);
  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(0, $response['status'], 'outputs success status');
  564. $this->assertEquals(array(self::$paste), $response['messages'], 'outputs data correctly');
  565. }
  566. /**
  567. * @runInSeparateProcess
  568. */
  569. public function testReadInvalidJson()
  570. {
  571. $this->reset();
  572. $_SERVER['QUERY_STRING'] = self::$pasteid . '&json';
  573. ob_start();
  574. new zerobin;
  575. $content = ob_get_contents();
  576. $response = json_decode($content, true);
  577. $this->assertEquals(1, $response['status'], 'outputs error status');
  578. }
  579. /**
  580. * @runInSeparateProcess
  581. */
  582. public function testDelete()
  583. {
  584. $this->reset();
  585. $this->_model->create(self::$pasteid, self::$paste);
  586. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  587. $_GET['pasteid'] = self::$pasteid;
  588. $_GET['deletetoken'] = hash_hmac('sha1', self::$pasteid, serversalt::get());
  589. ob_start();
  590. new zerobin;
  591. $content = ob_get_contents();
  592. $this->assertTag(
  593. array(
  594. 'id' => 'status',
  595. 'content' => 'Paste was properly deleted'
  596. ),
  597. $content,
  598. 'outputs deleted status correctly'
  599. );
  600. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  601. }
  602. /**
  603. * @runInSeparateProcess
  604. */
  605. public function testDeleteInvalidId()
  606. {
  607. $this->reset();
  608. $this->_model->create(self::$pasteid, self::$paste);
  609. $_GET['pasteid'] = 'foo';
  610. $_GET['deletetoken'] = 'bar';
  611. ob_start();
  612. new zerobin;
  613. $content = ob_get_contents();
  614. $this->assertTag(
  615. array(
  616. 'id' => 'errormessage',
  617. 'content' => 'Invalid paste ID'
  618. ),
  619. $content,
  620. 'outputs delete error correctly'
  621. );
  622. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  623. }
  624. /**
  625. * @runInSeparateProcess
  626. */
  627. public function testDeleteInexistantId()
  628. {
  629. $this->reset();
  630. $_GET['pasteid'] = self::$pasteid;
  631. $_GET['deletetoken'] = 'bar';
  632. ob_start();
  633. new zerobin;
  634. $content = ob_get_contents();
  635. $this->assertTag(
  636. array(
  637. 'id' => 'errormessage',
  638. 'content' => 'Paste does not exist'
  639. ),
  640. $content,
  641. 'outputs delete error correctly'
  642. );
  643. }
  644. /**
  645. * @runInSeparateProcess
  646. */
  647. public function testDeleteInvalidToken()
  648. {
  649. $this->reset();
  650. $this->_model->create(self::$pasteid, self::$paste);
  651. $_GET['pasteid'] = self::$pasteid;
  652. $_GET['deletetoken'] = 'bar';
  653. ob_start();
  654. new zerobin;
  655. $content = ob_get_contents();
  656. $this->assertTag(
  657. array(
  658. 'id' => 'errormessage',
  659. 'content' => 'Wrong deletion token'
  660. ),
  661. $content,
  662. 'outputs delete error correctly'
  663. );
  664. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  665. }
  666. /**
  667. * @runInSeparateProcess
  668. */
  669. public function testDeleteBurnAfterReading()
  670. {
  671. $this->reset();
  672. $burnPaste = self::$paste;
  673. $burnPaste['meta']['burnafterreading'] = true;
  674. $this->_model->create(self::$pasteid, $burnPaste);
  675. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  676. $_GET['pasteid'] = self::$pasteid;
  677. $_GET['deletetoken'] = 'burnafterreading';
  678. ob_start();
  679. new zerobin;
  680. $content = ob_get_contents();
  681. $response = json_decode($content, true);
  682. $this->assertEquals(0, $response['status'], 'outputs status');
  683. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  684. }
  685. /**
  686. * @runInSeparateProcess
  687. */
  688. public function testDeleteInvalidBurnAfterReading()
  689. {
  690. $this->reset();
  691. $this->_model->create(self::$pasteid, self::$paste);
  692. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  693. $_GET['pasteid'] = self::$pasteid;
  694. $_GET['deletetoken'] = 'burnafterreading';
  695. ob_start();
  696. new zerobin;
  697. $content = ob_get_contents();
  698. $response = json_decode($content, true);
  699. $this->assertEquals(1, $response['status'], 'outputs status');
  700. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  701. }
  702. /**
  703. * @runInSeparateProcess
  704. */
  705. public function testDeleteExpired()
  706. {
  707. $this->reset();
  708. $expiredPaste = self::$paste;
  709. $expiredPaste['meta']['expire_date'] = $expiredPaste['meta']['postdate'];
  710. $this->_model->create(self::$pasteid, $expiredPaste);
  711. $_SERVER['QUERY_STRING'] = self::$pasteid;
  712. ob_start();
  713. new zerobin;
  714. $content = ob_get_contents();
  715. $this->assertTag(
  716. array(
  717. 'id' => 'errormessage',
  718. 'content' => 'Paste does not exist'
  719. ),
  720. $content,
  721. 'outputs error correctly'
  722. );
  723. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  724. }
  725. }