EncryptionTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Encryption\Tests\Crypto;
  24. use OCA\Encryption\Exceptions\PublicKeyMissingException;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Output\OutputInterface;
  27. use Test\TestCase;
  28. use OCA\Encryption\Crypto\Encryption;
  29. class EncryptionTest extends TestCase {
  30. /** @var Encryption */
  31. private $instance;
  32. /** @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject */
  33. private $keyManagerMock;
  34. /** @var \OCA\Encryption\Crypto\EncryptAll|\PHPUnit_Framework_MockObject_MockObject */
  35. private $encryptAllMock;
  36. /** @var \OCA\Encryption\Crypto\DecryptAll|\PHPUnit_Framework_MockObject_MockObject */
  37. private $decryptAllMock;
  38. /** @var \OCA\Encryption\Session|\PHPUnit_Framework_MockObject_MockObject */
  39. private $sessionMock;
  40. /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit_Framework_MockObject_MockObject */
  41. private $cryptMock;
  42. /** @var \OCA\Encryption\Util|\PHPUnit_Framework_MockObject_MockObject */
  43. private $utilMock;
  44. /** @var \OCP\ILogger|\PHPUnit_Framework_MockObject_MockObject */
  45. private $loggerMock;
  46. /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
  47. private $l10nMock;
  48. /** @var \OCP\Files\Storage|\PHPUnit_Framework_MockObject_MockObject */
  49. private $storageMock;
  50. public function setUp() {
  51. parent::setUp();
  52. $this->storageMock = $this->getMockBuilder('OCP\Files\Storage')
  53. ->disableOriginalConstructor()->getMock();
  54. $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->utilMock = $this->getMockBuilder('OCA\Encryption\Util')
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->sessionMock = $this->getMockBuilder('OCA\Encryption\Session')
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->encryptAllMock = $this->getMockBuilder('OCA\Encryption\Crypto\EncryptAll')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->decryptAllMock = $this->getMockBuilder('OCA\Encryption\Crypto\DecryptAll')
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->loggerMock = $this->getMockBuilder('OCP\ILogger')
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->l10nMock = $this->getMockBuilder('OCP\IL10N')
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->l10nMock->expects($this->any())
  79. ->method('t')
  80. ->with($this->anything())
  81. ->willReturnArgument(0);
  82. $this->instance = new Encryption(
  83. $this->cryptMock,
  84. $this->keyManagerMock,
  85. $this->utilMock,
  86. $this->sessionMock,
  87. $this->encryptAllMock,
  88. $this->decryptAllMock,
  89. $this->loggerMock,
  90. $this->l10nMock
  91. );
  92. }
  93. /**
  94. * test if public key from one of the recipients is missing
  95. */
  96. public function testEndUser1() {
  97. $this->instance->begin('/foo/bar', 'user1', 'r', array(), array('users' => array('user1', 'user2', 'user3')));
  98. $this->endTest();
  99. }
  100. /**
  101. * test if public key from owner is missing
  102. *
  103. * @expectedException \OCA\Encryption\Exceptions\PublicKeyMissingException
  104. */
  105. public function testEndUser2() {
  106. $this->instance->begin('/foo/bar', 'user2', 'r', array(), array('users' => array('user1', 'user2', 'user3')));
  107. $this->endTest();
  108. }
  109. /**
  110. * common part of testEndUser1 and testEndUser2
  111. *
  112. * @throws PublicKeyMissingException
  113. */
  114. public function endTest() {
  115. // prepare internal variables
  116. self::invokePrivate($this->instance, 'isWriteOperation', [true]);
  117. self::invokePrivate($this->instance, 'writeCache', ['']);
  118. $this->keyManagerMock->expects($this->any())
  119. ->method('getPublicKey')
  120. ->will($this->returnCallback([$this, 'getPublicKeyCallback']));
  121. $this->keyManagerMock->expects($this->any())
  122. ->method('addSystemKeys')
  123. ->will($this->returnCallback([$this, 'addSystemKeysCallback']));
  124. $this->cryptMock->expects($this->any())
  125. ->method('multiKeyEncrypt')
  126. ->willReturn(true);
  127. $this->instance->end('/foo/bar');
  128. }
  129. public function getPublicKeyCallback($uid) {
  130. if ($uid === 'user2') {
  131. throw new PublicKeyMissingException($uid);
  132. }
  133. return $uid;
  134. }
  135. public function addSystemKeysCallback($accessList, $publicKeys) {
  136. $this->assertSame(2, count($publicKeys));
  137. $this->assertArrayHasKey('user1', $publicKeys);
  138. $this->assertArrayHasKey('user3', $publicKeys);
  139. return $publicKeys;
  140. }
  141. /**
  142. * @dataProvider dataProviderForTestGetPathToRealFile
  143. */
  144. public function testGetPathToRealFile($path, $expected) {
  145. $this->assertSame($expected,
  146. self::invokePrivate($this->instance, 'getPathToRealFile', array($path))
  147. );
  148. }
  149. public function dataProviderForTestGetPathToRealFile() {
  150. return array(
  151. array('/user/files/foo/bar.txt', '/user/files/foo/bar.txt'),
  152. array('/user/files/foo.txt', '/user/files/foo.txt'),
  153. array('/user/files_versions/foo.txt.v543534', '/user/files/foo.txt'),
  154. array('/user/files_versions/foo/bar.txt.v5454', '/user/files/foo/bar.txt'),
  155. );
  156. }
  157. /**
  158. * @dataProvider dataTestBegin
  159. */
  160. public function testBegin($mode, $header, $legacyCipher, $defaultCipher, $fileKey, $expected) {
  161. $this->sessionMock->expects($this->once())
  162. ->method('decryptAllModeActivated')
  163. ->willReturn(false);
  164. $this->sessionMock->expects($this->never())->method('getDecryptAllUid');
  165. $this->sessionMock->expects($this->never())->method('getDecryptAllKey');
  166. $this->keyManagerMock->expects($this->never())->method('getEncryptedFileKey');
  167. $this->keyManagerMock->expects($this->never())->method('getShareKey');
  168. $this->cryptMock->expects($this->never())->method('multiKeyDecrypt');
  169. $this->cryptMock->expects($this->any())
  170. ->method('getCipher')
  171. ->willReturn($defaultCipher);
  172. $this->cryptMock->expects($this->any())
  173. ->method('getLegacyCipher')
  174. ->willReturn($legacyCipher);
  175. if (empty($fileKey)) {
  176. $this->cryptMock->expects($this->once())
  177. ->method('generateFileKey')
  178. ->willReturn('fileKey');
  179. } else {
  180. $this->cryptMock->expects($this->never())
  181. ->method('generateFileKey');
  182. }
  183. $this->keyManagerMock->expects($this->once())
  184. ->method('getFileKey')
  185. ->willReturn($fileKey);
  186. $result = $this->instance->begin('/user/files/foo.txt', 'user', $mode, $header, []);
  187. $this->assertArrayHasKey('cipher', $result);
  188. $this->assertSame($expected, $result['cipher']);
  189. if ($mode === 'w') {
  190. $this->assertTrue(self::invokePrivate($this->instance, 'isWriteOperation'));
  191. } else {
  192. $this->assertFalse(self::invokePrivate($this->instance, 'isWriteOperation'));
  193. }
  194. }
  195. public function dataTestBegin() {
  196. return array(
  197. array('w', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'defaultCipher'),
  198. array('r', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'myCipher'),
  199. array('w', [], 'legacyCipher', 'defaultCipher', '', 'defaultCipher'),
  200. array('r', [], 'legacyCipher', 'defaultCipher', 'file_key', 'legacyCipher'),
  201. );
  202. }
  203. /**
  204. * test begin() if decryptAll mode was activated
  205. */
  206. public function testBeginDecryptAll() {
  207. $path = '/user/files/foo.txt';
  208. $recoveryKeyId = 'recoveryKeyId';
  209. $recoveryShareKey = 'recoveryShareKey';
  210. $decryptAllKey = 'decryptAllKey';
  211. $fileKey = 'fileKey';
  212. $this->sessionMock->expects($this->once())
  213. ->method('decryptAllModeActivated')
  214. ->willReturn(true);
  215. $this->sessionMock->expects($this->once())
  216. ->method('getDecryptAllUid')
  217. ->willReturn($recoveryKeyId);
  218. $this->sessionMock->expects($this->once())
  219. ->method('getDecryptAllKey')
  220. ->willReturn($decryptAllKey);
  221. $this->keyManagerMock->expects($this->once())
  222. ->method('getEncryptedFileKey')
  223. ->willReturn('encryptedFileKey');
  224. $this->keyManagerMock->expects($this->once())
  225. ->method('getShareKey')
  226. ->with($path, $recoveryKeyId)
  227. ->willReturn($recoveryShareKey);
  228. $this->cryptMock->expects($this->once())
  229. ->method('multiKeyDecrypt')
  230. ->with('encryptedFileKey', $recoveryShareKey, $decryptAllKey)
  231. ->willReturn($fileKey);
  232. $this->keyManagerMock->expects($this->never())->method('getFileKey');
  233. $this->instance->begin($path, 'user', 'r', [], []);
  234. $this->assertSame($fileKey,
  235. $this->invokePrivate($this->instance, 'fileKey')
  236. );
  237. }
  238. /**
  239. * test begin() if encryption is not initialized but the master key is enabled
  240. * in this case we can initialize the encryption without a username/password
  241. * and continue
  242. */
  243. public function testBeginInitMasterKey() {
  244. $this->sessionMock->expects($this->once())->method('isReady')->willReturn(false);
  245. $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
  246. ->willReturn(true);
  247. $this->keyManagerMock->expects($this->once())->method('init')->with('', '');
  248. $this->instance->begin('/user/files/welcome.txt', 'user', 'r', [], []);
  249. }
  250. /**
  251. * @dataProvider dataTestUpdate
  252. *
  253. * @param string $fileKey
  254. * @param boolean $expected
  255. */
  256. public function testUpdate($fileKey, $expected) {
  257. $this->keyManagerMock->expects($this->once())
  258. ->method('getFileKey')->willReturn($fileKey);
  259. $this->keyManagerMock->expects($this->any())
  260. ->method('getPublicKey')->willReturn('publicKey');
  261. $this->keyManagerMock->expects($this->any())
  262. ->method('addSystemKeys')
  263. ->willReturnCallback(function($accessList, $publicKeys) {
  264. return $publicKeys;
  265. });
  266. $this->keyManagerMock->expects($this->never())->method('getVersion');
  267. $this->keyManagerMock->expects($this->never())->method('setVersion');
  268. $this->assertSame($expected,
  269. $this->instance->update('path', 'user1', ['users' => ['user1']])
  270. );
  271. }
  272. public function dataTestUpdate() {
  273. return array(
  274. array('', false),
  275. array('fileKey', true)
  276. );
  277. }
  278. public function testUpdateNoUsers() {
  279. $this->invokePrivate($this->instance, 'rememberVersion', [['path' => 2]]);
  280. $this->keyManagerMock->expects($this->never())->method('getFileKey');
  281. $this->keyManagerMock->expects($this->never())->method('getPublicKey');
  282. $this->keyManagerMock->expects($this->never())->method('addSystemKeys');
  283. $this->keyManagerMock->expects($this->once())->method('setVersion')
  284. ->willReturnCallback(function($path, $version, $view) {
  285. $this->assertSame('path', $path);
  286. $this->assertSame(2, $version);
  287. $this->assertTrue($view instanceof \OC\Files\View);
  288. });
  289. $this->instance->update('path', 'user1', []);
  290. }
  291. /**
  292. * Test case if the public key is missing. ownCloud should still encrypt
  293. * the file for the remaining users
  294. */
  295. public function testUpdateMissingPublicKey() {
  296. $this->keyManagerMock->expects($this->once())
  297. ->method('getFileKey')->willReturn('fileKey');
  298. $this->keyManagerMock->expects($this->any())
  299. ->method('getPublicKey')->willReturnCallback(
  300. function($user) {
  301. throw new PublicKeyMissingException($user);
  302. }
  303. );
  304. $this->keyManagerMock->expects($this->any())
  305. ->method('addSystemKeys')
  306. ->willReturnCallback(function($accessList, $publicKeys) {
  307. return $publicKeys;
  308. });
  309. $this->cryptMock->expects($this->once())->method('multiKeyEncrypt')
  310. ->willReturnCallback(
  311. function($fileKey, $publicKeys) {
  312. $this->assertEmpty($publicKeys);
  313. $this->assertSame('fileKey', $fileKey);
  314. }
  315. );
  316. $this->keyManagerMock->expects($this->never())->method('getVersion');
  317. $this->keyManagerMock->expects($this->never())->method('setVersion');
  318. $this->assertTrue(
  319. $this->instance->update('path', 'user1', ['users' => ['user1']])
  320. );
  321. }
  322. /**
  323. * by default the encryption module should encrypt regular files, files in
  324. * files_versions and files in files_trashbin
  325. *
  326. * @dataProvider dataTestShouldEncrypt
  327. */
  328. public function testShouldEncrypt($path, $shouldEncryptHomeStorage, $isHomeStorage, $expected) {
  329. $this->utilMock->expects($this->once())->method('shouldEncryptHomeStorage')
  330. ->willReturn($shouldEncryptHomeStorage);
  331. if ($shouldEncryptHomeStorage === false) {
  332. $this->storageMock->expects($this->once())->method('instanceOfStorage')
  333. ->with('\OCP\Files\IHomeStorage')->willReturn($isHomeStorage);
  334. $this->utilMock->expects($this->once())->method('getStorage')->with($path)
  335. ->willReturn($this->storageMock);
  336. }
  337. $this->assertSame($expected,
  338. $this->instance->shouldEncrypt($path)
  339. );
  340. }
  341. public function dataTestShouldEncrypt() {
  342. return array(
  343. array('/user1/files/foo.txt', true, true, true),
  344. array('/user1/files_versions/foo.txt', true, true, true),
  345. array('/user1/files_trashbin/foo.txt', true, true, true),
  346. array('/user1/some_folder/foo.txt', true, true, false),
  347. array('/user1/foo.txt', true, true, false),
  348. array('/user1/files', true, true, false),
  349. array('/user1/files_trashbin', true, true, false),
  350. array('/user1/files_versions', true, true, false),
  351. // test if shouldEncryptHomeStorage is set to false
  352. array('/user1/files/foo.txt', false, true, false),
  353. array('/user1/files_versions/foo.txt', false, false, true),
  354. );
  355. }
  356. /**
  357. * @expectedException \OC\Encryption\Exceptions\DecryptionFailedException
  358. * @expectedExceptionMessage Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.
  359. */
  360. public function testDecrypt() {
  361. $this->instance->decrypt('abc');
  362. }
  363. public function testPrepareDecryptAll() {
  364. /** @var \Symfony\Component\Console\Input\InputInterface $input */
  365. $input = $this->createMock(InputInterface::class);
  366. /** @var \Symfony\Component\Console\Output\OutputInterface $output */
  367. $output = $this->createMock(OutputInterface::class);
  368. $this->decryptAllMock->expects($this->once())->method('prepare')
  369. ->with($input, $output, 'user');
  370. $this->instance->prepareDecryptAll($input, $output, 'user');
  371. }
  372. }