zerobin.php 30 KB

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