KeyManagerTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 <pvince81@owncloud.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->at(0))->method('setStatus')
  243. ->with(Session::INIT_EXECUTED);
  244. $instance->expects($this->any())->method('getMasterKeyId')->willReturn('masterKeyId');
  245. $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
  246. $instance->expects($this->any())->method('getSystemPrivateKey')->with('masterKeyId')->willReturn('privateMasterKey');
  247. $instance->expects($this->any())->method('getPrivateKey')->with($this->userId)->willReturn('privateUserKey');
  248. if ($useMasterKey) {
  249. $this->cryptMock->expects($this->once())->method('decryptPrivateKey')
  250. ->with('privateMasterKey', 'masterKeyPassword', 'masterKeyId')
  251. ->willReturn('key');
  252. } else {
  253. $this->cryptMock->expects($this->once())->method('decryptPrivateKey')
  254. ->with('privateUserKey', 'pass', $this->userId)
  255. ->willReturn('key');
  256. }
  257. $this->sessionMock->expects($this->once())->method('setPrivateKey')
  258. ->with('key');
  259. $this->assertTrue($instance->init($this->userId, 'pass'));
  260. }
  261. public function dataTestInit() {
  262. return [
  263. [true],
  264. [false]
  265. ];
  266. }
  267. public function testSetRecoveryKey() {
  268. $this->keyStorageMock->expects($this->exactly(2))
  269. ->method('setSystemUserKey')
  270. ->willReturn(true);
  271. $this->cryptMock->expects($this->any())
  272. ->method('encryptPrivateKey')
  273. ->with($this->equalTo('privateKey'), $this->equalTo('pass'))
  274. ->willReturn('decryptedPrivateKey');
  275. $this->assertTrue(
  276. $this->instance->setRecoveryKey('pass',
  277. ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])
  278. );
  279. }
  280. public function testSetSystemPrivateKey() {
  281. $this->keyStorageMock->expects($this->exactly(1))
  282. ->method('setSystemUserKey')
  283. ->with($this->equalTo('keyId.privateKey'), $this->equalTo('key'))
  284. ->willReturn(true);
  285. $this->assertTrue(
  286. $this->instance->setSystemPrivateKey('keyId', 'key')
  287. );
  288. }
  289. public function testGetSystemPrivateKey() {
  290. $this->keyStorageMock->expects($this->exactly(1))
  291. ->method('getSystemUserKey')
  292. ->with($this->equalTo('keyId.privateKey'))
  293. ->willReturn('systemPrivateKey');
  294. $this->assertSame('systemPrivateKey',
  295. $this->instance->getSystemPrivateKey('keyId')
  296. );
  297. }
  298. public function testGetEncryptedFileKey() {
  299. $this->keyStorageMock->expects($this->once())
  300. ->method('getFileKey')
  301. ->with('/', 'fileKey')
  302. ->willReturn(true);
  303. $this->assertTrue($this->instance->getEncryptedFileKey('/'));
  304. }
  305. public function dataTestGetFileKey() {
  306. return [
  307. ['user1', false, 'privateKey', true],
  308. ['user1', false, false, ''],
  309. ['user1', true, 'privateKey', true],
  310. ['user1', true, false, ''],
  311. [null, false, 'privateKey', true],
  312. [null, false, false, ''],
  313. [null, true, 'privateKey', true],
  314. [null, true, false, '']
  315. ];
  316. }
  317. /**
  318. * @dataProvider dataTestGetFileKey
  319. *
  320. * @param $uid
  321. * @param $isMasterKeyEnabled
  322. * @param $privateKey
  323. * @param $expected
  324. */
  325. public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $expected) {
  326. $path = '/foo.txt';
  327. if ($isMasterKeyEnabled) {
  328. $expectedUid = 'masterKeyId';
  329. $this->configMock->expects($this->any())->method('getSystemValue')->with('secret')
  330. ->willReturn('password');
  331. } elseif (!$uid) {
  332. $expectedUid = 'systemKeyId';
  333. } else {
  334. $expectedUid = $uid;
  335. }
  336. $this->invokePrivate($this->instance, 'masterKeyId', ['masterKeyId']);
  337. $this->keyStorageMock->expects($this->at(0))
  338. ->method('getFileKey')
  339. ->with($path, 'fileKey', 'OC_DEFAULT_MODULE')
  340. ->willReturn(true);
  341. $this->keyStorageMock->expects($this->at(1))
  342. ->method('getFileKey')
  343. ->with($path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE')
  344. ->willReturn(true);
  345. $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')
  346. ->willReturn($isMasterKeyEnabled);
  347. if (is_null($uid)) {
  348. $this->keyStorageMock->expects($this->once())
  349. ->method('getSystemUserKey')
  350. ->willReturn(true);
  351. $this->cryptMock->expects($this->once())
  352. ->method('decryptPrivateKey')
  353. ->willReturn($privateKey);
  354. } else {
  355. $this->keyStorageMock->expects($this->never())
  356. ->method('getSystemUserKey');
  357. $this->sessionMock->expects($this->once())->method('getPrivateKey')->willReturn($privateKey);
  358. }
  359. if ($privateKey) {
  360. $this->cryptMock->expects($this->once())
  361. ->method('multiKeyDecrypt')
  362. ->willReturn(true);
  363. } else {
  364. $this->cryptMock->expects($this->never())
  365. ->method('multiKeyDecrypt');
  366. }
  367. $this->assertSame($expected,
  368. $this->instance->getFileKey($path, $uid)
  369. );
  370. }
  371. public function testDeletePrivateKey() {
  372. $this->keyStorageMock->expects($this->once())
  373. ->method('deleteUserKey')
  374. ->with('user1', 'privateKey')
  375. ->willReturn(true);
  376. $this->assertTrue(self::invokePrivate($this->instance,
  377. 'deletePrivateKey',
  378. [$this->userId]));
  379. }
  380. public function testDeleteAllFileKeys() {
  381. $this->keyStorageMock->expects($this->once())
  382. ->method('deleteAllFileKeys')
  383. ->willReturn(true);
  384. $this->assertTrue($this->instance->deleteAllFileKeys('/'));
  385. }
  386. /**
  387. * test add public share key and or recovery key to the list of public keys
  388. *
  389. * @dataProvider dataTestAddSystemKeys
  390. *
  391. * @param array $accessList
  392. * @param array $publicKeys
  393. * @param string $uid
  394. * @param array $expectedKeys
  395. */
  396. public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys) {
  397. $publicShareKeyId = 'publicShareKey';
  398. $recoveryKeyId = 'recoveryKey';
  399. $this->keyStorageMock->expects($this->any())
  400. ->method('getSystemUserKey')
  401. ->willReturnCallback(function ($keyId, $encryptionModuleId) {
  402. return $keyId;
  403. });
  404. $this->utilMock->expects($this->any())
  405. ->method('isRecoveryEnabledForUser')
  406. ->willReturnCallback(function ($uid) {
  407. if ($uid === 'user1') {
  408. return true;
  409. }
  410. return false;
  411. });
  412. // set key IDs
  413. self::invokePrivate($this->instance, 'publicShareKeyId', [$publicShareKeyId]);
  414. self::invokePrivate($this->instance, 'recoveryKeyId', [$recoveryKeyId]);
  415. $result = $this->instance->addSystemKeys($accessList, $publicKeys, $uid);
  416. foreach ($expectedKeys as $expected) {
  417. $this->assertArrayHasKey($expected, $result);
  418. }
  419. $this->assertSameSize($expectedKeys, $result);
  420. }
  421. /**
  422. * data provider for testAddSystemKeys()
  423. *
  424. * @return array
  425. */
  426. public function dataTestAddSystemKeys() {
  427. return [
  428. [['public' => true],[], 'user1', ['publicShareKey', 'recoveryKey']],
  429. [['public' => false], [], 'user1', ['recoveryKey']],
  430. [['public' => true],[], 'user2', ['publicShareKey']],
  431. [['public' => false], [], 'user2', []],
  432. ];
  433. }
  434. public function testGetMasterKeyId() {
  435. $this->assertSame('systemKeyId', $this->instance->getMasterKeyId());
  436. }
  437. public function testGetPublicMasterKey() {
  438. $this->keyStorageMock->expects($this->once())->method('getSystemUserKey')
  439. ->with('systemKeyId.publicKey', \OCA\Encryption\Crypto\Encryption::ID)
  440. ->willReturn(true);
  441. $this->assertTrue(
  442. $this->instance->getPublicMasterKey()
  443. );
  444. }
  445. public function testGetMasterKeyPassword() {
  446. $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
  447. ->willReturn('password');
  448. $this->assertSame('password',
  449. $this->invokePrivate($this->instance, 'getMasterKeyPassword', [])
  450. );
  451. }
  452. public function testGetMasterKeyPasswordException() {
  453. $this->expectException(\Exception::class);
  454. $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
  455. ->willReturn('');
  456. $this->invokePrivate($this->instance, 'getMasterKeyPassword', []);
  457. }
  458. /**
  459. * @dataProvider dataTestValidateMasterKey
  460. *
  461. * @param $masterKey
  462. */
  463. public function testValidateMasterKey($masterKey) {
  464. /** @var \OCA\Encryption\KeyManager | \PHPUnit\Framework\MockObject\MockObject $instance */
  465. $instance = $this->getMockBuilder(KeyManager::class)
  466. ->setConstructorArgs(
  467. [
  468. $this->keyStorageMock,
  469. $this->cryptMock,
  470. $this->configMock,
  471. $this->userMock,
  472. $this->sessionMock,
  473. $this->logMock,
  474. $this->utilMock,
  475. $this->lockingProviderMock
  476. ]
  477. )->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
  478. ->getMock();
  479. $instance->expects($this->once())->method('getPublicMasterKey')
  480. ->willReturn($masterKey);
  481. $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
  482. $this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
  483. if (empty($masterKey)) {
  484. $this->cryptMock->expects($this->once())->method('createKeyPair')
  485. ->willReturn(['publicKey' => 'public', 'privateKey' => 'private']);
  486. $this->keyStorageMock->expects($this->once())->method('setSystemUserKey')
  487. ->with('systemKeyId.publicKey', 'public', \OCA\Encryption\Crypto\Encryption::ID);
  488. $this->cryptMock->expects($this->once())->method('encryptPrivateKey')
  489. ->with('private', 'masterKeyPassword', 'systemKeyId')
  490. ->willReturn('EncryptedKey');
  491. $this->lockingProviderMock->expects($this->once())
  492. ->method('acquireLock');
  493. $instance->expects($this->once())->method('setSystemPrivateKey')
  494. ->with('systemKeyId', 'headerEncryptedKey');
  495. } else {
  496. $this->cryptMock->expects($this->never())->method('createKeyPair');
  497. $this->keyStorageMock->expects($this->never())->method('setSystemUserKey');
  498. $this->cryptMock->expects($this->never())->method('encryptPrivateKey');
  499. $instance->expects($this->never())->method('setSystemPrivateKey');
  500. }
  501. $instance->validateMasterKey();
  502. }
  503. public function testValidateMasterKeyLocked() {
  504. /** @var \OCA\Encryption\KeyManager | \PHPUnit_Framework_MockObject_MockObject $instance */
  505. $instance = $this->getMockBuilder(KeyManager::class)
  506. ->setConstructorArgs(
  507. [
  508. $this->keyStorageMock,
  509. $this->cryptMock,
  510. $this->configMock,
  511. $this->userMock,
  512. $this->sessionMock,
  513. $this->logMock,
  514. $this->utilMock,
  515. $this->lockingProviderMock
  516. ]
  517. )->setMethods(['getPublicMasterKey', 'getPrivateMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
  518. ->getMock();
  519. $instance->expects($this->once())->method('getPublicMasterKey')
  520. ->willReturn('');
  521. $instance->expects($this->once())->method('getPrivateMasterKey')
  522. ->willReturn('');
  523. $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
  524. $this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
  525. $this->lockingProviderMock->expects($this->once())
  526. ->method('acquireLock')
  527. ->willThrowException(new LockedException('encryption-generateMasterKey'));
  528. $this->expectException(LockedException::class);
  529. $instance->validateMasterKey();
  530. }
  531. public function dataTestValidateMasterKey() {
  532. return [
  533. ['masterKey'],
  534. ['']
  535. ];
  536. }
  537. public function testGetVersionWithoutFileInfo() {
  538. $view = $this->getMockBuilder(View::class)
  539. ->disableOriginalConstructor()->getMock();
  540. $view->expects($this->once())
  541. ->method('getFileInfo')
  542. ->with('/admin/files/myfile.txt')
  543. ->willReturn(false);
  544. /** @var \OC\Files\View $view */
  545. $this->assertSame(0, $this->instance->getVersion('/admin/files/myfile.txt', $view));
  546. }
  547. public function testGetVersionWithFileInfo() {
  548. $view = $this->getMockBuilder(View::class)
  549. ->disableOriginalConstructor()->getMock();
  550. $fileInfo = $this->getMockBuilder(FileInfo::class)
  551. ->disableOriginalConstructor()->getMock();
  552. $fileInfo->expects($this->once())
  553. ->method('getEncryptedVersion')
  554. ->willReturn(1337);
  555. $view->expects($this->once())
  556. ->method('getFileInfo')
  557. ->with('/admin/files/myfile.txt')
  558. ->willReturn($fileInfo);
  559. /** @var \OC\Files\View $view */
  560. $this->assertSame(1337, $this->instance->getVersion('/admin/files/myfile.txt', $view));
  561. }
  562. public function testSetVersionWithFileInfo() {
  563. $view = $this->getMockBuilder(View::class)
  564. ->disableOriginalConstructor()->getMock();
  565. $cache = $this->getMockBuilder(ICache::class)
  566. ->disableOriginalConstructor()->getMock();
  567. $cache->expects($this->once())
  568. ->method('update')
  569. ->with(123, ['encrypted' => 5, 'encryptedVersion' => 5]);
  570. $storage = $this->getMockBuilder(Storage::class)
  571. ->disableOriginalConstructor()->getMock();
  572. $storage->expects($this->once())
  573. ->method('getCache')
  574. ->willReturn($cache);
  575. $fileInfo = $this->getMockBuilder(FileInfo::class)
  576. ->disableOriginalConstructor()->getMock();
  577. $fileInfo->expects($this->once())
  578. ->method('getStorage')
  579. ->willReturn($storage);
  580. $fileInfo->expects($this->once())
  581. ->method('getId')
  582. ->willReturn(123);
  583. $view->expects($this->once())
  584. ->method('getFileInfo')
  585. ->with('/admin/files/myfile.txt')
  586. ->willReturn($fileInfo);
  587. /** @var \OC\Files\View $view */
  588. $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
  589. }
  590. public function testSetVersionWithoutFileInfo() {
  591. $view = $this->getMockBuilder(View::class)
  592. ->disableOriginalConstructor()->getMock();
  593. $view->expects($this->once())
  594. ->method('getFileInfo')
  595. ->with('/admin/files/myfile.txt')
  596. ->willReturn(false);
  597. /** @var \OC\Files\View $view */
  598. $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
  599. }
  600. public function testBackupUserKeys() {
  601. $this->keyStorageMock->expects($this->once())->method('backupUserKeys')
  602. ->with('OC_DEFAULT_MODULE', 'test', 'user1');
  603. $this->instance->backupUserKeys('test', 'user1');
  604. }
  605. }