Commit 016a9968 authored by Predhumeau Manon's avatar Predhumeau Manon Committed by Guillaume Perréal
Browse files

Corrections in code and add tests

parent 48b42b9a
No related merge requests found
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
Showing with 65 additions and 42 deletions
+65 -42
...@@ -28,17 +28,6 @@ class VirusScannerListener ...@@ -28,17 +28,6 @@ class VirusScannerListener
public function __construct(Client $client) public function __construct(Client $client)
{ {
$this->client = $client; $this->client = $client;
try {
// Check clamd server's state
$this->client->ping();
// Reload the virus database
$this->client->reload();
} catch (ConnectionException $connectionException) {
$this->client->shutdown();
$this->client = null;
}
} }
...@@ -47,27 +36,42 @@ class VirusScannerListener ...@@ -47,27 +36,42 @@ class VirusScannerListener
*/ */
public function onFileUploadCompleted(FileUploadCompleteEvent $event) public function onFileUploadCompleted(FileUploadCompleteEvent $event)
{ {
if ($this->client != null) { $this->client = $this->getClient();
$file = $event->getUploadedFile();
$path = $file->getLocalPath(); $file = $event->getUploadedFile();
$path = $file->getLocalPath();
$result = $this->client->scanFile($path);
$hasVirus = $result['status'] === Client::RESULT_FOUND;
$meta = $file->getMetadata();
$meta['virus_scanner'] = ['has_virus' => $hasVirus];
$result = $this->client->scanFile($path); if ($hasVirus) {
$hasVirus = $result['status'] == Client::RESULT_FOUND; $desc = $result['reason'];
$meta['virus_scanner']['detected'] = $result['reason'];
}
$meta = $file->getMetadata(); $file->setMetadata($meta);
$meta['virus_scanner'] = ['has_virus' => $hasVirus];
if ($hasVirus) { if ($hasVirus) {
$desc = $result['reason']; throw new RejectedFileException($file, $desc ? sprintf('Found the %s virus !', $desc) : 'Virus found !');
$meta['virus_scanner']['detected'] = $result['reason']; }
} }
$file->setMetadata($meta); /**
* @return Client
* @throws ConnectionException
*/
private function getClient(): Client
{
try {
// Check clamd server's state
$this->client->ping();
return $this->client;
if ($hasVirus) { } catch (ConnectionException $connectionException) {
throw new RejectedFileException($file, $desc ? sprintf('Found the %s virus !', $desc) : 'Virus found !'); throw $connectionException;
}
$this->client->shutdown();
} }
} }
} }
...@@ -10,9 +10,9 @@ parameters: ...@@ -10,9 +10,9 @@ parameters:
irstea_file_upload.max_chunk_size: 0 irstea_file_upload.max_chunk_size: 0
irstea_file_upload.clamav_socket_path: unix:///var/run/clamav/clamd.ctl irstea_file_upload.virus_scanner.clamav_socket_path: unix:///var/run/clamav/clamd.ctl
irstea_file_upload.client_timeout: 30 irstea_file_upload.virus_scanner.client_timeout: 30
irstea_file_upload.client_mode: PHP_NORMAL_READ irstea_file_upload.virus_scanner.client_mode: !php/const:PHP_NORMAL_READ
services: services:
...@@ -74,22 +74,22 @@ services: ...@@ -74,22 +74,22 @@ services:
- "%irstea_file_upload.filesystem.name%" - "%irstea_file_upload.filesystem.name%"
# Scanner anti-virus # Scanner anti-virus
irstea_file_upload.socket: irstea_file_upload.virus_scanner.socket:
class: Socket\Raw\Socket class: Socket\Raw\Socket
arguments: arguments:
- "%irstea_file_upload.clamav_socket_path" - "%irstea_file_upload.virus_scanner.clamav_socket_path"
irstea_file_upload.client: irstea_file_upload.virus_scanner.client:
class: Xenolope\Quahog\Client class: Xenolope\Quahog\Client
arguments: arguments:
- "@irstea_file_upload.socket" - "@irstea_file_upload.virus_scanner.socket"
- "%irstea_file_upload.client_timeout" - "%irstea_file_upload.virus_scanner.client_timeout"
- "%irstea_file_upload.client_mode" - "%irstea_file_upload.virus_scanner.client_mode"
irstea_file_upload.virus_scanner: irstea_file_upload.virus_scanner:
class: Irstea\FileUploadBundle\Listener\VirusScannerListener class: Irstea\FileUploadBundle\Listener\VirusScannerListener
arguments: arguments:
- "@irstea_file_upload.client" - "@irstea_file_upload.virus_scanner.client"
tags: tags:
- { name: kernel.event_listener, event: file_upload.complete, method: onFileUploadCompleted } - { name: kernel.event_listener, event: file_upload.complete, method: onFileUploadCompleted }
......
...@@ -12,6 +12,7 @@ use PHPUnit_Framework_MockObject_MockObject; ...@@ -12,6 +12,7 @@ use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use Xenolope\Quahog\Client; use Xenolope\Quahog\Client;
use Xenolope\Quahog\Exception\ConnectionException;
/** /**
* Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:43:16. * Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:43:16.
...@@ -46,14 +47,15 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase ...@@ -46,14 +47,15 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
$this->client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); $this->client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock();
$this->file = $this->getMock(UploadedFileInterface::class); $this->file = $this->getMock(UploadedFileInterface::class);
$this->file->expects(static::once())->method('getLocalPath')->willReturn('foopath'); $this->file->expects($this->any())->method('getLocalPath')->willReturn('foopath');
$this->file->expects(static::once())->method('getMetadata')->willReturn([]); $this->file->expects($this->any())->method('getMetadata')->willReturn([]);
$this->event = new FileUploadCompleteEvent($this->file); $this->event = new FileUploadCompleteEvent($this->file);
$this->listener = new VirusScannerListener($this->client); $this->listener = new VirusScannerListener($this->client);
} }
public function testOnFileUploadCompletedResultOk() public function testOnFileUploadCompletedResultOk()
{ {
$result = [ $result = [
...@@ -62,8 +64,8 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase ...@@ -62,8 +64,8 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
'status' => Client::RESULT_OK 'status' => Client::RESULT_OK
]; ];
$this->client->expects(static::once())->method('scanFile')->with('foopath')->willReturn($result); $this->client->expects($this->any())->method('scanFile')->with('foopath')->willReturn($result);
$this->file->expects(static::once())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]); $this->file->expects($this->any())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]);
$this->listener->onFileUploadCompleted($this->event); $this->listener->onFileUploadCompleted($this->event);
} }
...@@ -84,10 +86,27 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase ...@@ -84,10 +86,27 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
->with('foopath') ->with('foopath')
->willReturn($result); ->willReturn($result);
$this->file->expects(static::once()) $this->file->expects($this->any())
->method('setMetadata') ->method('setMetadata')
->with(['virus_scanner' => ['has_virus' => true, 'detected' => 'Terrible virus']]); ->with(['virus_scanner' => ['has_virus' => true, 'detected' => 'Terrible virus']]);
$this->listener->onFileUploadCompleted($this->event); $this->listener->onFileUploadCompleted($this->event);
} }
/**
* @expectedException Xenolope\Quahog\Exception\ConnectionException
*/
public function testGetClientFailed()
{
$this->client->expects($this->any())->method('ping')->willThrowException(new ConnectionException());
$this->listener->onFileUploadCompleted($this->event);
}
public function testGetClientSucceed()
{
$this->client->expects($this->any())->method('ping')->willReturn(true);
$this->listener->onFileUploadCompleted($this->event);
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment