zerobin.php 29 KB

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