StorageTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @copyright (C) 2015 ownCloud, Inc.
  6. *
  7. * @author Bjoern Schiessle <schiessle@owncloud.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library 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
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. namespace Test\Encryption\Keys;
  23. use OC\Encryption\Keys\Storage;
  24. use OC\Files\View;
  25. use OCP\IConfig;
  26. use Test\TestCase;
  27. class StorageTest extends TestCase {
  28. /** @var Storage */
  29. protected $storage;
  30. /** @var \PHPUnit_Framework_MockObject_MockObject */
  31. protected $util;
  32. /** @var \PHPUnit_Framework_MockObject_MockObject */
  33. protected $view;
  34. /** @var \PHPUnit_Framework_MockObject_MockObject */
  35. protected $config;
  36. public function setUp() {
  37. parent::setUp();
  38. $this->util = $this->getMockBuilder('OC\Encryption\Util')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->view = $this->getMockBuilder(View::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->config = $this->getMockBuilder(IConfig::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->storage = new Storage($this->view, $this->util);
  48. }
  49. public function testSetFileKey() {
  50. $this->util->expects($this->any())
  51. ->method('getUidAndFilename')
  52. ->willReturn(array('user1', '/files/foo.txt'));
  53. $this->util->expects($this->any())
  54. ->method('stripPartialFileExtension')
  55. ->willReturnArgument(0);
  56. $this->util->expects($this->any())
  57. ->method('isSystemWideMountPoint')
  58. ->willReturn(false);
  59. $this->view->expects($this->once())
  60. ->method('file_put_contents')
  61. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'),
  62. $this->equalTo('key'))
  63. ->willReturn(strlen('key'));
  64. $this->assertTrue(
  65. $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key', 'encModule')
  66. );
  67. }
  68. public function dataTestGetFileKey() {
  69. return [
  70. ['/files/foo.txt', '/files/foo.txt', true, 'key'],
  71. ['/files/foo.txt.ocTransferId2111130212.part', '/files/foo.txt', true, 'key'],
  72. ['/files/foo.txt.ocTransferId2111130212.part', '/files/foo.txt', false, 'key2'],
  73. ];
  74. }
  75. /**
  76. * @dataProvider dataTestGetFileKey
  77. *
  78. * @param string $path
  79. * @param string $strippedPartialName
  80. * @param bool $originalKeyExists
  81. * @param string $expectedKeyContent
  82. */
  83. public function testGetFileKey($path, $strippedPartialName, $originalKeyExists, $expectedKeyContent) {
  84. $this->util->expects($this->any())
  85. ->method('getUidAndFilename')
  86. ->willReturnMap([
  87. ['user1/files/foo.txt', ['user1', '/files/foo.txt']],
  88. ['user1/files/foo.txt.ocTransferId2111130212.part', ['user1', '/files/foo.txt.ocTransferId2111130212.part']],
  89. ]);
  90. // we need to strip away the part file extension in order to reuse a
  91. // existing key if it exists, otherwise versions will break
  92. $this->util->expects($this->once())
  93. ->method('stripPartialFileExtension')
  94. ->willReturn('user1' . $strippedPartialName);
  95. $this->util->expects($this->any())
  96. ->method('isSystemWideMountPoint')
  97. ->willReturn(false);
  98. $this->view->expects($this->at(0))
  99. ->method('file_exists')
  100. ->with($this->equalTo('/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey'))
  101. ->willReturn($originalKeyExists);
  102. if (!$originalKeyExists) {
  103. $this->view->expects($this->at(1))
  104. ->method('file_exists')
  105. ->with($this->equalTo('/user1/files_encryption/keys' . $path . '/encModule/fileKey'))
  106. ->willReturn(true);
  107. $this->view->expects($this->once())
  108. ->method('file_get_contents')
  109. ->with($this->equalTo('/user1/files_encryption/keys' . $path . '/encModule/fileKey'))
  110. ->willReturn('key2');
  111. } else {
  112. $this->view->expects($this->once())
  113. ->method('file_get_contents')
  114. ->with($this->equalTo('/user1/files_encryption/keys' . $strippedPartialName . '/encModule/fileKey'))
  115. ->willReturn('key');
  116. }
  117. $this->assertSame($expectedKeyContent,
  118. $this->storage->getFileKey('user1' . $path, 'fileKey', 'encModule')
  119. );
  120. }
  121. public function testSetFileKeySystemWide() {
  122. $this->util->expects($this->any())
  123. ->method('getUidAndFilename')
  124. ->willReturn(array('user1', '/files/foo.txt'));
  125. $this->util->expects($this->any())
  126. ->method('isSystemWideMountPoint')
  127. ->willReturn(true);
  128. $this->util->expects($this->any())
  129. ->method('stripPartialFileExtension')
  130. ->willReturnArgument(0);
  131. $this->view->expects($this->once())
  132. ->method('file_put_contents')
  133. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'),
  134. $this->equalTo('key'))
  135. ->willReturn(strlen('key'));
  136. $this->assertTrue(
  137. $this->storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key', 'encModule')
  138. );
  139. }
  140. public function testGetFileKeySystemWide() {
  141. $this->util->expects($this->any())
  142. ->method('getUidAndFilename')
  143. ->willReturn(array('user1', '/files/foo.txt'));
  144. $this->util->expects($this->any())
  145. ->method('stripPartialFileExtension')
  146. ->willReturnArgument(0);
  147. $this->util->expects($this->any())
  148. ->method('isSystemWideMountPoint')
  149. ->willReturn(true);
  150. $this->view->expects($this->once())
  151. ->method('file_get_contents')
  152. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  153. ->willReturn('key');
  154. $this->view->expects($this->once())
  155. ->method('file_exists')
  156. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  157. ->willReturn(true);
  158. $this->assertSame('key',
  159. $this->storage->getFileKey('user1/files/foo.txt', 'fileKey', 'encModule')
  160. );
  161. }
  162. public function testSetSystemUserKey() {
  163. $this->view->expects($this->once())
  164. ->method('file_put_contents')
  165. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'),
  166. $this->equalTo('key'))
  167. ->willReturn(strlen('key'));
  168. $this->assertTrue(
  169. $this->storage->setSystemUserKey('shareKey_56884', 'key', 'encModule')
  170. );
  171. }
  172. public function testSetUserKey() {
  173. $this->view->expects($this->once())
  174. ->method('file_put_contents')
  175. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'),
  176. $this->equalTo('key'))
  177. ->willReturn(strlen('key'));
  178. $this->assertTrue(
  179. $this->storage->setUserKey('user1', 'publicKey', 'key', 'encModule')
  180. );
  181. }
  182. public function testGetSystemUserKey() {
  183. $this->view->expects($this->once())
  184. ->method('file_get_contents')
  185. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  186. ->willReturn('key');
  187. $this->view->expects($this->once())
  188. ->method('file_exists')
  189. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  190. ->willReturn(true);
  191. $this->assertSame('key',
  192. $this->storage->getSystemUserKey('shareKey_56884', 'encModule')
  193. );
  194. }
  195. public function testGetUserKey() {
  196. $this->view->expects($this->once())
  197. ->method('file_get_contents')
  198. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  199. ->willReturn('key');
  200. $this->view->expects($this->once())
  201. ->method('file_exists')
  202. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  203. ->willReturn(true);
  204. $this->assertSame('key',
  205. $this->storage->getUserKey('user1', 'publicKey', 'encModule')
  206. );
  207. }
  208. public function testDeleteUserKey() {
  209. $this->view->expects($this->once())
  210. ->method('file_exists')
  211. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  212. ->willReturn(true);
  213. $this->view->expects($this->once())
  214. ->method('unlink')
  215. ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
  216. ->willReturn(true);
  217. $this->assertTrue(
  218. $this->storage->deleteUserKey('user1', 'publicKey', 'encModule')
  219. );
  220. }
  221. public function testDeleteSystemUserKey() {
  222. $this->view->expects($this->once())
  223. ->method('file_exists')
  224. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  225. ->willReturn(true);
  226. $this->view->expects($this->once())
  227. ->method('unlink')
  228. ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
  229. ->willReturn(true);
  230. $this->assertTrue(
  231. $this->storage->deleteSystemUserKey('shareKey_56884', 'encModule')
  232. );
  233. }
  234. public function testDeleteFileKeySystemWide() {
  235. $this->util->expects($this->any())
  236. ->method('getUidAndFilename')
  237. ->willReturn(array('user1', '/files/foo.txt'));
  238. $this->util->expects($this->any())
  239. ->method('stripPartialFileExtension')
  240. ->willReturnArgument(0);
  241. $this->util->expects($this->any())
  242. ->method('isSystemWideMountPoint')
  243. ->willReturn(true);
  244. $this->view->expects($this->once())
  245. ->method('file_exists')
  246. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  247. ->willReturn(true);
  248. $this->view->expects($this->once())
  249. ->method('unlink')
  250. ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  251. ->willReturn(true);
  252. $this->assertTrue(
  253. $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey', 'encModule')
  254. );
  255. }
  256. public function testDeleteFileKey() {
  257. $this->util->expects($this->any())
  258. ->method('getUidAndFilename')
  259. ->willReturn(array('user1', '/files/foo.txt'));
  260. $this->util->expects($this->any())
  261. ->method('stripPartialFileExtension')
  262. ->willReturnArgument(0);
  263. $this->util->expects($this->any())
  264. ->method('isSystemWideMountPoint')
  265. ->willReturn(false);
  266. $this->view->expects($this->once())
  267. ->method('file_exists')
  268. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  269. ->willReturn(true);
  270. $this->view->expects($this->once())
  271. ->method('unlink')
  272. ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
  273. ->willReturn(true);
  274. $this->assertTrue(
  275. $this->storage->deleteFileKey('user1/files/foo.txt', 'fileKey', 'encModule')
  276. );
  277. }
  278. /**
  279. * @dataProvider dataProviderCopyRename
  280. */
  281. public function testRenameKeys($source, $target, $systemWideMountSource, $systemWideMountTarget, $expectedSource, $expectedTarget) {
  282. $this->view->expects($this->any())
  283. ->method('file_exists')
  284. ->willReturn(true);
  285. $this->view->expects($this->any())
  286. ->method('is_dir')
  287. ->willReturn(true);
  288. $this->view->expects($this->once())
  289. ->method('rename')
  290. ->with(
  291. $this->equalTo($expectedSource),
  292. $this->equalTo($expectedTarget))
  293. ->willReturn(true);
  294. $this->util->expects($this->any())
  295. ->method('getUidAndFilename')
  296. ->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
  297. $this->util->expects($this->any())
  298. ->method('isSystemWideMountPoint')
  299. ->willReturnCallback(function($path, $owner) use ($systemWideMountSource, $systemWideMountTarget) {
  300. if(strpos($path, 'source.txt') !== false) {
  301. return $systemWideMountSource;
  302. }
  303. return $systemWideMountTarget;
  304. });
  305. $this->storage->renameKeys($source, $target);
  306. }
  307. /**
  308. * @dataProvider dataProviderCopyRename
  309. */
  310. public function testCopyKeys($source, $target, $systemWideMountSource, $systemWideMountTarget , $expectedSource, $expectedTarget) {
  311. $this->view->expects($this->any())
  312. ->method('file_exists')
  313. ->willReturn(true);
  314. $this->view->expects($this->any())
  315. ->method('is_dir')
  316. ->willReturn(true);
  317. $this->view->expects($this->once())
  318. ->method('copy')
  319. ->with(
  320. $this->equalTo($expectedSource),
  321. $this->equalTo($expectedTarget))
  322. ->willReturn(true);
  323. $this->util->expects($this->any())
  324. ->method('getUidAndFilename')
  325. ->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
  326. $this->util->expects($this->any())
  327. ->method('isSystemWideMountPoint')
  328. ->willReturnCallback(function($path, $owner) use ($systemWideMountSource, $systemWideMountTarget) {
  329. if(strpos($path, 'source.txt') !== false) {
  330. return $systemWideMountSource;
  331. }
  332. return $systemWideMountTarget;
  333. });
  334. $this->storage->copyKeys($source, $target);
  335. }
  336. public function getUidAndFilenameCallback() {
  337. $args = func_get_args();
  338. $path = $args[0];
  339. $parts = explode('/', $path);
  340. return array($parts[1], '/' . implode('/', array_slice($parts, 2)));
  341. }
  342. public function dataProviderCopyRename() {
  343. return array(
  344. array('/user1/files/source.txt', '/user1/files/target.txt', false, false,
  345. '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
  346. array('/user1/files/foo/source.txt', '/user1/files/target.txt', false, false,
  347. '/user1/files_encryption/keys/files/foo/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
  348. array('/user1/files/source.txt', '/user1/files/foo/target.txt', false, false,
  349. '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/foo/target.txt/'),
  350. array('/user1/files/source.txt', '/user1/files/foo/target.txt', true, true,
  351. '/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/foo/target.txt/'),
  352. array('/user1/files/source.txt', '/user1/files/target.txt', false, true,
  353. '/user1/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/target.txt/'),
  354. array('/user1/files/source.txt', '/user1/files/target.txt', true, false,
  355. '/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
  356. array('/user2/files/source.txt', '/user1/files/target.txt', false, false,
  357. '/user2/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
  358. array('/user2/files/foo/source.txt', '/user1/files/target.txt', false, false,
  359. '/user2/files_encryption/keys/files/foo/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
  360. array('/user2/files/source.txt', '/user1/files/foo/target.txt', false, false,
  361. '/user2/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/foo/target.txt/'),
  362. array('/user2/files/source.txt', '/user1/files/foo/target.txt', true, true,
  363. '/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/foo/target.txt/'),
  364. array('/user2/files/source.txt', '/user1/files/target.txt', false, true,
  365. '/user2/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/target.txt/'),
  366. array('/user2/files/source.txt', '/user1/files/target.txt', true, false,
  367. '/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
  368. );
  369. }
  370. /**
  371. * @dataProvider dataTestGetPathToKeys
  372. *
  373. * @param string $path
  374. * @param boolean $systemWideMountPoint
  375. * @param string $storageRoot
  376. * @param string $expected
  377. */
  378. public function testGetPathToKeys($path, $systemWideMountPoint, $storageRoot, $expected) {
  379. $this->invokePrivate($this->storage, 'root_dir', [$storageRoot]);
  380. $this->util->expects($this->any())
  381. ->method('getUidAndFilename')
  382. ->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
  383. $this->util->expects($this->any())
  384. ->method('isSystemWideMountPoint')
  385. ->willReturn($systemWideMountPoint);
  386. $this->assertSame($expected,
  387. self::invokePrivate($this->storage, 'getPathToKeys', [$path])
  388. );
  389. }
  390. public function dataTestGetPathToKeys() {
  391. return [
  392. ['/user1/files/source.txt', false, '', '/user1/files_encryption/keys/files/source.txt/'],
  393. ['/user1/files/source.txt', true, '', '/files_encryption/keys/files/source.txt/'],
  394. ['/user1/files/source.txt', false, 'storageRoot', '/storageRoot/user1/files_encryption/keys/files/source.txt/'],
  395. ['/user1/files/source.txt', true, 'storageRoot', '/storageRoot/files_encryption/keys/files/source.txt/'],
  396. ];
  397. }
  398. public function testKeySetPreparation() {
  399. $this->view->expects($this->any())
  400. ->method('file_exists')
  401. ->willReturn(false);
  402. $this->view->expects($this->any())
  403. ->method('is_dir')
  404. ->willReturn(false);
  405. $this->view->expects($this->any())
  406. ->method('mkdir')
  407. ->will($this->returnCallback(array($this, 'mkdirCallback')));
  408. $this->mkdirStack = array(
  409. '/user1/files_encryption/keys/foo',
  410. '/user1/files_encryption/keys',
  411. '/user1/files_encryption',
  412. '/user1');
  413. self::invokePrivate($this->storage, 'keySetPreparation', array('/user1/files_encryption/keys/foo'));
  414. }
  415. public function mkdirCallback() {
  416. $args = func_get_args();
  417. $expected = array_pop($this->mkdirStack);
  418. $this->assertSame($expected, $args[0]);
  419. }
  420. /**
  421. * @dataProvider dataTestGetFileKeyDir
  422. *
  423. * @param bool $isSystemWideMountPoint
  424. * @param string $storageRoot
  425. * @param string $expected
  426. */
  427. public function testGetFileKeyDir($isSystemWideMountPoint, $storageRoot, $expected) {
  428. $path = '/user1/files/foo/bar.txt';
  429. $owner = 'user1';
  430. $relativePath = '/foo/bar.txt';
  431. $this->invokePrivate($this->storage, 'root_dir', [$storageRoot]);
  432. $this->util->expects($this->once())->method('isSystemWideMountPoint')
  433. ->willReturn($isSystemWideMountPoint);
  434. $this->util->expects($this->once())->method('getUidAndFilename')
  435. ->with($path)->willReturn([$owner, $relativePath]);
  436. $this->assertSame($expected,
  437. $this->invokePrivate($this->storage, 'getFileKeyDir', ['OC_DEFAULT_MODULE', $path])
  438. );
  439. }
  440. public function dataTestGetFileKeyDir() {
  441. return [
  442. [false, '', '/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  443. [true, '', '/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  444. [false, 'newStorageRoot', '/newStorageRoot/user1/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  445. [true, 'newStorageRoot', '/newStorageRoot/files_encryption/keys/foo/bar.txt/OC_DEFAULT_MODULE/'],
  446. ];
  447. }
  448. /**
  449. * @dataProvider dataTestBackupUserKeys
  450. * @param bool $createBackupDir
  451. */
  452. public function testBackupUserKeys($createBackupDir) {
  453. $storage = $this->getMockBuilder('OC\Encryption\Keys\Storage')
  454. ->setConstructorArgs([$this->view, $this->util])
  455. ->setMethods(['getTimestamp'])
  456. ->getMock();
  457. $storage->expects($this->any())->method('getTimestamp')->willReturn('1234567');
  458. $this->view->expects($this->once())->method('file_exists')
  459. ->with('user1/files_encryption/backup')->willReturn(!$createBackupDir);
  460. if ($createBackupDir) {
  461. $this->view->expects($this->at(1))->method('mkdir')
  462. ->with('user1/files_encryption/backup');
  463. $this->view->expects($this->at(2))->method('mkdir')
  464. ->with('user1/files_encryption/backup/test.encryptionModule.1234567');
  465. } else {
  466. $this->view->expects($this->once())->method('mkdir')
  467. ->with('user1/files_encryption/backup/test.encryptionModule.1234567');
  468. }
  469. $this->view->expects($this->once())->method('copy')
  470. ->with(
  471. 'user1/files_encryption/encryptionModule',
  472. 'user1/files_encryption/backup/test.encryptionModule.1234567'
  473. )->willReturn(true);
  474. $this->assertTrue($storage->backupUserKeys('encryptionModule', 'test', 'user1'));
  475. }
  476. public function dataTestBackupUserKeys() {
  477. return [
  478. [true], [false]
  479. ];
  480. }
  481. }