Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/undo-ransomware/ransomware_detection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMatthias Held <ilovemilk@wusa.io>2018-07-24 18:21:56 +0300
committerMatthias Held <ilovemilk@wusa.io>2018-07-24 18:21:56 +0300
commitbd4e9e0fb268438e8f21369ad971613823e41879 (patch)
tree7be80df8138e4050f579e99a1032afc648eda87f /tests
parent3fbaae5aa2026ee893f7b4a9b9fc53ca9ff35b76 (diff)
Add storage scanner
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Controller/BasicControllerTest.php101
-rw-r--r--tests/Unit/Controller/MonitoringControllerTest.php341
-rw-r--r--tests/Unit/Controller/ScanControllerTest.php274
-rw-r--r--tests/Unit/Scanner/StorageStructureTest.php72
4 files changed, 788 insertions, 0 deletions
diff --git a/tests/Unit/Controller/BasicControllerTest.php b/tests/Unit/Controller/BasicControllerTest.php
new file mode 100644
index 0000000..b49592e
--- /dev/null
+++ b/tests/Unit/Controller/BasicControllerTest.php
@@ -0,0 +1,101 @@
+<?php
+
+/**
+ * @copyright Copyright (c) 2018 Matthias Held <matthias.held@uni-konstanz.de>
+ * @author Matthias Held <matthias.held@uni-konstanz.de>
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\RansomwareDetection\tests\Unit\Controller;
+
+use OCA\RansomwareDetection\Controller\BasicController;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\Files\File;
+use OCP\Files\Folder;
+use Test\TestCase;
+
+class BasicControllerTest extends TestCase
+{
+ /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
+ protected $request;
+
+ /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+ protected $userSession;
+
+ /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
+ protected $config;
+
+ /** @var string */
+ protected $userId = 'john';
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->request = $this->getMockBuilder('OCP\IRequest')
+ ->getMock();
+ $this->userSession = $this->getMockBuilder('OCP\IUserSession')
+ ->getMock();
+ $this->config = $this->getMockBuilder('OCP\IConfig')
+ ->getMock();
+ }
+
+ public function testGetDebugMode()
+ {
+ $controller = new BasicController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ 'john'
+ );
+
+ $result = $controller->getDebugMode();
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_ACCEPTED);
+ }
+
+ public function testGetColorMode()
+ {
+ $controller = new BasicController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ 'john'
+ );
+
+ $result = $controller->getColorMode();
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_ACCEPTED);
+ }
+
+ public function testChangeColorMode()
+ {
+ $controller = new BasicController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ 'john'
+ );
+
+ $result = $controller->changeColorMode(1);
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_ACCEPTED);
+ }
+}
diff --git a/tests/Unit/Controller/MonitoringControllerTest.php b/tests/Unit/Controller/MonitoringControllerTest.php
new file mode 100644
index 0000000..3a07604
--- /dev/null
+++ b/tests/Unit/Controller/MonitoringControllerTest.php
@@ -0,0 +1,341 @@
+<?php
+
+/**
+ * @copyright Copyright (c) 2018 Matthias Held <matthias.held@uni-konstanz.de>
+ * @author Matthias Held <matthias.held@uni-konstanz.de>
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\RansomwareDetection\tests\Unit\Controller;
+
+use OCA\RansomwareDetection\Monitor;
+use OCA\RansomwareDetection\Analyzer\SequenceAnalyzer;
+use OCA\RansomwareDetection\Analyzer\SequenceResult;
+use OCA\RansomwareDetection\Db\FileOperation;
+use OCA\RansomwareDetection\Controller\MonitoringController;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\Files\File;
+use OCP\Files\Folder;
+use Test\TestCase;
+
+class MonitoringControllerTest extends TestCase
+{
+ /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
+ protected $request;
+
+ /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+ protected $userSession;
+
+ /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
+ protected $config;
+
+ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
+ protected $logger;
+
+ /** @var Classifier|\PHPUnit_Framework_MockObject_MockObject */
+ protected $classifier;
+
+ /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
+ protected $folder;
+
+ /** @var FileOperationService|\PHPUnit_Framework_MockObject_MockObject */
+ protected $service;
+
+ /** @var SequenceAnalyzer|\PHPUnit_Framework_MockObject_MockObject */
+ protected $sequenceAnalyzer;
+
+ /** @var string */
+ protected $userId = 'john';
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->request = $this->getMockBuilder('OCP\IRequest')
+ ->getMock();
+ $this->userSession = $this->getMockBuilder('OCP\IUserSession')
+ ->getMock();
+ $this->config = $this->getMockBuilder('OCP\IConfig')
+ ->getMock();
+ $this->logger = $this->getMockBuilder('OCP\ILogger')
+ ->getMock();
+ $this->folder = $this->getMockBuilder('OCP\Files\Folder')
+ ->getMock();
+ $connection = $this->getMockBuilder('OCP\IDBConnection')
+ ->getMock();
+ $mapper = $this->getMockBuilder('OCA\RansomwareDetection\Db\FileOperationMapper')
+ ->setConstructorArgs([$connection])
+ ->getMock();
+ $this->service = $this->getMockBuilder('OCA\RansomwareDetection\Service\FileOperationService')
+ ->setConstructorArgs([$mapper, $this->userId])
+ ->getMock();
+ $this->classifier = $this->getMockBuilder('OCA\RansomwareDetection\Classifier')
+ ->setConstructorArgs([$this->logger, $mapper, $this->service])
+ ->getMock();
+ $this->sequenceAnalyzer = $this->createMock(SequenceAnalyzer::class);
+ }
+
+ public function testListFileOperations()
+ {
+ $controller = new MonitoringController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ $this->classifier,
+ $this->logger,
+ $this->folder,
+ $this->service,
+ $this->sequenceAnalyzer,
+ 'john'
+ );
+ $file = $this->getMockBuilder(FileOperation::class)
+ ->setMethods(['getSequence'])
+ ->getMock();
+
+ $sequenceResult = new SequenceResult(0, 0, 0, 0, 0, 0);
+
+ $file->method('getSequence')
+ ->willReturn(1);
+
+ $this->service->method('findAll')
+ ->willReturn([$file]);
+
+ $this->classifier->method('classifyFile');
+ $this->sequenceAnalyzer->method('analyze')
+ ->willReturn($sequenceResult);
+
+ $result = $controller->listFileOperations();
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_ACCEPTED);
+ }
+
+ public function testDeleteSequence()
+ {
+ $controller = new MonitoringController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ $this->classifier,
+ $this->logger,
+ $this->folder,
+ $this->service,
+ $this->sequenceAnalyzer,
+ 'john'
+ );
+ $this->service->method('deleteSequenceById')
+ ->with(1)
+ ->will($this->returnValue([]));
+
+ $result = $controller->deleteSequence(1);
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_ACCEPTED);
+ }
+
+ public function dataRecover()
+ {
+ $fileOperationWrite = new FileOperation();
+ $fileOperationWrite->setCommand(Monitor::WRITE);
+ $fileOperationWrite->setPath('/admin/files');
+ $fileOperationWrite->setOriginalName('test.jpg');
+
+ $fileOperationRead = new FileOperation();
+ $fileOperationRead->setCommand(Monitor::READ);
+ $fileOperationRead->setPath('/admin/files');
+ $fileOperationRead->setOriginalName('test.jpg');
+
+ $fileOperationDelete = new FileOperation();
+ $fileOperationDelete->setCommand(Monitor::DELETE);
+ $fileOperationDelete->setPath('/admin/file');
+ $fileOperationDelete->setOriginalName('test.jpg');
+
+ $fileOperationRename = new FileOperation();
+ $fileOperationRename->setCommand(Monitor::RENAME);
+ $fileOperationRename->setPath('/admin/file');
+ $fileOperationRename->setOriginalName('test.jpg');
+
+ return [
+ ['id' => 4, 'fileOperation' => new FileOperation(), 'deleted' => false, 'response' => Http::STATUS_OK],
+ ['id' => 1, 'fileOperation' => $fileOperationRead, 'deleted' => true, 'response' => Http::STATUS_OK],
+ ['id' => 2, 'fileOperation' => $fileOperationRename, 'deleted' => true, 'response' => Http::STATUS_OK],
+ ];
+ }
+
+ /**
+ * @dataProvider dataRecover
+ *
+ * @param array $fileIds
+ * @param FileOperation $fileOperation
+ * @param bool $deleted
+ * @param HttpResponse $response
+ */
+ public function testRecover($fileIds, $fileOperation, $deleted, $response)
+ {
+ $controller = $this->getMockBuilder(MonitoringController::class)
+ ->setConstructorArgs(['ransomware_detection', $this->request, $this->userSession, $this->config, $this->classifier,
+ $this->logger, $this->folder, $this->service, $this->sequenceAnalyzer, 'john', ])
+ ->setMethods(['deleteFromStorage', 'getTrashFiles'])
+ ->getMock();
+
+ $controller->expects($this->any())
+ ->method('getTrashFiles')
+ ->willReturn([]);
+
+ $this->service->method('find')
+ ->willReturn($fileOperation);
+
+ $controller->expects($this->any())
+ ->method('deleteFromStorage')
+ ->willReturn($deleted);
+
+ $this->service->method('deleteById');
+
+ $result = $controller->recover($fileIds);
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), $response);
+ }
+
+ public function testRecoverMultipleObjectsReturnedException()
+ {
+ $controller = $this->getMockBuilder(MonitoringController::class)
+ ->setConstructorArgs(['ransomware_detection', $this->request, $this->userSession, $this->config, $this->classifier,
+ $this->logger, $this->folder, $this->service, $this->sequenceAnalyzer, 'john', ])
+ ->setMethods(['getTrashFiles'])
+ ->getMock();
+
+ $fileOperationWrite = new FileOperation();
+ $fileOperationWrite->setCommand(Monitor::WRITE);
+ $fileOperationWrite->setPath('/admin/files');
+ $fileOperationWrite->setOriginalName('test.jpg');
+
+ $controller->expects($this->any())
+ ->method('getTrashFiles')
+ ->willReturn([]);
+
+ $this->service->method('find')
+ ->will($this->throwException(new \OCP\AppFramework\Db\MultipleObjectsReturnedException('test')));
+
+ $result = $controller->recover(1);
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_BAD_REQUEST);
+ }
+
+ public function testDoesNotExistException()
+ {
+ $controller = new MonitoringController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ $this->classifier,
+ $this->logger,
+ $this->folder,
+ $this->service,
+ $this->sequenceAnalyzer,
+ 'john'
+ );
+
+ $fileOperationWrite = new FileOperation();
+ $fileOperationWrite->setCommand(Monitor::WRITE);
+ $fileOperationWrite->setPath('/admin/files');
+ $fileOperationWrite->setOriginalName('test.jpg');
+
+ $this->service->method('find')
+ ->will($this->throwException(new \OCP\AppFramework\Db\DoesNotExistException('test')));
+
+ $result = $controller->recover(1);
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_BAD_REQUEST);
+ }
+
+ public function testDeleteFromStorage()
+ {
+ $controller = new MonitoringController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ $this->classifier,
+ $this->logger,
+ $this->folder,
+ $this->service,
+ $this->sequenceAnalyzer,
+ 'john'
+ );
+ $file = $this->createMock(File::class);
+ $file->method('isDeletable')
+ ->willReturn(true);
+
+ $file->method('delete');
+
+ $this->folder->expects($this->once())
+ ->method('get')
+ ->willReturn($file);
+ $this->assertTrue($this->invokePrivate($controller, 'deleteFromStorage', ['/admin/files/test.jpg']));
+ }
+
+ public function testDeleteFromStorageNotPossible()
+ {
+ $controller = new MonitoringController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ $this->classifier,
+ $this->logger,
+ $this->folder,
+ $this->service,
+ $this->sequenceAnalyzer,
+ 'john'
+ );
+ $file = $this->createMock(File::class);
+
+ $file->method('isDeletable')
+ ->willReturn(false);
+
+ $file->method('delete');
+
+ $this->folder->expects($this->once())
+ ->method('get')
+ ->willReturn($file);
+ $this->assertFalse($this->invokePrivate($controller, 'deleteFromStorage', ['/admin/files/test.jpg']));
+ }
+
+ public function testDeleteFromStorageNotFoundException()
+ {
+ $controller = new MonitoringController(
+ 'ransomware_detection',
+ $this->request,
+ $this->userSession,
+ $this->config,
+ $this->classifier,
+ $this->logger,
+ $this->folder,
+ $this->service,
+ $this->sequenceAnalyzer,
+ 'john'
+ );
+ $folder = $this->createMock(Folder::class);
+
+ $this->folder->expects($this->once())
+ ->method('get')
+ ->will($this->throwException(new \OCP\Files\NotFoundException('test')));
+
+ $this->assertTrue($this->invokePrivate($controller, 'deleteFromStorage', ['/admin/files']));
+ }
+}
diff --git a/tests/Unit/Controller/ScanControllerTest.php b/tests/Unit/Controller/ScanControllerTest.php
new file mode 100644
index 0000000..28145f1
--- /dev/null
+++ b/tests/Unit/Controller/ScanControllerTest.php
@@ -0,0 +1,274 @@
+<?php
+
+/**
+ * @copyright Copyright (c) 2018 Matthias Held <matthias.held@uni-konstanz.de>
+ * @author Matthias Held <matthias.held@uni-konstanz.de>
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\RansomwareDetection\tests\Unit\Controller;
+
+use OCA\RansomwareDetection\Monitor;
+use OCA\RansomwareDetection\Classifier;
+use OCA\RansomwareDetection\Analyzer\SequenceAnalyzer;
+use OCA\RansomwareDetection\Analyzer\SequenceResult;
+use OCA\RansomwareDetection\Analyzer\SequenceSizeAnalyzer;
+use OCA\RansomwareDetection\Analyzer\FileTypeFunnellingAnalyzer;
+use OCA\RansomwareDetection\Analyzer\EntropyFunnellingAnalyzer;
+use OCA\RansomwareDetection\Analyzer\EntropyAnalyzer;
+use OCA\RansomwareDetection\Analyzer\EntropyResult;
+use OCA\RansomwareDetection\Analyzer\FileCorruptionAnalyzer;
+use OCA\RansomwareDetection\Analyzer\FileNameAnalyzer;
+use OCA\RansomwareDetection\Analyzer\FileNameResult;
+use OCA\RansomwareDetection\AppInfo\Application;
+use OCA\RansomwareDetection\Controller\ScanController;
+use OCA\RansomwareDetection\Db\FileOperation;
+use OCA\RansomwareDetection\Service\FileOperationService;
+use OCA\RansomwareDetection\Scanner\StorageStructure;
+use OCA\RansomwareDetection\Entropy\Entropy;
+use OCP\Files\IRootFolder;
+use OCA\Files_Trashbin\Trashbin;
+use OCA\Files_Trashbin\Helper;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\OCSController;
+use OCP\Files\File;
+use OCP\Files\Folder;
+use OCP\IConfig;
+use OCP\IUserSession;
+use OCP\IRequest;
+use OCP\IDBConnection;
+use OCP\ILogger;
+use Test\TestCase;
+
+class ScanControllerTest extends TestCase
+{
+ /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
+ protected $request;
+
+ /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+ protected $userSession;
+
+ /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
+ protected $config;
+
+ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
+ protected $logger;
+
+ /** @var Classifier|\PHPUnit_Framework_MockObject_MockObject */
+ protected $classifier;
+
+ /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
+ protected $folder;
+
+ /** @var FileOperationService|\PHPUnit_Framework_MockObject_MockObject */
+ protected $service;
+
+ /** @var SequenceAnalyzer|\PHPUnit_Framework_MockObject_MockObject */
+ protected $sequenceAnalyzer;
+
+ /** @var EntropyAnalyzer|\PHPUnit_Framework_MockObject_MockObject */
+ protected $entropyAnalyzer;
+
+ /** @var FileCorruptionAnalyzer|\PHPUnit_Framework_MockObject_MockObject */
+ protected $fileCorruptionAnalyzer;
+
+ /** @var FileNameAnalyzer|\PHPUnit_Framework_MockObject_MockObject */
+ protected $fileNameAnalyzer;
+
+ /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
+ protected $connection;
+
+ /** @var string */
+ protected $userId = 'john';
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->request = $this->getMockBuilder('OCP\IRequest')
+ ->getMock();
+ $this->userSession = $this->getMockBuilder('OCP\IUserSession')
+ ->getMock();
+ $this->config = $this->getMockBuilder('OCP\IConfig')
+ ->getMock();
+ $this->logger = $this->getMockBuilder('OCP\ILogger')
+ ->getMock();
+ $this->folder = $this->getMockBuilder('OCP\Files\Folder')
+ ->getMock();
+ $this->connection = $this->getMockBuilder('OCP\IDBConnection')
+ ->getMock();
+ $mapper = $this->getMockBuilder('OCA\RansomwareDetection\Db\FileOperationMapper')
+ ->setConstructorArgs([$this->connection])
+ ->getMock();
+ $this->service = $this->getMockBuilder('OCA\RansomwareDetection\Service\FileOperationService')
+ ->setConstructorArgs([$mapper, $this->userId])
+ ->getMock();
+ $this->classifier = $this->getMockBuilder('OCA\RansomwareDetection\Classifier')
+ ->setConstructorArgs([$this->logger, $mapper, $this->service])
+ ->getMock();
+ $sequenceSizeAnalyzer = $this->getMockBuilder('OCA\RansomwareDetection\Analyzer\SequenceSizeAnalyzer')
+ ->getMock();
+ $fileTypeFunnellingAnalyzer = $this->getMockBuilder('OCA\RansomwareDetection\Analyzer\FileTypeFunnellingAnalyzer')
+ ->getMock();
+ $entropyFunnellingAnalyzer = $this->getMockBuilder('OCA\RansomwareDetection\Analyzer\EntropyFunnellingAnalyzer')
+ ->setConstructorArgs([$this->logger])
+ ->getMock();
+ $this->sequenceAnalyzer = $this->getMockBuilder('OCA\RansomwareDetection\Analyzer\SequenceAnalyzer')
+ ->setConstructorArgs([$sequenceSizeAnalyzer, $fileTypeFunnellingAnalyzer, $entropyFunnellingAnalyzer])
+ ->setMethods(['analyze'])
+ ->getMock();
+ $rootFolder = $this->createMock(IRootFolder::class);
+ $entropy = $this->createMock(Entropy::class);
+ $this->entropyAnalyzer = $this->getMockBuilder('OCA\RansomwareDetection\Analyzer\EntropyAnalyzer')
+ ->setConstructorArgs([$this->logger, $rootFolder, $entropy, $this->userId])
+ ->getMock();
+ $this->fileCorruptionAnalyzer = $this->getMockBuilder('OCA\RansomwareDetection\Analyzer\FileCorruptionAnalyzer')
+ ->setConstructorArgs([$this->logger, $rootFolder, $this->userId])
+ ->getMock();
+ $this->fileNameAnalyzer = $this->getMockBuilder('OCA\RansomwareDetection\Analyzer\FileNameAnalyzer')
+ ->setConstructorArgs([$this->logger, $entropy])
+ ->getMock();
+ }
+
+ public function dataRecover()
+ {
+ return [
+ ['id' => 4, 'command' => Monitor::DELETE, 'path' => '/test.pdf', 'timestamp' => 12345, 'restored' => false, 'response' => Http::STATUS_BAD_REQUEST],
+ ['id' => 4, 'command' => Monitor::DELETE, 'path' => '/test.pdf', 'timestamp' => 12345, 'restored' => true, 'response' => Http::STATUS_OK],
+ ['id' => 4, 'command' => Monitor::WRITE, 'path' => '/test.pdf', 'timestamp' => 12345, 'restored' => true, 'response' => Http::STATUS_OK],
+ ['id' => 4, 'command' => Monitor::WRITE, 'path' => '/test.pdf', 'timestamp' => 12345, 'restored' => false, 'response' => Http::STATUS_BAD_REQUEST],
+ ['id' => 4, 'command' => Monitor::CREATE, 'path' => '/test.pdf', 'timestamp' => 12345, 'restored' => false, 'response' => Http::STATUS_BAD_REQUEST],
+ ['id' => 4, 'command' => Monitor::RENAME, 'path' => '/test.pdf', 'timestamp' => 12345, 'restored' => false, 'response' => Http::STATUS_BAD_REQUEST],
+ ];
+ }
+
+ /**
+ * @dataProvider dataRecover
+ *
+ * @param integer $id
+ * @param integer $command
+ * @param string $path
+ * @param integer $timestamp
+ * @param boolean $restored
+ * @param HttpResponse $response
+ */
+ public function testRecover($id, $command, $path, $timestamp, $restored, $response)
+ {
+ $controller = $this->getMockBuilder(ScanController::class)
+ ->setConstructorArgs(['ransomware_detection', $this->request, $this->userSession, $this->config, $this->classifier,
+ $this->logger, $this->folder, $this->service, $this->sequenceAnalyzer, $this->entropyAnalyzer,
+ $this->fileCorruptionAnalyzer, $this->fileNameAnalyzer, $this->connection, $this->userId])
+ ->setMethods(['deleteFromStorage', 'restoreFromTrashbin'])
+ ->getMock();
+
+ $controller->expects($this->any())
+ ->method('deleteFromStorage')
+ ->willReturn($restored);
+
+ $controller->expects($this->any())
+ ->method('restoreFromTrashbin')
+ ->willReturn($restored);
+
+ $result = $controller->recover($id, $command, $path ,$timestamp);
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), $response);
+ }
+
+ public function testFilesToScan()
+ {
+ $controller = $this->getMockBuilder(ScanController::class)
+ ->setConstructorArgs(['ransomware_detection', $this->request, $this->userSession, $this->config, $this->classifier,
+ $this->logger, $this->folder, $this->service, $this->sequenceAnalyzer, $this->entropyAnalyzer,
+ $this->fileCorruptionAnalyzer, $this->fileNameAnalyzer, $this->connection, $this->userId])
+ ->setMethods(['getStorageStructure', 'getTrashStorageStructure', 'getLastActivity'])
+ ->getMock();
+
+ $controller->expects($this->any())
+ ->method('getStorageStructure')
+ ->willReturn(new StorageStructure());
+
+ $controller->expects($this->any())
+ ->method('getTrashStorageStructure')
+ ->willReturn(new StorageStructure());
+
+ $controller->expects($this->any())
+ ->method('getLastActivity')
+ ->willReturn(123);
+
+ $result = $controller->filesToScan();
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), Http::STATUS_ACCEPTED);
+ }
+
+ public function dataScanSequence()
+ {
+ $fileOperation1 = new FileOperation();
+ $fileOperation1->setCommand(Monitor::WRITE);
+ $fileOperation1->setOriginalName('test.csv');
+ $fileOperation1->setNewName('test.csv');
+ $fileOperation1->setPath('files/test.csv');
+ $fileOperation1->setSize(123000);
+ $fileOperation1->setType('file');
+ $fileOperation1->setMimeType('pdf');
+ $fileOperation1->setCorrupted(1);
+ $fileOperation1->setTimestamp(123);
+ $fileOperation1->setSequence(1);
+ $fileOperation1->setEntropy(7.9);
+ $fileOperation1->setStandardDeviation(0.1);
+ $fileOperation1->setFileNameEntropy(4.0);
+ $fileOperation1->setFileClass(EntropyResult::NORMAL);
+ $fileOperation1->setFileNameClass(FileNameResult::NORMAL);
+ $fileOperation1->setSuspicionClass(Classifier::HIGH_LEVEL_OF_SUSPICION);
+
+ $sequenceResult = new SequenceResult(1, 0.0, 1.1, 2.2, 4.5, []);
+
+ return [
+ ['sequence' => [], 'fileOperation' => new FileOperation(), 'sequenceResult' => $sequenceResult,'response' => Http::STATUS_BAD_REQUEST],
+ ['sequence' => [['timestamp' => 123]], 'fileOperation' => $fileOperation1, 'sequenceResult' => $sequenceResult, 'response' => Http::STATUS_OK]
+ ];
+ }
+
+ /**
+ * @dataProvider dataScanSequence
+ *
+ * @param array $sequence
+ * @param sequenceResult $sequenceResult
+ * @param FileOperation $fileOperation
+ * @param HttpResponse $response
+ */
+ public function testScanSequence($sequence, $fileOperation, $sequenceResult, $response)
+ {
+ $controller = $this->getMockBuilder(ScanController::class)
+ ->setConstructorArgs(['ransomware_detection', $this->request, $this->userSession, $this->config, $this->classifier,
+ $this->logger, $this->folder, $this->service, $this->sequenceAnalyzer, $this->entropyAnalyzer,
+ $this->fileCorruptionAnalyzer, $this->fileNameAnalyzer, $this->connection, $this->userId])
+ ->setMethods(['getLastActivity', 'buildFileOperation'])
+ ->getMock();
+
+ $controller->expects($this->any())
+ ->method('buildFileOperation')
+ ->willReturn($fileOperation);
+
+ $this->sequenceAnalyzer->expects($this->any())
+ ->method('analyze')
+ ->willReturn($sequenceResult);
+
+ $result = $controller->scanSequence($sequence);
+ $this->assertTrue($result instanceof JSONResponse);
+ $this->assertEquals($result->getStatus(), $response);
+ }
+}
diff --git a/tests/Unit/Scanner/StorageStructureTest.php b/tests/Unit/Scanner/StorageStructureTest.php
new file mode 100644
index 0000000..5d41f36
--- /dev/null
+++ b/tests/Unit/Scanner/StorageStructureTest.php
@@ -0,0 +1,72 @@
+<?php
+
+/**
+ * @copyright Copyright (c) 2018 Matthias Held <matthias.held@uni-konstanz.de>
+ * @author Matthias Held <matthias.held@uni-konstanz.de>
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\RansomwareDetection\tests\Unit\Scanner;
+
+use OCA\RansomwareDetection\Scanner\StorageStructure;
+use Test\TestCase;
+
+class StorageStructureTest extends TestCase
+{
+ /** @var StorageStructure */
+ protected $storageStructure;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->storageStructure = new StorageStructure();
+ }
+
+ public function testDefaultParameters() {
+ $this->assertEquals($this->storageStructure->getNumberOfFiles(), 0);
+ $this->assertEquals($this->storageStructure->getFiles(), []);
+ }
+
+ public function testGetNumberOfFiles() {
+ $this->assertEquals($this->storageStructure->getNumberOfFiles(), 0);
+ }
+
+ public function testSetNumberOfFiles() {
+ $this->storageStructure->setNumberOfFiles(10);
+ $this->assertEquals($this->storageStructure->getNumberOfFiles(), 10);
+ }
+
+ public function testIncreaseNumberOfFiles() {
+ $this->storageStructure->increaseNumberOfFiles();
+ $this->assertEquals($this->storageStructure->getNumberOfFiles(), 1);
+ }
+
+ public function testGetFiles() {
+ $this->assertEquals($this->storageStructure->getFiles(), []);
+ }
+
+ public function testSetFiles() {
+ $this->storageStructure->setFiles([10]);
+ $this->assertEquals($this->storageStructure->getFiles(), [10]);
+ }
+
+ public function testAddFiles() {
+ $this->storageStructure->addFile(11);
+ $this->storageStructure->addFile(10);
+ $this->assertEquals($this->storageStructure->getFiles(), [11, 10]);
+ }
+}