PrivateBinTest.php 38 KB

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