privatebin.php 37 KB

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