StorageTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @copyright (C) 2015 ownCloud, Inc.
  6. *
  7. * @author Bjoern Schiessle <schiessle@owncloud.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. namespace Test\Encryption\Keys;
  23. use OC\Encryption\Keys\Storage;
  24. use OC\Files\View;
  25. use OCP\IConfig;
  26. use OCP\Security\ICrypto;
  27. use PHPUnit\Framework\MockObject\MockObject;
  28. use Test\TestCase;
  29. class StorageTest extends TestCase {
  30. /** @var Storage */
  31. protected $storage;
  32. /** @var \PHPUnit\Framework\MockObject\MockObject */
  33. protected $util;
  34. /** @var \PHPUnit\Framework\MockObject\MockObject */
  35. protected $view;
  36. /** @var \PHPUnit\Framework\MockObject\MockObject */
  37. protected $config;
  38. /** @var MockObject|ICrypto */
  39. protected $crypto;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->util = $this->getMockBuilder('OC\Encryption\Util')
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->view = $this->getMockBuilder(View::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->crypto = $this->createMock(ICrypto::class);
  49. $this->crypto->method('encrypt')
  50. ->willReturnCallback(function ($data, $pass) {
  51. return $data;
  52. });
  53. $this->crypto->method('decrypt')
  54. ->willReturnCallback(function ($data, $pass) {
  55. return $data;
  56. });
  57. $this->config = $this->getMockBuilder(IConfig::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->storage = new Storage($this->view, $this->util, $this->crypto, $this->config);
  61. }
  62. public function testSetFileKey() {
  63. $this->config->method('getSystemValue')
  64. ->with('version')
  65. ->willReturn('20.0.0.2');
  66. $this->util->expects($this->any())
  67. ->method('getUidAndFilename')
  68. ->willReturn(['user1', '/files/foo.txt']);
  69. $this->util->expects($this->any())
  70. ->method('stripPartialFileExtension')
  71. ->willReturnArgument(0);
  72. $this->util->expects($this->any())
  73. ->method('isSystemWideMountPoint')
  74. ->willReturn(false);
  75. $data = json_encode(['key' => base64_encode('key')]);
  76. $this->view->expects($this->once())
  77. ->method('file_put_contents')
  78. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'),
  79. $this->equalTo($data))
  80. ->willReturn(strlen($data));
  81. $this->assertTrue(
  82. $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key', 'encModule')
  83. );
  84. }
  85. public function testSetFileOld() {
  86. $this->config->method('getSystemValue')
  87. ->with('version')
  88. ->willReturn('20.0.0.0');
  89. $this->util->expects($this->any())
  90. ->method('getUidAndFilename')
  91. ->willReturn(['user1', '/files/foo.txt']);
  92. $this->util->expects($this->any())
  93. ->method('stripPartialFileExtension')
  94. ->willReturnArgument(0);
  95. $this->util->expects($this->any())
  96. ->method('isSystemWideMountPoint')
  97. ->willReturn(false);
  98. $this->crypto->expects($this->never())
  99. ->method('encrypt');
  100. $this->view->expects($this->once())
  101. ->method('file_put_contents')
  102. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'),
  103. $this->equalTo('key'))
  104. ->willReturn(strlen('key'));
  105. $this->assertTrue(
  106. $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key', 'encModule')
  107. );
  108. }
  109. public function dataTestGetFileKey() {
  110. return [
  111. ['/files/foo.txt', '/files/foo.txt', true, 'key'],
  112. ['/files/foo.txt.ocTransferId2111130212.part', '/files/foo.txt', true, 'key'],
  113. ['/files/foo.txt.ocTransferId2111130212.part', '/files/foo.txt', false, 'key2'],
  114. ];
  115. }
  116. /**
  117. * @dataProvider dataTestGetFileKey
  118. *
  119. * @param string $path
  120. * @param string $strippedPartialName
  121. * @param bool $originalKeyExists
  122. * @param string $expectedKeyContent
  123. */
  124. public function testGetFileKey($path, $strippedPartialName, $originalKeyExists, $expectedKeyContent) {
  125. $this->config->method('getSystemValue')
  126. ->with('version')
  127. ->willReturn('20.0.0.2');
  128. $this->util->expects($this->any())
  129. ->method('getUidAndFilename')
  130. ->willReturnMap([
  131. ['user1/files/foo.txt', ['user1', '/files/foo.txt']],
  132. ['user1/files/foo.txt.ocTransferId2111130212.part', ['user1', '/files/foo.txt.ocTransferId2111130212.part']],
  133. ]);
  134. // we need to strip away the part file extension in order to reuse a
  135. // existing key if it exists, otherwise versions will break
  136. $this->util->expects($this->once())
  137. ->method('stripPartialFileExtension')
  138. ->willReturn('user1' . $strippedPartialName);
  139. $this->util->expects($this->any())
  140. ->method('isSystemWideMountPoint')
  141. ->willReturn(false);
  142. $this->view->expects($this->at(0))
  143. ->method('file_exists')
  144. ->with($this->equalTo('/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey'))
  145. ->willReturn($originalKeyExists);
  146. $this->crypto->method('decrypt')
  147. ->willReturnCallback(function ($data, $pass) {
  148. return $data;
  149. });
  150. if (!$originalKeyExists) {
  151. $this->view->expects($this->at(1))
  152. ->method('file_exists')
  153. ->with($this->equalTo('/user1/files_encryption/keys' . $path . '/encModule/fileKey'))
  154. ->willReturn(true);
  155. $this->view->expects($this->once())
  156. ->method('file_get_contents')
  157. ->with($this->equalTo('/user1/files_encryption/keys' . $path . '/encModule/fileKey'))
  158. ->willReturn(json_encode(['key' => base64_encode('key2')]));
  159. } else {
  160. $this->view->expects($this->once())
  161. ->method('file_get_contents')
  162. ->with($this->equalTo('/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey'))
  163. ->willReturn(json_encode(['key' => base64_encode('key')]));
  164. }
  165. $this->assertSame($expectedKeyContent,
  166. $this->storage->getFileKey('user1' . $path, 'fileKey', 'encModule')
  167. );
  168. }
  169. public function testSetFileKeySystemWide() {
  170. $this->config->method('getSystemValue')
  171. ->with('version')
  172. ->willReturn('20.0.0.2');
  173. $this->util->expects($this->any())
  174. ->method('getUidAndFilename')
  175. ->willReturn(['user1', '/files/foo.txt']);
  176. $this->util->expects($this->any())
  177. ->method('isSystemWideMountPoint')
  178. ->willReturn(true);
  179. $this->util->expects($this->any())
  180. ->method('stripPartialFileExtension')
  181. ->willReturnArgument(0);
  182. $this->crypto->method('encrypt')
  183. ->willReturnCallback(function ($data, $pass) {
  184. return $data;
  185. });
  186. $data = json_encode(['key' => base64_encode('key')]);
  187. $this->view->expects($this->once())
  188. ->method('file_put_contents')
  189. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'),
  190. $this->equalTo($data))
  191. ->willReturn(strlen($data));
  192. $this->assertTrue(
  193. $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key', 'encModule')
  194. );
  195. }
  196. public function testGetFileKeySystemWide() {
  197. $this->config->method('getSystemValue')
  198. ->with('version')
  199. ->willReturn('20.0.0.2');
  200. $this->util->expects($this->any())
  201. ->method('getUidAndFilename')
  202. ->willReturn(['user1', '/files/foo.txt']);
  203. $this->util->expects($this->any())
  204. ->method('stripPartialFileExtension')
  205. ->willReturnArgument(0);
  206. $this->util->expects($this->any())
  207. ->method('isSystemWideMountPoint')
  208. ->willReturn(true);
  209. $this->view->expects($this->once())
  210. ->method('file_get_contents')
  211. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  212. ->willReturn(json_encode(['key' => base64_encode('key')]));
  213. $this->view->expects($this->once())
  214. ->method('file_exists')
  215. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  216. ->willReturn(true);
  217. $this->assertSame('key',
  218. $this->storage->getFileKey('user1/files/foo.txt', 'fileKey', 'encModule')
  219. );
  220. }
  221. public function testSetSystemUserKey() {
  222. $this->config->method('getSystemValue')
  223. ->with('version')
  224. ->willReturn('20.0.0.2');
  225. $data = json_encode([
  226. 'key' => base64_encode('key'),
  227. 'uid' => null]
  228. );
  229. $this->view->expects($this->once())
  230. ->method('file_put_contents')
  231. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'),
  232. $this->equalTo($data))
  233. ->willReturn(strlen($data));
  234. $this->assertTrue(
  235. $this->storage->setSystemUserKey('shareKey_56884', 'key', 'encModule')
  236. );
  237. }
  238. public function testSetUserKey() {
  239. $this->config->method('getSystemValue')
  240. ->with('version')
  241. ->willReturn('20.0.0.2');
  242. $data = json_encode([
  243. 'key' => base64_encode('key'),
  244. 'uid' => 'user1']
  245. );
  246. $this->view->expects($this->once())
  247. ->method('file_put_contents')
  248. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'),
  249. $this->equalTo($data))
  250. ->willReturn(strlen($data));
  251. $this->assertTrue(
  252. $this->storage->setUserKey('user1', 'publicKey', 'key', 'encModule')
  253. );
  254. }
  255. public function testGetSystemUserKey() {
  256. $this->config->method('getSystemValue')
  257. ->with('version')
  258. ->willReturn('20.0.0.2');
  259. $data = json_encode([
  260. 'key' => base64_encode('key'),
  261. 'uid' => null]
  262. );
  263. $this->view->expects($this->once())
  264. ->method('file_get_contents')
  265. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  266. ->willReturn($data);
  267. $this->view->expects($this->once())
  268. ->method('file_exists')
  269. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  270. ->willReturn(true);
  271. $this->assertSame('key',
  272. $this->storage->getSystemUserKey('shareKey_56884', 'encModule')
  273. );
  274. }
  275. public function testGetUserKey() {
  276. $this->config->method('getSystemValue')
  277. ->with('version')
  278. ->willReturn('20.0.0.2');
  279. $data = json_encode([
  280. 'key' => base64_encode('key'),
  281. 'uid' => 'user1']
  282. );
  283. $this->view->expects($this->once())
  284. ->method('file_get_contents')
  285. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  286. ->willReturn($data);
  287. $this->view->expects($this->once())
  288. ->method('file_exists')
  289. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  290. ->willReturn(true);
  291. $this->assertSame('key',
  292. $this->storage->getUserKey('user1', 'publicKey', 'encModule')
  293. );
  294. }
  295. public function testDeleteUserKey() {
  296. $this->view->expects($this->once())
  297. ->method('file_exists')
  298. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  299. ->willReturn(true);
  300. $this->view->expects($this->once())
  301. ->method('unlink')
  302. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  303. ->willReturn(true);
  304. $this->assertTrue(
  305. $this->storage->deleteUserKey('user1', 'publicKey', 'encModule')
  306. );
  307. }
  308. public function testDeleteSystemUserKey() {
  309. $this->view->expects($this->once())
  310. ->method('file_exists')
  311. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  312. ->willReturn(true);
  313. $this->view->expects($this->once())
  314. ->method('unlink')
  315. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  316. ->willReturn(true);
  317. $this->assertTrue(
  318. $this->storage->deleteSystemUserKey('shareKey_56884', 'encModule')
  319. );
  320. }
  321. public function testDeleteFileKeySystemWide() {
  322. $this->util->expects($this->any())
  323. ->method('getUidAndFilename')
  324. ->willReturn(['user1', '/files/foo.txt']);
  325. $this->util->expects($this->any())
  326. ->method('stripPartialFileExtension')
  327. ->willReturnArgument(0);
  328. $this->util->expects($this->any())
  329. ->method('isSystemWideMountPoint')
  330. ->willReturn(true);
  331. $this->view->expects($this->once())
  332. ->method('file_exists')
  333. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  334. ->willReturn(true);
  335. $this->view->expects($this->once())
  336. ->method('unlink')
  337. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  338. ->willReturn(true);
  339. $this->assertTrue(
  340. $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey', 'encModule')
  341. );
  342. }
  343. public function testDeleteFileKey() {
  344. $this->util->expects($this->any())
  345. ->method('getUidAndFilename')
  346. ->willReturn(['user1', '/files/foo.txt']);
  347. $this->util->expects($this->any())
  348. ->method('stripPartialFileExtension')
  349. ->willReturnArgument(0);
  350. $this->util->expects($this->any())
  351. ->method('isSystemWideMountPoint')
  352. ->willReturn(false);
  353. $this->view->expects($this->once())
  354. ->method('file_exists')
  355. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  356. ->willReturn(true);
  357. $this->view->expects($this->once())
  358. ->method('unlink')
  359. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  360. ->willReturn(true);
  361. $this->assertTrue(
  362. $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey', 'encModule')
  363. );
  364. }
  365. /**
  366. * @dataProvider dataProviderCopyRename
  367. */
  368. public function testRenameKeys($source, $target, $systemWideMountSource, $systemWideMountTarget, $expectedSource, $expectedTarget) {
  369. $this->view->expects($this->any())
  370. ->method('file_exists')
  371. ->willReturn(true);
  372. $this->view->expects($this->any())
  373. ->method('is_dir')
  374. ->willReturn(true);
  375. $this->view->expects($this->once())
  376. ->method('rename')
  377. ->with(
  378. $this->equalTo($expectedSource),
  379. $this->equalTo($expectedTarget))
  380. ->willReturn(true);
  381. $this->util->expects($this->any())
  382. ->method('getUidAndFilename')
  383. ->willReturnCallback([$this, 'getUidAndFilenameCallback']);
  384. $this->util->expects($this->any())
  385. ->method('isSystemWideMountPoint')
  386. ->willReturnCallback(function ($path, $owner) use ($systemWideMountSource, $systemWideMountTarget) {
  387. if (strpos($path, 'source.txt') !== false) {
  388. return $systemWideMountSource;
  389. }
  390. return $systemWideMountTarget;
  391. });
  392. $this->storage->renameKeys($source, $target);
  393. }
  394. /**
  395. * @dataProvider dataProviderCopyRename
  396. */
  397. public function testCopyKeys($source, $target, $systemWideMountSource, $systemWideMountTarget , $expectedSource, $expectedTarget) {
  398. $this->view->expects($this->any())
  399. ->method('file_exists')
  400. ->willReturn(true);
  401. $this->view->expects($this->any())
  402. ->method('is_dir')
  403. ->willReturn(true);
  404. $this->view->expects($this->once())
  405. ->method('copy')
  406. ->with(
  407. $this->equalTo($expectedSource),
  408. $this->equalTo($expectedTarget))
  409. ->willReturn(true);
  410. $this->util->expects($this->any())
  411. ->method('getUidAndFilename')
  412. ->willReturnCallback([$this, 'getUidAndFilenameCallback']);
  413. $this->util->expects($this->any())
  414. ->method('isSystemWideMountPoint')
  415. ->willReturnCallback(function ($path, $owner) use ($systemWideMountSource, $systemWideMountTarget) {
  416. if (strpos($path, 'source.txt') !== false) {
  417. return $systemWideMountSource;
  418. }
  419. return $systemWideMountTarget;
  420. });
  421. $this->storage->copyKeys($source, $target);
  422. }
  423. public function getUidAndFilenameCallback() {
  424. $args = func_get_args();
  425. $path = $args[0];
  426. $parts = explode('/', $path);
  427. return [$parts[1], '/' . implode('/', array_slice($parts, 2))];
  428. }
  429. public function dataProviderCopyRename() {
  430. return [
  431. ['/user1/files/source.txt', '/user1/files/target.txt', false, false,
  432. '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'],
  433. ['/user1/files/foo/source.txt', '/user1/files/target.txt', false, false,
  434. '/user1/files_encryption/keys/files/foo/source.txt/', '/user1/files_encryption/keys/files/target.txt/'],
  435. ['/user1/files/source.txt', '/user1/files/foo/target.txt', false, false,
  436. '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/foo/target.txt/'],
  437. ['/user1/files/source.txt', '/user1/files/foo/target.txt', true, true,
  438. '/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/foo/target.txt/'],
  439. ['/user1/files/source.txt', '/user1/files/target.txt', false, true,
  440. '/user1/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/target.txt/'],
  441. ['/user1/files/source.txt', '/user1/files/target.txt', true, false,
  442. '/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'],
  443. ['/user2/files/source.txt', '/user1/files/target.txt', false, false,
  444. '/user2/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'],
  445. ['/user2/files/foo/source.txt', '/user1/files/target.txt', false, false,
  446. '/user2/files_encryption/keys/files/foo/source.txt/', '/user1/files_encryption/keys/files/target.txt/'],
  447. ['/user2/files/source.txt', '/user1/files/foo/target.txt', false, false,
  448. '/user2/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/foo/target.txt/'],
  449. ['/user2/files/source.txt', '/user1/files/foo/target.txt', true, true,
  450. '/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/foo/target.txt/'],
  451. ['/user2/files/source.txt', '/user1/files/target.txt', false, true,
  452. '/user2/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/target.txt/'],
  453. ['/user2/files/source.txt', '/user1/files/target.txt', true, false,
  454. '/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'],
  455. ];
  456. }
  457. /**
  458. * @dataProvider dataTestGetPathToKeys
  459. *
  460. * @param string $path
  461. * @param boolean $systemWideMountPoint
  462. * @param string $storageRoot
  463. * @param string $expected
  464. */
  465. public function testGetPathToKeys($path, $systemWideMountPoint, $storageRoot, $expected) {
  466. $this->invokePrivate($this->storage, 'root_dir', [$storageRoot]);
  467. $this->util->expects($this->any())
  468. ->method('getUidAndFilename')
  469. ->willReturnCallback([$this, 'getUidAndFilenameCallback']);
  470. $this->util->expects($this->any())
  471. ->method('isSystemWideMountPoint')
  472. ->willReturn($systemWideMountPoint);
  473. $this->assertSame($expected,
  474. self::invokePrivate($this->storage, 'getPathToKeys', [$path])
  475. );
  476. }
  477. public function dataTestGetPathToKeys() {
  478. return [
  479. ['/user1/files/source.txt', false, '', '/user1/files_encryption/keys/files/source.txt/'],
  480. ['/user1/files/source.txt', true, '', '/files_encryption/keys/files/source.txt/'],
  481. ['/user1/files/source.txt', false, 'storageRoot', '/storageRoot/user1/files_encryption/keys/files/source.txt/'],
  482. ['/user1/files/source.txt', true, 'storageRoot', '/storageRoot/files_encryption/keys/files/source.txt/'],
  483. ];
  484. }
  485. public function testKeySetPreparation() {
  486. $this->view->expects($this->any())
  487. ->method('file_exists')
  488. ->willReturn(false);
  489. $this->view->expects($this->any())
  490. ->method('is_dir')
  491. ->willReturn(false);
  492. $this->view->expects($this->any())
  493. ->method('mkdir')
  494. ->willReturnCallback([$this, 'mkdirCallback']);
  495. $this->mkdirStack = [
  496. '/user1/files_encryption/keys/foo',
  497. '/user1/files_encryption/keys',
  498. '/user1/files_encryption',
  499. '/user1'];
  500. self::invokePrivate($this->storage, 'keySetPreparation', ['/user1/files_encryption/keys/foo']);
  501. }
  502. public function mkdirCallback() {
  503. $args = func_get_args();
  504. $expected = array_pop($this->mkdirStack);
  505. $this->assertSame($expected, $args[0]);
  506. }
  507. /**
  508. * @dataProvider dataTestGetFileKeyDir
  509. *
  510. * @param bool $isSystemWideMountPoint
  511. * @param string $storageRoot
  512. * @param string $expected
  513. */
  514. public function testGetFileKeyDir($isSystemWideMountPoint, $storageRoot, $expected) {
  515. $path = '/user1/files/foo/bar.txt';
  516. $owner = 'user1';
  517. $relativePath = '/foo/bar.txt';
  518. $this->invokePrivate($this->storage, 'root_dir', [$storageRoot]);
  519. $this->util->expects($this->once())->method('isSystemWideMountPoint')
  520. ->willReturn($isSystemWideMountPoint);
  521. $this->util->expects($this->once())->method('getUidAndFilename')
  522. ->with($path)->willReturn([$owner, $relativePath]);
  523. $this->assertSame($expected,
  524. $this->invokePrivate($this->storage, 'getFileKeyDir', ['OC_DEFAULT_MODULE', $path])
  525. );
  526. }
  527. public function dataTestGetFileKeyDir() {
  528. return [
  529. [false, '', '/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  530. [true, '', '/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  531. [false, 'newStorageRoot', '/newStorageRoot/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  532. [true, 'newStorageRoot', '/newStorageRoot/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  533. ];
  534. }
  535. /**
  536. * @dataProvider dataTestBackupUserKeys
  537. * @param bool $createBackupDir
  538. */
  539. public function testBackupUserKeys($createBackupDir) {
  540. $storage = $this->getMockBuilder('OC\Encryption\Keys\Storage')
  541. ->setConstructorArgs([$this->view, $this->util, $this->crypto, $this->config])
  542. ->setMethods(['getTimestamp'])
  543. ->getMock();
  544. $storage->expects($this->any())->method('getTimestamp')->willReturn('1234567');
  545. $this->view->expects($this->once())->method('file_exists')
  546. ->with('user1/files_encryption/backup')->willReturn(!$createBackupDir);
  547. if ($createBackupDir) {
  548. $this->view->expects($this->at(1))->method('mkdir')
  549. ->with('user1/files_encryption/backup');
  550. $this->view->expects($this->at(2))->method('mkdir')
  551. ->with('user1/files_encryption/backup/test.encryptionModule.1234567');
  552. } else {
  553. $this->view->expects($this->once())->method('mkdir')
  554. ->with('user1/files_encryption/backup/test.encryptionModule.1234567');
  555. }
  556. $this->view->expects($this->once())->method('copy')
  557. ->with(
  558. 'user1/files_encryption/encryptionModule',
  559. 'user1/files_encryption/backup/test.encryptionModule.1234567'
  560. )->willReturn(true);
  561. $this->assertTrue($storage->backupUserKeys('encryptionModule', 'test', 'user1'));
  562. }
  563. public function dataTestBackupUserKeys() {
  564. return [
  565. [true], [false]
  566. ];
  567. }
  568. }