privatebin.php 38 KB

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