diff options
author | Victor Dubiniuk <victor.dubiniuk@gmail.com> | 2016-11-11 23:13:53 +0300 |
---|---|---|
committer | Victor Dubiniuk <victor.dubiniuk@gmail.com> | 2016-11-13 21:29:16 +0300 |
commit | b3480aef4067f09756428f09b94a8698b135ecd8 (patch) | |
tree | 5c3ee515288519f430f339b4e90d486d1ac89638 /tests | |
parent | 7dff3342798b21ce9ac27ee7b04f148b1d0088bd (diff) |
Add Dummy Clam
Diffstat (limited to 'tests')
-rw-r--r-- | tests/AvirWrapperTest.php | 107 | ||||
-rw-r--r-- | tests/DummyClam.php | 68 | ||||
-rw-r--r-- | tests/ItemTest.php | 5 | ||||
-rw-r--r-- | tests/TestBase.php | 24 | ||||
-rw-r--r-- | tests/avirserver.php | 10 |
5 files changed, 212 insertions, 2 deletions
diff --git a/tests/AvirWrapperTest.php b/tests/AvirWrapperTest.php new file mode 100644 index 0000000..6d7a581 --- /dev/null +++ b/tests/AvirWrapperTest.php @@ -0,0 +1,107 @@ +<?php + +/** + * Copyright (c) 2016 Victor Dubiniuk <victor.dubiniuk@gmail.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +namespace OCA\Files_antivirus\Tests; + +use OC\Files\Filesystem; +use OC\Files\Storage\Storage; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use OCA\Files_Antivirus\AppConfig; +use OCA\Files_Antivirus\AvirWrapper; +use OCA\Files_Antivirus\ScannerFactory; +use Test\Util\User\Dummy; + +// mmm. IDK why autoloader fails on this class +include_once dirname(dirname(dirname(__DIR__))) . '/tests/lib/Util/User/Dummy.php'; + +class AvirWrapperTest extends TestBase { + /** + * @var Temporary + */ + private $storage; + + protected $scannerFactory; + + protected $isWrapperRegistered = false; + + public function setUp() { + parent::setUp(); + $logger = $this->container->query('Logger'); + $this->scannerFactory = new ScannerFactory( + $this->streamConfig, + $logger + ); + + \OC_User::clearBackends(); + \OC_User::useBackend(new Dummy()); + Filesystem::clearMounts(); + + //login + \OC::$server->getUserManager()->createUser('testo', 'test'); + \OC::$server->getUserSession()->login('testo', 'test'); + \OC::$server->getSession()->set('user_id', 'testo'); + \OC::$server->getUserFolder('testo'); + \OC_Util::setupFS('testo'); + + $this->storage = new Temporary(array()); + if (!$this->isWrapperRegistered) { + Filesystem::addStorageWrapper( + 'oc_avir_test', + function ($mountPoint, $storage) use ($logger) { + /** + * @var Storage $storage + */ + if ($storage instanceof Storage) { + return new AvirWrapper([ + 'storage' => $storage, + 'scannerFactory' => $this->scannerFactory, + 'l10n' => $this->l10n, + 'logger' => $logger + ]); + } else { + return $storage; + } + }, + 1 + ); + $this->isWrapperRegistered = true; + } + Filesystem::init('testo', ''); + } + + /** + * @expectedException \OCP\Files\InvalidContentException + */ + /*public function testInfected(){ + $fd = Filesystem::fopen('killing bee', 'w+'); + @fwrite($fd, 'it ' . DummyClam::TEST_SIGNATURE); + @fclose($fd); + Filesystem::unlink('killing kee'); + }*/ + + /** + * @expectedException \OCP\Files\InvalidContentException + */ + public function testBigInfected(){ + $fd = Filesystem::fopen('killing whale', 'w+'); + @fwrite($fd, str_repeat('0', DummyClam::TEST_STREAM_SIZE-2)); + @fwrite($fd, DummyClam::TEST_SIGNATURE); + @fclose($fd); + Filesystem::unlink('killing whale'); + } + + public function tearDown() { + parent::tearDown(); + Filesystem::tearDown(); + \OC_Util::tearDownFS(); + \OC::$server->getUserManager()->get('testo')->delete(); + } +} diff --git a/tests/DummyClam.php b/tests/DummyClam.php new file mode 100644 index 0000000..55724e9 --- /dev/null +++ b/tests/DummyClam.php @@ -0,0 +1,68 @@ +<?php +/** + * Copyright (c) 2016 Victor Dubiniuk <victor.dubiniuk@gmail.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Files_antivirus\Tests; + +class DummyClam { + const TEST_STREAM_SIZE = 524288; // 512K + const TEST_SIGNATURE = 'does the job'; + private $chunkSize = 8192; // 8K + private $socketDN; + private $socket; + + public function __construct($socketPath){ + $this->socketDN = $socketPath; + } + + public function startServer(){ + $this->socket = stream_socket_server($this->socketDN, $errNo, $errStr); + if (!is_resource($this->socket)){ + throw new \Exception( + sprintf( + 'Unable to open socket. Error code: %s. Error message: "%s"', + $errNo, + $errStr + ) + ); + } + // listen + while (true){ + $connection = @stream_socket_accept($this->socket); + if (is_resource($connection)){ + stream_set_blocking($connection, false); + $this->handleConnection($connection); + @fclose($connection); + } + } + } + protected function handleConnection($connection){ + $buffer = ''; + $isAborted = false; + do { + $chunk = fread($connection, $this->chunkSize); + $nextBufferSize = strlen($buffer) + strlen($chunk); + if ($nextBufferSize > self::TEST_STREAM_SIZE){ + $isAborted = true; + break; + } + $buffer = $buffer . $chunk; + } while (!$this->shouldCloseConnection($buffer)); + if (!$isAborted){ + //echo $buffer; + $response = strpos($buffer, self::TEST_SIGNATURE) !== false + ? 'Ohoho: Criminal.Joboholic FOUND' + : 'Scanned OK' + ; + fwrite($connection, $response); + } + } + protected function shouldCloseConnection($buffer){ + $needle = pack('N', 0); + return substr($buffer,-strlen($needle)) == $needle; + } +} diff --git a/tests/ItemTest.php b/tests/ItemTest.php index fd704fe..8461d8a 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -51,4 +51,9 @@ class ItemTest extends TestBase { $chunk = $item->fread(); $this->assertEquals(self::CONTENT, $chunk); } + + public function tearDown() { + parent::tearDown(); + \OC_Util::tearDownFS(); + } } diff --git a/tests/TestBase.php b/tests/TestBase.php index 15637b5..d33b1a0 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -17,6 +17,7 @@ abstract class TestBase extends \PHPUnit_Framework_TestCase { protected $application; protected $container; protected $config; + protected $streamConfig; protected $l10n; @@ -35,14 +36,21 @@ abstract class TestBase extends \PHPUnit_Framework_TestCase { ; $this->config->method('__call') ->will($this->returnCallback(array($this, 'getAppValue'))); - + + $this->streamConfig = $this->getMockBuilder('\OCA\Files_Antivirus\AppConfig') + ->disableOriginalConstructor() + ->getMock() + ; + $this->streamConfig->method('__call') + ->will($this->returnCallback(array($this, 'getAppStreamValue'))); + $this->l10n = $this->getMockBuilder('\OCP\IL10N') ->disableOriginalConstructor() ->getMock() ; $this->l10n->method('t')->will($this->returnArgument(0)); } - + public function getAppValue($methodName){ switch ($methodName){ case 'getAvPath': @@ -51,4 +59,16 @@ abstract class TestBase extends \PHPUnit_Framework_TestCase { return 'executable'; } } + public function getAppStreamValue($methodName){ + switch ($methodName){ + case 'getAvHost': + return '127.0.0.1'; + case 'getAvPort': + return 5555; + case 'getAvStreamMaxLength': + return DummyClam::TEST_STREAM_SIZE; + case 'getAvMode': + return 'daemon'; + } + } } diff --git a/tests/avirserver.php b/tests/avirserver.php new file mode 100644 index 0000000..c3b8899 --- /dev/null +++ b/tests/avirserver.php @@ -0,0 +1,10 @@ +<?php + +namespace OCA\Files_antivirus\Tests; + +include __DIR__ . '/DummyClam.php'; + +set_time_limit(0); +$socketPath = 'tcp://0.0.0.0:5555'; +$clam = new DummyClam($socketPath); +$clam->startServer(); |