zerobin.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. $paste = $this->_model->read($response['id']);
  239. $this->assertEquals(time() + 300, $paste->meta->expire_date, 'time is set correctly');
  240. }
  241. /**
  242. * @runInSeparateProcess
  243. */
  244. public function testCreateValidExpireWithDiscussion()
  245. {
  246. $this->reset();
  247. $options = parse_ini_file(CONF, true);
  248. $options['traffic']['limit'] = 0;
  249. helper::confBackup();
  250. helper::createIniFile(CONF, $options);
  251. $_POST = helper::getPaste();
  252. $_POST['expire'] = '5min';
  253. $_POST['opendiscussion'] = '1';
  254. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  255. $_SERVER['REQUEST_METHOD'] = 'POST';
  256. $_SERVER['REMOTE_ADDR'] = '::1';
  257. ob_start();
  258. new zerobin;
  259. $content = ob_get_contents();
  260. $response = json_decode($content, true);
  261. $this->assertEquals(0, $response['status'], 'outputs status');
  262. $this->assertEquals(
  263. hash_hmac('sha1', $response['id'], serversalt::get()),
  264. $response['deletetoken'],
  265. 'outputs valid delete token'
  266. );
  267. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  268. $paste = $this->_model->read($response['id']);
  269. $this->assertEquals(time() + 300, $paste->meta->expire_date, 'time is set correctly');
  270. $this->assertEquals(1, $paste->meta->opendiscussion, 'time is set correctly');
  271. }
  272. /**
  273. * @runInSeparateProcess
  274. */
  275. public function testCreateInvalidExpire()
  276. {
  277. $this->reset();
  278. $options = parse_ini_file(CONF, true);
  279. $options['traffic']['limit'] = 0;
  280. helper::confBackup();
  281. helper::createIniFile(CONF, $options);
  282. $_POST = helper::getPaste();
  283. $_POST['expire'] = 'foo';
  284. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  285. $_SERVER['REQUEST_METHOD'] = 'POST';
  286. $_SERVER['REMOTE_ADDR'] = '::1';
  287. ob_start();
  288. new zerobin;
  289. $content = ob_get_contents();
  290. $response = json_decode($content, true);
  291. $this->assertEquals(0, $response['status'], 'outputs status');
  292. $this->assertEquals(
  293. hash_hmac('sha1', $response['id'], serversalt::get()),
  294. $response['deletetoken'],
  295. 'outputs valid delete token'
  296. );
  297. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  298. }
  299. /**
  300. * @runInSeparateProcess
  301. */
  302. public function testCreateInvalidBurn()
  303. {
  304. $this->reset();
  305. $options = parse_ini_file(CONF, true);
  306. $options['traffic']['limit'] = 0;
  307. helper::confBackup();
  308. helper::createIniFile(CONF, $options);
  309. $_POST = helper::getPaste();
  310. $_POST['burnafterreading'] = 'neither 1 nor 0';
  311. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  312. $_SERVER['REQUEST_METHOD'] = 'POST';
  313. $_SERVER['REMOTE_ADDR'] = '::1';
  314. ob_start();
  315. new zerobin;
  316. $content = ob_get_contents();
  317. $response = json_decode($content, true);
  318. $this->assertEquals(1, $response['status'], 'outputs error status');
  319. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  320. }
  321. /**
  322. * @runInSeparateProcess
  323. */
  324. public function testCreateInvalidOpenDiscussion()
  325. {
  326. $this->reset();
  327. $options = parse_ini_file(CONF, true);
  328. $options['traffic']['limit'] = 0;
  329. helper::confBackup();
  330. helper::createIniFile(CONF, $options);
  331. $_POST = helper::getPaste();
  332. $_POST['opendiscussion'] = 'neither 1 nor 0';
  333. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  334. $_SERVER['REQUEST_METHOD'] = 'POST';
  335. $_SERVER['REMOTE_ADDR'] = '::1';
  336. ob_start();
  337. new zerobin;
  338. $content = ob_get_contents();
  339. $response = json_decode($content, true);
  340. $this->assertEquals(1, $response['status'], 'outputs error status');
  341. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  342. }
  343. /**
  344. * @runInSeparateProcess
  345. */
  346. public function testCreateAttachment()
  347. {
  348. $this->reset();
  349. $options = parse_ini_file(CONF, true);
  350. $options['traffic']['limit'] = 0;
  351. $options['main']['fileupload'] = true;
  352. helper::confBackup();
  353. helper::createIniFile(CONF, $options);
  354. $_POST = helper::getPasteWithAttachment();
  355. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  356. $_SERVER['REQUEST_METHOD'] = 'POST';
  357. $_SERVER['REMOTE_ADDR'] = '::1';
  358. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exists before posting data');
  359. ob_start();
  360. new zerobin;
  361. $content = ob_get_contents();
  362. $response = json_decode($content, true);
  363. $this->assertEquals(0, $response['status'], 'outputs status');
  364. $this->assertEquals(
  365. hash_hmac('sha1', $response['id'], serversalt::get()),
  366. $response['deletetoken'],
  367. 'outputs valid delete token'
  368. );
  369. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  370. $original = json_decode(json_encode($_POST));
  371. $stored = $this->_model->read($response['id']);
  372. foreach (array('data', 'attachment', 'attachmentname') as $key) {
  373. $this->assertEquals($original->$key, $stored->$key);
  374. }
  375. }
  376. /**
  377. * @runInSeparateProcess
  378. */
  379. public function testCreateValidNick()
  380. {
  381. $this->reset();
  382. $options = parse_ini_file(CONF, true);
  383. $options['traffic']['limit'] = 0;
  384. helper::confBackup();
  385. helper::createIniFile(CONF, $options);
  386. $_POST = helper::getPaste();
  387. $_POST['nickname'] = helper::getComment()['meta']['nickname'];
  388. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  389. $_SERVER['REQUEST_METHOD'] = 'POST';
  390. $_SERVER['REMOTE_ADDR'] = '::1';
  391. ob_start();
  392. new zerobin;
  393. $content = ob_get_contents();
  394. $response = json_decode($content, true);
  395. $this->assertEquals(0, $response['status'], 'outputs status');
  396. $this->assertEquals(
  397. hash_hmac('sha1', $response['id'], serversalt::get()),
  398. $response['deletetoken'],
  399. 'outputs valid delete token'
  400. );
  401. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  402. }
  403. /**
  404. * @runInSeparateProcess
  405. */
  406. public function testCreateInvalidNick()
  407. {
  408. $this->reset();
  409. $options = parse_ini_file(CONF, true);
  410. $options['traffic']['limit'] = 0;
  411. helper::confBackup();
  412. helper::createIniFile(CONF, $options);
  413. $_POST = helper::getCommentPost();
  414. $_POST['pasteid'] = helper::getPasteId();
  415. $_POST['parentid'] = helper::getPasteId();
  416. $_POST['nickname'] = 'foo';
  417. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  418. $_SERVER['REQUEST_METHOD'] = 'POST';
  419. $_SERVER['REMOTE_ADDR'] = '::1';
  420. $this->_model->create(helper::getPasteId(), helper::getPaste());
  421. ob_start();
  422. new zerobin;
  423. $content = ob_get_contents();
  424. $response = json_decode($content, true);
  425. $this->assertEquals(1, $response['status'], 'outputs error status');
  426. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  427. }
  428. /**
  429. * @runInSeparateProcess
  430. */
  431. public function testCreateComment()
  432. {
  433. $this->reset();
  434. $options = parse_ini_file(CONF, true);
  435. $options['traffic']['limit'] = 0;
  436. helper::confBackup();
  437. helper::createIniFile(CONF, $options);
  438. $_POST = helper::getCommentPost();
  439. $_POST['pasteid'] = helper::getPasteId();
  440. $_POST['parentid'] = helper::getPasteId();
  441. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  442. $_SERVER['REQUEST_METHOD'] = 'POST';
  443. $_SERVER['REMOTE_ADDR'] = '::1';
  444. $this->_model->create(helper::getPasteId(), helper::getPaste());
  445. ob_start();
  446. new zerobin;
  447. $content = ob_get_contents();
  448. $response = json_decode($content, true);
  449. $this->assertEquals(0, $response['status'], 'outputs status');
  450. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), $response['id']), 'paste exists after posting data');
  451. }
  452. /**
  453. * @runInSeparateProcess
  454. */
  455. public function testCreateInvalidComment()
  456. {
  457. $this->reset();
  458. $options = parse_ini_file(CONF, true);
  459. $options['traffic']['limit'] = 0;
  460. helper::confBackup();
  461. helper::createIniFile(CONF, $options);
  462. $_POST = helper::getCommentPost();
  463. $_POST['pasteid'] = helper::getPasteId();
  464. $_POST['parentid'] = 'foo';
  465. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  466. $_SERVER['REQUEST_METHOD'] = 'POST';
  467. $_SERVER['REMOTE_ADDR'] = '::1';
  468. $this->_model->create(helper::getPasteId(), helper::getPaste());
  469. ob_start();
  470. new zerobin;
  471. $content = ob_get_contents();
  472. $response = json_decode($content, true);
  473. $this->assertEquals(1, $response['status'], 'outputs error status');
  474. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  475. }
  476. /**
  477. * @runInSeparateProcess
  478. */
  479. public function testCreateCommentDiscussionDisabled()
  480. {
  481. $this->reset();
  482. $options = parse_ini_file(CONF, true);
  483. $options['traffic']['limit'] = 0;
  484. helper::confBackup();
  485. helper::createIniFile(CONF, $options);
  486. $_POST = helper::getCommentPost();
  487. $_POST['pasteid'] = helper::getPasteId();
  488. $_POST['parentid'] = helper::getPasteId();
  489. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  490. $_SERVER['REQUEST_METHOD'] = 'POST';
  491. $_SERVER['REMOTE_ADDR'] = '::1';
  492. $paste = helper::getPaste(array('opendiscussion' => false));
  493. $this->_model->create(helper::getPasteId(), $paste);
  494. ob_start();
  495. new zerobin;
  496. $content = ob_get_contents();
  497. $response = json_decode($content, true);
  498. $this->assertEquals(1, $response['status'], 'outputs error status');
  499. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  500. }
  501. /**
  502. * @runInSeparateProcess
  503. */
  504. public function testCreateCommentInvalidPaste()
  505. {
  506. $this->reset();
  507. $options = parse_ini_file(CONF, true);
  508. $options['traffic']['limit'] = 0;
  509. helper::confBackup();
  510. helper::createIniFile(CONF, $options);
  511. $_POST = helper::getCommentPost();
  512. $_POST['pasteid'] = helper::getPasteId();
  513. $_POST['parentid'] = helper::getPasteId();
  514. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  515. $_SERVER['REQUEST_METHOD'] = 'POST';
  516. $_SERVER['REMOTE_ADDR'] = '::1';
  517. ob_start();
  518. new zerobin;
  519. $content = ob_get_contents();
  520. $response = json_decode($content, true);
  521. $this->assertEquals(1, $response['status'], 'outputs error status');
  522. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  523. }
  524. /**
  525. * @runInSeparateProcess
  526. */
  527. public function testCreateDuplicateComment()
  528. {
  529. $this->reset();
  530. $options = parse_ini_file(CONF, true);
  531. $options['traffic']['limit'] = 0;
  532. helper::confBackup();
  533. helper::createIniFile(CONF, $options);
  534. $this->_model->create(helper::getPasteId(), helper::getPaste());
  535. $this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment());
  536. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data');
  537. $_POST = helper::getCommentPost();
  538. $_POST['pasteid'] = helper::getPasteId();
  539. $_POST['parentid'] = helper::getPasteId();
  540. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  541. $_SERVER['REQUEST_METHOD'] = 'POST';
  542. $_SERVER['REMOTE_ADDR'] = '::1';
  543. ob_start();
  544. new zerobin;
  545. $content = ob_get_contents();
  546. $response = json_decode($content, true);
  547. $this->assertEquals(1, $response['status'], 'outputs error status');
  548. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  549. }
  550. /**
  551. * @runInSeparateProcess
  552. */
  553. public function testRead()
  554. {
  555. $this->reset();
  556. $this->_model->create(helper::getPasteId(), helper::getPaste());
  557. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  558. ob_start();
  559. new zerobin;
  560. $content = ob_get_contents();
  561. $this->assertTag(
  562. array(
  563. 'id' => 'cipherdata',
  564. 'content' => htmlspecialchars(json_encode(helper::getPaste()), ENT_NOQUOTES)
  565. ),
  566. $content,
  567. 'outputs data correctly'
  568. );
  569. }
  570. /**
  571. * @runInSeparateProcess
  572. */
  573. public function testReadInvalidId()
  574. {
  575. $this->reset();
  576. $_SERVER['QUERY_STRING'] = 'foo';
  577. ob_start();
  578. new zerobin;
  579. $content = ob_get_contents();
  580. $this->assertTag(
  581. array(
  582. 'id' => 'errormessage',
  583. 'content' => 'Invalid paste ID'
  584. ),
  585. $content,
  586. 'outputs error correctly'
  587. );
  588. }
  589. /**
  590. * @runInSeparateProcess
  591. */
  592. public function testReadNonexisting()
  593. {
  594. $this->reset();
  595. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  596. ob_start();
  597. new zerobin;
  598. $content = ob_get_contents();
  599. $this->assertTag(
  600. array(
  601. 'id' => 'errormessage',
  602. 'content' => 'Paste does not exist'
  603. ),
  604. $content,
  605. 'outputs error correctly'
  606. );
  607. }
  608. /**
  609. * @runInSeparateProcess
  610. */
  611. public function testReadExpired()
  612. {
  613. $this->reset();
  614. $expiredPaste = helper::getPaste(array('expire_date' => 1344803344));
  615. $this->_model->create(helper::getPasteId(), $expiredPaste);
  616. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  617. ob_start();
  618. new zerobin;
  619. $content = ob_get_contents();
  620. $this->assertTag(
  621. array(
  622. 'id' => 'errormessage',
  623. 'content' => 'Paste does not exist'
  624. ),
  625. $content,
  626. 'outputs error correctly'
  627. );
  628. }
  629. /**
  630. * @runInSeparateProcess
  631. */
  632. public function testReadBurn()
  633. {
  634. $this->reset();
  635. $burnPaste = helper::getPaste(array('burnafterreading' => true));
  636. $this->_model->create(helper::getPasteId(), $burnPaste);
  637. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  638. ob_start();
  639. new zerobin;
  640. $content = ob_get_contents();
  641. $this->assertTag(
  642. array(
  643. 'id' => 'cipherdata',
  644. 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
  645. ),
  646. $content,
  647. 'outputs data correctly'
  648. );
  649. }
  650. /**
  651. * @runInSeparateProcess
  652. */
  653. public function testReadJson()
  654. {
  655. $this->reset();
  656. $this->_model->create(helper::getPasteId(), helper::getPaste());
  657. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  658. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  659. ob_start();
  660. new zerobin;
  661. $content = ob_get_contents();
  662. $response = json_decode($content, true);
  663. $this->assertEquals(0, $response['status'], 'outputs success status');
  664. $this->assertEquals(array(helper::getPaste()), $response['messages'], 'outputs data correctly');
  665. }
  666. /**
  667. * @runInSeparateProcess
  668. */
  669. public function testReadInvalidJson()
  670. {
  671. $this->reset();
  672. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  673. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  674. ob_start();
  675. new zerobin;
  676. $content = ob_get_contents();
  677. $response = json_decode($content, true);
  678. $this->assertEquals(1, $response['status'], 'outputs error status');
  679. }
  680. /**
  681. * @runInSeparateProcess
  682. */
  683. public function testReadOldSyntax()
  684. {
  685. $this->reset();
  686. $oldPaste = helper::getPaste();
  687. $oldPaste['meta'] = array(
  688. 'syntaxcoloring' => true,
  689. 'postdate' => $oldPaste['meta']['postdate'],
  690. 'opendiscussion' => $oldPaste['meta']['opendiscussion'],
  691. );
  692. $this->_model->create(helper::getPasteId(), $oldPaste);
  693. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  694. ob_start();
  695. new zerobin;
  696. $content = ob_get_contents();
  697. $oldPaste['meta']['formatter'] = 'syntaxhighlighting';
  698. $this->assertTag(
  699. array(
  700. 'id' => 'cipherdata',
  701. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  702. ),
  703. $content,
  704. 'outputs data correctly'
  705. );
  706. }
  707. /**
  708. * @runInSeparateProcess
  709. */
  710. public function testReadOldFormat()
  711. {
  712. $this->reset();
  713. $oldPaste = helper::getPaste();
  714. unset($oldPaste['meta']['formatter']);
  715. $this->_model->create(helper::getPasteId(), $oldPaste);
  716. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  717. ob_start();
  718. new zerobin;
  719. $content = ob_get_contents();
  720. $oldPaste['meta']['formatter'] = 'plaintext';
  721. $this->assertTag(
  722. array(
  723. 'id' => 'cipherdata',
  724. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  725. ),
  726. $content,
  727. 'outputs data correctly'
  728. );
  729. }
  730. /**
  731. * @runInSeparateProcess
  732. */
  733. public function testDelete()
  734. {
  735. $this->reset();
  736. $this->_model->create(helper::getPasteId(), helper::getPaste());
  737. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  738. $_GET['pasteid'] = helper::getPasteId();
  739. $_GET['deletetoken'] = hash_hmac('sha1', helper::getPasteId(), serversalt::get());
  740. ob_start();
  741. new zerobin;
  742. $content = ob_get_contents();
  743. $this->assertTag(
  744. array(
  745. 'id' => 'status',
  746. 'content' => 'Paste was properly deleted'
  747. ),
  748. $content,
  749. 'outputs deleted status correctly'
  750. );
  751. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  752. }
  753. /**
  754. * @runInSeparateProcess
  755. */
  756. public function testDeleteInvalidId()
  757. {
  758. $this->reset();
  759. $this->_model->create(helper::getPasteId(), helper::getPaste());
  760. $_GET['pasteid'] = 'foo';
  761. $_GET['deletetoken'] = 'bar';
  762. ob_start();
  763. new zerobin;
  764. $content = ob_get_contents();
  765. $this->assertTag(
  766. array(
  767. 'id' => 'errormessage',
  768. 'content' => 'Invalid paste ID'
  769. ),
  770. $content,
  771. 'outputs delete error correctly'
  772. );
  773. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
  774. }
  775. /**
  776. * @runInSeparateProcess
  777. */
  778. public function testDeleteInexistantId()
  779. {
  780. $this->reset();
  781. $_GET['pasteid'] = helper::getPasteId();
  782. $_GET['deletetoken'] = 'bar';
  783. ob_start();
  784. new zerobin;
  785. $content = ob_get_contents();
  786. $this->assertTag(
  787. array(
  788. 'id' => 'errormessage',
  789. 'content' => 'Paste does not exist'
  790. ),
  791. $content,
  792. 'outputs delete error correctly'
  793. );
  794. }
  795. /**
  796. * @runInSeparateProcess
  797. */
  798. public function testDeleteInvalidToken()
  799. {
  800. $this->reset();
  801. $this->_model->create(helper::getPasteId(), helper::getPaste());
  802. $_GET['pasteid'] = helper::getPasteId();
  803. $_GET['deletetoken'] = 'bar';
  804. ob_start();
  805. new zerobin;
  806. $content = ob_get_contents();
  807. $this->assertTag(
  808. array(
  809. 'id' => 'errormessage',
  810. 'content' => 'Wrong deletion token'
  811. ),
  812. $content,
  813. 'outputs delete error correctly'
  814. );
  815. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
  816. }
  817. /**
  818. * @runInSeparateProcess
  819. */
  820. public function testDeleteBurnAfterReading()
  821. {
  822. $this->reset();
  823. $burnPaste = helper::getPaste(array('burnafterreading' => true));
  824. $this->_model->create(helper::getPasteId(), $burnPaste);
  825. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  826. $_GET['pasteid'] = helper::getPasteId();
  827. $_GET['deletetoken'] = 'burnafterreading';
  828. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  829. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  830. ob_start();
  831. new zerobin;
  832. $content = ob_get_contents();
  833. $response = json_decode($content, true);
  834. $this->assertEquals(0, $response['status'], 'outputs status');
  835. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  836. }
  837. /**
  838. * @runInSeparateProcess
  839. */
  840. public function testDeleteInvalidBurnAfterReading()
  841. {
  842. $this->reset();
  843. $this->_model->create(helper::getPasteId(), helper::getPaste());
  844. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  845. $_GET['pasteid'] = helper::getPasteId();
  846. $_GET['deletetoken'] = 'burnafterreading';
  847. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
  848. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  849. ob_start();
  850. new zerobin;
  851. $content = ob_get_contents();
  852. $response = json_decode($content, true);
  853. $this->assertEquals(1, $response['status'], 'outputs status');
  854. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  855. }
  856. /**
  857. * @runInSeparateProcess
  858. */
  859. public function testDeleteExpired()
  860. {
  861. $this->reset();
  862. $expiredPaste = helper::getPaste(array('expire_date' => 1000));
  863. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exist before being created');
  864. $this->_model->create(helper::getPasteId(), $expiredPaste);
  865. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  866. $_GET['pasteid'] = helper::getPasteId();
  867. $_GET['deletetoken'] = 'does not matter in this context, but has to be set';
  868. ob_start();
  869. new zerobin;
  870. $content = ob_get_contents();
  871. $this->assertTag(
  872. array(
  873. 'id' => 'errormessage',
  874. 'content' => 'Paste does not exist'
  875. ),
  876. $content,
  877. 'outputs error correctly'
  878. );
  879. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  880. }
  881. }