This commit is contained in:
Victor Dubiniuk 2016-11-11 23:13:53 +03:00
Родитель 7dff334279
Коммит b3480aef40
6 изменённых файлов: 213 добавлений и 2 удалений

Просмотреть файл

@ -29,6 +29,7 @@ script:
# Run phpunit tests
- cd tests
- php avirserver.php &
- phpunit --configuration phpunit.xml
# Create coverage report

107
tests/AvirWrapperTest.php Normal file
Просмотреть файл

@ -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();
}
}

68
tests/DummyClam.php Normal file
Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -51,4 +51,9 @@ class ItemTest extends TestBase {
$chunk = $item->fread();
$this->assertEquals(self::CONTENT, $chunk);
}
public function tearDown() {
parent::tearDown();
\OC_Util::tearDownFS();
}
}

Просмотреть файл

@ -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';
}
}
}

10
tests/avirserver.php Normal file
Просмотреть файл

@ -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();