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
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Service/FileOperationServiceTest.php')
-rw-r--r--tests/Unit/Service/FileOperationServiceTest.php253
1 files changed, 253 insertions, 0 deletions
diff --git a/tests/Unit/Service/FileOperationServiceTest.php b/tests/Unit/Service/FileOperationServiceTest.php
new file mode 100644
index 0000000..be9dca8
--- /dev/null
+++ b/tests/Unit/Service/FileOperationServiceTest.php
@@ -0,0 +1,253 @@
+<?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\Service;
+
+use OCA\RansomwareDetection\Service\FileOperationService;
+use OCA\RansomwareDetection\Db\FileOperation;
+use OCA\RansomwareDetection\Db\FileOperationMapper;
+use OCA\RansomwareDetection\Tests\Unit\Db\MapperTestUtility;
+
+class FileOperationServiceTest extends MapperTestUtility
+{
+ /** @var FileOperationService */
+ protected $service;
+
+ /** @var FileOperationMapper */
+ protected $mapper;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->mapper = new FileOperationMapper($this->db);
+ $this->service = new FileOperationService($this->mapper, 'john');
+
+ // create mock FileOperation
+ $fileOperation1 = new FileOperation();
+ $fileOperation2 = new FileOperation();
+
+ $this->fileOperations = [$fileOperation1, $fileOperation2];
+
+ $this->twoRows = [
+ ['id' => $this->fileOperations[0]->getId()],
+ ['id' => $this->fileOperations[1]->getId()],
+ ];
+ }
+
+ public function testFind()
+ {
+ $userId = 'john';
+ $id = 3;
+ $rows = [['id' => $this->fileOperations[0]->getId()]];
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` '.
+ 'WHERE `id` = ? AND `user_id` = ?';
+
+ $this->setMapperResult($sql, [$id, $userId], $rows);
+
+ $result = $this->service->find($id);
+ $this->assertEquals($this->fileOperations[0], $result);
+ }
+
+ public function testFindNotFound()
+ {
+ $userId = 'john';
+ $id = 3;
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` '.
+ 'WHERE `id` = ? AND `user_id` = ?';
+
+ $this->setMapperResult($sql, [$id, $userId]);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\DoesNotExistException'
+ );
+ $this->service->find($id);
+ }
+
+ public function testFindMoreThanOneResultFound()
+ {
+ $userId = 'john';
+ $id = 3;
+ $rows = $this->twoRows;
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` '.
+ 'WHERE `id` = ? AND `user_id` = ?';
+
+ $this->setMapperResult($sql, [$id, $userId], $rows);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\MultipleObjectsReturnedException'
+ );
+ $this->service->find($id);
+ }
+
+ public function testFindOneByFileName()
+ {
+ $userId = 'john';
+ $name = 'test';
+ $rows = [['id' => $this->fileOperations[0]->getId()]];
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` '.
+ 'WHERE `original_name` = ? AND `user_id` = ?';
+
+ $this->setMapperResult($sql, [$name, $userId], $rows);
+
+ $result = $this->service->findOneByFileName($name);
+ $this->assertEquals($this->fileOperations[0], $result);
+ }
+
+ public function testFindOneByFileNameNotFound()
+ {
+ $userId = 'john';
+ $name = 'test';
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` '.
+ 'WHERE `original_name` = ? AND `user_id` = ?';
+
+ $this->setMapperResult($sql, [$name, $userId]);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\DoesNotExistException'
+ );
+ $this->service->findOneByFileName($name);
+ }
+
+ public function testFindOneByFileNameMoreThanOneResultFound()
+ {
+ $userId = 'john';
+ $name = 'test';
+ $rows = $this->twoRows;
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` '.
+ 'WHERE `original_name` = ? AND `user_id` = ?';
+
+ $this->setMapperResult($sql, [$name, $userId], $rows);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\MultipleObjectsReturnedException'
+ );
+ $this->service->findOneByFileName($name);
+ }
+
+ public function testFindOneWithHighestId()
+ {
+ $userId = 'john';
+ $rows = [['id' => $this->fileOperations[0]->getId()]];
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` WHERE `user_id` = ?'.
+ 'ORDER BY id DESC LIMIT 1';
+
+ $this->setMapperResult($sql, [$userId], $rows);
+
+ $result = $this->service->findOneWithHighestId();
+ $this->assertEquals($this->fileOperations[0], $result);
+ }
+
+ public function testFindOneWithHighestIdNotFound()
+ {
+ $userId = 'john';
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` WHERE `user_id` = ?'.
+ 'ORDER BY id DESC LIMIT 1';
+
+ $this->setMapperResult($sql, [$userId]);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\DoesNotExistException'
+ );
+ $this->service->findOneWithHighestId();
+ }
+
+ public function testFindOneWithHighestIdMoreThanOneResultFound()
+ {
+ $userId = 'john';
+ $rows = $this->twoRows;
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` WHERE `user_id` = ?'.
+ 'ORDER BY id DESC LIMIT 1';
+
+ $this->setMapperResult($sql, [$userId], $rows);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\MultipleObjectsReturnedException'
+ );
+ $this->service->findOneWithHighestId();
+ }
+
+ public function testFindAll()
+ {
+ $userId = 'john';
+ $rows = $this->twoRows;
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` WHERE `user_id` = ?';
+
+ $this->setMapperResult($sql, [$userId], $rows);
+ $result = $this->service->findAll();
+ $this->assertEquals($this->fileOperations, $result);
+ }
+
+ public function testFindSequenceById()
+ {
+ $userId = 'john';
+ $sequence = '1';
+ $rows = $this->twoRows;
+ $sql = 'SELECT * FROM `*PREFIX*ransomware_detection_file_operation` WHERE `sequence` = ? AND `user_id` = ?';
+
+ $this->setMapperResult($sql, [$sequence, $userId], $rows);
+ $result = $this->service->findSequenceById([$sequence]);
+ $this->assertEquals($this->fileOperations, $result);
+ }
+
+ public function testDeleteById()
+ {
+ $userId = 'john';
+ $fileOperation = new FileOperation();
+ $fileOperation->setUserId($userId);
+ $fileOperation->setId(3);
+
+ $sql = 'DELETE FROM `*PREFIX*ransomware_detection_file_operation` WHERE `id` = ? AND `user_id` = ?';
+ $arguments = [$fileOperation->getId(), $userId];
+
+ $this->setMapperResult($sql, $arguments, [], null, null, true);
+
+ $this->service->deleteById($fileOperation->getId());
+ }
+
+ public function testDeleteSequenceById()
+ {
+ $userId = 'john';
+ $fileOperation = new FileOperation();
+ $fileOperation->setId(3);
+ $fileOperation->setUserId($userId);
+ $fileOperation->setSequence(1);
+
+ $sql = 'DELETE FROM `*PREFIX*ransomware_detection_file_operation` WHERE `sequence` = ? AND `user_id` = ?';
+ $arguments = [$fileOperation->getSequence(), $userId];
+
+ $this->setMapperResult($sql, $arguments, [], null, null, true);
+
+ $this->service->deleteSequenceById($fileOperation->getSequence());
+ }
+
+ public function testDeleteFileOperationsBefore()
+ {
+ $userId = 'john';
+ $fileOperation = new FileOperation();
+ $fileOperation->setId(3);
+ $fileOperation->setUserId($userId);
+ $fileOperation->setSequence(1);
+ $fileOperation->setTimestamp(strtotime('-1 week'));
+
+ $sql = 'DELETE FROM `*PREFIX*ransomware_detection_file_operation` WHERE `timestamp` < ?';
+ $time = time();
+ $arguments = [$time];
+
+ $this->setMapperResult($sql, $arguments, [], null, null, true);
+
+ $this->service->deleteFileOperationsBefore($time);
+ }
+}