aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Files/Cache/UpdaterTest.php
blob: c6a3c4a3491e88abf020d64a967aac145be97531 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
 * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test\Files\Cache;

use OC\Files\Filesystem;
use OC\Files\Storage\Temporary;

/**
 * Class UpdaterTest
 *
 * @group DB
 *
 * @package Test\Files\Cache
 */
class UpdaterTest extends \Test\TestCase {
	/**
	 * @var \OC\Files\Storage\Storage
	 */
	protected $storage;

	/**
	 * @var \OC\Files\Cache\Cache
	 */
	protected $cache;

	/**
	 * @var \OC\Files\View
	 */
	protected $view;

	/**
	 * @var \OC\Files\Cache\Updater
	 */
	protected $updater;

	protected function setUp(): void {
		parent::setUp();

		$this->loginAsUser();

		$this->storage = new Temporary(array());
		$this->updater = $this->storage->getUpdater();
		$this->cache = $this->storage->getCache();
	}

	protected function tearDown(): void {
		Filesystem::clearMounts();

		$this->logout();
		parent::tearDown();
	}

	public function testNewFile() {
		$this->storage->file_put_contents('foo.txt', 'bar');
		$this->assertFalse($this->cache->inCache('foo.txt'));

		$this->updater->update('foo.txt');

		$this->assertTrue($this->cache->inCache('foo.txt'));
		$cached = $this->cache->get('foo.txt');
		$this->assertEquals(3, $cached['size']);
		$this->assertEquals('text/plain', $cached['mimetype']);
	}

	public function testUpdatedFile() {
		$this->storage->file_put_contents('foo.txt', 'bar');
		$this->updater->update('foo.txt');

		$cached = $this->cache->get('foo.txt');
		$this->assertEquals(3, $cached['size']);
		$this->assertEquals('text/plain', $cached['mimetype']);

		$this->storage->file_put_contents('foo.txt', 'qwerty');

		$cached = $this->cache->get('foo.txt');
		$this->assertEquals(3, $cached['size']);

		$this->updater->update('/foo.txt');

		$cached = $this->cache->get('foo.txt');
		$this->assertEquals(6, $cached['size']);
	}

	public function testParentSize() {
		$this->storage->getScanner()->scan('');

		$parentCached = $this->cache->get('');
		$this->assertEquals(0, $parentCached['size']);

		$this->storage->file_put_contents('foo.txt', 'bar');

		$parentCached = $this->cache->get('');
		$this->assertEquals(0, $parentCached['size']);

		$this->updater->update('foo.txt');

		$parentCached = $this->cache->get('');
		$this->assertEquals(3, $parentCached['size']);

		$this->storage->file_put_contents('foo.txt', 'qwerty');

		$parentCached = $this->cache->get('');
		$this->assertEquals(3, $parentCached['size']);

		$this->updater->update('foo.txt');

		$parentCached = $this->cache->get('');
		$this->assertEquals(6, $parentCached['size']);

		$this->storage->unlink('foo.txt');

		$parentCached = $this->cache->get('');
		$this->assertEquals(6, $parentCached['size']);

		$this->updater->remove('foo.txt');

		$parentCached = $this->cache->get('');
		$this->assertEquals(0, $parentCached['size']);
	}

	public function testMove() {
		$this->storage->file_put_contents('foo.txt', 'qwerty');
		$this->updater->update('foo.txt');

		$this->assertTrue($this->cache->inCache('foo.txt'));
		$this->assertFalse($this->cache->inCache('bar.txt'));
		$cached = $this->cache->get('foo.txt');

		$this->storage->rename('foo.txt', 'bar.txt');

		$this->assertTrue($this->cache->inCache('foo.txt'));
		$this->assertFalse($this->cache->inCache('bar.txt'));

		$this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');

		$this->assertFalse($this->cache->inCache('foo.txt'));
		$this->assertTrue($this->cache->inCache('bar.txt'));

		$cachedTarget = $this->cache->get('bar.txt');
		$this->assertEquals($cached['etag'], $cachedTarget['etag']);
		$this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
		$this->assertEquals($cached['size'], $cachedTarget['size']);
		$this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
	}

	public function testMoveNonExistingOverwrite() {
		$this->storage->file_put_contents('bar.txt', 'qwerty');
		$this->updater->update('bar.txt');

		$cached = $this->cache->get('bar.txt');

		$this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');

		$this->assertFalse($this->cache->inCache('foo.txt'));
		$this->assertTrue($this->cache->inCache('bar.txt'));

		$cachedTarget = $this->cache->get('bar.txt');
		$this->assertEquals($cached['etag'], $cachedTarget['etag']);
		$this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
		$this->assertEquals($cached['size'], $cachedTarget['size']);
		$this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
	}

	public function testUpdateStorageMTime() {
		$this->storage->mkdir('sub');
		$this->storage->mkdir('sub2');
		$this->storage->file_put_contents('sub/foo.txt', 'qwerty');

		$this->updater->update('sub');
		$this->updater->update('sub/foo.txt');
		$this->updater->update('sub2');

		$cachedSourceParent = $this->cache->get('sub');
		$cachedSource = $this->cache->get('sub/foo.txt');

		$this->storage->rename('sub/foo.txt', 'sub2/bar.txt');

		// simulate storage having a different mtime
		$testmtime = 1433323578;

		// source storage mtime change
		$this->storage->touch('sub', $testmtime);

		// target storage mtime change
		$this->storage->touch('sub2', $testmtime);
		// some storages (like Dropbox) change storage mtime on rename
		$this->storage->touch('sub2/bar.txt', $testmtime);

		$this->updater->renameFromStorage($this->storage, 'sub/foo.txt', 'sub2/bar.txt');

		$cachedTargetParent = $this->cache->get('sub2');
		$cachedTarget = $this->cache->get('sub2/bar.txt');

		$this->assertEquals($cachedSource['mtime'], $cachedTarget['mtime'], 'file mtime preserved');

		$this->assertNotEquals($cachedTarget['storage_mtime'], $cachedTarget['mtime'], 'mtime is not storage_mtime for moved file');

		$this->assertEquals($testmtime, $cachedTarget['storage_mtime'], 'target file storage_mtime propagated');
		$this->assertNotEquals($testmtime, $cachedTarget['mtime'], 'target file mtime changed, not from storage');

		$this->assertEquals($testmtime, $cachedTargetParent['storage_mtime'], 'target parent storage_mtime propagated');
		$this->assertNotEquals($testmtime, $cachedTargetParent['mtime'], 'target folder mtime changed, not from storage');
	}

	public function testNewFileDisabled() {
		$this->storage->file_put_contents('foo.txt', 'bar');
		$this->assertFalse($this->cache->inCache('foo.txt'));

		$this->updater->disable();
		$this->updater->update('/foo.txt');

		$this->assertFalse($this->cache->inCache('foo.txt'));
	}

	public function testMoveCrossStorage() {
		$storage2 = new Temporary(array());
		$cache2 = $storage2->getCache();
		Filesystem::mount($storage2, array(), '/bar');
		$this->storage->file_put_contents('foo.txt', 'qwerty');

		$this->updater->update('foo.txt');

		$this->assertTrue($this->cache->inCache('foo.txt'));
		$this->assertFalse($cache2->inCache('bar.txt'));
		$cached = $this->cache->get('foo.txt');

		// "rename"
		$storage2->file_put_contents('bar.txt', 'qwerty');
		$this->storage->unlink('foo.txt');

		$this->assertTrue($this->cache->inCache('foo.txt'));
		$this->assertFalse($cache2->inCache('bar.txt'));

		$storage2->getUpdater()->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');

		$this->assertFalse($this->cache->inCache('foo.txt'));
		$this->assertTrue($cache2->inCache('bar.txt'));

		$cachedTarget = $cache2->get('bar.txt');
		$this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
		$this->assertEquals($cached['size'], $cachedTarget['size']);
		$this->assertEquals($cached['etag'], $cachedTarget['etag']);
		$this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
	}

	public function testMoveFolderCrossStorage() {
		$storage2 = new Temporary(array());
		$cache2 = $storage2->getCache();
		Filesystem::mount($storage2, array(), '/bar');
		$this->storage->mkdir('foo');
		$this->storage->mkdir('foo/bar');
		$this->storage->file_put_contents('foo/foo.txt', 'qwerty');
		$this->storage->file_put_contents('foo/bar.txt', 'foo');
		$this->storage->file_put_contents('foo/bar/bar.txt', 'qwertyuiop');

		$this->storage->getScanner()->scan('');

		$this->assertTrue($this->cache->inCache('foo'));
		$this->assertTrue($this->cache->inCache('foo/foo.txt'));
		$this->assertTrue($this->cache->inCache('foo/bar.txt'));
		$this->assertTrue($this->cache->inCache('foo/bar'));
		$this->assertTrue($this->cache->inCache('foo/bar/bar.txt'));
		$cached = [];
		$cached[] = $this->cache->get('foo');
		$cached[] = $this->cache->get('foo/foo.txt');
		$cached[] = $this->cache->get('foo/bar.txt');
		$cached[] = $this->cache->get('foo/bar');
		$cached[] = $this->cache->get('foo/bar/bar.txt');

		// add extension to trigger the possible mimetype change
		$storage2->moveFromStorage($this->storage, 'foo', 'foo.b');
		$storage2->getUpdater()->renameFromStorage($this->storage, 'foo', 'foo.b');

		$this->assertFalse($this->cache->inCache('foo'));
		$this->assertFalse($this->cache->inCache('foo/foo.txt'));
		$this->assertFalse($this->cache->inCache('foo/bar.txt'));
		$this->assertFalse($this->cache->inCache('foo/bar'));
		$this->assertFalse($this->cache->inCache('foo/bar/bar.txt'));
		$this->assertTrue($cache2->inCache('foo.b'));
		$this->assertTrue($cache2->inCache('foo.b/foo.txt'));
		$this->assertTrue($cache2->inCache('foo.b/bar.txt'));
		$this->assertTrue($cache2->inCache('foo.b/bar'));
		$this->assertTrue($cache2->inCache('foo.b/bar/bar.txt'));

		$cachedTarget = [];
		$cachedTarget[] = $cache2->get('foo.b');
		$cachedTarget[] = $cache2->get('foo.b/foo.txt');
		$cachedTarget[] = $cache2->get('foo.b/bar.txt');
		$cachedTarget[] = $cache2->get('foo.b/bar');
		$cachedTarget[] = $cache2->get('foo.b/bar/bar.txt');

		foreach ($cached as $i => $old) {
			$new = $cachedTarget[$i];
			$this->assertEquals($old['mtime'], $new['mtime']);
			$this->assertEquals($old['size'], $new['size']);
			$this->assertEquals($old['etag'], $new['etag']);
			$this->assertEquals($old['fileid'], $new['fileid']);
			$this->assertEquals($old['mimetype'], $new['mimetype']);
		}
	}
}