Skip to content

Commit

Permalink
fix: return a fresh scanner instance
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Jun 17, 2024
1 parent ed3d6c7 commit 6250885
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@

use OC\Files\Filesystem;
use OC\Files\Storage\Wrapper\Jail;
use OCA\Files_Antivirus\AppConfig;
use OCA\Files_Antivirus\AvirWrapper;
use OCA\Files_Antivirus\Scanner\ExternalClam;
use OCA\Files_Antivirus\Scanner\ExternalKaspersky;
use OCA\Files_Antivirus\Scanner\ICAP;
use OCA\Files_Antivirus\Scanner\LocalClam;
use OCA\Files_Antivirus\Scanner\ScannerFactory;
use OCA\Files_Antivirus\StatusFactory;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\AppFramework\App;
Expand All @@ -21,8 +27,11 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IHomeStorage;
use OCP\Files\Storage\IStorage;
use OCP\Http\Client\IClientService;
use OCP\ICertificateManager;
use OCP\IL10N;
use OCP\Util;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class Application extends App implements IBootstrap {
Expand All @@ -33,6 +42,40 @@ public function __construct(array $urlParams = []) {
}

public function register(IRegistrationContext $context): void {
$context->registerService(ExternalClam::class, function (ContainerInterface $c) {
return new ExternalClam(
$c->get(AppConfig::class),
$c->get(LoggerInterface::class),
$c->get(StatusFactory::class),
);
}, false);

$context->registerService(LocalClam::class, function (ContainerInterface $c) {
return new LocalClam(
$c->get(AppConfig::class),
$c->get(LoggerInterface::class),
$c->get(StatusFactory::class),
);
}, false);

$context->registerService(ExternalKaspersky::class, function (ContainerInterface $c) {
return new ExternalKaspersky(
$c->get(AppConfig::class),
$c->get(LoggerInterface::class),
$c->get(StatusFactory::class),
$c->get(IClientService::class),
);
}, false);

$context->registerService(ICAP::class, function (ContainerInterface $c) {
return new ICAP(
$c->get(AppConfig::class),
$c->get(LoggerInterface::class),
$c->get(StatusFactory::class),
$c->get(ICertificateManager::class),
);
}, false);

Util::connectHook('OC_Filesystem', 'preSetup', $this, 'setupWrapper');
}

Expand Down
51 changes: 51 additions & 0 deletions tests/Scanner/ScannerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Scanner;

use OCA\Files_Antivirus\AppConfig;
use OCA\Files_Antivirus\Scanner\ScannerFactory;
use OCP\IConfig;
use OCP\IRequest;
use Test\TestCase;

/**
* @group DB
*/
class ScannerFactoryTest extends TestCase {
private IConfig $config;
private AppConfig $appConfig;
private IRequest $request;
private ScannerFactory $scannerFactory;

public function setUp(): void {
$this->config = $this->createMock(IConfig::class);
$this->config->method('getAppValue')
->with('files_antivirus', 'av_mode', 'executable')
->willReturn('daemon');

$this->appConfig = new AppConfig($this->config);

$this->request = $this->createMock(IRequest::class);

$this->scannerFactory = new ScannerFactory(
$this->appConfig,
\OC::$server,
$this->request,
);
}

public function testGetScanner() {
$instanceA = $this->scannerFactory->getScanner('/dev/null');
$instanceB = $this->scannerFactory->getScanner('/dev/null');

$this->assertNotSame($instanceA, $instanceB);
}

}

0 comments on commit 6250885

Please sign in to comment.