repairlegacystorage.php 9.3 KB

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