mountconfig.php 26 KB

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