1
0

privatebin.php 37 KB

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