serversalt.php 3.4 KB

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