1
0

KeyManagerTest.php 22 KB

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