1
0

ConfigurationTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Configuration;
  4. class ConfigurationTest extends TestCase
  5. {
  6. private $_minimalConfig;
  7. private $_options;
  8. private $_path;
  9. public function setUp(): void
  10. {
  11. /* Setup Routine */
  12. Helper::confBackup();
  13. $this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
  14. $this->_options = Configuration::getDefaults();
  15. $this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
  16. $this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
  17. $this->_options['purge']['dir'] = PATH . $this->_options['purge']['dir'];
  18. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_cfg';
  19. if (!is_dir($this->_path)) {
  20. mkdir($this->_path);
  21. }
  22. }
  23. public function tearDown(): void
  24. {
  25. /* Tear Down Routine */
  26. Helper::rmDir($this->_path);
  27. if (is_file(CONF)) {
  28. unlink(CONF);
  29. }
  30. Helper::confRestore();
  31. }
  32. public function testDefaultConfigFile()
  33. {
  34. $conf = new Configuration;
  35. $this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
  36. }
  37. public function testHandleFreshConfigFile()
  38. {
  39. Helper::createIniFile(CONF, $this->_options);
  40. $conf = new Configuration;
  41. $this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
  42. }
  43. public function testHandleMissingConfigFile()
  44. {
  45. if (is_file(CONF)) {
  46. unlink(CONF);
  47. }
  48. $conf = new Configuration;
  49. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
  50. }
  51. public function testHandleBlankConfigFile()
  52. {
  53. file_put_contents(CONF, '');
  54. $this->expectException(Exception::class);
  55. $this->expectExceptionCode(2);
  56. new Configuration;
  57. }
  58. public function testHandleMinimalConfigFile()
  59. {
  60. file_put_contents(CONF, $this->_minimalConfig);
  61. $conf = new Configuration;
  62. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
  63. }
  64. public function testHandleInvalidSection()
  65. {
  66. file_put_contents(CONF, $this->_minimalConfig);
  67. $conf = new Configuration;
  68. $this->expectException(Exception::class);
  69. $this->expectExceptionCode(3);
  70. $conf->getKey('foo', 'bar');
  71. }
  72. public function testHandleInvalidKey()
  73. {
  74. file_put_contents(CONF, $this->_minimalConfig);
  75. $conf = new Configuration;
  76. $this->expectException(Exception::class);
  77. $this->expectExceptionCode(4);
  78. $conf->getKey('foo');
  79. }
  80. public function testHandleGetKey()
  81. {
  82. file_put_contents(CONF, $this->_minimalConfig);
  83. $conf = new Configuration;
  84. $this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
  85. }
  86. public function testHandleWrongTypes()
  87. {
  88. $original_options = $this->_options;
  89. $original_options['main']['syntaxhighlightingtheme'] = 'foo';
  90. $options = $original_options;
  91. $options['main']['discussion'] = 'true';
  92. $options['main']['opendiscussion'] = 0;
  93. $options['main']['password'] = -1; // evaluates to TRUE
  94. $options['main']['fileupload'] = 'false';
  95. $options['expire_options']['foo'] = 'bar';
  96. $options['formatter_options'][] = 'foo';
  97. Helper::createIniFile(CONF, $options);
  98. $conf = new Configuration;
  99. $original_options['expire_options']['foo'] = intval('bar');
  100. $original_options['formatter_options'][0] = 'foo';
  101. $this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
  102. }
  103. public function testHandleMissingSubKeys()
  104. {
  105. $options = $this->_options;
  106. unset($options['expire_options']['1week']);
  107. unset($options['expire_options']['1year']);
  108. unset($options['expire_options']['never']);
  109. Helper::createIniFile(CONF, $options);
  110. $conf = new Configuration;
  111. $options['expire']['default'] = '5min';
  112. $this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
  113. }
  114. public function testHandlePreRenameConfig()
  115. {
  116. $options = $this->_options;
  117. $options['model']['class'] = 'zerobin_data';
  118. Helper::createIniFile(CONF, $options);
  119. $conf = new Configuration;
  120. $this->assertEquals('Filesystem', $conf->getKey('class', 'model'), 'old data class gets renamed');
  121. $options['model']['class'] = 'zerobin_db';
  122. Helper::createIniFile(CONF, $options);
  123. $conf = new Configuration;
  124. $this->assertEquals('Database', $conf->getKey('class', 'model'), 'old db class gets renamed');
  125. }
  126. public function testHandleConfigFileRename()
  127. {
  128. $options = $this->_options;
  129. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', $options);
  130. $options['main']['opendiscussion'] = true;
  131. $options['main']['fileupload'] = true;
  132. $options['main']['template'] = 'darkstrap';
  133. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $options);
  134. $conf = new Configuration;
  135. $this->assertFileExists(CONF, 'old configuration file gets converted');
  136. $this->assertFileDoesNotExist(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
  137. $this->assertFileDoesNotExist(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', 'old configuration sample file gets removed');
  138. $this->assertTrue(
  139. $conf->getKey('opendiscussion') &&
  140. $conf->getKey('fileupload') &&
  141. $conf->getKey('template') === 'darkstrap',
  142. 'configuration values get converted'
  143. );
  144. }
  145. public function testRenameIniSample()
  146. {
  147. $iniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
  148. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $this->_options);
  149. if (is_file(CONF)) {
  150. unlink(CONF);
  151. }
  152. rename(CONF_SAMPLE, $iniSample);
  153. new Configuration;
  154. $this->assertFileDoesNotExist($iniSample, 'old sample file gets removed');
  155. $this->assertFileExists(CONF_SAMPLE, 'new sample file gets created');
  156. $this->assertFileExists(CONF, 'old configuration file gets converted');
  157. $this->assertFileDoesNotExist(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
  158. }
  159. public function testConfigPath()
  160. {
  161. // setup
  162. $configFile = $this->_path . DIRECTORY_SEPARATOR . 'conf.php';
  163. $options = $this->_options;
  164. $options['main']['name'] = 'OtherBin';
  165. Helper::createIniFile($configFile, $options);
  166. // test
  167. putenv('CONFIG_PATH=' . $this->_path);
  168. $conf = new Configuration;
  169. $this->assertEquals('OtherBin', $conf->getKey('name'), 'changing config path is supported');
  170. // cleanup environment
  171. if (is_file($configFile)) {
  172. unlink($configFile);
  173. }
  174. putenv('CONFIG_PATH');
  175. }
  176. public function testConfigPathIni()
  177. {
  178. // setup
  179. $configFile = $this->_path . DIRECTORY_SEPARATOR . 'conf.ini';
  180. $configMigrated = $this->_path . DIRECTORY_SEPARATOR . 'conf.php';
  181. $options = $this->_options;
  182. $options['main']['name'] = 'OtherBin';
  183. Helper::createIniFile($configFile, $options);
  184. $this->assertFileDoesNotExist(CONF, 'configuration in the default location is non existing');
  185. // test
  186. putenv('CONFIG_PATH=' . $this->_path);
  187. $conf = new Configuration;
  188. $this->assertEquals('OtherBin', $conf->getKey('name'), 'changing config path is supported for ini files as well');
  189. $this->assertFileExists($configMigrated, 'old configuration file gets converted');
  190. $this->assertFileDoesNotExist($configFile, 'old configuration file gets removed');
  191. $this->assertFileDoesNotExist(CONF, 'configuration is not created in the default location');
  192. // cleanup environment
  193. if (is_file($configFile)) {
  194. unlink($configFile);
  195. }
  196. putenv('CONFIG_PATH');
  197. }
  198. }