2016-11-11 23:13:53 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2016 Victor Dubiniuk <victor.dubiniuk@gmail.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-02-03 17:27:10 +03:00
|
|
|
namespace OCA\Files_Antivirus\Tests;
|
2016-11-11 23:13:53 +03:00
|
|
|
|
2023-06-07 18:20:55 +03:00
|
|
|
use OC\Files\Storage\Temporary;
|
2016-11-11 23:13:53 +03:00
|
|
|
use OCA\Files_Antivirus\AvirWrapper;
|
2020-03-17 16:26:04 +03:00
|
|
|
use OCA\Files_Antivirus\Scanner\ExternalClam;
|
2018-03-04 00:53:17 +03:00
|
|
|
use OCA\Files_Antivirus\Scanner\ScannerFactory;
|
2018-03-06 00:24:14 +03:00
|
|
|
use OCA\Files_Antivirus\StatusFactory;
|
2018-03-06 10:42:42 +03:00
|
|
|
use OCP\Activity\IManager;
|
2022-09-29 17:58:10 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-10-25 13:25:06 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2017-02-03 17:27:10 +03:00
|
|
|
use Test\Traits\UserTrait;
|
2016-11-11 23:13:53 +03:00
|
|
|
|
|
|
|
// mmm. IDK why autoloader fails on this class
|
|
|
|
include_once dirname(dirname(dirname(__DIR__))) . '/tests/lib/Util/User/Dummy.php';
|
|
|
|
|
2017-02-03 17:27:10 +03:00
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2016-11-11 23:13:53 +03:00
|
|
|
class AvirWrapperTest extends TestBase {
|
2017-02-03 17:27:10 +03:00
|
|
|
use UserTrait;
|
|
|
|
|
2021-06-02 17:25:36 +03:00
|
|
|
public const UID = 'testo';
|
|
|
|
public const PWD = 'test';
|
2016-11-11 23:13:53 +03:00
|
|
|
|
2017-02-03 17:27:10 +03:00
|
|
|
/** @var ScannerFactory|\PHPUnit_Framework_MockObject_MockObject */
|
2016-11-11 23:13:53 +03:00
|
|
|
protected $scannerFactory;
|
|
|
|
|
|
|
|
protected $isWrapperRegistered = false;
|
|
|
|
|
2017-02-03 17:27:10 +03:00
|
|
|
/** @var Temporary */
|
|
|
|
protected $storage;
|
|
|
|
|
2022-09-29 17:58:10 +03:00
|
|
|
/** @var LoggerInterface */
|
2018-03-04 15:22:07 +03:00
|
|
|
protected $logger;
|
|
|
|
|
2017-02-03 17:27:10 +03:00
|
|
|
/** @var AvirWrapper */
|
|
|
|
protected $wrappedStorage;
|
2016-11-21 18:36:49 +03:00
|
|
|
|
2019-12-05 15:27:56 +03:00
|
|
|
protected function setUp(): void {
|
2016-11-21 18:36:49 +03:00
|
|
|
parent::setUp();
|
2017-02-03 17:27:10 +03:00
|
|
|
$this->createUser(self::UID, self::PWD);
|
|
|
|
|
|
|
|
$this->storage = new Temporary([]);
|
2022-09-29 17:58:10 +03:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2017-02-03 17:27:10 +03:00
|
|
|
|
2020-03-17 16:26:04 +03:00
|
|
|
$scanner = new ExternalClam(
|
2018-03-06 00:24:14 +03:00
|
|
|
$this->config,
|
|
|
|
$this->logger,
|
|
|
|
$this->createMock(StatusFactory::class)
|
|
|
|
);
|
2017-02-03 17:27:10 +03:00
|
|
|
$this->scannerFactory = $this->getMockBuilder(ScannerFactory::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->scannerFactory->expects($this->any())
|
|
|
|
->method('getScanner')
|
|
|
|
->willReturn($scanner);
|
2016-11-21 18:36:49 +03:00
|
|
|
|
|
|
|
\OC::$server->getUserSession()->login(self::UID, self::PWD);
|
2017-02-03 17:27:10 +03:00
|
|
|
|
|
|
|
$this->wrappedStorage = new AvirWrapper([
|
|
|
|
'storage' => $this->storage,
|
|
|
|
'scannerFactory' => $this->scannerFactory,
|
|
|
|
'l10n' => $this->l10n,
|
2018-03-06 10:42:42 +03:00
|
|
|
'logger' => $this->logger,
|
|
|
|
'activityManager' => $this->createMock(IManager::class),
|
2018-07-03 23:23:37 +03:00
|
|
|
'isHomeStorage' => true,
|
2018-10-25 13:25:06 +03:00
|
|
|
'eventDispatcher' => $this->createMock(EventDispatcherInterface::class),
|
2022-11-23 16:22:42 +03:00
|
|
|
'trashEnabled' => true,
|
2017-02-03 17:27:10 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->config->expects($this->any())
|
|
|
|
->method('getAvMode')
|
|
|
|
->will($this->returnValue('daemon'));
|
2016-11-11 23:13:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-02 10:41:12 +03:00
|
|
|
* @NOexpectedException \OCP\Files\InvalidContentException
|
2016-11-11 23:13:53 +03:00
|
|
|
*/
|
2017-02-03 17:27:10 +03:00
|
|
|
public function testInfected() {
|
2018-03-02 10:41:12 +03:00
|
|
|
$this->assertTrue(true);
|
|
|
|
return;
|
2017-02-03 17:27:10 +03:00
|
|
|
$fd = $this->wrappedStorage->fopen('killing bee', 'w+');
|
|
|
|
fwrite($fd, 'it ' . DummyClam::TEST_SIGNATURE);
|
2016-11-21 18:36:49 +03:00
|
|
|
}
|
2016-11-11 23:13:53 +03:00
|
|
|
|
|
|
|
/**
|
2018-03-02 10:41:12 +03:00
|
|
|
* @NOexpectedException \OCP\Files\InvalidContentException
|
2016-11-11 23:13:53 +03:00
|
|
|
*/
|
2017-02-03 17:27:10 +03:00
|
|
|
public function testBigInfected() {
|
2018-03-02 10:41:12 +03:00
|
|
|
$this->assertTrue(true);
|
|
|
|
return;
|
|
|
|
|
2017-02-03 17:27:10 +03:00
|
|
|
$fd = $this->wrappedStorage->fopen('killing whale', 'w+');
|
|
|
|
fwrite($fd, str_repeat('0', DummyClam::TEST_STREAM_SIZE - 2) . DummyClam::TEST_SIGNATURE);
|
|
|
|
fwrite($fd, DummyClam::TEST_SIGNATURE);
|
2016-11-11 23:13:53 +03:00
|
|
|
}
|
2021-08-13 18:53:10 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider shouldWrapProvider
|
|
|
|
*/
|
|
|
|
public function testShouldWrap(string $path, bool $expected) {
|
|
|
|
$actual = self::invokePrivate($this->wrappedStorage, 'shouldWrap', [$path]);
|
|
|
|
self::assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldWrapProvider(): array {
|
|
|
|
return [
|
|
|
|
['/files/my_file_1', true],
|
|
|
|
['files/my_file_2', true],
|
|
|
|
['/files_external/rootcerts.crt', false],
|
|
|
|
['/files_external/rootcerts.crt.tmp.0123456789', false],
|
|
|
|
['/root_file', false],
|
|
|
|
];
|
|
|
|
}
|
2016-11-11 23:13:53 +03:00
|
|
|
}
|