zerobin.php 33 KB

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