ServerSaltTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. public function testPathShenanigans()
  43. {
  44. // try setting an invalid path
  45. chmod($this->_invalidPath, 0000);
  46. ServerSalt::setPath($this->_invalidPath);
  47. $this->expectException(Exception::class);
  48. $this->expectExceptionCode(11);
  49. ServerSalt::get();
  50. }
  51. public function testFileRead()
  52. {
  53. // try setting an invalid file
  54. chmod($this->_invalidPath, 0700);
  55. file_put_contents($this->_invalidFile, '');
  56. chmod($this->_invalidFile, 0000);
  57. ServerSalt::setPath($this->_invalidPath);
  58. $this->expectException(Exception::class);
  59. $this->expectExceptionCode(20);
  60. ServerSalt::get();
  61. }
  62. public function testFileWrite()
  63. {
  64. // try setting an invalid file
  65. chmod($this->_invalidPath, 0700);
  66. if (is_file($this->_invalidFile)) {
  67. chmod($this->_invalidFile, 0600);
  68. unlink($this->_invalidFile);
  69. }
  70. file_put_contents($this->_invalidPath . DIRECTORY_SEPARATOR . '.htaccess', '');
  71. chmod($this->_invalidPath, 0500);
  72. ServerSalt::setPath($this->_invalidPath);
  73. $this->expectException(Exception::class);
  74. $this->expectExceptionCode(13);
  75. ServerSalt::get();
  76. }
  77. public function testPermissionShenanigans()
  78. {
  79. // try creating an invalid path
  80. chmod($this->_invalidPath, 0000);
  81. ServerSalt::setPath($this->_invalidPath . DIRECTORY_SEPARATOR . 'baz');
  82. $this->expectException(Exception::class);
  83. $this->expectExceptionCode(10);
  84. ServerSalt::get();
  85. }
  86. }