You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrationTest.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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;
  24. use OCA\Encryption\Migration;
  25. class MigrationTest extends \Test\TestCase {
  26. const TEST_ENCRYPTION_MIGRATION_USER1='test_encryption_user1';
  27. const TEST_ENCRYPTION_MIGRATION_USER2='test_encryption_user2';
  28. const TEST_ENCRYPTION_MIGRATION_USER3='test_encryption_user3';
  29. /** @var \OC\Files\View */
  30. private $view;
  31. private $public_share_key_id = 'share_key_id';
  32. private $recovery_key_id = 'recovery_key_id';
  33. private $moduleId;
  34. public static function setUpBeforeClass() {
  35. parent::setUpBeforeClass();
  36. \OC_User::createUser(self::TEST_ENCRYPTION_MIGRATION_USER1, 'foo');
  37. \OC_User::createUser(self::TEST_ENCRYPTION_MIGRATION_USER2, 'foo');
  38. \OC_User::createUser(self::TEST_ENCRYPTION_MIGRATION_USER3, 'foo');
  39. }
  40. public static function tearDownAfterClass() {
  41. \OC_User::deleteUser(self::TEST_ENCRYPTION_MIGRATION_USER1);
  42. \OC_User::deleteUser(self::TEST_ENCRYPTION_MIGRATION_USER2);
  43. \OC_User::deleteUser(self::TEST_ENCRYPTION_MIGRATION_USER3);
  44. parent::tearDownAfterClass();
  45. }
  46. public function setUp() {
  47. $this->view = new \OC\Files\View();
  48. $this->moduleId = \OCA\Encryption\Crypto\Encryption::ID;
  49. }
  50. protected function createDummyShareKeys($uid) {
  51. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/folder3/file3');
  52. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/file2');
  53. $this->view->mkdir($uid . '/files_encryption/keys/folder1/file.1');
  54. $this->view->mkdir($uid . '/files_encryption/keys/folder2/file.2.1');
  55. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  56. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  57. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  58. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  59. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  60. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  61. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  62. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  63. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  64. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  65. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey' , 'data');
  66. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey' , 'data');
  67. if ($this->public_share_key_id) {
  68. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . $this->public_share_key_id . '.shareKey' , 'data');
  69. }
  70. if ($this->recovery_key_id) {
  71. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/' . $this->recovery_key_id . '.shareKey' , 'data');
  72. }
  73. }
  74. protected function createDummyUserKeys($uid) {
  75. $this->view->mkdir($uid . '/files_encryption/');
  76. $this->view->mkdir('/files_encryption/public_keys');
  77. $this->view->file_put_contents($uid . '/files_encryption/' . $uid . '.privateKey', 'privateKey');
  78. $this->view->file_put_contents('/files_encryption/public_keys/' . $uid . '.publicKey', 'publicKey');
  79. }
  80. protected function createDummyFileKeys($uid) {
  81. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/folder3/file3');
  82. $this->view->mkdir($uid . '/files_encryption/keys/folder1/folder2/file2');
  83. $this->view->mkdir($uid . '/files_encryption/keys/folder1/file.1');
  84. $this->view->mkdir($uid . '/files_encryption/keys/folder2/file.2.1');
  85. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/folder3/file3/fileKey' , 'data');
  86. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/folder2/file2/fileKey' , 'data');
  87. $this->view->file_put_contents($uid . '/files_encryption/keys/folder1/file.1/fileKey' , 'data');
  88. $this->view->file_put_contents($uid . '/files_encryption/keys/folder2/file.2.1/fileKey' , 'data');
  89. }
  90. protected function createDummyFilesInTrash($uid) {
  91. $this->view->mkdir($uid . '/files_trashbin/keys/file1.d5457864');
  92. $this->view->mkdir($uid . '/files_trashbin/keys/folder1.d7437648723/file2');
  93. $this->view->file_put_contents($uid . '/files_trashbin/keys/file1.d5457864/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  94. $this->view->file_put_contents($uid . '/files_trashbin/keys/file1.d5457864/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  95. $this->view->file_put_contents($uid . '/files_trashbin/keys/folder1.d7437648723/file2/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey' , 'data');
  96. $this->view->file_put_contents($uid . '/files_trashbin/keys/file1.d5457864/fileKey' , 'data');
  97. $this->view->file_put_contents($uid . '/files_trashbin/keys/folder1.d7437648723/file2/fileKey' , 'data');
  98. }
  99. protected function createDummySystemWideKeys() {
  100. $this->view->mkdir('files_encryption');
  101. $this->view->mkdir('files_encryption/public_keys');
  102. $this->view->file_put_contents('files_encryption/systemwide_1.privateKey', 'data');
  103. $this->view->file_put_contents('files_encryption/systemwide_2.privateKey', 'data');
  104. $this->view->file_put_contents('files_encryption/public_keys/systemwide_1.publicKey', 'data');
  105. $this->view->file_put_contents('files_encryption/public_keys/systemwide_2.publicKey', 'data');
  106. }
  107. public function testMigrateToNewFolderStructure() {
  108. $this->createDummyUserKeys(self::TEST_ENCRYPTION_MIGRATION_USER1);
  109. $this->createDummyUserKeys(self::TEST_ENCRYPTION_MIGRATION_USER2);
  110. $this->createDummyUserKeys(self::TEST_ENCRYPTION_MIGRATION_USER3);
  111. $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER1);
  112. $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER2);
  113. $this->createDummyShareKeys(self::TEST_ENCRYPTION_MIGRATION_USER3);
  114. $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER1);
  115. $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER2);
  116. $this->createDummyFileKeys(self::TEST_ENCRYPTION_MIGRATION_USER3);
  117. $this->createDummyFilesInTrash(self::TEST_ENCRYPTION_MIGRATION_USER2);
  118. // no user for system wide mount points
  119. $this->createDummyFileKeys('');
  120. $this->createDummyShareKeys('');
  121. $this->createDummySystemWideKeys();
  122. $m = new Migration(\OC::$server->getConfig(), new \OC\Files\View(), \OC::$server->getDatabaseConnection());
  123. $m->reorganizeFolderStructure();
  124. $this->assertTrue(
  125. $this->view->file_exists(
  126. self::TEST_ENCRYPTION_MIGRATION_USER1 . '/files_encryption/' .
  127. $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.publicKey')
  128. );
  129. $this->assertTrue(
  130. $this->view->file_exists(
  131. self::TEST_ENCRYPTION_MIGRATION_USER2 . '/files_encryption/' .
  132. $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.publicKey')
  133. );
  134. $this->assertTrue(
  135. $this->view->file_exists(
  136. self::TEST_ENCRYPTION_MIGRATION_USER3 . '/files_encryption/' .
  137. $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.publicKey')
  138. );
  139. $this->assertTrue(
  140. $this->view->file_exists(
  141. '/files_encryption/' . $this->moduleId . '/systemwide_1.publicKey')
  142. );
  143. $this->assertTrue(
  144. $this->view->file_exists(
  145. '/files_encryption/' . $this->moduleId . '/systemwide_2.publicKey')
  146. );
  147. $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER1);
  148. $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER2);
  149. $this->verifyNewKeyPath(self::TEST_ENCRYPTION_MIGRATION_USER3);
  150. // system wide keys
  151. $this->verifyNewKeyPath('');
  152. // trash
  153. $this->verifyFilesInTrash(self::TEST_ENCRYPTION_MIGRATION_USER2);
  154. }
  155. protected function verifyFilesInTrash($uid) {
  156. // share keys
  157. $this->assertTrue(
  158. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')
  159. );
  160. $this->assertTrue(
  161. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')
  162. );
  163. $this->assertTrue(
  164. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/folder1.d7437648723/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey')
  165. );
  166. // file keys
  167. $this->assertTrue(
  168. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/fileKey')
  169. );
  170. $this->assertTrue(
  171. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/file1.d5457864/' . $this->moduleId . '/fileKey')
  172. );
  173. $this->assertTrue(
  174. $this->view->file_exists($uid . '/files_encryption/keys/files_trashbin/folder1.d7437648723/file2/' . $this->moduleId . '/fileKey')
  175. );
  176. }
  177. protected function verifyNewKeyPath($uid) {
  178. // private key
  179. if ($uid !== '') {
  180. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/' . $this->moduleId . '/'. $uid . '.privateKey'));
  181. }
  182. // file keys
  183. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/fileKey'));
  184. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/fileKey'));
  185. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/fileKey'));
  186. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' .$this->moduleId . '/fileKey'));
  187. // share keys
  188. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  189. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  190. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/folder3/file3/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  191. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  192. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  193. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/folder2/file2/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  194. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  195. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  196. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder1/file.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  197. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER1 . '.shareKey'));
  198. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER2 . '.shareKey'));
  199. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . self::TEST_ENCRYPTION_MIGRATION_USER3 . '.shareKey'));
  200. if ($this->public_share_key_id) {
  201. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . $this->public_share_key_id . '.shareKey'));
  202. }
  203. if ($this->recovery_key_id) {
  204. $this->assertTrue($this->view->file_exists($uid . '/files_encryption/keys/files/folder2/file.2.1/' . $this->moduleId . '/' . $this->recovery_key_id . '.shareKey'));
  205. }
  206. }
  207. private function prepareDB() {
  208. $config = \OC::$server->getConfig();
  209. $config->setAppValue('files_encryption', 'recoveryKeyId', 'recovery_id');
  210. $config->setAppValue('files_encryption', 'publicShareKeyId', 'share_id');
  211. $config->setAppValue('files_encryption', 'recoveryAdminEnabled', '1');
  212. $config->setUserValue(self::TEST_ENCRYPTION_MIGRATION_USER1, 'files_encryption', 'recoverKeyEnabled', '1');
  213. // delete default values set by the encryption app during initialization
  214. /** @var \OC\DB\Connection $connection */
  215. $connection = \OC::$server->getDatabaseConnection();
  216. $query = $connection->createQueryBuilder();
  217. $query->delete('`*PREFIX*appconfig`')
  218. ->where($query->expr()->eq('`appid`', ':appid'))
  219. ->setParameter('appid', 'encryption');
  220. $query->execute();
  221. $query = $connection->createQueryBuilder();
  222. $query->delete('`*PREFIX*preferences`')
  223. ->where($query->expr()->eq('`appid`', ':appid'))
  224. ->setParameter('appid', 'encryption');
  225. $query->execute();
  226. }
  227. public function testUpdateDB() {
  228. $this->prepareDB();
  229. $m = new Migration(\OC::$server->getConfig(), new \OC\Files\View(), \OC::$server->getDatabaseConnection());
  230. $m->updateDB();
  231. $this->verifyDB('`*PREFIX*appconfig`', 'files_encryption', 0);
  232. $this->verifyDB('`*PREFIX*preferences`', 'files_encryption', 0);
  233. $this->verifyDB('`*PREFIX*appconfig`', 'encryption', 3);
  234. $this->verifyDB('`*PREFIX*preferences`', 'encryption', 1);
  235. }
  236. public function verifyDB($table, $appid, $expected) {
  237. /** @var \OC\DB\Connection $connection */
  238. $connection = \OC::$server->getDatabaseConnection();
  239. $query = $connection->createQueryBuilder();
  240. $query->select('`appid`')
  241. ->from($table)
  242. ->where($query->expr()->eq('`appid`', ':appid'))
  243. ->setParameter('appid', $appid);
  244. $result = $query->execute();
  245. $values = $result->fetchAll();
  246. $this->assertSame($expected,
  247. count($values)
  248. );
  249. }
  250. /**
  251. * test update of the file cache
  252. */
  253. public function testUpdateFileCache() {
  254. $this->prepareFileCache();
  255. $m = new Migration(\OC::$server->getConfig(), new \OC\Files\View(), \OC::$server->getDatabaseConnection());
  256. self::invokePrivate($m, 'updateFileCache');
  257. // check results
  258. /** @var \OC\DB\Connection $connection */
  259. $connection = \OC::$server->getDatabaseConnection();
  260. $query = $connection->createQueryBuilder();
  261. $query->select('*')
  262. ->from('`*PREFIX*filecache`');
  263. $result = $query->execute();
  264. $entries = $result->fetchAll();
  265. foreach($entries as $entry) {
  266. if ((int)$entry['encrypted'] === 1) {
  267. $this->assertSame((int)$entry['unencrypted_size'], (int)$entry['size']);
  268. } else {
  269. $this->assertSame((int)$entry['unencrypted_size'] - 2, (int)$entry['size']);
  270. }
  271. }
  272. }
  273. public function prepareFileCache() {
  274. /** @var \OC\DB\Connection $connection */
  275. $connection = \OC::$server->getDatabaseConnection();
  276. $query = $connection->createQueryBuilder();
  277. $query->delete('`*PREFIX*filecache`');
  278. $query->execute();
  279. $query = $connection->createQueryBuilder();
  280. $result = $query->select('`fileid`')
  281. ->from('`*PREFIX*filecache`')
  282. ->setMaxResults(1)->execute()->fetchAll();
  283. $this->assertEmpty($result);
  284. $query = $connection->createQueryBuilder();
  285. $query->insert('`*PREFIX*filecache`')
  286. ->values(
  287. array(
  288. '`storage`' => ':storage',
  289. '`path_hash`' => ':path_hash',
  290. '`encrypted`' => ':encrypted',
  291. '`size`' => ':size',
  292. '`unencrypted_size`' => ':unencrypted_size'
  293. )
  294. );
  295. for ($i = 1; $i < 20; $i++) {
  296. $query->setParameter('storage', 1)
  297. ->setParameter('path_hash', $i)
  298. ->setParameter('encrypted', $i % 2)
  299. ->setParameter('size', $i)
  300. ->setParameter('unencrypted_size', $i + 2);
  301. $this->assertSame(1,
  302. $query->execute()
  303. );
  304. }
  305. $query = $connection->createQueryBuilder();
  306. $result = $query->select('`fileid`')
  307. ->from('`*PREFIX*filecache`')
  308. ->execute()->fetchAll();
  309. $this->assertSame(19, count($result));
  310. }
  311. }