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