zerobin.php 33 KB

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