summaryrefslogtreecommitdiffstats
path: root/tests/lib/Repair/RepairUnmergedSharesTest.php
blob: 2834cfe94b81cf1a64054773dcd0a56d2936794b (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
/**
 * @author Vincent Petry <pvince81@owncloud.com>
 *
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace Test\Repair;


use OC\Repair\RepairUnmergedShares;
use OC\Share\Constants;
use OCP\IUser;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use Test\TestCase;
use OC\Share20\DefaultShareProvider;
use OCP\IUserManager;
use OCP\IGroupManager;

/**
 * Tests for repairing invalid shares
 *
 * @group DB
 *
 * @see \OC\Repair\RepairUnmergedShares
 */
class RepairUnmergedSharesTest extends TestCase {

	/** @var IRepairStep */
	private $repair;

	/** @var \OCP\IDBConnection */
	private $connection;

	/** @var int */
	private $lastShareTime;

	/** @var IUserManager */
	private $userManager;

	/** @var IGroupManager */
	private $groupManager;

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

		$config = $this->getMockBuilder('OCP\IConfig')
			->disableOriginalConstructor()
			->getMock();
		$config->expects($this->any())
			->method('getSystemValue')
			->with('version')
			->will($this->returnValue('9.0.3.0'));

		$this->connection = \OC::$server->getDatabaseConnection();
		$this->deleteAllShares();

		$this->userManager = $this->createMock(IUserManager::class);
		$this->groupManager = $this->createMock(IGroupManager::class);

		// used to generate incremental stimes
		$this->lastShareTime = time();

		/** @var \OCP\IConfig $config */
		$this->repair = new RepairUnmergedShares($config, $this->connection, $this->userManager, $this->groupManager);
	}

	protected function tearDown() {
		$this->deleteAllShares();

		parent::tearDown();
	}

	protected function deleteAllShares() {
		$qb = $this->connection->getQueryBuilder();
		$qb->delete('share')->execute();
	}

	private function createShare($type, $sourceId, $recipient, $targetName, $permissions, $parentId = null) {
		$this->lastShareTime += 100;
		$qb = $this->connection->getQueryBuilder();
		$values = [
			'share_type' => $qb->expr()->literal($type),
			'share_with' => $qb->expr()->literal($recipient),
			'uid_owner' => $qb->expr()->literal('user1'),
			'item_type' => $qb->expr()->literal('folder'),
			'item_source' => $qb->expr()->literal($sourceId),
			'item_target' => $qb->expr()->literal('/' . $sourceId),
			'file_source' => $qb->expr()->literal($sourceId),
			'file_target' => $qb->expr()->literal($targetName),
			'permissions' => $qb->expr()->literal($permissions),
			'stime' => $qb->expr()->literal($this->lastShareTime),
		];
		if ($parentId !== null) {
			$values['parent'] = $qb->expr()->literal($parentId);
		}
		$qb->insert('share')
			->values($values)
			->execute();

		return $this->connection->lastInsertId('*PREFIX*share');
	}

	private function getShareById($id) {
		$query = $this->connection->getQueryBuilder();
		$results = $query
			->select('*')
			->from('share')
			->where($query->expr()->eq('id', $query->expr()->literal($id)))
			->execute()
			->fetchAll();

		if (!empty($results)) {
			return $results[0];
		}
		return null;
	}

	public function sharesDataProvider() {
		/**
		 * For all these test cases we have the following situation:
		 *
		 * - "user1" is the share owner
		 * - "user2" is the recipient, and member of "recipientgroup1" and "recipientgroup2"
		 * - "user1" is member of "samegroup1", "samegroup2" for same group tests
		 */
		return [
			[
				// #0 legitimate share:
				// - outsider shares with group1, group2
				// - recipient renamed, resulting in subshares
				// - one subshare for each group share
				// - targets of subshare all match
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 31],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test renamed', 31, 0],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test renamed', 31, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 31],
					// leave them alone
					['/test renamed', 31],
					['/test renamed', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #1 broken share:
				// - outsider shares with group1, group2
				// - only one subshare for two group shares
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 31],
					// child of the previous one
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (2)', 31, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 31],
					['/test', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #2 bogus share
				// - outsider shares with group1, group2
				// - one subshare for each group share, both with parenthesis
				// - but the targets do not match when grouped
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 31],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (2)', 31, 0],
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (3)', 31, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 31],
					// reset to original name as the sub-names have parenthesis
					['/test', 31],
					['/test', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #3 bogus share
				// - outsider shares with group1, group2
				// - one subshare for each group share, both renamed manually
				// - but the targets do not match when grouped
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 31],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test_renamed (1 legit paren)', 31, 0],
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test_renamed (2 legit paren)', 31, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 31],
					// reset to less recent subshare name
					['/test_renamed (2 legit paren)', 31],
					['/test_renamed (2 legit paren)', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #4 bogus share
				// - outsider shares with group1, group2
				// - one subshare for each group share, one with parenthesis
				// - but the targets do not match when grouped
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 31],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (2)', 31, 0],
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test_renamed', 31, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 31],
					// reset to less recent subshare name but without parenthesis
					['/test_renamed', 31],
					['/test_renamed', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #5 bogus share
				// - outsider shares with group1, group2
				// - one subshare for each group share
				// - first subshare not renamed (as in real world scenario)
				// - but the targets do not match when grouped
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 31],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test', 31, 0],
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (2)', 31, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 31],
					// reset to original name
					['/test', 31],
					['/test', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #6 bogus share:
				// - outsider shares with group1, group2
				// - one subshare for each group share
				// - non-matching targets
				// - recipient deletes one duplicate (unshare from self, permissions 0)
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 15],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (2)', 0, 0],
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (3)', 15, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 15],
					// subshares repaired and permissions restored to the max allowed
					['/test', 31],
					['/test', 15],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #7 bogus share:
				// - outsider shares with group1, group2
				// - one subshare for each group share
				// - non-matching targets
				// - recipient deletes ALL duplicates (unshare from self, permissions 0)
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 15],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (2)', 0, 0],
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (3)', 0, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					['/test', 31],
					['/test', 15],
					// subshares target repaired but left "deleted" as it was the user's choice
					['/test', 0],
					['/test', 0],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #8 bogus share:
				// - outsider shares with group1, group2 and also user2
				// - one subshare for each group share
				// - one extra share entry for direct share to user2
				// - non-matching targets
				// - user share has more permissions
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 1],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 15],
					// child of the previous ones
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (2)', 1, 0],
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/test (3)', 15, 1],
					[Constants::SHARE_TYPE_USER, 123, 'user2', '/test (4)', 31],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (5)', 31],
				],
				[
					['/test', 1],
					['/test', 15],
					// subshares repaired
					['/test', 1],
					['/test', 15],
					['/test', 31],
					// leave unrelated alone
					['/test (5)', 31],
				]
			],
			[
				// #9 bogus share:
				// - outsider shares with group1 and also user2
				// - no subshare at all
				// - one extra share entry for direct share to user2
				// - non-matching targets
				// - user share has more permissions
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 1],
					[Constants::SHARE_TYPE_USER, 123, 'user2', '/test (2)', 31],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (5)', 31],
				],
				[
					['/test', 1],
					// user share repaired
					['/test', 31],
					// leave unrelated alone
					['/test (5)', 31],
				]
			],
			[
				// #10 legitimate share with own group:
				// - insider shares with both groups the user is already in
				// - no subshares in this case
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'samegroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'samegroup2', '/test', 31],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					// leave all alone
					['/test', 31],
					['/test', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #11 legitimate shares:
				// - group share with same group
				// - group share with other group
				// - user share where recipient renamed
				// - user share where recipient did not rename
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'samegroup1', '/test', 31],
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					[Constants::SHARE_TYPE_USER, 123, 'user3', '/test legit rename', 31],
					[Constants::SHARE_TYPE_USER, 123, 'user4', '/test', 31],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					// leave all alone
					['/test', 31],
					['/test', 31],
					['/test legit rename', 31],
					['/test', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #12 legitimate share:
				// - outsider shares with group and user directly with different permissions
				// - no subshares
				// - same targets
				[
					[Constants::SHARE_TYPE_GROUP, 123, 'samegroup1', '/test', 1],
					[Constants::SHARE_TYPE_USER, 123, 'user3', '/test', 31],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (4)', 31],
				],
				[
					// leave all alone
					['/test', 1],
					['/test', 31],
					// leave unrelated alone
					['/test (4)', 31],
				]
			],
			[
				// #13 bogus share:
				// - outsider shares with group1, user2 and then group2
				// - user renamed share as soon as it arrived before the next share (order)
				// - one subshare for each group share
				// - one extra share entry for direct share to user2
				// - non-matching targets
				[
					// first share with group
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup1', '/test', 31],
					// recipient renames
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/first', 31, 0],
					// then direct share, user renames too
					[Constants::SHARE_TYPE_USER, 123, 'user2', '/second', 31],
					// another share with the second group
					[Constants::SHARE_TYPE_GROUP, 123, 'recipientgroup2', '/test', 31],
					// use renames it
					[DefaultShareProvider::SHARE_TYPE_USERGROUP, 123, 'user2', '/third', 31, 1],
					// different unrelated share
					[Constants::SHARE_TYPE_GROUP, 456, 'recipientgroup1', '/test (5)', 31],
				],
				[
					// group share with group1 left alone
					['/test', 31],
					// first subshare repaired
					['/third', 31],
					// direct user share repaired
					['/third', 31],
					// group share with group2 left alone
					['/test', 31],
					// second subshare repaired
					['/third', 31],
					// leave unrelated alone
					['/test (5)', 31],
				]
			],
		];
	}

	/**
	 * Test merge shares from group shares
	 *
	 * @dataProvider sharesDataProvider
	 */
	public function testMergeGroupShares($shares, $expectedShares) {
		$user1 = $this->createMock(IUser::class);
		$user1->expects($this->any())
			->method('getUID')
			->will($this->returnValue('user1'));

		$user2 = $this->createMock(IUser::class);
		$user2->expects($this->any())
			->method('getUID')
			->will($this->returnValue('user2'));

		$users = [$user1, $user2];

		$this->groupManager->expects($this->any())
			->method('getUserGroupIds')
			->will($this->returnValueMap([
				// owner
				[$user1, ['samegroup1', 'samegroup2']],
				// recipient
				[$user2, ['recipientgroup1', 'recipientgroup2']],
			]));

		$this->userManager->expects($this->once())
			->method('countUsers')
			->will($this->returnValue([2]));
		$this->userManager->expects($this->once())
			->method('callForAllUsers')
			->will($this->returnCallback(function(\Closure $closure) use ($users) {
				foreach ($users as $user) {
					$closure($user);
				}
			}));

		$shareIds = [];

		foreach ($shares as $share) {
			// if parent
			if (isset($share[5])) {
				// adjust to real id
				$share[5] = $shareIds[$share[5]];
			} else {
				$share[5] = null;
			}
			$shareIds[] = $this->createShare($share[0], $share[1], $share[2], $share[3], $share[4], $share[5]);
		}

		/** @var IOutput | \PHPUnit_Framework_MockObject_MockObject $outputMock */
		$outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
			->disableOriginalConstructor()
			->getMock();

		$this->repair->run($outputMock);

		foreach ($expectedShares as $index => $expectedShare) {
			$share = $this->getShareById($shareIds[$index]);
			$this->assertEquals($expectedShare[0], $share['file_target']);
			$this->assertEquals($expectedShare[1], $share['permissions']);
		}
	}

	public function duplicateNamesProvider() {
		return [
			// matching
			['filename (1).txt', true],
			['folder (2)', true],
			['filename (1)(2).txt', true],
			// non-matching
			['filename ().txt', false],
			['folder ()', false],
			['folder (1x)', false],
			['folder (x1)', false],
			['filename (a)', false],
			['filename (1).', false],
			['filename (1).txt.txt', false],
			['filename (1)..txt', false],
		];
	}

	/**
	 * @dataProvider duplicateNamesProvider
	 */
	public function testIsPotentialDuplicateName($name, $expectedResult) {
		$this->assertEquals($expectedResult, $this->invokePrivate($this->repair, 'isPotentialDuplicateName', [$name]));
	}
}