repairlegacystorage.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Repair;
  9. /**
  10. * Tests for the converting of legacy storages to home storages.
  11. *
  12. * @see \OC\Repair\RepairLegacyStorages
  13. */
  14. class RepairLegacyStorages extends \Test\TestCase {
  15. private $connection;
  16. private $config;
  17. private $user;
  18. private $repair;
  19. private $dataDir;
  20. private $oldDataDir;
  21. private $legacyStorageId;
  22. private $newStorageId;
  23. private $warnings;
  24. protected function setUp() {
  25. parent::setUp();
  26. $this->config = \OC::$server->getConfig();
  27. $this->connection = \OC_DB::getConnection();
  28. $this->oldDataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
  29. $this->repair = new \OC\Repair\RepairLegacyStorages($this->config, $this->connection);
  30. $this->warnings = [];
  31. $this->repair->listen('\OC\Repair', 'warning', function ($description){
  32. $this->warnings[] = $description;
  33. });
  34. }
  35. protected function tearDown() {
  36. \OC_User::deleteUser($this->user);
  37. $sql = 'DELETE FROM `*PREFIX*storages`';
  38. $this->connection->executeQuery($sql);
  39. $sql = 'DELETE FROM `*PREFIX*filecache`';
  40. $this->connection->executeQuery($sql);
  41. \OCP\Config::setSystemValue('datadirectory', $this->oldDataDir);
  42. $this->config->setAppValue('core', 'repairlegacystoragesdone', 'no');
  43. parent::tearDown();
  44. }
  45. function prepareSettings($dataDir, $userId) {
  46. // hard-coded string as we want a predictable fixed length
  47. // no data will be written there
  48. $this->dataDir = $dataDir;
  49. \OCP\Config::setSystemValue('datadirectory', $this->dataDir);
  50. $this->user = $userId;
  51. $this->legacyStorageId = 'local::' . $this->dataDir . $this->user . '/';
  52. $this->newStorageId = 'home::' . $this->user;
  53. \OC_User::createUser($this->user, $this->user);
  54. }
  55. /**
  56. * Create a storage entry
  57. *
  58. * @param string $storageId
  59. */
  60. private function createStorage($storageId) {
  61. $sql = 'INSERT INTO `*PREFIX*storages` (`id`)'
  62. . ' VALUES (?)';
  63. $storageId = \OC\Files\Cache\Storage::adjustStorageId($storageId);
  64. $numRows = $this->connection->executeUpdate($sql, array($storageId));
  65. $this->assertEquals(1, $numRows);
  66. return \OC_DB::insertid('*PREFIX*storages');
  67. }
  68. /**
  69. * Returns the storage id based on the numeric id
  70. *
  71. * @param int $numericId numeric id of the storage
  72. * @return string storage id or null if not found
  73. */
  74. private function getStorageId($storageId) {
  75. $numericId = \OC\Files\Cache\Storage::getNumericStorageId($storageId);
  76. if (!is_null($numericId)) {
  77. return (int)$numericId;
  78. }
  79. return null;
  80. }
  81. /**
  82. * Create dummy data in the filecache for the given storage numeric id
  83. *
  84. * @param string $storageId storage id
  85. */
  86. private function createData($storageId) {
  87. $cache = new \OC\Files\Cache\Cache($storageId);
  88. $cache->put(
  89. 'dummyfile.txt',
  90. array('size' => 5, 'mtime' => 12, 'mimetype' => 'text/plain')
  91. );
  92. }
  93. /**
  94. * Test that existing home storages are left alone when valid.
  95. * @dataProvider settingsProvider
  96. */
  97. public function testNoopWithExistingHomeStorage($dataDir, $userId) {
  98. $this->prepareSettings($dataDir, $userId);
  99. $newStorageNumId = $this->createStorage($this->newStorageId);
  100. $this->repair->run();
  101. $this->assertNull($this->getStorageId($this->legacyStorageId));
  102. $this->assertEquals($newStorageNumId, $this->getStorageId($this->newStorageId));
  103. }
  104. /**
  105. * Test that legacy storages are converted to home storages when
  106. * the latter does not exist.
  107. * @dataProvider settingsProvider
  108. */
  109. public function testConvertLegacyToHomeStorage($dataDir, $userId) {
  110. $this->prepareSettings($dataDir, $userId);
  111. $legacyStorageNumId = $this->createStorage($this->legacyStorageId);
  112. $this->repair->run();
  113. $this->assertNull($this->getStorageId($this->legacyStorageId));
  114. $this->assertEquals($legacyStorageNumId, $this->getStorageId($this->newStorageId));
  115. }
  116. /**
  117. * Test that legacy storages are converted to home storages
  118. * when home storage already exists but has no data.
  119. * @dataProvider settingsProvider
  120. */
  121. public function testConvertLegacyToExistingEmptyHomeStorage($dataDir, $userId) {
  122. $this->prepareSettings($dataDir, $userId);
  123. $legacyStorageNumId = $this->createStorage($this->legacyStorageId);
  124. $newStorageNumId = $this->createStorage($this->newStorageId);
  125. $this->createData($this->legacyStorageId);
  126. $this->repair->run();
  127. $this->assertNull($this->getStorageId($this->legacyStorageId));
  128. $this->assertEquals($legacyStorageNumId, $this->getStorageId($this->newStorageId));
  129. }
  130. /**
  131. * Test that legacy storages are converted to home storages
  132. * when home storage already exists and the legacy storage
  133. * has no data.
  134. * @dataProvider settingsProvider
  135. */
  136. public function testConvertEmptyLegacyToHomeStorage($dataDir, $userId) {
  137. $this->prepareSettings($dataDir, $userId);
  138. $legacyStorageNumId = $this->createStorage($this->legacyStorageId);
  139. $newStorageNumId = $this->createStorage($this->newStorageId);
  140. $this->createData($this->newStorageId);
  141. $this->repair->run();
  142. $this->assertNull($this->getStorageId($this->legacyStorageId));
  143. $this->assertEquals($newStorageNumId, $this->getStorageId($this->newStorageId));
  144. }
  145. /**
  146. * Test that nothing is done when both conflicting legacy
  147. * and home storage have data.
  148. * @dataProvider settingsProvider
  149. */
  150. public function testConflictNoop($dataDir, $userId) {
  151. $this->prepareSettings($dataDir, $userId);
  152. $legacyStorageNumId = $this->createStorage($this->legacyStorageId);
  153. $newStorageNumId = $this->createStorage($this->newStorageId);
  154. $this->createData($this->legacyStorageId);
  155. $this->createData($this->newStorageId);
  156. $this->repair->run();
  157. $this->assertEquals(2, count($this->warnings));
  158. $this->assertEquals('Could not repair legacy storage ', substr(current($this->warnings), 0, 32));
  159. // storages left alone
  160. $this->assertEquals($legacyStorageNumId, $this->getStorageId($this->legacyStorageId));
  161. $this->assertEquals($newStorageNumId, $this->getStorageId($this->newStorageId));
  162. // do not set the done flag
  163. $this->assertNotEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
  164. }
  165. /**
  166. * Test that the data dir local entry is left alone
  167. * @dataProvider settingsProvider
  168. */
  169. public function testDataDirEntryNoop($dataDir, $userId) {
  170. $this->prepareSettings($dataDir, $userId);
  171. $storageId = 'local::' . $this->dataDir;
  172. $numId = $this->createStorage($storageId);
  173. $this->repair->run();
  174. $this->assertEquals($numId, $this->getStorageId($storageId));
  175. }
  176. /**
  177. * Test that external local storages are left alone
  178. * @dataProvider settingsProvider
  179. */
  180. public function testLocalExtStorageNoop($dataDir, $userId) {
  181. $this->prepareSettings($dataDir, $userId);
  182. $storageId = 'local::/tmp/somedir/' . $this->user;
  183. $numId = $this->createStorage($storageId);
  184. $this->repair->run();
  185. $this->assertEquals($numId, $this->getStorageId($storageId));
  186. }
  187. /**
  188. * Test that other external storages are left alone
  189. * @dataProvider settingsProvider
  190. */
  191. public function testExtStorageNoop($dataDir, $userId) {
  192. $this->prepareSettings($dataDir, $userId);
  193. $storageId = 'smb::user@password/tmp/somedir/' . $this->user;
  194. $numId = $this->createStorage($storageId);
  195. $this->repair->run();
  196. $this->assertEquals($numId, $this->getStorageId($storageId));
  197. }
  198. /**
  199. * Provides data dir and user name
  200. */
  201. function settingsProvider() {
  202. return array(
  203. // regular data dir
  204. array(
  205. '/tmp/oc-autotest/datadir/',
  206. $this->getUniqueID('user_'),
  207. ),
  208. // long datadir / short user
  209. array(
  210. '/tmp/oc-autotest/datadir01234567890123456789012345678901234567890123456789END/',
  211. $this->getUniqueID('user_'),
  212. ),
  213. // short datadir / long user
  214. array(
  215. '/tmp/oc-autotest/datadir/',
  216. 'u123456789012345678901234567890123456789012345678901234567890END', // 64 chars
  217. ),
  218. );
  219. }
  220. /**
  221. * Only run the repair once
  222. */
  223. public function testOnlyRunOnce() {
  224. $output = array();
  225. $this->repair->listen('\OC\Repair', 'info', function ($description) use (&$output) {
  226. $output[] = 'info: ' . $description;
  227. });
  228. $this->prepareSettings('/tmp/oc-autotest/datadir', $this->getUniqueID('user_'));
  229. $this->assertNotEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
  230. $this->repair->run();
  231. $this->assertEquals(1, count($output));
  232. $this->assertEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
  233. $output = array();
  234. $this->repair->run();
  235. // no output which means it did not run
  236. $this->assertEquals(0, count($output));
  237. $this->assertEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
  238. }
  239. }