StorageTest.php 20 KB

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