KeyManagerTest.php 19 KB

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