ServerSaltTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Persistence\ServerSalt;
  4. class ServerSaltTest extends TestCase
  5. {
  6. private $_path;
  7. private $_invalidPath;
  8. private $_otherPath;
  9. private $_invalidFile;
  10. public function setUp(): void
  11. {
  12. /* Setup Routine */
  13. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  14. if (!is_dir($this->_path)) {
  15. mkdir($this->_path);
  16. }
  17. ServerSalt::setPath($this->_path);
  18. $this->_otherPath = $this->_path . DIRECTORY_SEPARATOR . 'foo';
  19. $this->_invalidPath = $this->_path . DIRECTORY_SEPARATOR . 'bar';
  20. if (!is_dir($this->_invalidPath)) {
  21. mkdir($this->_invalidPath);
  22. }
  23. $this->_invalidFile = $this->_invalidPath . DIRECTORY_SEPARATOR . 'salt.php';
  24. }
  25. public function tearDown(): void
  26. {
  27. /* Tear Down Routine */
  28. chmod($this->_invalidPath, 0700);
  29. Helper::rmDir($this->_path);
  30. }
  31. public function testGeneration()
  32. {
  33. // generating new salt
  34. ServerSalt::setPath($this->_path);
  35. $salt = ServerSalt::get();
  36. // try setting a different path and resetting it
  37. ServerSalt::setPath($this->_otherPath);
  38. $this->assertNotEquals($salt, ServerSalt::get());
  39. ServerSalt::setPath($this->_path);
  40. $this->assertEquals($salt, ServerSalt::get());
  41. }
  42. /**
  43. * @expectedException Exception
  44. * @expectedExceptionCode 11
  45. */
  46. public function testPathShenanigans()
  47. {
  48. // try setting an invalid path
  49. chmod($this->_invalidPath, 0000);
  50. ServerSalt::setPath($this->_invalidPath);
  51. ServerSalt::get();
  52. }
  53. /**
  54. * @expectedException Exception
  55. * @expectedExceptionCode 20
  56. */
  57. public function testFileRead()
  58. {
  59. // try setting an invalid file
  60. chmod($this->_invalidPath, 0700);
  61. file_put_contents($this->_invalidFile, '');
  62. chmod($this->_invalidFile, 0000);
  63. ServerSalt::setPath($this->_invalidPath);
  64. ServerSalt::get();
  65. }
  66. /**
  67. * @expectedException Exception
  68. * @expectedExceptionCode 13
  69. */
  70. public function testFileWrite()
  71. {
  72. // try setting an invalid file
  73. chmod($this->_invalidPath, 0700);
  74. if (is_file($this->_invalidFile)) {
  75. chmod($this->_invalidFile, 0600);
  76. unlink($this->_invalidFile);
  77. }
  78. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
  79. chmod($this->_invalidPath, 0500);
  80. ServerSalt::setPath($this->_invalidPath);
  81. ServerSalt::get();
  82. }
  83. /**
  84. * @expectedException Exception
  85. * @expectedExceptionCode 10
  86. */
  87. public function testPermissionShenanigans()
  88. {
  89. // try creating an invalid path
  90. chmod($this->_invalidPath, 0000);
  91. ServerSalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
  92. ServerSalt::get();
  93. }
  94. }