diff options
author | Matthias Held <ilovemilk@wusa.io> | 2018-09-14 21:37:58 +0300 |
---|---|---|
committer | Matthias Held <ilovemilk@wusa.io> | 2018-09-14 21:37:58 +0300 |
commit | 29a997e5b309ec320648c1f836238814ba4f4912 (patch) | |
tree | d5e56d80ac651fca723b81cae75af89cce78a5e1 | |
parent | 0a3747e6f5a18dbfc60c2984df33be2991230b4e (diff) |
Add tests
Signed-off-by: Matthias Held <matthias.held@uni-konstanz.de>
-rw-r--r-- | lib/Controller/ScanController.php | 94 | ||||
-rw-r--r-- | tests/Unit/Controller/ScanControllerTest.php | 58 |
2 files changed, 102 insertions, 50 deletions
diff --git a/lib/Controller/ScanController.php b/lib/Controller/ScanController.php index da840b5..623631d 100644 --- a/lib/Controller/ScanController.php +++ b/lib/Controller/ScanController.php @@ -266,12 +266,59 @@ class ScanController extends OCSController } /** + * Just for testing purpose to mock the external static method. + * + * @return array + */ + protected function getTrashFiles() { + return Helper::getTrashFiles("/", $this->userId, 'mtime', false); + } + + /** + * Just for testing purpose to mock the external static method. + * + * @param string $trashPath + * @param array $pathInfo + * @param integer $timestamp + * @return boolean + */ + protected function restoreFromTrashbin($trashPath, $name, $timestamp) + { + return Trashbin::restore($trashPath, $name, $timestamp); + } + + /** + * Get last activity. + * Visibility 'protected' is that it's possible to mock the database access. + * + * @param $objectId + */ + protected function getLastActivity($objectId) + { + $query = $this->connection->getQueryBuilder(); + $query->select('*')->from('activity'); + $query->where($query->expr()->eq('affecteduser', $query->createNamedParameter($this->userId))) + ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); + $result = $query->execute(); + while ($row = $result->fetch()) { + $rows[] = $row; + } + $result->closeCursor(); + if (is_array($rows)) { + return array_pop($rows); + } else { + $this->logger->debug('getLastActivity: No activity found.', array('app' => Application::APP_ID)); + return 0; + } + } + + /** * Builds a file operations from a file info array. * * @param array $file * @return FileOperation */ - protected function buildFileOperation($file) + private function buildFileOperation($file) { $fileOperation = new FileOperation(); $fileOperation->setUserId($this->userId); @@ -319,38 +366,14 @@ class ScanController extends OCSController } /** - * Get last activity. - * - * @param $objectId - */ - protected function getLastActivity($objectId) - { - $query = $this->connection->getQueryBuilder(); - $query->select('*')->from('activity'); - $query->where($query->expr()->eq('affecteduser', $query->createNamedParameter($this->userId))) - ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); - $result = $query->execute(); - while ($row = $result->fetch()) { - $rows[] = $row; - } - $result->closeCursor(); - if (is_array($rows)) { - return array_pop($rows); - } else { - $this->logger->debug('getLastActivity: No activity found.', array('app' => Application::APP_ID)); - return 0; - } - } - - /** * Get trash storage structure. * * @return StorageStructure */ - protected function getTrashStorageStructure() + private function getTrashStorageStructure() { $storageStructure = new StorageStructure(0, []); - $nodes = Helper::getTrashFiles("/", $this->userId, 'mtime', false); + $nodes = $this->getTrashFiles(); foreach ($nodes as $node) { $storageStructure->addFile($node); $storageStructure->increaseNumberOfFiles(); @@ -365,7 +388,7 @@ class ScanController extends OCSController * * @return StorageStructure */ - protected function getStorageStructure($node) + private function getStorageStructure($node) { // set count for node to 0 $storageStructure = new StorageStructure(0, []); @@ -404,7 +427,7 @@ class ScanController extends OCSController * * @return bool */ - protected function deleteFromStorage($path) + private function deleteFromStorage($path) { try { $node = $this->userFolder->get($path); @@ -422,17 +445,4 @@ class ScanController extends OCSController return true; } } - - /** - * Restores file from trash bin. - * - * @param string $trashPath - * @param array $pathInfo - * @param integer $timestamp - * @return boolean - */ - protected function restoreFromTrashbin($trashPath, $name, $timestamp) - { - return Trashbin::restore($trashPath, $name, $timestamp); - } } diff --git a/tests/Unit/Controller/ScanControllerTest.php b/tests/Unit/Controller/ScanControllerTest.php index a8b7bf2..5d0fa94 100644 --- a/tests/Unit/Controller/ScanControllerTest.php +++ b/tests/Unit/Controller/ScanControllerTest.php @@ -31,6 +31,7 @@ use OCA\RansomwareDetection\Analyzer\EntropyFunnellingAnalyzer; use OCA\RansomwareDetection\Analyzer\EntropyAnalyzer; use OCA\RansomwareDetection\Analyzer\EntropyResult; use OCA\RansomwareDetection\Analyzer\FileCorruptionAnalyzer; +use OCA\RansomwareDetection\Analyzer\FileCorruptionResult; use OCA\RansomwareDetection\Analyzer\FileExtensionAnalyzer; use OCA\RansomwareDetection\Analyzer\FileExtensionResult; use OCA\RansomwareDetection\AppInfo\Application; @@ -185,6 +186,21 @@ class ScanControllerTest extends TestCase ->method('restoreFromTrashbin') ->willReturn($restored); + $file = $this->getMockBuilder(File::class) + ->getMock(); + + $this->folder->expects($this->any()) + ->method('get') + ->willReturn($file); + + $file->expects($this->any()) + ->method('isDeletable') + ->willReturn($restored); + + $file->expects($this->any()) + ->method('delete') + ->willReturn($restored); + $result = $controller->recover($id, $sequence, $command, $path, $name, $timestamp); $this->assertTrue($result instanceof JSONResponse); $this->assertEquals($result->getStatus(), $response); @@ -196,16 +212,12 @@ class ScanControllerTest extends TestCase ->setConstructorArgs(['ransomware_detection', $this->request, $this->userSession, $this->config, $this->classifier, $this->logger, $this->folder, $this->service, $this->sequenceAnalyzer, $this->entropyAnalyzer, $this->fileCorruptionAnalyzer, $this->fileExtensionAnalyzer, $this->connection, $this->userId]) - ->setMethods(['getStorageStructure', 'getTrashStorageStructure', 'getLastActivity']) + ->setMethods(['getTrashFiles', 'getLastActivity']) ->getMock(); $controller->expects($this->any()) - ->method('getStorageStructure') - ->willReturn(new StorageStructure()); - - $controller->expects($this->any()) - ->method('getTrashStorageStructure') - ->willReturn(new StorageStructure()); + ->method('getTrashFiles') + ->willReturn(array()); $controller->expects($this->any()) ->method('getLastActivity') @@ -239,7 +251,7 @@ class ScanControllerTest extends TestCase return [ ['sequence' => [], 'fileOperation' => new FileOperation(), 'sequenceResult' => $sequenceResult,'response' => Http::STATUS_OK], - ['sequence' => [['timestamp' => 123]], 'fileOperation' => $fileOperation1, 'sequenceResult' => $sequenceResult, 'response' => Http::STATUS_OK] + ['sequence' => [['timestamp' => 123, 'path' => '/files_trashbin/test.bin', 'id' => 1]], 'fileOperation' => $fileOperation1, 'sequenceResult' => $sequenceResult, 'response' => Http::STATUS_OK] ]; } @@ -264,10 +276,40 @@ class ScanControllerTest extends TestCase ->method('buildFileOperation') ->willReturn($fileOperation); + $parentFolder = $this->getMockBuilder('OCP\Files\Folder') + ->getMock(); + + $this->folder->expects($this->any()) + ->method('getParent') + ->willReturn($parentFolder); + + $file = $this->getMockBuilder('OCP\Files\File') + ->getMock(); + + $parentFolder->expects($this->any()) + ->method('get') + ->willReturn($file); + + $file->expects($this->any()) + ->method('getInternalPath') + ->willReturn('/files_trashbin/foo.bar'); + $this->sequenceAnalyzer->expects($this->any()) ->method('analyze') ->willReturn($sequenceResult); + $this->fileExtensionAnalyzer->expects($this->any()) + ->method('analyze') + ->willReturn(new FileExtensionResult(FileExtensionResult::NOT_SUSPICIOUS)); + + $this->fileCorruptionAnalyzer->expects($this->any()) + ->method('analyze') + ->willReturn(new FileCorruptionResult(false)); + + $this->entropyAnalyzer->expects($this->any()) + ->method('analyze') + ->willReturn(new EntropyResult(EntropyResult::ENCRYPTED, 8.0, 0.04)); + $result = $controller->scanSequence($sequence); $this->assertTrue($result instanceof JSONResponse); $this->assertEquals($result->getStatus(), $response); |