1
0

MigrationTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Encryption\Tests;
  26. use OCA\Encryption\Migration;
  27. use OCP\ILogger;
  28. /**
  29. * Class MigrationTest
  30. *
  31. * @package OCA\Encryption\Tests
  32. * @group DB
  33. */
  34. class MigrationTest extends \Test\TestCase {
  35. const TEST_ENCRYPTION_MIGRATION_USER1='test_encryption_user1';
  36. const TEST_ENCRYPTION_MIGRATION_USER2='test_encryption_user2';
  37. const TEST_ENCRYPTION_MIGRATION_USER3='test_encryption_user3';
  38. /** @var \OC\Files\View */
  39. private $view;
  40. private $public_share_key_id = 'share_key_id';
  41. private $recovery_key_id = 'recovery_key_id';
  42. private $moduleId;
  43. /** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
  44. private $logger;
  45. public static function setUpBeforeClass() {
  46. parent::setUpBeforeClass();
  47. \OC::$server->getUserManager()->createUser(self::TEST_ENCRYPTION_MIGRATION_USER1, 'foo');
  48. \OC::$server->getUserManager()->createUser(self::TEST_ENCRYPTION_MIGRATION_USER2, 'foo');
  49. \OC::$server->getUserManager()->createUser(self::TEST_ENCRYPTION_MIGRATION_USER3, 'foo');
  50. }
  51. public static function tearDownAfterClass() {
  52. $user = \OC::$server->getUserManager()->get(self::TEST_ENCRYPTION_MIGRATION_USER1);
  53. if ($user !== null) { $user->delete(); }
  54. $user = \OC::$server->getUserManager()->get(self::TEST_ENCRYPTION_MIGRATION_USER2);
  55. if ($user !== null) { $user->delete(); }
  56. $user = \OC::$server->getUserManager()->get(self::TEST_ENCRYPTION_MIGRATION_USER3);
  57. if ($user !== null) { $user->delete(); }
  58. parent::tearDownAfterClass();
  59. }
  60. public function setUp() {
  61. $this->logger = $this->getMockBuilder('\OCP\ILogger')->disableOriginalConstructor()->getMock();
  62. $this->view = new \OC\Files\View();
  63. $this->moduleId = \OCA\Encryption\Crypto\Encryption::ID;
  64. }
  65. /**
  66. * @param string $uid
  67. */
  68. protected function createDummyShareKeys($uid) {
  69. $this->loginAsUser($uid);
  70. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/folder3/file3');
  71. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/file2');
  72. $this->view->mkdir($uid . '/files_encryption/keys/folder1/file.1');
  73. $this->view->mkdir($uid . '/files_encryption/keys/folder2/file.2.1');
  74. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  75. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  76. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  77. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  78. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  79. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  80. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  81. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  82. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  83. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  84. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  85. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  86. if ($this->public_share_key_id) {
  87. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . $this->public_share_key_id . '.shareKey' , 'data');
  88. }
  89. if ($this->recovery_key_id) {
  90. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . $this->recovery_key_id . '.shareKey' , 'data');
  91. }
  92. }
  93. /**
  94. * @param string $uid
  95. */
  96. protected function createDummyUserKeys($uid) {
  97. $this->loginAsUser($uid);
  98. $this->view->mkdir($uid . '/files_encryption/');
  99. $this->view->mkdir('/files_encryption/public_keys');
  100. $this->view->file_put_contents($uid . '/files_encryption/' . $uid . '.privateKey', 'privateKey');
  101. $this->view->file_put_contents('/files_encryption/public_keys/' . $uid . '.publicKey', 'publicKey');
  102. }
  103. /**
  104. * @param string $uid
  105. */
  106. protected function createDummyFileKeys($uid) {
  107. $this->loginAsUser($uid);
  108. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/folder3/file3');
  109. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/file2');
  110. $this->view->mkdir($uid . '/files_encryption/keys/folder1/file.1');
  111. $this->view->mkdir($uid . '/files_encryption/keys/folder2/file.2.1');
  112. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/fileKey' , 'data');
  113. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/fileKey' , 'data');
  114. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/fileKey' , 'data');
  115. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/fileKey' , 'data');
  116. }
  117. /**
  118. * @param string $uid
  119. */
  120. protected function createDummyFiles($uid) {
  121. $this->loginAsUser($uid);
  122. $this->view->mkdir($uid . '/files/folder1/folder2/folder3/file3');
  123. $this->view->mkdir($uid . '/files/folder1/folder2/file2');
  124. $this->view->mkdir($uid . '/files/folder1/file.1');
  125. $this->view->mkdir($uid . '/files/folder2/file.2.1');
  126. $this->view->file_put_contents($uid . '/files/folder1/folder2/folder3/file3/fileKey' , 'data');
  127. $this->view->file_put_contents($uid . '/files/folder1/folder2/file2/fileKey' , 'data');
  128. $this->view->file_put_contents($uid . '/files/folder1/file.1/fileKey' , 'data');
  129. $this->view->file_put_contents($uid . '/files/folder2/file.2.1/fileKey' , 'data');
  130. }
  131. /**
  132. * @param string $uid
  133. */
  134. protected function createDummyFilesInTrash($uid) {
  135. $this->loginAsUser($uid);
  136. $this->view->mkdir($uid . '/files_trashbin/keys/file1.d5457864');
  137. $this->view->mkdir($uid . '/files_trashbin/keys/folder1.d7437648723/file2');
  138. $this->view->file_put_contents($uid . '/files_trashbin/keys/file1.d5457864/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  139. $this->view->file_put_contents($uid . '/files_trashbin/keys/file1.d5457864/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  140. $this->view->file_put_contents($uid . '/files_trashbin/keys/folder1.d7437648723/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  141. $this->view->file_put_contents($uid . '/files_trashbin/keys/file1.d5457864/fileKey' , 'data');
  142. $this->view->file_put_contents($uid . '/files_trashbin/keys/folder1.d7437648723/file2/fileKey' , 'data');
  143. // create the files itself
  144. $this->view->mkdir($uid . '/files_trashbin/folder1.d7437648723');
  145. $this->view->file_put_contents($uid . '/files_trashbin/file1.d5457864' , 'data');
  146. $this->view->file_put_contents($uid . '/files_trashbin/folder1.d7437648723/file2' , 'data');
  147. }
  148. protected function createDummySystemWideKeys() {
  149. $this->view->mkdir('files_encryption');
  150. $this->view->mkdir('files_encryption/public_keys');
  151. $this->view->file_put_contents('files_encryption/systemwide_1.privateKey', 'data');
  152. $this->view->file_put_contents('files_encryption/systemwide_2.privateKey', 'data');
  153. $this->view->file_put_contents('files_encryption/public_keys/systemwide_1.publicKey', 'data');
  154. $this->view->file_put_contents('files_encryption/public_keys/systemwide_2.publicKey', 'data');
  155. }
  156. public function testMigrateToNewFolderStructure() {
  157. $this->createDummyUserKeys(self::TEST_ENCRYPTION_MIGRATION_USER1);
  158. $this->createDummyUserKeys(self::TEST_ENCRYPTION_MIGRATION_USER2);
  159. $this->createDummyUserKeys(self::TEST_ENCRYPTION_MIGRATION_USER3);
  160. $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER1);
  161. $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER2);
  162. $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER3);
  163. $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER1);
  164. $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER2);
  165. $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER3);
  166. $this->createDummyFiles(self::TEST_ENCRYPTION_MIGRATION_USER1);
  167. $this->createDummyFiles(self::TEST_ENCRYPTION_MIGRATION_USER2);
  168. $this->createDummyFiles(self::TEST_ENCRYPTION_MIGRATION_USER3);
  169. $this->createDummyFilesInTrash(self::TEST_ENCRYPTION_MIGRATION_USER2);
  170. // no user for system wide mount points
  171. $this->createDummyFileKeys('');
  172. $this->createDummyShareKeys('');
  173. $this->createDummySystemWideKeys();
  174. /** @var \PHPUnit_Framework_MockObject_MockObject|\OCA\Encryption\Migration $m */
  175. $m = $this->getMockBuilder('OCA\Encryption\Migration')
  176. ->setConstructorArgs(
  177. [
  178. \OC::$server->getConfig(),
  179. new \OC\Files\View(),
  180. \OC::$server->getDatabaseConnection(),
  181. $this->logger
  182. ]
  183. )->setMethods(['getSystemMountPoints'])->getMock();
  184. $m->expects($this->any())->method('getSystemMountPoints')
  185. ->will($this->returnValue([['mountpoint' => 'folder1'], ['mountpoint' => 'folder2']]));
  186. $m->reorganizeFolderStructure();
  187. // even if it runs twice folder should always move only once
  188. $m->reorganizeFolderStructure();
  189. $this->loginAsUser(self::TEST_ENCRYPTION_MIGRATION_USER1);
  190. $this->assertTrue(
  191. $this->view->file_exists(
  192. self::TEST_ENCRYPTION_MIGRATION_USER1 . '/files_encryption/' .
  193. $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.publicKey')
  194. );
  195. $this->loginAsUser(self::TEST_ENCRYPTION_MIGRATION_USER2);
  196. $this->assertTrue(
  197. $this->view->file_exists(
  198. self::TEST_ENCRYPTION_MIGRATION_USER2 . '/files_encryption/' .
  199. $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.publicKey')
  200. );
  201. $this->loginAsUser(self::TEST_ENCRYPTION_MIGRATION_USER3);
  202. $this->assertTrue(
  203. $this->view->file_exists(
  204. self::TEST_ENCRYPTION_MIGRATION_USER3 . '/files_encryption/' .
  205. $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.publicKey')
  206. );
  207. $this->loginAsUser(self::TEST_ENCRYPTION_MIGRATION_USER1);
  208. $this->assertTrue(
  209. $this->view->file_exists(
  210. '/files_encryption/' . $this->moduleId . '/systemwide_1.publicKey')
  211. );
  212. $this->assertTrue(
  213. $this->view->file_exists(
  214. '/files_encryption/' . $this->moduleId . '/systemwide_2.publicKey')
  215. );
  216. $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER1);
  217. $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER2);
  218. $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER3);
  219. // system wide keys
  220. $this->verifyNewKeyPath('');
  221. // trash
  222. $this->verifyFilesInTrash(self::TEST_ENCRYPTION_MIGRATION_USER2);
  223. }
  224. /**
  225. * @param string $uid
  226. */
  227. protected function verifyFilesInTrash($uid) {
  228. $this->loginAsUser($uid);
  229. // share keys
  230. $this->assertTrue(
  231. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')
  232. );
  233. $this->assertTrue(
  234. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')
  235. );
  236. $this->assertTrue(
  237. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/folder1.d7437648723/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')
  238. );
  239. // file keys
  240. $this->assertTrue(
  241. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/fileKey')
  242. );
  243. $this->assertTrue(
  244. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/fileKey')
  245. );
  246. $this->assertTrue(
  247. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/folder1.d7437648723/file2/' . $this->moduleId . '/fileKey')
  248. );
  249. }
  250. /**
  251. * @param string $uid
  252. */
  253. protected function verifyNewKeyPath($uid) {
  254. // private key
  255. if ($uid !== '') {
  256. $this->loginAsUser($uid);
  257. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/' . $this->moduleId . '/'. $uid . '.privateKey'));
  258. }
  259. // file keys
  260. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/fileKey'));
  261. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/fileKey'));
  262. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/fileKey'));
  263. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' .$this->moduleId . '/fileKey'));
  264. // share keys
  265. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  266. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  267. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  268. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  269. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  270. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  271. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  272. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  273. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  274. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  275. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  276. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  277. if ($this->public_share_key_id) {
  278. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . $this->public_share_key_id . '.shareKey'));
  279. }
  280. if ($this->recovery_key_id) {
  281. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . $this->recovery_key_id . '.shareKey'));
  282. }
  283. }
  284. private function prepareDB() {
  285. $config = \OC::$server->getConfig();
  286. $config->setAppValue('files_encryption', 'recoveryKeyId', 'recovery_id');
  287. $config->setAppValue('files_encryption', 'publicShareKeyId', 'share_id');
  288. $config->setAppValue('files_encryption', 'recoveryAdminEnabled', '1');
  289. $config->setUserValue(self::TEST_ENCRYPTION_MIGRATION_USER1, 'files_encryption', 'recoverKeyEnabled', '1');
  290. //$this->invokePrivate($config, 'cache', [[]]);
  291. $cache = $this->invokePrivate(\OC::$server->getAppConfig(), 'cache');
  292. unset($cache['encryption']);
  293. unset($cache['files_encryption']);
  294. $this->invokePrivate(\OC::$server->getAppConfig(), 'cache', [$cache]);
  295. $cache = $this->invokePrivate($config, 'userCache');
  296. unset($cache[self::TEST_ENCRYPTION_MIGRATION_USER1]);
  297. $this->invokePrivate(\OC::$server->getAppConfig(), 'userCache', [$cache]);
  298. // delete default values set by the encryption app during initialization
  299. /** @var \OCP\IDBConnection $connection */
  300. $connection = \OC::$server->getDatabaseConnection();
  301. $query = $connection->getQueryBuilder();
  302. $query->delete('appconfig')
  303. ->where($query->expr()->eq('appid', $query->createParameter('appid')))
  304. ->setParameter('appid', 'encryption');
  305. $query->execute();
  306. $query = $connection->getQueryBuilder();
  307. $query->delete('preferences')
  308. ->where($query->expr()->eq('appid', $query->createParameter('appid')))
  309. ->setParameter('appid', 'encryption');
  310. $query->execute();
  311. }
  312. public function testUpdateDB() {
  313. $this->prepareDB();
  314. $m = new Migration(\OC::$server->getConfig(), new \OC\Files\View(), \OC::$server->getDatabaseConnection(), $this->logger);
  315. $this->invokePrivate($m, 'installedVersion', ['0.7']);
  316. $m->updateDB();
  317. $this->verifyDB('appconfig', 'files_encryption', 0);
  318. $this->verifyDB('preferences', 'files_encryption', 0);
  319. $this->verifyDB('appconfig', 'encryption', 3);
  320. $this->verifyDB('preferences', 'encryption', 1);
  321. }
  322. /**
  323. * test update db if the db already contain some existing new values
  324. */
  325. public function testUpdateDBExistingNewConfig() {
  326. $this->prepareDB();
  327. $config = \OC::$server->getConfig();
  328. $config->setAppValue('encryption', 'publicShareKeyId', 'wrong_share_id');
  329. $config->setUserValue(self::TEST_ENCRYPTION_MIGRATION_USER1, 'encryption', 'recoverKeyEnabled', '9');
  330. $m = new Migration(\OC::$server->getConfig(), new \OC\Files\View(), \OC::$server->getDatabaseConnection(), $this->logger);
  331. $this->invokePrivate($m, 'installedVersion', ['0.7']);
  332. $m->updateDB();
  333. $this->verifyDB('appconfig', 'files_encryption', 0);
  334. $this->verifyDB('preferences', 'files_encryption', 0);
  335. $this->verifyDB('appconfig', 'encryption', 3);
  336. $this->verifyDB('preferences', 'encryption', 1);
  337. // check if the existing values where overwritten correctly
  338. /** @var \OC\DB\Connection $connection */
  339. $connection = \OC::$server->getDatabaseConnection();
  340. $query = $connection->getQueryBuilder();
  341. $query->select('configvalue')
  342. ->from('appconfig')
  343. ->where($query->expr()->andX(
  344. $query->expr()->eq('appid', $query->createParameter('appid')),
  345. $query->expr()->eq('configkey', $query->createParameter('configkey'))
  346. ))
  347. ->setParameter('appid', 'encryption')
  348. ->setParameter('configkey', 'publicShareKeyId');
  349. $result = $query->execute();
  350. $value = $result->fetch();
  351. $this->assertTrue(isset($value['configvalue']));
  352. $this->assertSame('share_id', $value['configvalue']);
  353. $query = $connection->getQueryBuilder();
  354. $query->select('configvalue')
  355. ->from('preferences')
  356. ->where($query->expr()->andX(
  357. $query->expr()->eq('appid', $query->createParameter('appid')),
  358. $query->expr()->eq('configkey', $query->createParameter('configkey')),
  359. $query->expr()->eq('userid', $query->createParameter('userid'))
  360. ))
  361. ->setParameter('appid', 'encryption')
  362. ->setParameter('configkey', 'recoverKeyEnabled')
  363. ->setParameter('userid', self::TEST_ENCRYPTION_MIGRATION_USER1);
  364. $result = $query->execute();
  365. $value = $result->fetch();
  366. $this->assertTrue(isset($value['configvalue']));
  367. $this->assertSame('1', $value['configvalue']);
  368. }
  369. /**
  370. * @param string $table
  371. * @param string $appid
  372. * @param integer $expected
  373. */
  374. public function verifyDB($table, $appid, $expected) {
  375. /** @var \OCP\IDBConnection $connection */
  376. $connection = \OC::$server->getDatabaseConnection();
  377. $query = $connection->getQueryBuilder();
  378. $query->select('appid')
  379. ->from($table)
  380. ->where($query->expr()->eq('appid', $query->createParameter('appid')))
  381. ->setParameter('appid', $appid);
  382. $result = $query->execute();
  383. $values = $result->fetchAll();
  384. $this->assertSame($expected,
  385. count($values)
  386. );
  387. }
  388. /**
  389. * test update of the file cache
  390. */
  391. public function testUpdateFileCache() {
  392. $this->prepareFileCache();
  393. $m = new Migration(\OC::$server->getConfig(), new \OC\Files\View(), \OC::$server->getDatabaseConnection(), $this->logger);
  394. $this->invokePrivate($m, 'installedVersion', ['0.7']);
  395. self::invokePrivate($m, 'updateFileCache');
  396. // check results
  397. /** @var \OCP\IDBConnection $connection */
  398. $connection = \OC::$server->getDatabaseConnection();
  399. $query = $connection->getQueryBuilder();
  400. $query->select('*')
  401. ->from('filecache');
  402. $result = $query->execute();
  403. $entries = $result->fetchAll();
  404. foreach($entries as $entry) {
  405. if ((int)$entry['encrypted'] === 1) {
  406. $this->assertSame((int)$entry['unencrypted_size'], (int)$entry['size']);
  407. } else {
  408. $this->assertSame((int)$entry['unencrypted_size'] - 2, (int)$entry['size']);
  409. }
  410. }
  411. }
  412. public function prepareFileCache() {
  413. /** @var \OCP\IDBConnection $connection */
  414. $connection = \OC::$server->getDatabaseConnection();
  415. $query = $connection->getQueryBuilder();
  416. $query->delete('filecache');
  417. $query->execute();
  418. $query = $connection->getQueryBuilder();
  419. $result = $query->select('fileid')
  420. ->from('filecache')
  421. ->setMaxResults(1)->execute()->fetchAll();
  422. $this->assertEmpty($result);
  423. $query = $connection->getQueryBuilder();
  424. $query->insert('filecache')
  425. ->values(
  426. array(
  427. 'storage' => $query->createParameter('storage'),
  428. 'path_hash' => $query->createParameter('path_hash'),
  429. 'encrypted' => $query->createParameter('encrypted'),
  430. 'size' => $query->createParameter('size'),
  431. 'unencrypted_size' => $query->createParameter('unencrypted_size'),
  432. )
  433. );
  434. for ($i = 1; $i < 20; $i++) {
  435. $query->setParameter('storage', 1)
  436. ->setParameter('path_hash', $i)
  437. ->setParameter('encrypted', $i % 2)
  438. ->setParameter('size', $i)
  439. ->setParameter('unencrypted_size', $i + 2);
  440. $this->assertSame(1,
  441. $query->execute()
  442. );
  443. }
  444. $query = $connection->getQueryBuilder();
  445. $result = $query->select('fileid')
  446. ->from('filecache')
  447. ->execute()->fetchAll();
  448. $this->assertSame(19, count($result));
  449. }
  450. /**
  451. * @dataProvider dataTestGetTargetDir
  452. */
  453. public function testGetTargetDir($user, $keyPath, $filename, $trash, $systemMounts, $expected) {
  454. $view = $this->getMockBuilder('\OC\Files\View')
  455. ->disableOriginalConstructor()->getMock();
  456. $view->expects($this->any())->method('file_exists')->willReturn(true);
  457. $m = $this->getMockBuilder('OCA\Encryption\Migration')
  458. ->setConstructorArgs(
  459. [
  460. \OC::$server->getConfig(),
  461. $view,
  462. \OC::$server->getDatabaseConnection(),
  463. $this->logger
  464. ]
  465. )->setMethods(['getSystemMountPoints'])->getMock();
  466. $m->expects($this->any())->method('getSystemMountPoints')
  467. ->willReturn($systemMounts);
  468. $this->assertSame($expected,
  469. $this->invokePrivate($m, 'getTargetDir', [$user, $keyPath, $filename, $trash])
  470. );
  471. }
  472. public function dataTestGetTargetDir() {
  473. return [
  474. [
  475. 'user1',
  476. '/files_encryption/keys/foo/bar.txt',
  477. 'user1.shareKey',
  478. false,
  479. [],
  480. 'user1/files_encryption/keys/files/foo/bar.txt/OC_DEFAULT_MODULE/user1.shareKey'
  481. ],
  482. [
  483. 'user1',
  484. '/files_trashbin/keys/foo/bar.txt',
  485. 'user1.shareKey',
  486. true,
  487. [],
  488. 'user1/files_encryption/keys/files_trashbin/foo/bar.txt/OC_DEFAULT_MODULE/user1.shareKey'
  489. ],
  490. [
  491. '',
  492. '/files_encryption/keys/foo/bar.txt',
  493. 'user1.shareKey',
  494. false,
  495. [['mountpoint' => 'foo']],
  496. '/files_encryption/keys/files/foo/bar.txt/OC_DEFAULT_MODULE/user1.shareKey'
  497. ],
  498. [
  499. '',
  500. '/files_encryption/keys/foo/bar.txt',
  501. 'user1.shareKey',
  502. false,
  503. [['mountpoint' => 'foobar']],
  504. false
  505. ],
  506. [
  507. '',
  508. '/files_encryption/keys/foobar/bar.txt',
  509. 'user1.shareKey',
  510. false,
  511. [['mountpoint' => 'foo']],
  512. false
  513. ]
  514. ];
  515. }
  516. }