ServerSaltTest.php 3.6 KB

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