serversalt.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. require 'mcrypt_mock.php';
  31. $this->assertNotEquals($salt, serversalt::generate());
  32. // try setting a different path and resetting it
  33. serversalt::setPath($this->_otherPath);
  34. $this->assertNotEquals($salt, serversalt::get());
  35. serversalt::setPath($this->_path);
  36. $this->assertEquals($salt, serversalt::get());
  37. }
  38. /**
  39. * @expectedException Exception
  40. * @expectedExceptionCode 11
  41. */
  42. public function testPathShenanigans()
  43. {
  44. // try setting an invalid path
  45. chmod($this->_invalidPath, 0000);
  46. serversalt::setPath($this->_invalidPath);
  47. serversalt::get();
  48. }
  49. /**
  50. * @expectedException Exception
  51. * @expectedExceptionCode 20
  52. */
  53. public function testFileRead()
  54. {
  55. // try setting an invalid file
  56. chmod($this->_invalidPath, 0700);
  57. file_put_contents($this->_invalidFile, '');
  58. chmod($this->_invalidFile, 0000);
  59. serversalt::setPath($this->_invalidPath);
  60. serversalt::get();
  61. }
  62. /**
  63. * @expectedException Exception
  64. * @expectedExceptionCode 13
  65. */
  66. public function testFileWrite()
  67. {
  68. // try setting an invalid file
  69. chmod($this->_invalidPath, 0700);
  70. @unlink($this->_invalidFile);
  71. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
  72. chmod($this->_invalidPath, 0500);
  73. serversalt::setPath($this->_invalidPath);
  74. serversalt::get();
  75. }
  76. /**
  77. * @expectedException Exception
  78. * @expectedExceptionCode 10
  79. */
  80. public function testPermissionShenanigans()
  81. {
  82. // try creating an invalid path
  83. chmod($this->_invalidPath, 0000);
  84. serversalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
  85. serversalt::get();
  86. }
  87. }