zerobin.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?php
  2. class zerobinTest extends PHPUnit_Framework_TestCase
  3. {
  4. private static $pasteid = '5e9bc25c89fb3bf9';
  5. private static $paste = array(
  6. 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
  7. 'meta' => array(
  8. 'postdate' => 1344803344,
  9. 'opendiscussion' => true,
  10. 'formatter' => 'syntaxhighlighting',
  11. ),
  12. );
  13. private static $commentid = '5a52eebf11c4c94b';
  14. private static $comment = array(
  15. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  16. 'meta' => array(
  17. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  18. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  19. 'postdate' => 1344803528,
  20. ),
  21. );
  22. private $_conf;
  23. private $_model;
  24. public function setUp()
  25. {
  26. /* Setup Routine */
  27. $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
  28. serversalt::setPath(PATH . 'data');
  29. $this->_conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  30. $this->reset();
  31. }
  32. public function tearDown()
  33. {
  34. /* Tear Down Routine */
  35. }
  36. public function reset()
  37. {
  38. $_POST = array();
  39. $_GET = array();
  40. $_SERVER = array();
  41. if ($this->_model->exists(self::$pasteid))
  42. $this->_model->delete(self::$pasteid);
  43. if (is_file($this->_conf . '.bak'))
  44. rename($this->_conf . '.bak', $this->_conf);
  45. }
  46. /**
  47. * @runInSeparateProcess
  48. */
  49. public function testView()
  50. {
  51. $this->reset();
  52. ob_start();
  53. new zerobin;
  54. $content = ob_get_contents();
  55. $this->assertTag(
  56. array(
  57. 'tag' => 'title',
  58. 'content' => 'ZeroBin'
  59. ),
  60. $content,
  61. 'outputs title correctly'
  62. );
  63. }
  64. /**
  65. * @runInSeparateProcess
  66. */
  67. public function testViewLanguageSelection()
  68. {
  69. $this->reset();
  70. $options = parse_ini_file($this->_conf, true);
  71. $options['main']['languageselection'] = true;
  72. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  73. rename($this->_conf, $this->_conf . '.bak');
  74. helper::createIniFile($this->_conf, $options);
  75. $_COOKIE['lang'] = 'de';
  76. ob_start();
  77. new zerobin;
  78. $content = ob_get_contents();
  79. $this->assertTag(
  80. array(
  81. 'tag' => 'title',
  82. 'content' => 'ZeroBin'
  83. ),
  84. $content,
  85. 'outputs title correctly'
  86. );
  87. }
  88. /**
  89. * @runInSeparateProcess
  90. */
  91. public function testHtaccess()
  92. {
  93. $this->reset();
  94. $dirs = array('cfg', 'lib');
  95. foreach ($dirs as $dir) {
  96. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  97. @unlink($file);
  98. }
  99. ob_start();
  100. new zerobin;
  101. $content = ob_get_contents();
  102. foreach ($dirs as $dir) {
  103. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  104. $this->assertFileExists(
  105. $file,
  106. "$dir htaccess recreated"
  107. );
  108. }
  109. }
  110. /**
  111. * @expectedException Exception
  112. * @expectedExceptionCode 2
  113. */
  114. public function testConf()
  115. {
  116. $this->reset();
  117. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  118. rename($this->_conf, $this->_conf . '.bak');
  119. file_put_contents($this->_conf, '');
  120. ob_start();
  121. new zerobin;
  122. $content = ob_get_contents();
  123. }
  124. /**
  125. * @runInSeparateProcess
  126. */
  127. public function testCreate()
  128. {
  129. $this->reset();
  130. $_POST = self::$paste;
  131. $_SERVER['REMOTE_ADDR'] = '::1';
  132. ob_start();
  133. new zerobin;
  134. $content = ob_get_contents();
  135. $response = json_decode($content, true);
  136. $this->assertEquals(0, $response['status'], 'outputs status');
  137. $this->assertEquals(
  138. hash_hmac('sha1', $response['id'], serversalt::get()),
  139. $response['deletetoken'],
  140. 'outputs valid delete token'
  141. );
  142. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  143. }
  144. /**
  145. * @runInSeparateProcess
  146. */
  147. public function testCreateInvalidTimelimit()
  148. {
  149. $this->reset();
  150. $_POST = self::$paste;
  151. $_SERVER['REMOTE_ADDR'] = '::1';
  152. ob_start();
  153. new zerobin;
  154. $content = ob_get_contents();
  155. $response = json_decode($content, true);
  156. $this->assertEquals(1, $response['status'], 'outputs error status');
  157. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  158. }
  159. /**
  160. * @runInSeparateProcess
  161. */
  162. public function testCreateInvalidSize()
  163. {
  164. $this->reset();
  165. $options = parse_ini_file($this->_conf, true);
  166. $options['main']['sizelimit'] = 10;
  167. $options['traffic']['limit'] = 0;
  168. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  169. rename($this->_conf, $this->_conf . '.bak');
  170. helper::createIniFile($this->_conf, $options);
  171. $_POST = self::$paste;
  172. $_SERVER['REMOTE_ADDR'] = '::1';
  173. ob_start();
  174. new zerobin;
  175. $content = ob_get_contents();
  176. $response = json_decode($content, true);
  177. $this->assertEquals(1, $response['status'], 'outputs error status');
  178. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  179. }
  180. /**
  181. * @runInSeparateProcess
  182. */
  183. public function testCreateProxyHeader()
  184. {
  185. $this->reset();
  186. $options = parse_ini_file($this->_conf, true);
  187. $options['traffic']['header'] = 'X_FORWARDED_FOR';
  188. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  189. rename($this->_conf, $this->_conf . '.bak');
  190. helper::createIniFile($this->_conf, $options);
  191. $_POST = self::$paste;
  192. $_SERVER['HTTP_X_FORWARDED_FOR'] = '::1';
  193. ob_start();
  194. new zerobin;
  195. $content = ob_get_contents();
  196. $response = json_decode($content, true);
  197. $this->assertEquals(1, $response['status'], 'outputs error status');
  198. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  199. }
  200. /**
  201. * @runInSeparateProcess
  202. */
  203. public function testCreateDuplicateId()
  204. {
  205. $this->reset();
  206. $options = parse_ini_file($this->_conf, true);
  207. $options['traffic']['limit'] = 0;
  208. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  209. rename($this->_conf, $this->_conf . '.bak');
  210. helper::createIniFile($this->_conf, $options);
  211. $this->_model->create(self::$pasteid, self::$paste);
  212. $_POST = self::$paste;
  213. $_SERVER['REMOTE_ADDR'] = '::1';
  214. ob_start();
  215. new zerobin;
  216. $content = ob_get_contents();
  217. $response = json_decode($content, true);
  218. $this->assertEquals(1, $response['status'], 'outputs error status');
  219. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  220. }
  221. /**
  222. * @runInSeparateProcess
  223. */
  224. public function testCreateValidExpire()
  225. {
  226. $this->reset();
  227. $options = parse_ini_file($this->_conf, true);
  228. $options['traffic']['limit'] = 0;
  229. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  230. rename($this->_conf, $this->_conf . '.bak');
  231. helper::createIniFile($this->_conf, $options);
  232. $_POST = self::$paste;
  233. $_POST['expire'] = '5min';
  234. $_POST['formatter'] = 'foo';
  235. $_SERVER['REMOTE_ADDR'] = '::1';
  236. ob_start();
  237. new zerobin;
  238. $content = ob_get_contents();
  239. $response = json_decode($content, true);
  240. $this->assertEquals(0, $response['status'], 'outputs status');
  241. $this->assertEquals(
  242. hash_hmac('sha1', $response['id'], serversalt::get()),
  243. $response['deletetoken'],
  244. 'outputs valid delete token'
  245. );
  246. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  247. }
  248. /**
  249. * @runInSeparateProcess
  250. */
  251. public function testCreateInvalidExpire()
  252. {
  253. $this->reset();
  254. $options = parse_ini_file($this->_conf, true);
  255. $options['traffic']['limit'] = 0;
  256. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  257. rename($this->_conf, $this->_conf . '.bak');
  258. helper::createIniFile($this->_conf, $options);
  259. $_POST = self::$paste;
  260. $_POST['expire'] = 'foo';
  261. $_SERVER['REMOTE_ADDR'] = '::1';
  262. ob_start();
  263. new zerobin;
  264. $content = ob_get_contents();
  265. $response = json_decode($content, true);
  266. $this->assertEquals(0, $response['status'], 'outputs status');
  267. $this->assertEquals(
  268. hash_hmac('sha1', $response['id'], serversalt::get()),
  269. $response['deletetoken'],
  270. 'outputs valid delete token'
  271. );
  272. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  273. }
  274. /**
  275. * @runInSeparateProcess
  276. */
  277. public function testCreateInvalidBurn()
  278. {
  279. $this->reset();
  280. $options = parse_ini_file($this->_conf, true);
  281. $options['traffic']['limit'] = 0;
  282. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  283. rename($this->_conf, $this->_conf . '.bak');
  284. helper::createIniFile($this->_conf, $options);
  285. $_POST = self::$paste;
  286. $_POST['burnafterreading'] = 'neither 1 nor 0';
  287. $_SERVER['REMOTE_ADDR'] = '::1';
  288. ob_start();
  289. new zerobin;
  290. $content = ob_get_contents();
  291. $response = json_decode($content, true);
  292. $this->assertEquals(1, $response['status'], 'outputs error status');
  293. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  294. }
  295. /**
  296. * @runInSeparateProcess
  297. */
  298. public function testCreateInvalidOpenDiscussion()
  299. {
  300. $this->reset();
  301. $options = parse_ini_file($this->_conf, true);
  302. $options['traffic']['limit'] = 0;
  303. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  304. rename($this->_conf, $this->_conf . '.bak');
  305. helper::createIniFile($this->_conf, $options);
  306. $_POST = self::$paste;
  307. $_POST['opendiscussion'] = 'neither 1 nor 0';
  308. $_SERVER['REMOTE_ADDR'] = '::1';
  309. ob_start();
  310. new zerobin;
  311. $content = ob_get_contents();
  312. $response = json_decode($content, true);
  313. $this->assertEquals(1, $response['status'], 'outputs error status');
  314. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  315. }
  316. /**
  317. * @runInSeparateProcess
  318. */
  319. public function testCreateAttachment()
  320. {
  321. $this->reset();
  322. $options = parse_ini_file($this->_conf, true);
  323. $options['traffic']['limit'] = 0;
  324. $options['main']['fileupload'] = true;
  325. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  326. rename($this->_conf, $this->_conf . '.bak');
  327. helper::createIniFile($this->_conf, $options);
  328. $_POST = self::$paste;
  329. $_POST['attachment'] = self::$comment['data'];
  330. $_POST['attachmentname'] = self::$comment['meta']['nickname'];
  331. $_SERVER['REMOTE_ADDR'] = '::1';
  332. ob_start();
  333. new zerobin;
  334. $content = ob_get_contents();
  335. $response = json_decode($content, true);
  336. $this->assertEquals(0, $response['status'], 'outputs status');
  337. $this->assertEquals(
  338. hash_hmac('sha1', $response['id'], serversalt::get()),
  339. $response['deletetoken'],
  340. 'outputs valid delete token'
  341. );
  342. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  343. }
  344. /**
  345. * @runInSeparateProcess
  346. */
  347. public function testCreateValidNick()
  348. {
  349. $this->reset();
  350. $options = parse_ini_file($this->_conf, true);
  351. $options['traffic']['limit'] = 0;
  352. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  353. rename($this->_conf, $this->_conf . '.bak');
  354. helper::createIniFile($this->_conf, $options);
  355. $_POST = self::$paste;
  356. $_POST['nickname'] = self::$comment['meta']['nickname'];
  357. $_SERVER['REMOTE_ADDR'] = '::1';
  358. ob_start();
  359. new zerobin;
  360. $content = ob_get_contents();
  361. $response = json_decode($content, true);
  362. $this->assertEquals(0, $response['status'], 'outputs status');
  363. $this->assertEquals(
  364. hash_hmac('sha1', $response['id'], serversalt::get()),
  365. $response['deletetoken'],
  366. 'outputs valid delete token'
  367. );
  368. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  369. }
  370. /**
  371. * @runInSeparateProcess
  372. */
  373. public function testCreateInvalidNick()
  374. {
  375. $this->reset();
  376. $options = parse_ini_file($this->_conf, true);
  377. $options['traffic']['limit'] = 0;
  378. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  379. rename($this->_conf, $this->_conf . '.bak');
  380. helper::createIniFile($this->_conf, $options);
  381. $_POST = self::$paste;
  382. $_POST['nickname'] = 'foo';
  383. $_SERVER['REMOTE_ADDR'] = '::1';
  384. ob_start();
  385. new zerobin;
  386. $content = ob_get_contents();
  387. $response = json_decode($content, true);
  388. $this->assertEquals(1, $response['status'], 'outputs error status');
  389. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  390. }
  391. /**
  392. * @runInSeparateProcess
  393. */
  394. public function testCreateComment()
  395. {
  396. $this->reset();
  397. $options = parse_ini_file($this->_conf, true);
  398. $options['traffic']['limit'] = 0;
  399. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  400. rename($this->_conf, $this->_conf . '.bak');
  401. helper::createIniFile($this->_conf, $options);
  402. $_POST = self::$comment;
  403. $_POST['pasteid'] = self::$pasteid;
  404. $_POST['parentid'] = self::$pasteid;
  405. $_SERVER['REMOTE_ADDR'] = '::1';
  406. $this->_model->create(self::$pasteid, self::$paste);
  407. ob_start();
  408. new zerobin;
  409. $content = ob_get_contents();
  410. $response = json_decode($content, true);
  411. $this->assertEquals(0, $response['status'], 'outputs status');
  412. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, $response['id']), 'paste exists after posting data');
  413. }
  414. /**
  415. * @runInSeparateProcess
  416. */
  417. public function testCreateInvalidComment()
  418. {
  419. $this->reset();
  420. $options = parse_ini_file($this->_conf, true);
  421. $options['traffic']['limit'] = 0;
  422. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  423. rename($this->_conf, $this->_conf . '.bak');
  424. helper::createIniFile($this->_conf, $options);
  425. $_POST = self::$comment;
  426. $_POST['pasteid'] = self::$pasteid;
  427. $_POST['parentid'] = 'foo';
  428. $_SERVER['REMOTE_ADDR'] = '::1';
  429. $this->_model->create(self::$pasteid, self::$paste);
  430. ob_start();
  431. new zerobin;
  432. $content = ob_get_contents();
  433. $response = json_decode($content, true);
  434. $this->assertEquals(1, $response['status'], 'outputs error status');
  435. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  436. }
  437. /**
  438. * @runInSeparateProcess
  439. */
  440. public function testCreateCommentDiscussionDisabled()
  441. {
  442. $this->reset();
  443. $options = parse_ini_file($this->_conf, true);
  444. $options['traffic']['limit'] = 0;
  445. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  446. rename($this->_conf, $this->_conf . '.bak');
  447. helper::createIniFile($this->_conf, $options);
  448. $_POST = self::$comment;
  449. $_POST['pasteid'] = self::$pasteid;
  450. $_POST['parentid'] = self::$pasteid;
  451. $_SERVER['REMOTE_ADDR'] = '::1';
  452. $paste = self::$paste;
  453. $paste['meta']['opendiscussion'] = false;
  454. $this->_model->create(self::$pasteid, $paste);
  455. ob_start();
  456. new zerobin;
  457. $content = ob_get_contents();
  458. $response = json_decode($content, true);
  459. $this->assertEquals(1, $response['status'], 'outputs error status');
  460. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  461. }
  462. /**
  463. * @runInSeparateProcess
  464. */
  465. public function testCreateCommentInvalidPaste()
  466. {
  467. $this->reset();
  468. $options = parse_ini_file($this->_conf, true);
  469. $options['traffic']['limit'] = 0;
  470. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  471. rename($this->_conf, $this->_conf . '.bak');
  472. helper::createIniFile($this->_conf, $options);
  473. $_POST = self::$comment;
  474. $_POST['pasteid'] = self::$pasteid;
  475. $_POST['parentid'] = self::$pasteid;
  476. $_SERVER['REMOTE_ADDR'] = '::1';
  477. ob_start();
  478. new zerobin;
  479. $content = ob_get_contents();
  480. $response = json_decode($content, true);
  481. $this->assertEquals(1, $response['status'], 'outputs error status');
  482. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  483. }
  484. /**
  485. * @runInSeparateProcess
  486. */
  487. public function testCreateDuplicateComment()
  488. {
  489. $this->reset();
  490. $options = parse_ini_file($this->_conf, true);
  491. $options['traffic']['limit'] = 0;
  492. if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
  493. rename($this->_conf, $this->_conf . '.bak');
  494. helper::createIniFile($this->_conf, $options);
  495. $this->_model->create(self::$pasteid, self::$paste);
  496. $this->_model->createComment(self::$pasteid, self::$pasteid, self::$commentid, self::$comment);
  497. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'comment exists before posting data');
  498. $_POST = self::$comment;
  499. $_POST['pasteid'] = self::$pasteid;
  500. $_POST['parentid'] = self::$pasteid;
  501. $_SERVER['REMOTE_ADDR'] = '::1';
  502. ob_start();
  503. new zerobin;
  504. $content = ob_get_contents();
  505. $response = json_decode($content, true);
  506. $this->assertEquals(1, $response['status'], 'outputs error status');
  507. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  508. }
  509. /**
  510. * @runInSeparateProcess
  511. */
  512. public function testRead()
  513. {
  514. $this->reset();
  515. $this->_model->create(self::$pasteid, self::$paste);
  516. $_SERVER['QUERY_STRING'] = self::$pasteid;
  517. ob_start();
  518. new zerobin;
  519. $content = ob_get_contents();
  520. $this->assertTag(
  521. array(
  522. 'id' => 'cipherdata',
  523. 'content' => htmlspecialchars(json_encode(self::$paste), ENT_NOQUOTES)
  524. ),
  525. $content,
  526. 'outputs data correctly'
  527. );
  528. }
  529. /**
  530. * @runInSeparateProcess
  531. */
  532. public function testReadInvalidId()
  533. {
  534. $this->reset();
  535. $_SERVER['QUERY_STRING'] = 'foo';
  536. ob_start();
  537. new zerobin;
  538. $content = ob_get_contents();
  539. $this->assertTag(
  540. array(
  541. 'id' => 'errormessage',
  542. 'content' => 'Invalid paste ID'
  543. ),
  544. $content,
  545. 'outputs error correctly'
  546. );
  547. }
  548. /**
  549. * @runInSeparateProcess
  550. */
  551. public function testReadNonexisting()
  552. {
  553. $this->reset();
  554. $_SERVER['QUERY_STRING'] = self::$pasteid;
  555. ob_start();
  556. new zerobin;
  557. $content = ob_get_contents();
  558. $this->assertTag(
  559. array(
  560. 'id' => 'errormessage',
  561. 'content' => 'Paste does not exist'
  562. ),
  563. $content,
  564. 'outputs error correctly'
  565. );
  566. }
  567. /**
  568. * @runInSeparateProcess
  569. */
  570. public function testReadExpired()
  571. {
  572. $this->reset();
  573. $expiredPaste = self::$paste;
  574. $expiredPaste['meta']['expire_date'] = $expiredPaste['meta']['postdate'];
  575. $this->_model->create(self::$pasteid, $expiredPaste);
  576. $_SERVER['QUERY_STRING'] = self::$pasteid;
  577. ob_start();
  578. new zerobin;
  579. $content = ob_get_contents();
  580. $this->assertTag(
  581. array(
  582. 'id' => 'errormessage',
  583. 'content' => 'Paste does not exist'
  584. ),
  585. $content,
  586. 'outputs error correctly'
  587. );
  588. }
  589. /**
  590. * @runInSeparateProcess
  591. */
  592. public function testReadBurn()
  593. {
  594. $this->reset();
  595. $burnPaste = self::$paste;
  596. $burnPaste['meta']['burnafterreading'] = true;
  597. $this->_model->create(self::$pasteid, $burnPaste);
  598. $_SERVER['QUERY_STRING'] = self::$pasteid;
  599. ob_start();
  600. new zerobin;
  601. $content = ob_get_contents();
  602. $this->assertTag(
  603. array(
  604. 'id' => 'cipherdata',
  605. 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
  606. ),
  607. $content,
  608. 'outputs data correctly'
  609. );
  610. }
  611. /**
  612. * @runInSeparateProcess
  613. */
  614. public function testReadJson()
  615. {
  616. $this->reset();
  617. $this->_model->create(self::$pasteid, self::$paste);
  618. $_SERVER['QUERY_STRING'] = self::$pasteid . '&json';
  619. ob_start();
  620. new zerobin;
  621. $content = ob_get_contents();
  622. $response = json_decode($content, true);
  623. $this->assertEquals(0, $response['status'], 'outputs success status');
  624. $this->assertEquals(array(self::$paste), $response['messages'], 'outputs data correctly');
  625. }
  626. /**
  627. * @runInSeparateProcess
  628. */
  629. public function testReadInvalidJson()
  630. {
  631. $this->reset();
  632. $_SERVER['QUERY_STRING'] = self::$pasteid . '&json';
  633. ob_start();
  634. new zerobin;
  635. $content = ob_get_contents();
  636. $response = json_decode($content, true);
  637. $this->assertEquals(1, $response['status'], 'outputs error status');
  638. }
  639. /**
  640. * @runInSeparateProcess
  641. */
  642. public function testReadOldSyntax()
  643. {
  644. $this->reset();
  645. $oldPaste = self::$paste;
  646. $oldPaste['meta']['syntaxcoloring'] = true;
  647. unset($oldPaste['meta']['formatter']);
  648. $this->_model->create(self::$pasteid, $oldPaste);
  649. $_SERVER['QUERY_STRING'] = self::$pasteid;
  650. ob_start();
  651. new zerobin;
  652. $content = ob_get_contents();
  653. $oldPaste['meta']['formatter'] = 'syntaxhighlighting';
  654. $this->assertTag(
  655. array(
  656. 'id' => 'cipherdata',
  657. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  658. ),
  659. $content,
  660. 'outputs data correctly'
  661. );
  662. }
  663. /**
  664. * @runInSeparateProcess
  665. */
  666. public function testReadOldFormat()
  667. {
  668. $this->reset();
  669. $oldPaste = self::$paste;
  670. unset($oldPaste['meta']['formatter']);
  671. $this->_model->create(self::$pasteid, $oldPaste);
  672. $_SERVER['QUERY_STRING'] = self::$pasteid;
  673. ob_start();
  674. new zerobin;
  675. $content = ob_get_contents();
  676. $oldPaste['meta']['formatter'] = 'plaintext';
  677. $this->assertTag(
  678. array(
  679. 'id' => 'cipherdata',
  680. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  681. ),
  682. $content,
  683. 'outputs data correctly'
  684. );
  685. }
  686. /**
  687. * @runInSeparateProcess
  688. */
  689. public function testDelete()
  690. {
  691. $this->reset();
  692. $this->_model->create(self::$pasteid, self::$paste);
  693. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  694. $_GET['pasteid'] = self::$pasteid;
  695. $_GET['deletetoken'] = hash_hmac('sha1', self::$pasteid, serversalt::get());
  696. ob_start();
  697. new zerobin;
  698. $content = ob_get_contents();
  699. $this->assertTag(
  700. array(
  701. 'id' => 'status',
  702. 'content' => 'Paste was properly deleted'
  703. ),
  704. $content,
  705. 'outputs deleted status correctly'
  706. );
  707. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  708. }
  709. /**
  710. * @runInSeparateProcess
  711. */
  712. public function testDeleteInvalidId()
  713. {
  714. $this->reset();
  715. $this->_model->create(self::$pasteid, self::$paste);
  716. $_GET['pasteid'] = 'foo';
  717. $_GET['deletetoken'] = 'bar';
  718. ob_start();
  719. new zerobin;
  720. $content = ob_get_contents();
  721. $this->assertTag(
  722. array(
  723. 'id' => 'errormessage',
  724. 'content' => 'Invalid paste ID'
  725. ),
  726. $content,
  727. 'outputs delete error correctly'
  728. );
  729. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  730. }
  731. /**
  732. * @runInSeparateProcess
  733. */
  734. public function testDeleteInexistantId()
  735. {
  736. $this->reset();
  737. $_GET['pasteid'] = self::$pasteid;
  738. $_GET['deletetoken'] = 'bar';
  739. ob_start();
  740. new zerobin;
  741. $content = ob_get_contents();
  742. $this->assertTag(
  743. array(
  744. 'id' => 'errormessage',
  745. 'content' => 'Paste does not exist'
  746. ),
  747. $content,
  748. 'outputs delete error correctly'
  749. );
  750. }
  751. /**
  752. * @runInSeparateProcess
  753. */
  754. public function testDeleteInvalidToken()
  755. {
  756. $this->reset();
  757. $this->_model->create(self::$pasteid, self::$paste);
  758. $_GET['pasteid'] = self::$pasteid;
  759. $_GET['deletetoken'] = 'bar';
  760. ob_start();
  761. new zerobin;
  762. $content = ob_get_contents();
  763. $this->assertTag(
  764. array(
  765. 'id' => 'errormessage',
  766. 'content' => 'Wrong deletion token'
  767. ),
  768. $content,
  769. 'outputs delete error correctly'
  770. );
  771. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  772. }
  773. /**
  774. * @runInSeparateProcess
  775. */
  776. public function testDeleteBurnAfterReading()
  777. {
  778. $this->reset();
  779. $burnPaste = self::$paste;
  780. $burnPaste['meta']['burnafterreading'] = true;
  781. $this->_model->create(self::$pasteid, $burnPaste);
  782. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  783. $_GET['pasteid'] = self::$pasteid;
  784. $_GET['deletetoken'] = 'burnafterreading';
  785. ob_start();
  786. new zerobin;
  787. $content = ob_get_contents();
  788. $response = json_decode($content, true);
  789. $this->assertEquals(0, $response['status'], 'outputs status');
  790. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  791. }
  792. /**
  793. * @runInSeparateProcess
  794. */
  795. public function testDeleteInvalidBurnAfterReading()
  796. {
  797. $this->reset();
  798. $this->_model->create(self::$pasteid, self::$paste);
  799. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  800. $_GET['pasteid'] = self::$pasteid;
  801. $_GET['deletetoken'] = 'burnafterreading';
  802. ob_start();
  803. new zerobin;
  804. $content = ob_get_contents();
  805. $response = json_decode($content, true);
  806. $this->assertEquals(1, $response['status'], 'outputs status');
  807. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  808. }
  809. /**
  810. * @runInSeparateProcess
  811. */
  812. public function testDeleteExpired()
  813. {
  814. $this->reset();
  815. $expiredPaste = self::$paste;
  816. $expiredPaste['meta']['expire_date'] = 1000;
  817. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste does not exist before being created');
  818. $this->_model->create(self::$pasteid, $expiredPaste);
  819. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  820. $_GET['pasteid'] = self::$pasteid;
  821. $_GET['deletetoken'] = 'does not matter in this context, but has to be set';
  822. ob_start();
  823. new zerobin;
  824. $content = ob_get_contents();
  825. $this->assertTag(
  826. array(
  827. 'id' => 'errormessage',
  828. 'content' => 'Paste does not exist'
  829. ),
  830. $content,
  831. 'outputs error correctly'
  832. );
  833. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  834. }
  835. }