privatebin.php 38 KB

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