zerobin.php 33 KB

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