ServerSaltTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. use PrivateBin\Persistence\ServerSalt;
  3. class ServerSaltTest extends PHPUnit_Framework_TestCase
  4. {
  5. private $_path;
  6. private $_invalidPath;
  7. private $_otherPath;
  8. private $_invalidFile;
  9. public function setUp()
  10. {
  11. /* Setup Routine */
  12. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  13. if (!is_dir($this->_path)) {
  14. mkdir($this->_path);
  15. }
  16. ServerSalt::setPath($this->_path);
  17. $this->_otherPath = $this->_path . DIRECTORY_SEPARATOR . 'foo';
  18. $this->_invalidPath = $this->_path . DIRECTORY_SEPARATOR . 'bar';
  19. if (!is_dir($this->_invalidPath)) {
  20. mkdir($this->_invalidPath);
  21. }
  22. $this->_invalidFile = $this->_invalidPath . DIRECTORY_SEPARATOR . 'salt.php';
  23. }
  24. public function tearDown()
  25. {
  26. /* Tear Down Routine */
  27. chmod($this->_invalidPath, 0700);
  28. Helper::rmDir($this->_path);
  29. }
  30. public function testGeneration()
  31. {
  32. // generating new salt
  33. ServerSalt::setPath($this->_path);
  34. $salt = ServerSalt::get();
  35. // mcrypt mock
  36. if (!function_exists('mcrypt_create_iv')) {
  37. if (!defined('MCRYPT_DEV_URANDOM')) {
  38. define('MCRYPT_DEV_URANDOM', 1);
  39. }
  40. function mcrypt_create_iv($int, $flag)
  41. {
  42. $randomSalt = '';
  43. for ($i = 0; $i < $int; ++$i) {
  44. $randomSalt .= base_convert(mt_rand(), 10, 16);
  45. }
  46. // hex2bin requires an even length, pad if necessary
  47. if (strlen($randomSalt) % 2) {
  48. $randomSalt = '0' . $randomSalt;
  49. }
  50. return hex2bin($randomSalt);
  51. }
  52. $this->assertNotEquals($salt, ServerSalt::generate());
  53. }
  54. // try setting a different path and resetting it
  55. ServerSalt::setPath($this->_otherPath);
  56. $this->assertNotEquals($salt, ServerSalt::get());
  57. ServerSalt::setPath($this->_path);
  58. $this->assertEquals($salt, ServerSalt::get());
  59. }
  60. /**
  61. * @expectedException Exception
  62. * @expectedExceptionCode 11
  63. */
  64. public function testPathShenanigans()
  65. {
  66. // try setting an invalid path
  67. chmod($this->_invalidPath, 0000);
  68. ServerSalt::setPath($this->_invalidPath);
  69. ServerSalt::get();
  70. }
  71. /**
  72. * @expectedException Exception
  73. * @expectedExceptionCode 20
  74. */
  75. public function testFileRead()
  76. {
  77. // try setting an invalid file
  78. chmod($this->_invalidPath, 0700);
  79. file_put_contents($this->_invalidFile, '');
  80. chmod($this->_invalidFile, 0000);
  81. ServerSalt::setPath($this->_invalidPath);
  82. ServerSalt::get();
  83. }
  84. /**
  85. * @expectedException Exception
  86. * @expectedExceptionCode 13
  87. */
  88. public function testFileWrite()
  89. {
  90. // try setting an invalid file
  91. chmod($this->_invalidPath, 0700);
  92. if (is_file($this->_invalidFile)) {
  93. chmod($this->_invalidFile, 0600);
  94. unlink($this->_invalidFile);
  95. }
  96. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
  97. chmod($this->_invalidPath, 0500);
  98. ServerSalt::setPath($this->_invalidPath);
  99. ServerSalt::get();
  100. }
  101. /**
  102. * @expectedException Exception
  103. * @expectedExceptionCode 10
  104. */
  105. public function testPermissionShenanigans()
  106. {
  107. // try creating an invalid path
  108. chmod($this->_invalidPath, 0000);
  109. ServerSalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
  110. ServerSalt::get();
  111. }
  112. }