zerobin.php 33 KB

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