zerobin.php 29 KB

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