1
0

privatebin.php 37 KB

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