1
0

KeyManagerTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests;
  8. use OC\Files\FileInfo;
  9. use OC\Files\View;
  10. use OCA\Encryption\Crypto\Crypt;
  11. use OCA\Encryption\KeyManager;
  12. use OCA\Encryption\Session;
  13. use OCA\Encryption\Util;
  14. use OCP\Encryption\Keys\IStorage;
  15. use OCP\Files\Cache\ICache;
  16. use OCP\Files\Storage;
  17. use OCP\IConfig;
  18. use OCP\IUserSession;
  19. use OCP\Lock\ILockingProvider;
  20. use OCP\Lock\LockedException;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Psr\Log\LoggerInterface;
  23. use Test\TestCase;
  24. class KeyManagerTest extends TestCase {
  25. /**
  26. * @var KeyManager
  27. */
  28. private $instance;
  29. /**
  30. * @var string
  31. */
  32. private $userId;
  33. /** @var string */
  34. private $systemKeyId;
  35. /** @var \OCP\Encryption\Keys\IStorage|\PHPUnit\Framework\MockObject\MockObject */
  36. private $keyStorageMock;
  37. /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject */
  38. private $cryptMock;
  39. /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  40. private $userMock;
  41. /** @var \OCA\Encryption\Session|\PHPUnit\Framework\MockObject\MockObject */
  42. private $sessionMock;
  43. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  44. private $logMock;
  45. /** @var \OCA\Encryption\Util|\PHPUnit\Framework\MockObject\MockObject */
  46. private $utilMock;
  47. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  48. private $configMock;
  49. /** @var ILockingProvider|MockObject */
  50. private $lockingProviderMock;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->userId = 'user1';
  54. $this->systemKeyId = 'systemKeyId';
  55. $this->keyStorageMock = $this->createMock(IStorage::class);
  56. $this->cryptMock = $this->getMockBuilder(Crypt::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->configMock = $this->createMock(IConfig::class);
  60. $this->configMock->expects($this->any())
  61. ->method('getAppValue')
  62. ->willReturn($this->systemKeyId);
  63. $this->userMock = $this->createMock(IUserSession::class);
  64. $this->sessionMock = $this->getMockBuilder(Session::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->logMock = $this->createMock(LoggerInterface::class);
  68. $this->utilMock = $this->getMockBuilder(Util::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->lockingProviderMock = $this->createMock(ILockingProvider::class);
  72. $this->instance = new KeyManager(
  73. $this->keyStorageMock,
  74. $this->cryptMock,
  75. $this->configMock,
  76. $this->userMock,
  77. $this->sessionMock,
  78. $this->logMock,
  79. $this->utilMock,
  80. $this->lockingProviderMock
  81. );
  82. }
  83. public function testDeleteShareKey() {
  84. $this->keyStorageMock->expects($this->any())
  85. ->method('deleteFileKey')
  86. ->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey'))
  87. ->willReturn(true);
  88. $this->assertTrue(
  89. $this->instance->deleteShareKey('/path', 'keyId')
  90. );
  91. }
  92. public function testGetPrivateKey() {
  93. $this->keyStorageMock->expects($this->any())
  94. ->method('getUserKey')
  95. ->with($this->equalTo($this->userId), $this->equalTo('privateKey'))
  96. ->willReturn('privateKey');
  97. $this->assertSame('privateKey',
  98. $this->instance->getPrivateKey($this->userId)
  99. );
  100. }
  101. public function testGetPublicKey() {
  102. $this->keyStorageMock->expects($this->any())
  103. ->method('getUserKey')
  104. ->with($this->equalTo($this->userId), $this->equalTo('publicKey'))
  105. ->willReturn('publicKey');
  106. $this->assertSame('publicKey',
  107. $this->instance->getPublicKey($this->userId)
  108. );
  109. }
  110. public function testRecoveryKeyExists() {
  111. $this->keyStorageMock->expects($this->any())
  112. ->method('getSystemUserKey')
  113. ->with($this->equalTo($this->systemKeyId . '.publicKey'))
  114. ->willReturn('recoveryKey');
  115. $this->assertTrue($this->instance->recoveryKeyExists());
  116. }
  117. public function testCheckRecoveryKeyPassword() {
  118. $this->keyStorageMock->expects($this->any())
  119. ->method('getSystemUserKey')
  120. ->with($this->equalTo($this->systemKeyId . '.privateKey'))
  121. ->willReturn('recoveryKey');
  122. $this->cryptMock->expects($this->any())
  123. ->method('decryptPrivateKey')
  124. ->with($this->equalTo('recoveryKey'), $this->equalTo('pass'))
  125. ->willReturn('decryptedRecoveryKey');
  126. $this->assertTrue($this->instance->checkRecoveryPassword('pass'));
  127. }
  128. public function testSetPublicKey() {
  129. $this->keyStorageMock->expects($this->any())
  130. ->method('setUserKey')
  131. ->with(
  132. $this->equalTo($this->userId),
  133. $this->equalTo('publicKey'),
  134. $this->equalTo('key'))
  135. ->willReturn(true);
  136. $this->assertTrue(
  137. $this->instance->setPublicKey($this->userId, 'key')
  138. );
  139. }
  140. public function testSetPrivateKey() {
  141. $this->keyStorageMock->expects($this->any())
  142. ->method('setUserKey')
  143. ->with(
  144. $this->equalTo($this->userId),
  145. $this->equalTo('privateKey'),
  146. $this->equalTo('key'))
  147. ->willReturn(true);
  148. $this->assertTrue(
  149. $this->instance->setPrivateKey($this->userId, 'key')
  150. );
  151. }
  152. /**
  153. * @dataProvider dataTestUserHasKeys
  154. */
  155. public function testUserHasKeys($key, $expected) {
  156. $this->keyStorageMock->expects($this->exactly(2))
  157. ->method('getUserKey')
  158. ->with($this->equalTo($this->userId), $this->anything())
  159. ->willReturn($key);
  160. $this->assertSame($expected,
  161. $this->instance->userHasKeys($this->userId)
  162. );
  163. }
  164. public function dataTestUserHasKeys() {
  165. return [
  166. ['key', true],
  167. ['', false]
  168. ];
  169. }
  170. public function testUserHasKeysMissingPrivateKey() {
  171. $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class);
  172. $this->keyStorageMock->expects($this->exactly(2))
  173. ->method('getUserKey')
  174. ->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) {
  175. if ($keyID === 'privateKey') {
  176. return '';
  177. }
  178. return 'key';
  179. });
  180. $this->instance->userHasKeys($this->userId);
  181. }
  182. public function testUserHasKeysMissingPublicKey() {
  183. $this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class);
  184. $this->keyStorageMock->expects($this->exactly(2))
  185. ->method('getUserKey')
  186. ->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) {
  187. if ($keyID === 'publicKey') {
  188. return '';
  189. }
  190. return 'key';
  191. });
  192. $this->instance->userHasKeys($this->userId);
  193. }
  194. /**
  195. * @dataProvider dataTestInit
  196. *
  197. * @param bool $useMasterKey
  198. */
  199. public function testInit($useMasterKey) {
  200. /** @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject $instance */
  201. $instance = $this->getMockBuilder(KeyManager::class)
  202. ->setConstructorArgs(
  203. [
  204. $this->keyStorageMock,
  205. $this->cryptMock,
  206. $this->configMock,
  207. $this->userMock,
  208. $this->sessionMock,
  209. $this->logMock,
  210. $this->utilMock,
  211. $this->lockingProviderMock
  212. ]
  213. )->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey'])
  214. ->getMock();
  215. $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
  216. ->willReturn($useMasterKey);
  217. $this->sessionMock->expects($this->exactly(2))->method('setStatus')
  218. ->withConsecutive(
  219. [Session::INIT_EXECUTED],
  220. [Session::INIT_SUCCESSFUL],
  221. );
  222. $instance->expects($this->any())->method('getMasterKeyId')->willReturn('masterKeyId');
  223. $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
  224. $instance->expects($this->any())->method('getSystemPrivateKey')->with('masterKeyId')->willReturn('privateMasterKey');
  225. $instance->expects($this->any())->method('getPrivateKey')->with($this->userId)->willReturn('privateUserKey');
  226. if ($useMasterKey) {
  227. $this->cryptMock->expects($this->once())->method('decryptPrivateKey')
  228. ->with('privateMasterKey', 'masterKeyPassword', 'masterKeyId')
  229. ->willReturn('key');
  230. } else {
  231. $this->cryptMock->expects($this->once())->method('decryptPrivateKey')
  232. ->with('privateUserKey', 'pass', $this->userId)
  233. ->willReturn('key');
  234. }
  235. $this->sessionMock->expects($this->once())->method('setPrivateKey')
  236. ->with('key');
  237. $this->assertTrue($instance->init($this->userId, 'pass'));
  238. }
  239. public function dataTestInit() {
  240. return [
  241. [true],
  242. [false]
  243. ];
  244. }
  245. public function testSetRecoveryKey() {
  246. $this->keyStorageMock->expects($this->exactly(2))
  247. ->method('setSystemUserKey')
  248. ->willReturn(true);
  249. $this->cryptMock->expects($this->any())
  250. ->method('encryptPrivateKey')
  251. ->with($this->equalTo('privateKey'), $this->equalTo('pass'))
  252. ->willReturn('decryptedPrivateKey');
  253. $this->assertTrue(
  254. $this->instance->setRecoveryKey('pass',
  255. ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])
  256. );
  257. }
  258. public function testSetSystemPrivateKey() {
  259. $this->keyStorageMock->expects($this->exactly(1))
  260. ->method('setSystemUserKey')
  261. ->with($this->equalTo('keyId.privateKey'), $this->equalTo('key'))
  262. ->willReturn(true);
  263. $this->assertTrue(
  264. $this->instance->setSystemPrivateKey('keyId', 'key')
  265. );
  266. }
  267. public function testGetSystemPrivateKey() {
  268. $this->keyStorageMock->expects($this->exactly(1))
  269. ->method('getSystemUserKey')
  270. ->with($this->equalTo('keyId.privateKey'))
  271. ->willReturn('systemPrivateKey');
  272. $this->assertSame('systemPrivateKey',
  273. $this->instance->getSystemPrivateKey('keyId')
  274. );
  275. }
  276. public function testGetEncryptedFileKey() {
  277. $this->keyStorageMock->expects($this->once())
  278. ->method('getFileKey')
  279. ->with('/', 'fileKey')
  280. ->willReturn(true);
  281. $this->assertTrue($this->instance->getEncryptedFileKey('/'));
  282. }
  283. public function dataTestGetFileKey() {
  284. return [
  285. ['user1', false, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
  286. ['user1', false, 'privateKey', '', 'multiKeyDecryptResult'],
  287. ['user1', false, false, 'legacyKey', ''],
  288. ['user1', false, false, '', ''],
  289. ['user1', true, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
  290. ['user1', true, 'privateKey', '', 'multiKeyDecryptResult'],
  291. ['user1', true, false, 'legacyKey', ''],
  292. ['user1', true, false, '', ''],
  293. [null, false, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
  294. [null, false, 'privateKey', '', 'multiKeyDecryptResult'],
  295. [null, false, false, 'legacyKey', ''],
  296. [null, false, false, '', ''],
  297. [null, true, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
  298. [null, true, 'privateKey', '', 'multiKeyDecryptResult'],
  299. [null, true, false, 'legacyKey', ''],
  300. [null, true, false, '', ''],
  301. ];
  302. }
  303. /**
  304. * @dataProvider dataTestGetFileKey
  305. *
  306. * @param $uid
  307. * @param $isMasterKeyEnabled
  308. * @param $privateKey
  309. * @param $expected
  310. */
  311. public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $encryptedFileKey, $expected) {
  312. $path = '/foo.txt';
  313. if ($isMasterKeyEnabled) {
  314. $expectedUid = 'masterKeyId';
  315. $this->configMock->expects($this->any())->method('getSystemValue')->with('secret')
  316. ->willReturn('password');
  317. } elseif (!$uid) {
  318. $expectedUid = 'systemKeyId';
  319. } else {
  320. $expectedUid = $uid;
  321. }
  322. $this->invokePrivate($this->instance, 'masterKeyId', ['masterKeyId']);
  323. $this->keyStorageMock->expects($this->exactly(2))
  324. ->method('getFileKey')
  325. ->withConsecutive(
  326. [$path, 'fileKey', 'OC_DEFAULT_MODULE'],
  327. [$path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE'],
  328. )
  329. ->willReturnOnConsecutiveCalls(
  330. $encryptedFileKey,
  331. 'fileKey',
  332. );
  333. $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')
  334. ->willReturn($isMasterKeyEnabled);
  335. if (is_null($uid)) {
  336. $this->keyStorageMock->expects($this->once())
  337. ->method('getSystemUserKey')
  338. ->willReturn(true);
  339. $this->cryptMock->expects($this->once())
  340. ->method('decryptPrivateKey')
  341. ->willReturn($privateKey);
  342. } else {
  343. $this->keyStorageMock->expects($this->never())
  344. ->method('getSystemUserKey');
  345. $this->sessionMock->expects($this->once())->method('getPrivateKey')->willReturn($privateKey);
  346. }
  347. if (!empty($encryptedFileKey)) {
  348. $this->cryptMock->expects($this->never())
  349. ->method('multiKeyDecrypt');
  350. if ($privateKey) {
  351. $this->cryptMock->expects($this->once())
  352. ->method('multiKeyDecryptLegacy')
  353. ->willReturn('multiKeyDecryptResult');
  354. } else {
  355. $this->cryptMock->expects($this->never())
  356. ->method('multiKeyDecryptLegacy');
  357. }
  358. } else {
  359. $this->cryptMock->expects($this->never())
  360. ->method('multiKeyDecryptLegacy');
  361. if ($privateKey) {
  362. $this->cryptMock->expects($this->once())
  363. ->method('multiKeyDecrypt')
  364. ->willReturn('multiKeyDecryptResult');
  365. } else {
  366. $this->cryptMock->expects($this->never())
  367. ->method('multiKeyDecrypt');
  368. }
  369. }
  370. $this->assertSame($expected,
  371. $this->instance->getFileKey($path, $uid, null)
  372. );
  373. }
  374. public function testDeletePrivateKey() {
  375. $this->keyStorageMock->expects($this->once())
  376. ->method('deleteUserKey')
  377. ->with('user1', 'privateKey')
  378. ->willReturn(true);
  379. $this->assertTrue(self::invokePrivate($this->instance,
  380. 'deletePrivateKey',
  381. [$this->userId]));
  382. }
  383. public function testDeleteAllFileKeys() {
  384. $this->keyStorageMock->expects($this->once())
  385. ->method('deleteAllFileKeys')
  386. ->willReturn(true);
  387. $this->assertTrue($this->instance->deleteAllFileKeys('/'));
  388. }
  389. /**
  390. * test add public share key and or recovery key to the list of public keys
  391. *
  392. * @dataProvider dataTestAddSystemKeys
  393. *
  394. * @param array $accessList
  395. * @param array $publicKeys
  396. * @param string $uid
  397. * @param array $expectedKeys
  398. */
  399. public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys) {
  400. $publicShareKeyId = 'publicShareKey';
  401. $recoveryKeyId = 'recoveryKey';
  402. $this->keyStorageMock->expects($this->any())
  403. ->method('getSystemUserKey')
  404. ->willReturnCallback(function ($keyId, $encryptionModuleId) {
  405. return $keyId;
  406. });
  407. $this->utilMock->expects($this->any())
  408. ->method('isRecoveryEnabledForUser')
  409. ->willReturnCallback(function ($uid) {
  410. if ($uid === 'user1') {
  411. return true;
  412. }
  413. return false;
  414. });
  415. // set key IDs
  416. self::invokePrivate($this->instance, 'publicShareKeyId', [$publicShareKeyId]);
  417. self::invokePrivate($this->instance, 'recoveryKeyId', [$recoveryKeyId]);
  418. $result = $this->instance->addSystemKeys($accessList, $publicKeys, $uid);
  419. foreach ($expectedKeys as $expected) {
  420. $this->assertArrayHasKey($expected, $result);
  421. }
  422. $this->assertSameSize($expectedKeys, $result);
  423. }
  424. /**
  425. * data provider for testAddSystemKeys()
  426. *
  427. * @return array
  428. */
  429. public function dataTestAddSystemKeys() {
  430. return [
  431. [['public' => true],[], 'user1', ['publicShareKey', 'recoveryKey']],
  432. [['public' => false], [], 'user1', ['recoveryKey']],
  433. [['public' => true],[], 'user2', ['publicShareKey']],
  434. [['public' => false], [], 'user2', []],
  435. ];
  436. }
  437. public function testGetMasterKeyId() {
  438. $this->assertSame('systemKeyId', $this->instance->getMasterKeyId());
  439. }
  440. public function testGetPublicMasterKey() {
  441. $this->keyStorageMock->expects($this->once())->method('getSystemUserKey')
  442. ->with('systemKeyId.publicKey', \OCA\Encryption\Crypto\Encryption::ID)
  443. ->willReturn(true);
  444. $this->assertTrue(
  445. $this->instance->getPublicMasterKey()
  446. );
  447. }
  448. public function testGetMasterKeyPassword() {
  449. $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
  450. ->willReturn('password');
  451. $this->assertSame('password',
  452. $this->invokePrivate($this->instance, 'getMasterKeyPassword', [])
  453. );
  454. }
  455. public function testGetMasterKeyPasswordException() {
  456. $this->expectException(\Exception::class);
  457. $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
  458. ->willReturn('');
  459. $this->invokePrivate($this->instance, 'getMasterKeyPassword', []);
  460. }
  461. /**
  462. * @dataProvider dataTestValidateMasterKey
  463. *
  464. * @param $masterKey
  465. */
  466. public function testValidateMasterKey($masterKey) {
  467. /** @var \OCA\Encryption\KeyManager | \PHPUnit\Framework\MockObject\MockObject $instance */
  468. $instance = $this->getMockBuilder(KeyManager::class)
  469. ->setConstructorArgs(
  470. [
  471. $this->keyStorageMock,
  472. $this->cryptMock,
  473. $this->configMock,
  474. $this->userMock,
  475. $this->sessionMock,
  476. $this->logMock,
  477. $this->utilMock,
  478. $this->lockingProviderMock
  479. ]
  480. )->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
  481. ->getMock();
  482. $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
  483. ->willReturn(true);
  484. $instance->expects($this->once())->method('getPublicMasterKey')
  485. ->willReturn($masterKey);
  486. $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
  487. $this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
  488. if (empty($masterKey)) {
  489. $this->cryptMock->expects($this->once())->method('createKeyPair')
  490. ->willReturn(['publicKey' => 'public', 'privateKey' => 'private']);
  491. $this->keyStorageMock->expects($this->once())->method('setSystemUserKey')
  492. ->with('systemKeyId.publicKey', 'public', \OCA\Encryption\Crypto\Encryption::ID);
  493. $this->cryptMock->expects($this->once())->method('encryptPrivateKey')
  494. ->with('private', 'masterKeyPassword', 'systemKeyId')
  495. ->willReturn('EncryptedKey');
  496. $this->lockingProviderMock->expects($this->once())
  497. ->method('acquireLock');
  498. $instance->expects($this->once())->method('setSystemPrivateKey')
  499. ->with('systemKeyId', 'headerEncryptedKey');
  500. } else {
  501. $this->cryptMock->expects($this->never())->method('createKeyPair');
  502. $this->keyStorageMock->expects($this->never())->method('setSystemUserKey');
  503. $this->cryptMock->expects($this->never())->method('encryptPrivateKey');
  504. $instance->expects($this->never())->method('setSystemPrivateKey');
  505. }
  506. $instance->validateMasterKey();
  507. }
  508. public function testValidateMasterKeyLocked() {
  509. /** @var \OCA\Encryption\KeyManager | \PHPUnit_Framework_MockObject_MockObject $instance */
  510. $instance = $this->getMockBuilder(KeyManager::class)
  511. ->setConstructorArgs(
  512. [
  513. $this->keyStorageMock,
  514. $this->cryptMock,
  515. $this->configMock,
  516. $this->userMock,
  517. $this->sessionMock,
  518. $this->logMock,
  519. $this->utilMock,
  520. $this->lockingProviderMock
  521. ]
  522. )->setMethods(['getPublicMasterKey', 'getPrivateMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
  523. ->getMock();
  524. $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
  525. ->willReturn(true);
  526. $instance->expects($this->once())->method('getPublicMasterKey')
  527. ->willReturn('');
  528. $instance->expects($this->once())->method('getPrivateMasterKey')
  529. ->willReturn('');
  530. $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
  531. $this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
  532. $this->lockingProviderMock->expects($this->once())
  533. ->method('acquireLock')
  534. ->willThrowException(new LockedException('encryption-generateMasterKey'));
  535. $this->expectException(LockedException::class);
  536. $instance->validateMasterKey();
  537. }
  538. public function dataTestValidateMasterKey() {
  539. return [
  540. ['masterKey'],
  541. ['']
  542. ];
  543. }
  544. public function testGetVersionWithoutFileInfo() {
  545. $view = $this->getMockBuilder(View::class)
  546. ->disableOriginalConstructor()->getMock();
  547. $view->expects($this->once())
  548. ->method('getFileInfo')
  549. ->with('/admin/files/myfile.txt')
  550. ->willReturn(false);
  551. /** @var \OC\Files\View $view */
  552. $this->assertSame(0, $this->instance->getVersion('/admin/files/myfile.txt', $view));
  553. }
  554. public function testGetVersionWithFileInfo() {
  555. $view = $this->getMockBuilder(View::class)
  556. ->disableOriginalConstructor()->getMock();
  557. $fileInfo = $this->getMockBuilder(FileInfo::class)
  558. ->disableOriginalConstructor()->getMock();
  559. $fileInfo->expects($this->once())
  560. ->method('getEncryptedVersion')
  561. ->willReturn(1337);
  562. $view->expects($this->once())
  563. ->method('getFileInfo')
  564. ->with('/admin/files/myfile.txt')
  565. ->willReturn($fileInfo);
  566. /** @var \OC\Files\View $view */
  567. $this->assertSame(1337, $this->instance->getVersion('/admin/files/myfile.txt', $view));
  568. }
  569. public function testSetVersionWithFileInfo() {
  570. $view = $this->getMockBuilder(View::class)
  571. ->disableOriginalConstructor()->getMock();
  572. $cache = $this->getMockBuilder(ICache::class)
  573. ->disableOriginalConstructor()->getMock();
  574. $cache->expects($this->once())
  575. ->method('update')
  576. ->with(123, ['encrypted' => 5, 'encryptedVersion' => 5]);
  577. $storage = $this->getMockBuilder(Storage::class)
  578. ->disableOriginalConstructor()->getMock();
  579. $storage->expects($this->once())
  580. ->method('getCache')
  581. ->willReturn($cache);
  582. $fileInfo = $this->getMockBuilder(FileInfo::class)
  583. ->disableOriginalConstructor()->getMock();
  584. $fileInfo->expects($this->once())
  585. ->method('getStorage')
  586. ->willReturn($storage);
  587. $fileInfo->expects($this->once())
  588. ->method('getId')
  589. ->willReturn(123);
  590. $view->expects($this->once())
  591. ->method('getFileInfo')
  592. ->with('/admin/files/myfile.txt')
  593. ->willReturn($fileInfo);
  594. /** @var \OC\Files\View $view */
  595. $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
  596. }
  597. public function testSetVersionWithoutFileInfo() {
  598. $view = $this->getMockBuilder(View::class)
  599. ->disableOriginalConstructor()->getMock();
  600. $view->expects($this->once())
  601. ->method('getFileInfo')
  602. ->with('/admin/files/myfile.txt')
  603. ->willReturn(false);
  604. /** @var \OC\Files\View $view */
  605. $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
  606. }
  607. public function testBackupUserKeys() {
  608. $this->keyStorageMock->expects($this->once())->method('backupUserKeys')
  609. ->with('OC_DEFAULT_MODULE', 'test', 'user1');
  610. $this->instance->backupUserKeys('test', 'user1');
  611. }
  612. }