diff options
author | Victor Dubiniuk <victor.dubiniuk@gmail.com> | 2016-11-21 18:36:49 +0300 |
---|---|---|
committer | Victor Dubiniuk <victor.dubiniuk@gmail.com> | 2016-11-21 19:26:07 +0300 |
commit | f44e715f6e501bcd4296ebdbfea1913a5e3a62e7 (patch) | |
tree | 44ee6501d0902d7727988f057fb2e5be6897bf07 /tests | |
parent | b3480aef4067f09756428f09b94a8698b135ecd8 (diff) |
Finally fix tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/AvirWrapperTest.php | 99 | ||||
-rw-r--r-- | tests/DummyClam.php | 40 | ||||
-rw-r--r-- | tests/Mock/Config.php | 29 | ||||
-rw-r--r-- | tests/Mock/ScannerFactory.php | 19 | ||||
-rw-r--r-- | tests/TestBase.php | 19 |
5 files changed, 122 insertions, 84 deletions
diff --git a/tests/AvirWrapperTest.php b/tests/AvirWrapperTest.php index 6d7a581..3726802 100644 --- a/tests/AvirWrapperTest.php +++ b/tests/AvirWrapperTest.php @@ -12,96 +12,89 @@ 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 OCA\Files_antivirus\Tests\Mock\Config; 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; + + const UID = 'testo'; + const PWD = 'test'; protected $scannerFactory; protected $isWrapperRegistered = false; - public function setUp() { - parent::setUp(); - $logger = $this->container->query('Logger'); - $this->scannerFactory = new ScannerFactory( - $this->streamConfig, - $logger - ); - + public static function setUpBeforeClass() { + parent::setUpBeforeClass(); \OC_User::clearBackends(); \OC_User::useBackend(new Dummy()); - Filesystem::clearMounts(); + } + + public function setUp() { + parent::setUp(); + if (!\OC::$server->getUserManager()->get(self::UID)) { + \OC::$server->getUserManager()->createUser(self::UID, self::PWD); + } - //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->scannerFactory = new Mock\ScannerFactory( + new Mock\Config($this->container->query('CoreConfig')), + $this->container->query('Logger') + ); - $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, 'wrapperCallback'], + 2 ); $this->isWrapperRegistered = true; } - Filesystem::init('testo', ''); + + \OC::$server->getUserSession()->login(self::UID, self::PWD); + \OC::$server->getSession()->set('user_id', self::UID); + \OC::$server->getUserFolder(self::UID); } /** * @expectedException \OCP\Files\InvalidContentException */ - /*public function testInfected(){ + 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, str_repeat('0', DummyClam::TEST_STREAM_SIZE-2) . DummyClam::TEST_SIGNATURE ); @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(); + public function wrapperCallback($mountPoint, $storage){ + /** + * @var Storage $storage + */ + if ($storage instanceof Storage) { + return new AvirWrapper([ + 'storage' => $storage, + 'scannerFactory' => $this->scannerFactory, + 'l10n' => $this->l10n, + 'logger' => $this->container->query('Logger') + ]); + } else { + return $storage; + } + } + + public static function tearDownAfterClassClass() { + parent::tearDownAfterClass(); + \OC::$server->getUserManager()->get(self::UID)->delete(); + \OC_User::clearBackends(); } } diff --git a/tests/DummyClam.php b/tests/DummyClam.php index 55724e9..613f6a2 100644 --- a/tests/DummyClam.php +++ b/tests/DummyClam.php @@ -32,9 +32,9 @@ class DummyClam { } // listen while (true){ - $connection = @stream_socket_accept($this->socket); + $connection = @stream_socket_accept($this->socket, -1); if (is_resource($connection)){ - stream_set_blocking($connection, false); + //echo 'connected' . PHP_EOL; $this->handleConnection($connection); @fclose($connection); } @@ -43,26 +43,42 @@ class DummyClam { protected function handleConnection($connection){ $buffer = ''; $isAborted = false; + $command = fread($connection, 10); + //starts from INSTREAM\n from the first packet; + + if ($command !== "nINSTREAM\n"){ + return; + } + //echo $command; do { - $chunk = fread($connection, $this->chunkSize); - $nextBufferSize = strlen($buffer) + strlen($chunk); + $binaryChunkSize = @fread($connection, 4); + $chunkSizeArray = unpack('Nlength', $binaryChunkSize); + $chunkSize = $chunkSizeArray['length']; + if ($chunkSize === 0){ + break; + } + + $dataChunk = ''; + do { + $dataChunk .= @fread($connection, $chunkSize); + } while (is_resource($connection) && strlen($dataChunk) !== $chunkSize); + + $nextBufferSize = strlen($buffer) + strlen($dataChunk); if ($nextBufferSize > self::TEST_STREAM_SIZE){ $isAborted = true; break; } - $buffer = $buffer . $chunk; - } while (!$this->shouldCloseConnection($buffer)); + + $buffer = $buffer . $dataChunk; + } while (true); + if (!$isAborted){ - //echo $buffer; $response = strpos($buffer, self::TEST_SIGNATURE) !== false - ? 'Ohoho: Criminal.Joboholic FOUND' + ? "Ohoho: Criminal.Joboholic FOUND" : 'Scanned OK' ; + //echo str_replace('0', '', $buffer) . $response; fwrite($connection, $response); } } - protected function shouldCloseConnection($buffer){ - $needle = pack('N', 0); - return substr($buffer,-strlen($needle)) == $needle; - } } diff --git a/tests/Mock/Config.php b/tests/Mock/Config.php new file mode 100644 index 0000000..6dbd624 --- /dev/null +++ b/tests/Mock/Config.php @@ -0,0 +1,29 @@ +<?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\Mock; + +use \OCA\Files_antivirus\AppConfig; +use \OCA\Files_antivirus\Tests\DummyClam; + +class Config extends AppConfig { + public function getAppValue($key) { + $map = [ + 'av_host' => '127.0.0.1', + 'av_port' => 5555, + 'av_stream_max_length' => DummyClam::TEST_STREAM_SIZE, + 'av_mode' => 'daemon' + ]; + if (array_key_exists($key, $map)){ + return $map[$key]; + } + return ''; + } +}
\ No newline at end of file diff --git a/tests/Mock/ScannerFactory.php b/tests/Mock/ScannerFactory.php new file mode 100644 index 0000000..ac298b6 --- /dev/null +++ b/tests/Mock/ScannerFactory.php @@ -0,0 +1,19 @@ +<?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\Mock; + +use OCA\Files_Antivirus\Scanner\External; + +class ScannerFactory extends \OCA\Files_antivirus\ScannerFactory{ + public function getScanner() { + return new External($this->appConfig); + } +} diff --git a/tests/TestBase.php b/tests/TestBase.php index d33b1a0..08ef6fd 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -17,7 +17,6 @@ abstract class TestBase extends \PHPUnit_Framework_TestCase { protected $application; protected $container; protected $config; - protected $streamConfig; protected $l10n; @@ -37,12 +36,6 @@ 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() @@ -59,16 +52,4 @@ 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'; - } - } } |