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

github.com/nextcloud/ransomware_detection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Analyzer/FileCorruptionAnalyzerTest.php')
-rw-r--r--tests/Unit/Analyzer/FileCorruptionAnalyzerTest.php156
1 files changed, 156 insertions, 0 deletions
diff --git a/tests/Unit/Analyzer/FileCorruptionAnalyzerTest.php b/tests/Unit/Analyzer/FileCorruptionAnalyzerTest.php
new file mode 100644
index 0000000..f93d19e
--- /dev/null
+++ b/tests/Unit/Analyzer/FileCorruptionAnalyzerTest.php
@@ -0,0 +1,156 @@
+<?php
+
+/**
+ * @copyright Copyright (c) 2017 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\Analyzer;
+
+use OCA\RansomwareDetection\Analyzer\FileCorruptionAnalyzer;
+use OCP\Files\Folder;
+use OCP\Files\File;
+use OCP\Files\IRootFolder;
+use OCP\ILogger;
+use Test\TestCase;
+
+class FileCorruptionAnalyzerTest extends TestCase
+{
+ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
+ protected $logger;
+
+ /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
+ protected $rootFolder;
+
+ /** @var string */
+ protected $userId = 'john';
+
+ /** @var FileCorruptionAnalyzer */
+ protected $fileCorruptionAnalyzer;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->logger = $this->createMock(ILogger::class);
+ $this->rootFolder = $this->createMock(IRootFolder::class);
+
+ $this->fileCorruptionAnalyzer = $this->getMockBuilder(FileCorruptionAnalyzer::class)
+ ->setConstructorArgs([$this->logger, $this->rootFolder, $this->userId])
+ ->setMethods(array('isCorrupted'))
+ ->getMock();
+ }
+
+ public function dataAnalyze()
+ {
+ return [
+ ['isCorrupted' => true],
+ ['isCorrupted' => false],
+ ];
+ }
+
+ /**
+ * @dataProvider dataAnalyze
+ *
+ * @param bool $isCorrupted
+ */
+ public function testAnalyze($isCorrupted)
+ {
+ $file = $this->createMock(File::class);
+ $file->method('getContent')
+ ->willReturn('test');
+
+ $userRoot = $this->createMock(Folder::class);
+ $userRoot->method('get')
+ ->willReturn($file);
+
+ $userFolder = $this->createMock(Folder::class);
+ $userFolder->method('getParent')
+ ->willReturn($userRoot);
+
+ $this->rootFolder->method('getUserFolder')
+ ->willReturn($userFolder);
+
+ $this->fileCorruptionAnalyzer->method('isCorrupted')
+ ->willReturn($isCorrupted);
+
+ $result = $this->fileCorruptionAnalyzer->analyze($file);
+
+ $this->assertEquals($isCorrupted, $result);
+ }
+
+ public function dataIsCorrupted()
+ {
+ return [
+ ['data' => 'ffff', 'extension' => 'unknown', 'result' => true],
+ ['data' => 'ffd8ffffffff', 'extension' => 'csv', 'result' => true],
+ ['data' => 'ffd8ffffffff', 'extension' => 'jpg', 'result' => false],
+ ];
+ }
+
+ /**
+ * @dataProvider dataIsCorrupted
+ *
+ * @param string $data
+ * @param string $extension
+ * @param bool $result
+ */
+ public function testIsCorrupted($data, $extension, $result)
+ {
+ $isCorrupted = self::getMethod('isCorrupted');
+
+ $node = $this->createMock(File::class);
+
+ $node->expects($this->once())
+ ->method('getContent')
+ ->willReturn(hex2bin($data));
+
+ $node->expects($this->any())
+ ->method('getPath')
+ ->willReturn('/admin/files/file.'.$extension);
+
+ $this->assertEquals($isCorrupted->invokeArgs($this->fileCorruptionAnalyzer, [$node])->isCorrupted(), $result);
+ }
+
+ public function testIsCorruptedCatchException()
+ {
+ $isCorrupted = self::getMethod('isCorrupted');
+
+ $node = $this->createMock(File::class);
+
+ $node->expects($this->once())
+ ->method('getContent')
+ ->will($this->throwException(new \OCP\Files\NotPermittedException()));
+
+ $this->assertEquals($isCorrupted->invokeArgs($this->fileCorruptionAnalyzer, [$node])->isCorrupted(), false);
+ }
+ /**
+ * Get protected method.
+ *
+ * @param string $name
+ *
+ * @return $method
+ */
+ protected static function getMethod($name)
+ {
+ $class = new \ReflectionClass(FileCorruptionAnalyzer::class);
+ $method = $class->getMethod($name);
+ $method->setAccessible(true);
+
+ return $method;
+ }
+}