ServerSaltTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. use PrivateBin\Persistence\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 = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_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. // try setting a different path and resetting it
  36. ServerSalt::setPath($this->_otherPath);
  37. $this->assertNotEquals($salt, ServerSalt::get());
  38. ServerSalt::setPath($this->_path);
  39. $this->assertEquals($salt, ServerSalt::get());
  40. }
  41. /**
  42. * @expectedException Exception
  43. * @expectedExceptionCode 11
  44. */
  45. public function testPathShenanigans()
  46. {
  47. // try setting an invalid path
  48. chmod($this->_invalidPath, 0000);
  49. ServerSalt::setPath($this->_invalidPath);
  50. ServerSalt::get();
  51. }
  52. /**
  53. * @expectedException Exception
  54. * @expectedExceptionCode 20
  55. */
  56. public function testFileRead()
  57. {
  58. // try setting an invalid file
  59. chmod($this->_invalidPath, 0700);
  60. file_put_contents($this->_invalidFile, '');
  61. chmod($this->_invalidFile, 0000);
  62. ServerSalt::setPath($this->_invalidPath);
  63. ServerSalt::get();
  64. }
  65. /**
  66. * @expectedException Exception
  67. * @expectedExceptionCode 13
  68. */
  69. public function testFileWrite()
  70. {
  71. // try setting an invalid file
  72. chmod($this->_invalidPath, 0700);
  73. if (is_file($this->_invalidFile)) {
  74. chmod($this->_invalidFile, 0600);
  75. unlink($this->_invalidFile);
  76. }
  77. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
  78. chmod($this->_invalidPath, 0500);
  79. ServerSalt::setPath($this->_invalidPath);
  80. ServerSalt::get();
  81. }
  82. /**
  83. * @expectedException Exception
  84. * @expectedExceptionCode 10
  85. */
  86. public function testPermissionShenanigans()
  87. {
  88. // try creating an invalid path
  89. chmod($this->_invalidPath, 0000);
  90. ServerSalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
  91. ServerSalt::get();
  92. }
  93. }