ServerSaltTest.php 3.6 KB

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