serversalt.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. use PrivateBin\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 = PATH . '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. @unlink($this->_invalidFile);
  93. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
  94. chmod($this->_invalidPath, 0500);
  95. serversalt::setPath($this->_invalidPath);
  96. serversalt::get();
  97. }
  98. /**
  99. * @expectedException Exception
  100. * @expectedExceptionCode 10
  101. */
  102. public function testPermissionShenanigans()
  103. {
  104. // try creating an invalid path
  105. chmod($this->_invalidPath, 0000);
  106. serversalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
  107. serversalt::get();
  108. }
  109. }