zerobin.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. class zerobinTest extends PHPUnit_Framework_TestCase
  3. {
  4. private static $pasteid = '501f02e9eeb8bcec';
  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 $_model;
  13. public function setUp()
  14. {
  15. /* Setup Routine */
  16. $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
  17. serversalt::setPath(PATH . 'data');
  18. $this->reset();
  19. }
  20. public function tearDown()
  21. {
  22. /* Tear Down Routine */
  23. }
  24. public function reset()
  25. {
  26. $_POST = array();
  27. $_GET = array();
  28. $_SERVER = array();
  29. if ($this->_model->exists(self::$pasteid))
  30. $this->_model->delete(self::$pasteid);
  31. }
  32. /**
  33. * @runInSeparateProcess
  34. */
  35. public function testView()
  36. {
  37. $this->reset();
  38. ob_start();
  39. new zerobin;
  40. $content = ob_get_contents();
  41. $this->assertTag(
  42. array(
  43. 'tag' => 'title',
  44. 'content' => 'ZeroBin'
  45. ),
  46. $content,
  47. 'outputs title correctly'
  48. );
  49. }
  50. /**
  51. * @runInSeparateProcess
  52. */
  53. public function testCreate()
  54. {
  55. $this->reset();
  56. $_POST = self::$paste;
  57. $_SERVER['REMOTE_ADDR'] = '::1';
  58. ob_start();
  59. new zerobin;
  60. $content = ob_get_contents();
  61. $response = json_decode($content, true);
  62. $this->assertEquals($response['status'], 0, 'outputs status');
  63. $this->assertEquals(
  64. $response['deletetoken'],
  65. hash_hmac('sha1', $response['id'], serversalt::get()),
  66. 'outputs valid delete token'
  67. );
  68. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  69. }
  70. /**
  71. * @runInSeparateProcess
  72. */
  73. public function testRead()
  74. {
  75. $this->reset();
  76. $this->_model->create(self::$pasteid, self::$paste);
  77. $_SERVER['QUERY_STRING'] = self::$pasteid;
  78. ob_start();
  79. new zerobin;
  80. $content = ob_get_contents();
  81. $this->assertTag(
  82. array(
  83. 'id' => 'cipherdata',
  84. 'content' => htmlspecialchars(json_encode(self::$paste), ENT_NOQUOTES)
  85. ),
  86. $content,
  87. 'outputs data correctly'
  88. );
  89. }
  90. /**
  91. * @runInSeparateProcess
  92. */
  93. public function testDelete()
  94. {
  95. $this->reset();
  96. $this->_model->create(self::$pasteid, self::$paste);
  97. $this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists before deleting data');
  98. $_GET['pasteid'] = self::$pasteid;
  99. $_GET['deletetoken'] = hash_hmac('sha1', self::$pasteid, serversalt::get());
  100. ob_start();
  101. new zerobin;
  102. $content = ob_get_contents();
  103. $this->assertTag(
  104. array(
  105. 'id' => 'status',
  106. 'content' => 'Paste was properly deleted'
  107. ),
  108. $content,
  109. 'outputs deleted status correctly'
  110. );
  111. $this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
  112. }
  113. }