zerobin.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. ),
  11. );
  12. private static $commentid = '5a52eebf11c4c94b';
  13. private static $comment = array(
  14. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  15. 'meta' => array(
  16. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  17. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  18. 'postdate' => 1344803528,
  19. ),
  20. );
  21. private $_model;
  22. public function setUp()
  23. {
  24. /* Setup Routine */
  25. $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
  26. serversalt::setPath(PATH . 'data');
  27. $this->reset();
  28. }
  29. public function tearDown()
  30. {
  31. /* Tear Down Routine */
  32. }
  33. public function reset()
  34. {
  35. $_POST = array();
  36. $_GET = array();
  37. $_SERVER = array();
  38. if ($this->_model->exists(self::$pasteid))
  39. $this->_model->delete(self::$pasteid);
  40. $conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  41. if (is_file($conf . '.bak'))
  42. rename($conf . '.bak', $conf);
  43. }
  44. /**
  45. * @runInSeparateProcess
  46. */
  47. public function testView()
  48. {
  49. $this->reset();
  50. ob_start();
  51. new zerobin;
  52. $content = ob_get_contents();
  53. $this->assertTag(
  54. array(
  55. 'tag' => 'title',
  56. 'content' => 'ZeroBin'
  57. ),
  58. $content,
  59. 'outputs title correctly'
  60. );
  61. }
  62. /**
  63. * @runInSeparateProcess
  64. */
  65. public function testHtaccess()
  66. {
  67. $this->reset();
  68. $dirs = array('cfg', 'lib');
  69. foreach ($dirs as $dir) {
  70. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  71. @unlink($file);
  72. }
  73. ob_start();
  74. new zerobin;
  75. $content = ob_get_contents();
  76. foreach ($dirs as $dir) {
  77. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  78. $this->assertFileExists(
  79. $file,
  80. "$dir htaccess recreated"
  81. );
  82. }
  83. }
  84. /**
  85. * @expectedException Exception
  86. * @expectedExceptionCode 2
  87. */
  88. public function testConf()
  89. {
  90. $this->reset();
  91. $conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  92. if (!is_file($conf . '.bak') && is_file($conf))
  93. rename($conf, $conf . '.bak');
  94. file_put_contents($conf, '');
  95. ob_start();
  96. new zerobin;
  97. $content = ob_get_contents();
  98. }
  99. /**
  100. * @runInSeparateProcess
  101. */
  102. public function testConfMissingExpireLabel()
  103. {
  104. $this->reset();
  105. $conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  106. $options = parse_ini_file($conf, true);
  107. $options['expire_options']['foobar123'] = 10;
  108. if (!is_file($conf . '.bak') && is_file($conf))
  109. rename($conf, $conf . '.bak');
  110. helper::createIniFile($conf, $options);
  111. ini_set('magic_quotes_gpc', 1);
  112. ob_start();
  113. new zerobin;
  114. $content = ob_get_contents();
  115. }
  116. /**
  117. * @runInSeparateProcess
  118. */
  119. public function testCreate()
  120. {
  121. $this->reset();
  122. $_POST = self::$paste;
  123. $_SERVER['REMOTE_ADDR'] = '::1';
  124. ob_start();
  125. new zerobin;
  126. $content = ob_get_contents();
  127. $response = json_decode($content, true);
  128. $this->assertEquals(0, $response['status'], 'outputs status');
  129. $this->assertEquals(
  130. hash_hmac('sha1', $response['id'], serversalt::get()),
  131. $response['deletetoken'],
  132. 'outputs valid delete token'
  133. );
  134. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  135. }
  136. /**
  137. * @runInSeparateProcess
  138. */
  139. public function testCreateValidExpire()
  140. {
  141. $this->reset();
  142. $_POST = self::$paste;
  143. $_POST['expire'] = '5min';
  144. $_SERVER['REMOTE_ADDR'] = '::1';
  145. sleep(11);
  146. ob_start();
  147. new zerobin;
  148. $content = ob_get_contents();
  149. $response = json_decode($content, true);
  150. $this->assertEquals(0, $response['status'], 'outputs status');
  151. $this->assertEquals(
  152. hash_hmac('sha1', $response['id'], serversalt::get()),
  153. $response['deletetoken'],
  154. 'outputs valid delete token'
  155. );
  156. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  157. }
  158. /**
  159. * @runInSeparateProcess
  160. */
  161. public function testCreateInvalidExpire()
  162. {
  163. $this->reset();
  164. $_POST = self::$paste;
  165. $_POST['expire'] = 'foo';
  166. $_SERVER['REMOTE_ADDR'] = '::1';
  167. sleep(11);
  168. ob_start();
  169. new zerobin;
  170. $content = ob_get_contents();
  171. $response = json_decode($content, true);
  172. $this->assertEquals(0, $response['status'], 'outputs status');
  173. $this->assertEquals(
  174. hash_hmac('sha1', $response['id'], serversalt::get()),
  175. $response['deletetoken'],
  176. 'outputs valid delete token'
  177. );
  178. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  179. }
  180. /**
  181. * @runInSeparateProcess
  182. */
  183. public function testCreateInvalidBurn()
  184. {
  185. $this->reset();
  186. $_POST = self::$paste;
  187. $_POST['burnafterreading'] = 'neither 1 nor 0';
  188. $_SERVER['REMOTE_ADDR'] = '::1';
  189. sleep(11);
  190. ob_start();
  191. new zerobin;
  192. $content = ob_get_contents();
  193. $response = json_decode($content, true);
  194. $this->assertEquals(1, $response['status'], 'outputs error status');
  195. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  196. }
  197. /**
  198. * @runInSeparateProcess
  199. */
  200. public function testCreateInvalidOpenDiscussion()
  201. {
  202. $this->reset();
  203. $_POST = self::$paste;
  204. $_POST['opendiscussion'] = 'neither 1 nor 0';
  205. $_SERVER['REMOTE_ADDR'] = '::1';
  206. sleep(11);
  207. ob_start();
  208. new zerobin;
  209. $content = ob_get_contents();
  210. $response = json_decode($content, true);
  211. $this->assertEquals(1, $response['status'], 'outputs error status');
  212. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  213. }
  214. /**
  215. * @runInSeparateProcess
  216. */
  217. public function testCreateValidNick()
  218. {
  219. $this->reset();
  220. $_POST = self::$paste;
  221. $_POST['nickname'] = self::$comment['meta']['nickname'];
  222. $_SERVER['REMOTE_ADDR'] = '::1';
  223. sleep(11);
  224. ob_start();
  225. new zerobin;
  226. $content = ob_get_contents();
  227. $response = json_decode($content, true);
  228. $this->assertEquals(0, $response['status'], 'outputs status');
  229. $this->assertEquals(
  230. hash_hmac('sha1', $response['id'], serversalt::get()),
  231. $response['deletetoken'],
  232. 'outputs valid delete token'
  233. );
  234. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  235. }
  236. /**
  237. * @runInSeparateProcess
  238. */
  239. public function testCreateInvalidNick()
  240. {
  241. $this->reset();
  242. $_POST = self::$paste;
  243. $_POST['nickname'] = 'foo';
  244. $_SERVER['REMOTE_ADDR'] = '::1';
  245. sleep(11);
  246. ob_start();
  247. new zerobin;
  248. $content = ob_get_contents();
  249. $response = json_decode($content, true);
  250. $this->assertEquals(1, $response['status'], 'outputs error status');
  251. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
  252. }
  253. /**
  254. * @runInSeparateProcess
  255. */
  256. public function testCreateComment()
  257. {
  258. $this->reset();
  259. $_POST = self::$comment;
  260. $_POST['pasteid'] = self::$pasteid;
  261. $_POST['parentid'] = self::$pasteid;
  262. $_SERVER['REMOTE_ADDR'] = '::1';
  263. $this->_model->create(self::$pasteid, self::$paste);
  264. sleep(11);
  265. ob_start();
  266. new zerobin;
  267. $content = ob_get_contents();
  268. $response = json_decode($content, true);
  269. $this->assertEquals(0, $response['status'], 'outputs status');
  270. $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, $response['id']), 'paste exists after posting data');
  271. }
  272. /**
  273. * @runInSeparateProcess
  274. */
  275. public function testCreateCommentDiscussionDisabled()
  276. {
  277. $this->reset();
  278. $_POST = self::$comment;
  279. $_POST['pasteid'] = self::$pasteid;
  280. $_POST['parentid'] = self::$pasteid;
  281. $_SERVER['REMOTE_ADDR'] = '::1';
  282. $paste = self::$paste;
  283. $paste['meta']['opendiscussion'] = false;
  284. $this->_model->create(self::$pasteid, $paste);
  285. sleep(11);
  286. ob_start();
  287. new zerobin;
  288. $content = ob_get_contents();
  289. $response = json_decode($content, true);
  290. $this->assertEquals(1, $response['status'], 'outputs error status');
  291. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  292. }
  293. /**
  294. * @runInSeparateProcess
  295. */
  296. public function testCreateCommentInvalidPaste()
  297. {
  298. $this->reset();
  299. $_POST = self::$comment;
  300. $_POST['pasteid'] = self::$pasteid;
  301. $_POST['parentid'] = self::$pasteid;
  302. $_SERVER['REMOTE_ADDR'] = '::1';
  303. sleep(11);
  304. ob_start();
  305. new zerobin;
  306. $content = ob_get_contents();
  307. $response = json_decode($content, true);
  308. $this->assertEquals(1, $response['status'], 'outputs error status');
  309. $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
  310. }
  311. /**
  312. * @runInSeparateProcess
  313. */
  314. public function testRead()
  315. {
  316. $this->reset();
  317. $this->_model->create(self::$pasteid, self::$paste);
  318. $_SERVER['QUERY_STRING'] = self::$pasteid;
  319. ob_start();
  320. new zerobin;
  321. $content = ob_get_contents();
  322. $this->assertTag(
  323. array(
  324. 'id' => 'cipherdata',
  325. 'content' => htmlspecialchars(json_encode(self::$paste), ENT_NOQUOTES)
  326. ),
  327. $content,
  328. 'outputs data correctly'
  329. );
  330. }
  331. /**
  332. * @runInSeparateProcess
  333. */
  334. public function testReadInvalidId()
  335. {
  336. $this->reset();
  337. $_SERVER['QUERY_STRING'] = 'foo';
  338. ob_start();
  339. new zerobin;
  340. $content = ob_get_contents();
  341. $this->assertTag(
  342. array(
  343. 'id' => 'errormessage',
  344. 'content' => 'Invalid paste ID'
  345. ),
  346. $content,
  347. 'outputs error correctly'
  348. );
  349. }
  350. /**
  351. * @runInSeparateProcess
  352. */
  353. public function testReadNonexisting()
  354. {
  355. $this->reset();
  356. $_SERVER['QUERY_STRING'] = self::$pasteid;
  357. ob_start();
  358. new zerobin;
  359. $content = ob_get_contents();
  360. $this->assertTag(
  361. array(
  362. 'id' => 'errormessage',
  363. 'content' => 'Paste does not exist'
  364. ),
  365. $content,
  366. 'outputs error correctly'
  367. );
  368. }
  369. /**
  370. * @runInSeparateProcess
  371. */
  372. public function testReadExpired()
  373. {
  374. $this->reset();
  375. $expiredPaste = self::$paste;
  376. $expiredPaste['meta']['expire_date'] = $expiredPaste['meta']['postdate'];
  377. $this->_model->create(self::$pasteid, $expiredPaste);
  378. $_SERVER['QUERY_STRING'] = self::$pasteid;
  379. ob_start();
  380. new zerobin;
  381. $content = ob_get_contents();
  382. $this->assertTag(
  383. array(
  384. 'id' => 'errormessage',
  385. 'content' => 'Paste does not exist'
  386. ),
  387. $content,
  388. 'outputs error correctly'
  389. );
  390. }
  391. /**
  392. * @runInSeparateProcess
  393. */
  394. public function testReadBurn()
  395. {
  396. $this->reset();
  397. $burnPaste = self::$paste;
  398. $burnPaste['meta']['burnafterreading'] = true;
  399. $this->_model->create(self::$pasteid, $burnPaste);
  400. $_SERVER['QUERY_STRING'] = self::$pasteid;
  401. ob_start();
  402. new zerobin;
  403. $content = ob_get_contents();
  404. $this->assertTag(
  405. array(
  406. 'id' => 'cipherdata',
  407. 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
  408. ),
  409. $content,
  410. 'outputs data correctly'
  411. );
  412. }
  413. /**
  414. * @runInSeparateProcess
  415. */
  416. public function testReadJson()
  417. {
  418. $this->reset();
  419. $this->_model->create(self::$pasteid, self::$paste);
  420. $_SERVER['QUERY_STRING'] = self::$pasteid . '&json';
  421. ob_start();
  422. new zerobin;
  423. $content = ob_get_contents();
  424. $response = json_decode($content, true);
  425. $this->assertEquals(0, $response['status'], 'outputs success status');
  426. $this->assertEquals(array(self::$paste), $response['messages'], 'outputs data correctly');
  427. }
  428. /**
  429. * @runInSeparateProcess
  430. */
  431. public function testDelete()
  432. {
  433. $this->reset();
  434. $this->_model->create(self::$pasteid, self::$paste);
  435. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  436. $_GET['pasteid'] = self::$pasteid;
  437. $_GET['deletetoken'] = hash_hmac('sha1', self::$pasteid, serversalt::get());
  438. ob_start();
  439. new zerobin;
  440. $content = ob_get_contents();
  441. $this->assertTag(
  442. array(
  443. 'id' => 'status',
  444. 'content' => 'Paste was properly deleted'
  445. ),
  446. $content,
  447. 'outputs deleted status correctly'
  448. );
  449. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  450. }
  451. /**
  452. * @runInSeparateProcess
  453. */
  454. public function testDeleteInvalidId()
  455. {
  456. $this->reset();
  457. $this->_model->create(self::$pasteid, self::$paste);
  458. $_GET['pasteid'] = 'foo';
  459. $_GET['deletetoken'] = 'bar';
  460. ob_start();
  461. new zerobin;
  462. $content = ob_get_contents();
  463. $this->assertTag(
  464. array(
  465. 'id' => 'errormessage',
  466. 'content' => 'Invalid paste ID'
  467. ),
  468. $content,
  469. 'outputs delete error correctly'
  470. );
  471. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  472. }
  473. /**
  474. * @runInSeparateProcess
  475. */
  476. public function testDeleteInexistantId()
  477. {
  478. $this->reset();
  479. $_GET['pasteid'] = self::$pasteid;
  480. $_GET['deletetoken'] = 'bar';
  481. ob_start();
  482. new zerobin;
  483. $content = ob_get_contents();
  484. $this->assertTag(
  485. array(
  486. 'id' => 'errormessage',
  487. 'content' => 'Paste does not exist'
  488. ),
  489. $content,
  490. 'outputs delete error correctly'
  491. );
  492. }
  493. /**
  494. * @runInSeparateProcess
  495. */
  496. public function testDeleteInvalidToken()
  497. {
  498. $this->reset();
  499. $this->_model->create(self::$pasteid, self::$paste);
  500. $_GET['pasteid'] = self::$pasteid;
  501. $_GET['deletetoken'] = 'bar';
  502. ob_start();
  503. new zerobin;
  504. $content = ob_get_contents();
  505. $this->assertTag(
  506. array(
  507. 'id' => 'errormessage',
  508. 'content' => 'Wrong deletion token'
  509. ),
  510. $content,
  511. 'outputs delete error correctly'
  512. );
  513. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after failing to delete data');
  514. }
  515. /**
  516. * @runInSeparateProcess
  517. */
  518. public function testDeleteBurnAfterReading()
  519. {
  520. $this->reset();
  521. $burnPaste = self::$paste;
  522. $burnPaste['meta']['burnafterreading'] = true;
  523. $this->_model->create(self::$pasteid, $burnPaste);
  524. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  525. $_GET['pasteid'] = self::$pasteid;
  526. $_GET['deletetoken'] = 'burnafterreading';
  527. ob_start();
  528. new zerobin;
  529. $content = ob_get_contents();
  530. $response = json_decode($content, true);
  531. $this->assertEquals(0, $response['status'], 'outputs status');
  532. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  533. }
  534. /**
  535. * @runInSeparateProcess
  536. */
  537. public function testDeleteInvalidBurnAfterReading()
  538. {
  539. $this->reset();
  540. $this->_model->create(self::$pasteid, self::$paste);
  541. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  542. $_GET['pasteid'] = self::$pasteid;
  543. $_GET['deletetoken'] = 'burnafterreading';
  544. ob_start();
  545. new zerobin;
  546. $content = ob_get_contents();
  547. $response = json_decode($content, true);
  548. $this->assertEquals(1, $response['status'], 'outputs status');
  549. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  550. }
  551. }