KeyManagerTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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\ILogger;
  44. use OCP\IUserSession;
  45. use OCP\Lock\ILockingProvider;
  46. use OCP\Lock\LockedException;
  47. use PHPUnit\Framework\MockObject\MockObject;
  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 \OCP\ILogger|\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(ILogger::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', true],
  311. ['user1', false, false, ''],
  312. ['user1', true, 'privateKey', true],
  313. ['user1', true, false, ''],
  314. [null, false, 'privateKey', true],
  315. [null, false, false, ''],
  316. [null, true, 'privateKey', true],
  317. [null, true, false, '']
  318. ];
  319. }
  320. /**
  321. * @dataProvider dataTestGetFileKey
  322. *
  323. * @param $uid
  324. * @param $isMasterKeyEnabled
  325. * @param $privateKey
  326. * @param $expected
  327. */
  328. public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $expected) {
  329. $path = '/foo.txt';
  330. if ($isMasterKeyEnabled) {
  331. $expectedUid = 'masterKeyId';
  332. $this->configMock->expects($this->any())->method('getSystemValue')->with('secret')
  333. ->willReturn('password');
  334. } elseif (!$uid) {
  335. $expectedUid = 'systemKeyId';
  336. } else {
  337. $expectedUid = $uid;
  338. }
  339. $this->invokePrivate($this->instance, 'masterKeyId', ['masterKeyId']);
  340. $this->keyStorageMock->expects($this->exactly(2))
  341. ->method('getFileKey')
  342. ->withConsecutive(
  343. [$path, 'fileKey', 'OC_DEFAULT_MODULE'],
  344. [$path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE'],
  345. )
  346. ->willReturnOnConsecutiveCalls(
  347. true,
  348. true,
  349. );
  350. $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')
  351. ->willReturn($isMasterKeyEnabled);
  352. if (is_null($uid)) {
  353. $this->keyStorageMock->expects($this->once())
  354. ->method('getSystemUserKey')
  355. ->willReturn(true);
  356. $this->cryptMock->expects($this->once())
  357. ->method('decryptPrivateKey')
  358. ->willReturn($privateKey);
  359. } else {
  360. $this->keyStorageMock->expects($this->never())
  361. ->method('getSystemUserKey');
  362. $this->sessionMock->expects($this->once())->method('getPrivateKey')->willReturn($privateKey);
  363. }
  364. if ($privateKey) {
  365. $this->cryptMock->expects($this->once())
  366. ->method('multiKeyDecrypt')
  367. ->willReturn(true);
  368. } else {
  369. $this->cryptMock->expects($this->never())
  370. ->method('multiKeyDecrypt');
  371. }
  372. $this->assertSame($expected,
  373. $this->instance->getFileKey($path, $uid)
  374. );
  375. }
  376. public function testDeletePrivateKey() {
  377. $this->keyStorageMock->expects($this->once())
  378. ->method('deleteUserKey')
  379. ->with('user1', 'privateKey')
  380. ->willReturn(true);
  381. $this->assertTrue(self::invokePrivate($this->instance,
  382. 'deletePrivateKey',
  383. [$this->userId]));
  384. }
  385. public function testDeleteAllFileKeys() {
  386. $this->keyStorageMock->expects($this->once())
  387. ->method('deleteAllFileKeys')
  388. ->willReturn(true);
  389. $this->assertTrue($this->instance->deleteAllFileKeys('/'));
  390. }
  391. /**
  392. * test add public share key and or recovery key to the list of public keys
  393. *
  394. * @dataProvider dataTestAddSystemKeys
  395. *
  396. * @param array $accessList
  397. * @param array $publicKeys
  398. * @param string $uid
  399. * @param array $expectedKeys
  400. */
  401. public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys) {
  402. $publicShareKeyId = 'publicShareKey';
  403. $recoveryKeyId = 'recoveryKey';
  404. $this->keyStorageMock->expects($this->any())
  405. ->method('getSystemUserKey')
  406. ->willReturnCallback(function ($keyId, $encryptionModuleId) {
  407. return $keyId;
  408. });
  409. $this->utilMock->expects($this->any())
  410. ->method('isRecoveryEnabledForUser')
  411. ->willReturnCallback(function ($uid) {
  412. if ($uid === 'user1') {
  413. return true;
  414. }
  415. return false;
  416. });
  417. // set key IDs
  418. self::invokePrivate($this->instance, 'publicShareKeyId', [$publicShareKeyId]);
  419. self::invokePrivate($this->instance, 'recoveryKeyId', [$recoveryKeyId]);
  420. $result = $this->instance->addSystemKeys($accessList, $publicKeys, $uid);
  421. foreach ($expectedKeys as $expected) {
  422. $this->assertArrayHasKey($expected, $result);
  423. }
  424. $this->assertSameSize($expectedKeys, $result);
  425. }
  426. /**
  427. * data provider for testAddSystemKeys()
  428. *
  429. * @return array
  430. */
  431. public function dataTestAddSystemKeys() {
  432. return [
  433. [['public' => true],[], 'user1', ['publicShareKey', 'recoveryKey']],
  434. [['public' => false], [], 'user1', ['recoveryKey']],
  435. [['public' => true],[], 'user2', ['publicShareKey']],
  436. [['public' => false], [], 'user2', []],
  437. ];
  438. }
  439. public function testGetMasterKeyId() {
  440. $this->assertSame('systemKeyId', $this->instance->getMasterKeyId());
  441. }
  442. public function testGetPublicMasterKey() {
  443. $this->keyStorageMock->expects($this->once())->method('getSystemUserKey')
  444. ->with('systemKeyId.publicKey', \OCA\Encryption\Crypto\Encryption::ID)
  445. ->willReturn(true);
  446. $this->assertTrue(
  447. $this->instance->getPublicMasterKey()
  448. );
  449. }
  450. public function testGetMasterKeyPassword() {
  451. $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
  452. ->willReturn('password');
  453. $this->assertSame('password',
  454. $this->invokePrivate($this->instance, 'getMasterKeyPassword', [])
  455. );
  456. }
  457. public function testGetMasterKeyPasswordException() {
  458. $this->expectException(\Exception::class);
  459. $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
  460. ->willReturn('');
  461. $this->invokePrivate($this->instance, 'getMasterKeyPassword', []);
  462. }
  463. /**
  464. * @dataProvider dataTestValidateMasterKey
  465. *
  466. * @param $masterKey
  467. */
  468. public function testValidateMasterKey($masterKey) {
  469. /** @var \OCA\Encryption\KeyManager | \PHPUnit\Framework\MockObject\MockObject $instance */
  470. $instance = $this->getMockBuilder(KeyManager::class)
  471. ->setConstructorArgs(
  472. [
  473. $this->keyStorageMock,
  474. $this->cryptMock,
  475. $this->configMock,
  476. $this->userMock,
  477. $this->sessionMock,
  478. $this->logMock,
  479. $this->utilMock,
  480. $this->lockingProviderMock
  481. ]
  482. )->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
  483. ->getMock();
  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. $instance->expects($this->once())->method('getPublicMasterKey')
  525. ->willReturn('');
  526. $instance->expects($this->once())->method('getPrivateMasterKey')
  527. ->willReturn('');
  528. $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
  529. $this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
  530. $this->lockingProviderMock->expects($this->once())
  531. ->method('acquireLock')
  532. ->willThrowException(new LockedException('encryption-generateMasterKey'));
  533. $this->expectException(LockedException::class);
  534. $instance->validateMasterKey();
  535. }
  536. public function dataTestValidateMasterKey() {
  537. return [
  538. ['masterKey'],
  539. ['']
  540. ];
  541. }
  542. public function testGetVersionWithoutFileInfo() {
  543. $view = $this->getMockBuilder(View::class)
  544. ->disableOriginalConstructor()->getMock();
  545. $view->expects($this->once())
  546. ->method('getFileInfo')
  547. ->with('/admin/files/myfile.txt')
  548. ->willReturn(false);
  549. /** @var \OC\Files\View $view */
  550. $this->assertSame(0, $this->instance->getVersion('/admin/files/myfile.txt', $view));
  551. }
  552. public function testGetVersionWithFileInfo() {
  553. $view = $this->getMockBuilder(View::class)
  554. ->disableOriginalConstructor()->getMock();
  555. $fileInfo = $this->getMockBuilder(FileInfo::class)
  556. ->disableOriginalConstructor()->getMock();
  557. $fileInfo->expects($this->once())
  558. ->method('getEncryptedVersion')
  559. ->willReturn(1337);
  560. $view->expects($this->once())
  561. ->method('getFileInfo')
  562. ->with('/admin/files/myfile.txt')
  563. ->willReturn($fileInfo);
  564. /** @var \OC\Files\View $view */
  565. $this->assertSame(1337, $this->instance->getVersion('/admin/files/myfile.txt', $view));
  566. }
  567. public function testSetVersionWithFileInfo() {
  568. $view = $this->getMockBuilder(View::class)
  569. ->disableOriginalConstructor()->getMock();
  570. $cache = $this->getMockBuilder(ICache::class)
  571. ->disableOriginalConstructor()->getMock();
  572. $cache->expects($this->once())
  573. ->method('update')
  574. ->with(123, ['encrypted' => 5, 'encryptedVersion' => 5]);
  575. $storage = $this->getMockBuilder(Storage::class)
  576. ->disableOriginalConstructor()->getMock();
  577. $storage->expects($this->once())
  578. ->method('getCache')
  579. ->willReturn($cache);
  580. $fileInfo = $this->getMockBuilder(FileInfo::class)
  581. ->disableOriginalConstructor()->getMock();
  582. $fileInfo->expects($this->once())
  583. ->method('getStorage')
  584. ->willReturn($storage);
  585. $fileInfo->expects($this->once())
  586. ->method('getId')
  587. ->willReturn(123);
  588. $view->expects($this->once())
  589. ->method('getFileInfo')
  590. ->with('/admin/files/myfile.txt')
  591. ->willReturn($fileInfo);
  592. /** @var \OC\Files\View $view */
  593. $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
  594. }
  595. public function testSetVersionWithoutFileInfo() {
  596. $view = $this->getMockBuilder(View::class)
  597. ->disableOriginalConstructor()->getMock();
  598. $view->expects($this->once())
  599. ->method('getFileInfo')
  600. ->with('/admin/files/myfile.txt')
  601. ->willReturn(false);
  602. /** @var \OC\Files\View $view */
  603. $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
  604. }
  605. public function testBackupUserKeys() {
  606. $this->keyStorageMock->expects($this->once())->method('backupUserKeys')
  607. ->with('OC_DEFAULT_MODULE', 'test', 'user1');
  608. $this->instance->backupUserKeys('test', 'user1');
  609. }
  610. }