1
0

StorageTest.php 21 KB

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