serversalt.php 3.3 KB

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