Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude file name patterns; ignore gentoo webapp files #14198

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/**
* Class ExcludeFileByNameFilterIterator provides a custom iterator which excludes
* entries with the specified file name from the file list.
* entries with the specified file name from the file list. These file names are matched exactly.
*
* @package OC\Integritycheck\Iterator
*/
Expand All @@ -42,21 +42,42 @@ class ExcludeFileByNameFilterIterator extends \RecursiveFilterIterator {
'.DS_Store', // Mac OS X
'Thumbs.db', // Microsoft Windows
'.directory', // Dolphin (KDE)
'.webapp', // Gentoo/Funtoo & derivatives use a tool known as webapp-config to manager wep-apps.
'.webapp', // Gentoo/Funtoo & derivatives use a tool known as webapp-config to manage web-apps.
];

/**
* Array of excluded file name parts. Those are not scanned by the integrity checker.
* These strings are regular expressions and any file names
* matching these expressions are ignored.
*
* @var array
*/
private $excludedFilenamePatterns = [
'/^\.webapp-nextcloud-(\d+\.){2}(\d+)(-r\d+)?$/', // Gentoo/Funtoo & derivatives use a tool known as webapp-config to manage wep-apps.
];

/**
* @return bool
*/
public function accept() {
if($this->isDir()) {
/** @var \SplFileInfo $current */
$current = $this->current();

if ($current->isDir()) {
return true;
}

return !\in_array(
$this->current()->getFilename(),
$this->excludedFilenames,
true
);
$currentFileName = $current->getFilename();
if (in_array($currentFileName, $this->excludedFilenames, true)){
return false;
}

foreach ($this->excludedFilenamePatterns as $pattern){
if (preg_match($pattern, $currentFileName) > 0){
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2015, 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\IntegrityCheck\Iterator;

use \OC\IntegrityCheck\Iterator\ExcludeFileByNameFilterIterator;
use Test\TestCase;

class ExcludeFileByNameFilterIteratorTest extends TestCase {
/** @var ExcludeFileByNameFilterIterator|\PHPUnit\Framework\MockObject\MockObject */
protected $filter;

public function setUp() {
parent::setUp();
$this->filter = $this->getMockBuilder(ExcludeFileByNameFilterIterator::class)
->disableOriginalConstructor()
->setMethods(['current'])
->getMock();
}

public function fileNameProvider(): array {
return [
['a file', true],
['Thumbs.db', false],
['another file', true],
['.directory', false],
['.webapp-nextcloud-15.0.2', false],
['.webapp-nextcloud-14.0.5-r3', false],
['wx.webapp-nextcloud-obee', true],
];
}

/**
* @dataProvider fileNameProvider
* @param string $fileName
* @param bool $expectedResult
*/
public function testAcceptForFiles($fileName, $expectedResult): void {
$iteratorMock = $this->getMockBuilder(\RecursiveDirectoryIterator::class)
->disableOriginalConstructor()
->setMethods(['getFilename', 'isDir'])
->getMock();

$iteratorMock->method('getFilename')
->willReturn($fileName);
$iteratorMock->method('isDir')
->willReturn(false);
$this->filter->method('current')
->willReturn($iteratorMock);

$actualResult = $this->filter->accept();
$this->assertEquals($expectedResult, $actualResult);
}

/**
* @dataProvider fileNameProvider
* @param string $fileName
* @param bool $expectedResult
*/
public function testAcceptForDirs($fileName, $expectedResult): void {
$iteratorMock = $this->getMockBuilder(\RecursiveDirectoryIterator::class)
->disableOriginalConstructor()
->setMethods(['getFilename', 'isDir'])
->getMock();

$iteratorMock->method('getFilename')
->willReturn($fileName);
$iteratorMock->method('isDir')
->willReturn(true);
$this->filter->method('current')
->willReturn($iteratorMock);

$actualResult = $this->filter->accept();
$this->assertTrue($actualResult);
}
}