serversalt.php 3.4 KB

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