mountconfig.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. class Test_Mount_Config_Dummy_Storage extends \OC\Files\Storage\Common {
  27. public function __construct($params) {
  28. if (isset($params['simulateFail']) && $params['simulateFail'] == true) {
  29. throw new \Exception('Simulated config validation fail');
  30. }
  31. }
  32. public function getId() {
  33. return 'dummy_storage';
  34. }
  35. public function mkdir($path) {
  36. return false;
  37. }
  38. public function rmdir($path) {
  39. return false;
  40. }
  41. public function opendir($path) {
  42. return false;
  43. }
  44. public function filetype($path) {
  45. return false;
  46. }
  47. public function file_exists($path) {
  48. return false;
  49. }
  50. public function unlink($path) {
  51. return false;
  52. }
  53. public function fopen($path, $mode) {
  54. return false;
  55. }
  56. public function touch($path, $mtime = null) {
  57. return false;
  58. }
  59. public function stat($path) {
  60. return false;
  61. }
  62. public function test() {
  63. return true;
  64. }
  65. }
  66. class Test_Mount_Config_Storage_No_Personal extends Test_Mount_Config_Dummy_Storage {
  67. }
  68. class Test_Mount_Config_Hook_Test {
  69. static $signal;
  70. static $params;
  71. public static function setUpHooks() {
  72. self::clear();
  73. \OCP\Util::connectHook(
  74. \OC\Files\Filesystem::CLASSNAME,
  75. \OC\Files\Filesystem::signal_create_mount,
  76. '\Test_Mount_Config_Hook_Test', 'createHookCallback');
  77. \OCP\Util::connectHook(
  78. \OC\Files\Filesystem::CLASSNAME,
  79. \OC\Files\Filesystem::signal_delete_mount,
  80. '\Test_Mount_Config_Hook_Test', 'deleteHookCallback');
  81. }
  82. public static function clear() {
  83. self::$signal = null;
  84. self::$params = null;
  85. }
  86. public static function createHookCallback($params) {
  87. self::$signal = \OC\Files\Filesystem::signal_create_mount;
  88. self::$params = $params;
  89. }
  90. public static function deleteHookCallback($params) {
  91. self::$signal = \OC\Files\Filesystem::signal_delete_mount;
  92. self::$params = $params;
  93. }
  94. public static function getLastCall() {
  95. return array(self::$signal, self::$params);
  96. }
  97. }
  98. /**
  99. * Class Test_Mount_Config
  100. */
  101. class Test_Mount_Config extends \Test\TestCase {
  102. private $dataDir;
  103. private $userHome;
  104. private $oldAllowedBackends;
  105. const TEST_USER1 = 'user1';
  106. const TEST_USER2 = 'user2';
  107. const TEST_GROUP1 = 'group1';
  108. const TEST_GROUP1B = 'group1b';
  109. const TEST_GROUP2 = 'group2';
  110. const TEST_GROUP2B = 'group2b';
  111. protected function setUp() {
  112. parent::setUp();
  113. OC_Mount_Config::registerBackend('Test_Mount_Config_Dummy_Storage', array(
  114. 'backend' => 'dummy',
  115. 'priority' => 150,
  116. 'configuration' => array()
  117. )
  118. );
  119. OC_Mount_Config::registerBackend('Test_Mount_Config_Storage_No_Personal', array(
  120. 'backend' => 'dummy no personal',
  121. 'priority' => 150,
  122. 'configuration' => array()
  123. )
  124. );
  125. \OC_User::createUser(self::TEST_USER1, self::TEST_USER1);
  126. \OC_User::createUser(self::TEST_USER2, self::TEST_USER2);
  127. \OC_Group::createGroup(self::TEST_GROUP1);
  128. \OC_Group::createGroup(self::TEST_GROUP1B);
  129. \OC_Group::addToGroup(self::TEST_USER1, self::TEST_GROUP1);
  130. \OC_Group::addToGroup(self::TEST_USER1, self::TEST_GROUP1B);
  131. \OC_Group::createGroup(self::TEST_GROUP2);
  132. \OC_Group::createGroup(self::TEST_GROUP2B);
  133. \OC_Group::addToGroup(self::TEST_USER2, self::TEST_GROUP2);
  134. \OC_Group::addToGroup(self::TEST_USER2, self::TEST_GROUP2B);
  135. \OC_User::setUserId(self::TEST_USER1);
  136. $this->userHome = \OC_User::getHome(self::TEST_USER1);
  137. @mkdir($this->userHome);
  138. $this->dataDir = \OC_Config::getValue(
  139. 'datadirectory',
  140. \OC::$SERVERROOT . '/data/'
  141. );
  142. $this->oldAllowedBackends = OCP\Config::getAppValue(
  143. 'files_external',
  144. 'user_mounting_backends',
  145. ''
  146. );
  147. OCP\Config::setAppValue(
  148. 'files_external',
  149. 'user_mounting_backends',
  150. 'Test_Mount_Config_Dummy_Storage'
  151. );
  152. OC_Mount_Config::$skipTest = true;
  153. Test_Mount_Config_Hook_Test::setupHooks();
  154. }
  155. protected function tearDown() {
  156. Test_Mount_Config_Hook_Test::clear();
  157. OC_Mount_Config::$skipTest = false;
  158. \OC_User::deleteUser(self::TEST_USER2);
  159. \OC_User::deleteUser(self::TEST_USER1);
  160. \OC_Group::deleteGroup(self::TEST_GROUP1);
  161. \OC_Group::deleteGroup(self::TEST_GROUP1B);
  162. \OC_Group::deleteGroup(self::TEST_GROUP2);
  163. \OC_Group::deleteGroup(self::TEST_GROUP2B);
  164. @unlink($this->dataDir . '/mount.json');
  165. OCP\Config::setAppValue(
  166. 'files_external',
  167. 'user_mounting_backends',
  168. $this->oldAllowedBackends
  169. );
  170. parent::tearDown();
  171. }
  172. /**
  173. * Reads the global config, for checking
  174. */
  175. private function readGlobalConfig() {
  176. $configFile = $this->dataDir . '/mount.json';
  177. return json_decode(file_get_contents($configFile), true);
  178. }
  179. private function writeGlobalConfig($config) {
  180. $configFile = $this->dataDir . '/mount.json';
  181. file_put_contents($configFile, json_encode($config));
  182. }
  183. /**
  184. * Reads the user config, for checking
  185. */
  186. private function readUserConfig() {
  187. $configFile = $this->userHome . '/mount.json';
  188. return json_decode(file_get_contents($configFile), true);
  189. }
  190. /**
  191. * Write the user config, to simulate existing files
  192. */
  193. private function writeUserConfig($config) {
  194. $configFile = $this->userHome . '/mount.json';
  195. file_put_contents($configFile, json_encode($config));
  196. }
  197. /**
  198. * Test mount point validation
  199. */
  200. public function testAddMountPointValidation() {
  201. $storageClass = 'Test_Mount_Config_Dummy_Storage';
  202. $mountType = 'user';
  203. $applicable = 'all';
  204. $isPersonal = false;
  205. $this->assertFalse(OC_Mount_Config::addMountPoint('', $storageClass, array(), $mountType, $applicable, $isPersonal));
  206. $this->assertFalse(OC_Mount_Config::addMountPoint('/', $storageClass, array(), $mountType, $applicable, $isPersonal));
  207. }
  208. /**
  209. * Test adding a global mount point
  210. */
  211. public function testAddGlobalMountPoint() {
  212. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  213. $applicable = 'all';
  214. $isPersonal = false;
  215. $storageOptions = array(
  216. 'host' => 'localhost',
  217. 'user' => 'testuser',
  218. 'password' => '12345',
  219. );
  220. $this->assertEquals(0, OC_Mount_Config::addMountPoint('/ext', 'Test_Mount_Config_Dummy_Storage', $storageOptions, $mountType, $applicable, $isPersonal));
  221. $config = $this->readGlobalConfig();
  222. $this->assertEquals(1, count($config));
  223. $this->assertTrue(isset($config[$mountType]));
  224. $this->assertTrue(isset($config[$mountType][$applicable]));
  225. $this->assertTrue(isset($config[$mountType][$applicable]['/$user/files/ext']));
  226. $this->assertEquals(
  227. 'Test_Mount_Config_Dummy_Storage',
  228. $config[$mountType][$applicable]['/$user/files/ext']['class']
  229. );
  230. }
  231. /**
  232. * Test adding a personal mount point
  233. */
  234. public function testAddMountPointSingleUser() {
  235. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  236. $applicable = self::TEST_USER1;
  237. $isPersonal = true;
  238. $storageOptions = array(
  239. 'host' => 'localhost',
  240. 'user' => 'testuser',
  241. 'password' => '12345',
  242. );
  243. $this->assertEquals(0, OC_Mount_Config::addMountPoint('/ext', 'Test_Mount_Config_Dummy_Storage', $storageOptions, $mountType, $applicable, $isPersonal));
  244. $config = $this->readUserConfig();
  245. $this->assertEquals(1, count($config));
  246. $this->assertTrue(isset($config[$mountType]));
  247. $this->assertTrue(isset($config[$mountType][$applicable]));
  248. $this->assertTrue(isset($config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext']));
  249. $this->assertEquals(
  250. 'Test_Mount_Config_Dummy_Storage',
  251. $config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext']['class']
  252. );
  253. }
  254. /**
  255. * Test adding a personal mount point using disallowed backend
  256. */
  257. public function testAddDisallowedBackendMountPointSingleUser() {
  258. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  259. $applicable = self::TEST_USER1;
  260. $isPersonal = true;
  261. // local
  262. $this->assertFalse(OC_Mount_Config::addMountPoint('/ext', '\OC\Files\Storage\Local', array(), $mountType, $applicable, $isPersonal));
  263. $storageOptions = array(
  264. 'host' => 'localhost',
  265. 'user' => 'testuser',
  266. 'password' => '12345',
  267. );
  268. // non-local but forbidden
  269. $this->assertFalse(OC_Mount_Config::addMountPoint('/ext', 'Test_Mount_Config_Storage_No_Personal', $storageOptions, $mountType, $applicable, $isPersonal));
  270. $this->assertFalse(file_exists($this->userHome . '/mount.json'));
  271. }
  272. /**
  273. * Test adding a mount point with an non-existant backend
  274. */
  275. public function testAddMountPointUnexistClass() {
  276. $storageClass = 'Unexist_Storage';
  277. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  278. $applicable = self::TEST_USER1;
  279. $isPersonal = false;
  280. $this->assertFalse(OC_Mount_Config::addMountPoint('/ext', $storageClass, array(), $mountType, $applicable, $isPersonal));
  281. }
  282. /**
  283. * Provider for testing configurations with different
  284. * "applicable" values (all, user, groups)
  285. */
  286. public function applicableConfigProvider() {
  287. return array(
  288. // applicable to "all"
  289. array(
  290. OC_Mount_Config::MOUNT_TYPE_USER,
  291. 'all',
  292. array(
  293. 'users' => array('all'),
  294. 'groups' => array()
  295. )
  296. ),
  297. // applicable to single user
  298. array(
  299. OC_Mount_Config::MOUNT_TYPE_USER,
  300. self::TEST_USER1,
  301. array(
  302. 'users' => array(self::TEST_USER1),
  303. 'groups' => array()
  304. )
  305. ),
  306. // applicable to single group
  307. array(
  308. OC_Mount_Config::MOUNT_TYPE_GROUP,
  309. self::TEST_GROUP1,
  310. array(
  311. 'users' => array(),
  312. 'groups' => array(self::TEST_GROUP1)
  313. )
  314. ),
  315. );
  316. }
  317. /**
  318. * Test reading and writing global config
  319. *
  320. * @dataProvider applicableConfigProvider
  321. */
  322. public function testReadWriteGlobalConfig($mountType, $applicable, $expectApplicableArray) {
  323. $mountType = $mountType;
  324. $applicable = $applicable;
  325. $isPersonal = false;
  326. $options = array(
  327. 'host' => 'smbhost',
  328. 'user' => 'smbuser',
  329. 'password' => 'smbpassword',
  330. 'share' => 'smbshare',
  331. 'root' => 'smbroot'
  332. );
  333. // write config
  334. $this->assertEquals(
  335. 0,
  336. OC_Mount_Config::addMountPoint(
  337. '/ext',
  338. 'Test_Mount_Config_Dummy_Storage',
  339. $options,
  340. $mountType,
  341. $applicable,
  342. $isPersonal
  343. )
  344. );
  345. // re-read config
  346. $config = OC_Mount_Config::getSystemMountPoints();
  347. $this->assertEquals(1, count($config));
  348. $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']);
  349. $this->assertEquals('ext', $config[0]['mountpoint']);
  350. $this->assertEquals($expectApplicableArray, $config[0]['applicable']);
  351. $savedOptions = $config[0]['options'];
  352. $this->assertEquals($options, $savedOptions);
  353. // key order needs to be preserved for the UI...
  354. $this->assertEquals(array_keys($options), array_keys($savedOptions));
  355. }
  356. /**
  357. * Test reading and writing config
  358. */
  359. public function testReadWritePersonalConfig() {
  360. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  361. $applicable = self::TEST_USER1;
  362. $isPersonal = true;
  363. $options = array(
  364. 'host' => 'smbhost',
  365. 'user' => 'smbuser',
  366. 'password' => 'smbpassword',
  367. 'share' => 'smbshare',
  368. 'root' => 'smbroot'
  369. );
  370. // write config
  371. $this->assertEquals(
  372. 0,
  373. OC_Mount_Config::addMountPoint(
  374. '/ext',
  375. 'Test_Mount_Config_Dummy_Storage',
  376. $options,
  377. $mountType,
  378. $applicable,
  379. $isPersonal
  380. )
  381. );
  382. // re-read config
  383. $config = OC_Mount_Config::getPersonalMountPoints();
  384. $this->assertEquals(1, count($config));
  385. $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']);
  386. $this->assertEquals('ext', $config[0]['mountpoint']);
  387. $savedOptions = $config[0]['options'];
  388. $this->assertEquals($options, $savedOptions);
  389. // key order needs to be preserved for the UI...
  390. $this->assertEquals(array_keys($options), array_keys($savedOptions));
  391. }
  392. public function testHooks() {
  393. $mountPoint = '/test';
  394. $mountType = 'user';
  395. $applicable = 'all';
  396. $isPersonal = false;
  397. $mountConfig = array(
  398. 'host' => 'smbhost',
  399. 'user' => 'smbuser',
  400. 'password' => 'smbpassword',
  401. 'share' => 'smbshare',
  402. 'root' => 'smbroot'
  403. );
  404. // write config
  405. $this->assertEquals(
  406. 0,
  407. OC_Mount_Config::addMountPoint(
  408. $mountPoint,
  409. 'Test_Mount_Config_Dummy_Storage',
  410. $mountConfig,
  411. $mountType,
  412. $applicable,
  413. $isPersonal
  414. )
  415. );
  416. list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall();
  417. $this->assertEquals(
  418. \OC\Files\Filesystem::signal_create_mount,
  419. $hookName
  420. );
  421. $this->assertEquals(
  422. $mountPoint,
  423. $params[\OC\Files\Filesystem::signal_param_path]
  424. );
  425. $this->assertEquals(
  426. $mountType,
  427. $params[\OC\Files\Filesystem::signal_param_mount_type]
  428. );
  429. $this->assertEquals(
  430. $applicable,
  431. $params[\OC\Files\Filesystem::signal_param_users]
  432. );
  433. Test_Mount_Config_Hook_Test::clear();
  434. // edit
  435. $mountConfig['host'] = 'anothersmbhost';
  436. $this->assertEquals(
  437. 0,
  438. OC_Mount_Config::addMountPoint(
  439. $mountPoint,
  440. 'Test_Mount_Config_Dummy_Storage',
  441. $mountConfig,
  442. $mountType,
  443. $applicable,
  444. $isPersonal
  445. )
  446. );
  447. // hook must not be called on edit
  448. list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall();
  449. $this->assertEquals(
  450. null,
  451. $hookName
  452. );
  453. Test_Mount_Config_Hook_Test::clear();
  454. $this->assertTrue(
  455. OC_Mount_Config::removeMountPoint(
  456. $mountPoint,
  457. $mountType,
  458. $applicable,
  459. $isPersonal
  460. )
  461. );
  462. list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall();
  463. $this->assertEquals(
  464. \OC\Files\Filesystem::signal_delete_mount,
  465. $hookName
  466. );
  467. $this->assertEquals(
  468. $mountPoint,
  469. $params[\OC\Files\Filesystem::signal_param_path]
  470. );
  471. $this->assertEquals(
  472. $mountType,
  473. $params[\OC\Files\Filesystem::signal_param_mount_type]
  474. );
  475. $this->assertEquals(
  476. $applicable,
  477. $params[\OC\Files\Filesystem::signal_param_users]
  478. );
  479. }
  480. /**
  481. * Test password obfuscation
  482. */
  483. public function testPasswordObfuscation() {
  484. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  485. $applicable = self::TEST_USER1;
  486. $isPersonal = true;
  487. $mountConfig = array(
  488. 'host' => 'smbhost',
  489. 'user' => 'smbuser',
  490. 'password' => 'smbpassword',
  491. 'share' => 'smbshare',
  492. 'root' => 'smbroot'
  493. );
  494. // write config
  495. $this->assertEquals(
  496. 0,
  497. OC_Mount_Config::addMountPoint(
  498. '/ext',
  499. 'Test_Mount_Config_Dummy_Storage',
  500. $mountConfig,
  501. $mountType,
  502. $applicable,
  503. $isPersonal
  504. )
  505. );
  506. // note: password re-reading is covered by testReadWritePersonalConfig
  507. // check that password inside the file is NOT in plain text
  508. $config = $this->readUserConfig();
  509. $savedConfig = $config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext']['options'];
  510. // no more clear text password in file (kept because of key order)
  511. $this->assertEquals('', $savedConfig['password']);
  512. // encrypted password is present
  513. $this->assertNotEquals($mountConfig['password'], $savedConfig['password_encrypted']);
  514. }
  515. /**
  516. * Test read legacy passwords
  517. */
  518. public function testReadLegacyPassword() {
  519. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  520. $applicable = self::TEST_USER1;
  521. $isPersonal = true;
  522. $mountConfig = array(
  523. 'host' => 'smbhost',
  524. 'user' => 'smbuser',
  525. 'password' => 'smbpassword',
  526. 'share' => 'smbshare',
  527. 'root' => 'smbroot'
  528. );
  529. // write config
  530. $this->assertEquals(
  531. 0,
  532. OC_Mount_Config::addMountPoint(
  533. '/ext',
  534. 'Test_Mount_Config_Dummy_Storage',
  535. $mountConfig,
  536. $mountType,
  537. $applicable,
  538. $isPersonal
  539. )
  540. );
  541. $config = $this->readUserConfig();
  542. // simulate non-encrypted password situation
  543. $config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext']['options']['password'] = 'smbpasswd';
  544. $this->writeUserConfig($config);
  545. // re-read config, password was read correctly
  546. $config = OC_Mount_Config::getPersonalMountPoints();
  547. $savedMountConfig = $config[0]['options'];
  548. $this->assertEquals($mountConfig, $savedMountConfig);
  549. }
  550. public function testVariableSubstitution() {
  551. $legacyBackendOptions = [
  552. 'user' => 'someuser',
  553. 'password' => 'somepassword',
  554. 'replacethis' => '$user',
  555. ];
  556. $legacyBackendOptions = \OC_Mount_Config::encryptPasswords($legacyBackendOptions);
  557. $legacyConfig = [
  558. 'class' => '\OC\Files\Storage\SMB',
  559. 'options' => $legacyBackendOptions,
  560. 'mountOptions' => ['preview' => false, 'int' => 1],
  561. ];
  562. // different mount options
  563. $legacyConfig2 = [
  564. 'class' => '\OC\Files\Storage\SMB',
  565. 'options' => $legacyBackendOptions,
  566. 'mountOptions' => ['preview' => true, 'string' => 'abc'],
  567. ];
  568. $json = [
  569. 'user' => [
  570. self::TEST_USER1 => [
  571. '/$user/files/somemount' => $legacyConfig,
  572. '/$user/files/anothermount' => $legacyConfig2,
  573. ],
  574. ],
  575. ];
  576. $this->writeGlobalConfig($json);
  577. // re-read config, password was read correctly
  578. $config = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1);
  579. $config1 = $config['/' . self::TEST_USER1 . '/files/somemount'];
  580. $config2 = $config['/' . self::TEST_USER1 . '/files/anothermount'];
  581. $this->assertSame(self::TEST_USER1, $config1['options']['replacethis']);
  582. $this->assertSame(self::TEST_USER1, $config1['options']['replacethis']);
  583. $this->assertSame(1, $config1['mountOptions']['int']);
  584. $this->assertSame(true, $config2['mountOptions']['preview']);
  585. $this->assertSame('abc', $config2['mountOptions']['string']);
  586. }
  587. public function mountDataProvider() {
  588. return array(
  589. // Tests for visible mount points
  590. // system mount point for all users
  591. array(
  592. false,
  593. OC_Mount_Config::MOUNT_TYPE_USER,
  594. 'all',
  595. self::TEST_USER1,
  596. true,
  597. ),
  598. // system mount point for a specific user
  599. array(
  600. false,
  601. OC_Mount_Config::MOUNT_TYPE_USER,
  602. self::TEST_USER1,
  603. self::TEST_USER1,
  604. true,
  605. ),
  606. // system mount point for a specific group
  607. array(
  608. false,
  609. OC_Mount_Config::MOUNT_TYPE_GROUP,
  610. self::TEST_GROUP1,
  611. self::TEST_USER1,
  612. true,
  613. ),
  614. // user mount point
  615. array(
  616. true,
  617. OC_Mount_Config::MOUNT_TYPE_USER,
  618. self::TEST_USER1,
  619. self::TEST_USER1,
  620. true,
  621. ),
  622. // Tests for non-visible mount points
  623. // system mount point for another user
  624. array(
  625. false,
  626. OC_Mount_Config::MOUNT_TYPE_USER,
  627. self::TEST_USER2,
  628. self::TEST_USER1,
  629. false,
  630. ),
  631. // system mount point for a specific group
  632. array(
  633. false,
  634. OC_Mount_Config::MOUNT_TYPE_GROUP,
  635. self::TEST_GROUP2,
  636. self::TEST_USER1,
  637. false,
  638. ),
  639. // user mount point
  640. array(
  641. true,
  642. OC_Mount_Config::MOUNT_TYPE_USER,
  643. self::TEST_USER1,
  644. self::TEST_USER2,
  645. false,
  646. ),
  647. );
  648. }
  649. /**
  650. * Test mount points used at mount time, making sure
  651. * the configuration is prepared properly.
  652. *
  653. * @dataProvider mountDataProvider
  654. * @param bool $isPersonal true for personal mount point, false for system mount point
  655. * @param string $mountType mount type
  656. * @param string $applicable target user/group or "all"
  657. * @param string $testUser user for which to retrieve the mount points
  658. * @param bool $expectVisible whether to expect the mount point to be visible for $testUser
  659. */
  660. public function testMount($isPersonal, $mountType, $applicable, $testUser, $expectVisible) {
  661. $mountConfig = array(
  662. 'host' => 'someost',
  663. 'user' => 'someuser',
  664. 'password' => 'somepassword',
  665. 'root' => 'someroot',
  666. 'share' => '',
  667. );
  668. // add mount point as "test" user
  669. $this->assertEquals(
  670. 0,
  671. OC_Mount_Config::addMountPoint(
  672. '/ext',
  673. 'Test_Mount_Config_Dummy_Storage',
  674. $mountConfig,
  675. $mountType,
  676. $applicable,
  677. $isPersonal
  678. )
  679. );
  680. // check mount points in the perspective of user $testUser
  681. \OC_User::setUserId($testUser);
  682. $mountPoints = OC_Mount_Config::getAbsoluteMountPoints($testUser);
  683. if ($expectVisible) {
  684. $this->assertEquals(1, count($mountPoints));
  685. $this->assertTrue(isset($mountPoints['/' . self::TEST_USER1 . '/files/ext']));
  686. $this->assertEquals('Test_Mount_Config_Dummy_Storage', $mountPoints['/' . self::TEST_USER1 . '/files/ext']['class']);
  687. $this->assertEquals($mountConfig, $mountPoints['/' . self::TEST_USER1 . '/files/ext']['options']);
  688. }
  689. else {
  690. $this->assertEquals(0, count($mountPoints));
  691. }
  692. }
  693. /**
  694. * Test the same config for multiple users.
  695. * The config will be merged by getSystemMountPoints().
  696. */
  697. public function testConfigMerging() {
  698. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  699. $isPersonal = false;
  700. $options = array(
  701. 'host' => 'smbhost',
  702. 'user' => 'smbuser',
  703. 'password' => 'smbpassword',
  704. 'share' => 'smbshare',
  705. 'root' => 'smbroot'
  706. );
  707. // write config
  708. $this->assertEquals(
  709. 0,
  710. OC_Mount_Config::addMountPoint(
  711. '/ext',
  712. 'Test_Mount_Config_Dummy_Storage',
  713. $options,
  714. OC_Mount_Config::MOUNT_TYPE_USER,
  715. self::TEST_USER1,
  716. $isPersonal
  717. )
  718. );
  719. $this->assertEquals(
  720. 0,
  721. OC_Mount_Config::addMountPoint(
  722. '/ext',
  723. 'Test_Mount_Config_Dummy_Storage',
  724. $options,
  725. OC_Mount_Config::MOUNT_TYPE_USER,
  726. self::TEST_USER2,
  727. $isPersonal
  728. )
  729. );
  730. $this->assertEquals(
  731. 0,
  732. OC_Mount_Config::addMountPoint(
  733. '/ext',
  734. 'Test_Mount_Config_Dummy_Storage',
  735. $options,
  736. OC_Mount_Config::MOUNT_TYPE_GROUP,
  737. self::TEST_GROUP2,
  738. $isPersonal
  739. )
  740. );
  741. $this->assertEquals(
  742. 0,
  743. OC_Mount_Config::addMountPoint(
  744. '/ext',
  745. 'Test_Mount_Config_Dummy_Storage',
  746. $options,
  747. OC_Mount_Config::MOUNT_TYPE_GROUP,
  748. self::TEST_GROUP1,
  749. $isPersonal
  750. )
  751. );
  752. // re-read config
  753. $config = OC_Mount_Config::getSystemMountPoints();
  754. $this->assertEquals(1, count($config));
  755. $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']);
  756. $this->assertEquals('ext', $config[0]['mountpoint']);
  757. $this->assertEquals($options, $config[0]['options']);
  758. $this->assertEquals(array(self::TEST_USER1, self::TEST_USER2), $config[0]['applicable']['users']);
  759. $this->assertEquals(array(self::TEST_GROUP2, self::TEST_GROUP1), $config[0]['applicable']['groups']);
  760. }
  761. /**
  762. * Create then re-read mount points configs where the mount points
  763. * have the same path, the config must NOT be merged.
  764. */
  765. public function testRereadMountpointWithSamePath() {
  766. $mountType = OC_Mount_Config::MOUNT_TYPE_USER;
  767. $isPersonal = false;
  768. $options1 = array(
  769. 'host' => 'smbhost',
  770. 'user' => 'smbuser',
  771. 'password' => 'smbpassword',
  772. 'share' => 'smbshare',
  773. 'root' => 'smbroot'
  774. );
  775. // write config
  776. $this->assertEquals(
  777. 0,
  778. OC_Mount_Config::addMountPoint(
  779. '/ext',
  780. 'Test_Mount_Config_Dummy_Storage',
  781. $options1,
  782. $mountType,
  783. self::TEST_USER1,
  784. $isPersonal
  785. )
  786. );
  787. $options2 = array(
  788. 'host' => 'anothersmbhost',
  789. 'user' => 'anothersmbuser',
  790. 'password' => 'anothersmbpassword',
  791. 'share' => 'anothersmbshare',
  792. 'root' => 'anothersmbroot'
  793. );
  794. $this->assertEquals(
  795. 0,
  796. OC_Mount_Config::addMountPoint(
  797. '/ext',
  798. 'Test_Mount_Config_Dummy_Storage',
  799. $options2,
  800. $mountType,
  801. self::TEST_USER2,
  802. $isPersonal
  803. )
  804. );
  805. // re-read config
  806. $config = OC_Mount_Config::getSystemMountPoints();
  807. $this->assertEquals(2, count($config));
  808. $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']);
  809. $this->assertEquals('ext', $config[0]['mountpoint']);
  810. $this->assertEquals($options1, $config[0]['options']);
  811. $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[1]['class']);
  812. $this->assertEquals('ext', $config[1]['mountpoint']);
  813. $this->assertEquals($options2, $config[1]['options']);
  814. }
  815. public function priorityDataProvider() {
  816. return array(
  817. // test 1 - group vs group
  818. array(
  819. array(
  820. array(
  821. 'isPersonal' => false,
  822. 'mountType' => OC_Mount_Config::MOUNT_TYPE_GROUP,
  823. 'applicable' => self::TEST_GROUP1,
  824. 'priority' => 50
  825. ),
  826. array(
  827. 'isPersonal' => false,
  828. 'mountType' => OC_Mount_Config::MOUNT_TYPE_GROUP,
  829. 'applicable' => self::TEST_GROUP1B,
  830. 'priority' => 60
  831. )
  832. ),
  833. 1
  834. ),
  835. // test 2 - user vs personal
  836. array(
  837. array(
  838. array(
  839. 'isPersonal' => false,
  840. 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER,
  841. 'applicable' => self::TEST_USER1,
  842. 'priority' => 2000
  843. ),
  844. array(
  845. 'isPersonal' => true,
  846. 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER,
  847. 'applicable' => self::TEST_USER1,
  848. 'priority' => null
  849. )
  850. ),
  851. 1
  852. ),
  853. // test 3 - all vs group vs user
  854. array(
  855. array(
  856. array(
  857. 'isPersonal' => false,
  858. 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER,
  859. 'applicable' => 'all',
  860. 'priority' => 70
  861. ),
  862. array(
  863. 'isPersonal' => false,
  864. 'mountType' => OC_Mount_Config::MOUNT_TYPE_GROUP,
  865. 'applicable' => self::TEST_GROUP1,
  866. 'priority' => 60
  867. ),
  868. array(
  869. 'isPersonal' => false,
  870. 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER,
  871. 'applicable' => self::TEST_USER1,
  872. 'priority' => 50
  873. )
  874. ),
  875. 2
  876. )
  877. );
  878. }
  879. /**
  880. * Ensure priorities are being respected
  881. * Test user is self::TEST_USER1
  882. *
  883. * @dataProvider priorityDataProvider
  884. * @param array[] $mounts array of associative array of mount parameters:
  885. * bool $isPersonal
  886. * string $mountType
  887. * string $applicable
  888. * int|null $priority null for personal
  889. * @param int $expected index of expected visible mount
  890. */
  891. public function testPriority($mounts, $expected) {
  892. $mountConfig = array(
  893. 'host' => 'somehost',
  894. 'user' => 'someuser',
  895. 'password' => 'somepassword',
  896. 'root' => 'someroot',
  897. 'share' => '',
  898. );
  899. // Add mount points
  900. foreach($mounts as $i => $mount) {
  901. $this->assertEquals(
  902. 0,
  903. OC_Mount_Config::addMountPoint(
  904. '/ext',
  905. 'Test_Mount_Config_Dummy_Storage',
  906. $mountConfig + array('id' => $i),
  907. $mount['mountType'],
  908. $mount['applicable'],
  909. $mount['isPersonal'],
  910. $mount['priority']
  911. )
  912. );
  913. }
  914. // Get mount points for user
  915. $mountPoints = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1);
  916. $this->assertEquals(1, count($mountPoints));
  917. $this->assertEquals($expected, $mountPoints['/'.self::TEST_USER1.'/files/ext']['options']['id']);
  918. }
  919. /**
  920. * Test for persistence of priority when changing mount options
  921. */
  922. public function testPriorityPersistence() {
  923. $class = 'Test_Mount_Config_Dummy_Storage';
  924. $priority = 123;
  925. $mountConfig = array(
  926. 'host' => 'somehost',
  927. 'user' => 'someuser',
  928. 'password' => 'somepassword',
  929. 'root' => 'someroot',
  930. 'share' => '',
  931. );
  932. $this->assertEquals(
  933. 0,
  934. OC_Mount_Config::addMountPoint(
  935. '/ext',
  936. $class,
  937. $mountConfig,
  938. OC_Mount_Config::MOUNT_TYPE_USER,
  939. self::TEST_USER1,
  940. false,
  941. $priority
  942. )
  943. );
  944. // Check for correct priority
  945. $mountPoints = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1);
  946. $this->assertEquals($priority,
  947. $mountPoints['/'.self::TEST_USER1.'/files/ext']['priority']);
  948. // Simulate changed mount options (without priority set)
  949. $this->assertEquals(
  950. 0,
  951. OC_Mount_Config::addMountPoint(
  952. '/ext',
  953. $class,
  954. $mountConfig,
  955. OC_Mount_Config::MOUNT_TYPE_USER,
  956. self::TEST_USER1,
  957. false
  958. )
  959. );
  960. // Check for correct priority
  961. $mountPoints = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1);
  962. $this->assertEquals($priority,
  963. $mountPoints['/'.self::TEST_USER1.'/files/ext']['priority']);
  964. }
  965. /*
  966. * Test for correct personal configuration loading in file sharing scenarios
  967. */
  968. public function testMultiUserPersonalConfigLoading() {
  969. $mountConfig = array(
  970. 'host' => 'somehost',
  971. 'user' => 'someuser',
  972. 'password' => 'somepassword',
  973. 'root' => 'someroot',
  974. 'share' => '',
  975. );
  976. // Create personal mount point
  977. $this->assertEquals(
  978. 0,
  979. OC_Mount_Config::addMountPoint(
  980. '/ext',
  981. 'Test_Mount_Config_Dummy_Storage',
  982. $mountConfig,
  983. OC_Mount_Config::MOUNT_TYPE_USER,
  984. self::TEST_USER1,
  985. true
  986. )
  987. );
  988. // Ensure other user can read mount points
  989. \OC_User::setUserId(self::TEST_USER2);
  990. $mountPointsMe = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER2);
  991. $mountPointsOther = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1);
  992. $this->assertEquals(0, count($mountPointsMe));
  993. $this->assertEquals(1, count($mountPointsOther));
  994. $this->assertTrue(isset($mountPointsOther['/'.self::TEST_USER1.'/files/ext']));
  995. $this->assertEquals('Test_Mount_Config_Dummy_Storage',
  996. $mountPointsOther['/'.self::TEST_USER1.'/files/ext']['class']);
  997. $this->assertEquals($mountConfig,
  998. $mountPointsOther['/'.self::TEST_USER1.'/files/ext']['options']);
  999. }
  1000. public function testAllowWritingIncompleteConfigIfStorageContructorFails() {
  1001. $storageClass = 'Test_Mount_Config_Dummy_Storage';
  1002. $mountType = 'user';
  1003. $applicable = 'all';
  1004. $isPersonal = false;
  1005. $this->assertEquals(
  1006. 0,
  1007. OC_Mount_Config::addMountPoint(
  1008. '/ext',
  1009. $storageClass,
  1010. array('simulateFail' => true),
  1011. $mountType,
  1012. $applicable,
  1013. $isPersonal
  1014. )
  1015. );
  1016. // config can be retrieved afterwards
  1017. $mounts = OC_Mount_Config::getSystemMountPoints();
  1018. $this->assertEquals(1, count($mounts));
  1019. // no storage id was set
  1020. $this->assertFalse(isset($mounts[0]['storage_id']));
  1021. }
  1022. }