PrivateBinTest.php 40 KB

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