1
0
mirror of https://github.com/moodle/moodle.git synced 2025-04-14 13:02:07 +02:00
This commit is contained in:
Andrew Nicols 2020-01-14 14:15:46 +08:00
commit 6813b79f81
6 changed files with 361 additions and 91 deletions

@ -49,17 +49,56 @@ class antivirus_clamav_runningmethod_setting extends admin_setting_configselect
/**
* Validate data.
*
* This ensures that unix socket transport is supported by this system.
* This ensures that the selected socket transport is supported by this system.
*
* @param string $data
* @return mixed True on success, else error message.
*/
public function validate($data) {
$supportedtransports = stream_get_transports();
if ($data === 'unixsocket') {
$supportedtransports = stream_get_transports();
if (!array_search('unix', $supportedtransports)) {
if (array_search('unix', $supportedtransports) === false) {
return get_string('errornounixsocketssupported', 'antivirus_clamav');
}
} else if ($data === 'tcpsocket') {
if (array_search('tcp', $supportedtransports) === false) {
return get_string('errornotcpsocketssupported', 'antivirus_clamav');
}
}
return true;
}
}
/**
* Abstract socket checking class
*
* @package antivirus_clamav
* @copyright 2015 Ruslan Kabalin, Lancaster University.
* @copyright 2019 Didier Raboud, Liip AG.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class antivirus_clamav_socket_setting extends admin_setting_configtext {
/**
* Ping ClamAV socket.
*
* This ensures that a socket setting is correct and that ClamAV is running.
*
* @param string $socketaddress Address to the socket to connect to (for stream_socket_client)
* @return mixed True on success, else error message.
*/
protected function validate_clamav_socket($socketaddress) {
$socket = stream_socket_client($socketaddress, $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
if (!$socket) {
return get_string('errorcantopensocket', 'antivirus_clamav', "$errstr ($errno)");
} else {
// Send PING query to ClamAV socket to check its running state.
fwrite($socket, "nPING\n");
$response = stream_get_line($socket, 4);
fclose($socket);
if ($response !== 'PONG') {
return get_string('errorclamavnoresponse', 'antivirus_clamav');
}
}
return true;
}
@ -71,7 +110,7 @@ class antivirus_clamav_runningmethod_setting extends admin_setting_configselect
* @copyright 2015 Ruslan Kabalin, Lancaster University.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class antivirus_clamav_pathtounixsocket_setting extends admin_setting_configtext {
class antivirus_clamav_pathtounixsocket_setting extends antivirus_clamav_socket_setting {
/**
* Validate data.
*
@ -87,19 +126,38 @@ class antivirus_clamav_pathtounixsocket_setting extends admin_setting_configtext
}
$runningmethod = get_config('antivirus_clamav', 'runningmethod');
if ($runningmethod === 'unixsocket') {
$socket = stream_socket_client('unix://' . $data, $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
if (!$socket) {
return get_string('errorcantopensocket', 'antivirus_clamav', "$errstr ($errno)");
} else {
// Send PING query to ClamAV socket to check its running state.
fwrite($socket, "nPING\n");
$response = stream_get_line($socket, 4);
fclose($socket);
if ($response !== 'PONG') {
return get_string('errorclamavnoresponse', 'antivirus_clamav');
}
}
return $this->validate_clamav_socket('unix://' . $data);
}
return true;
}
}
/**
* Admin setting for Internet domain socket host, adds verification.
*
* @package antivirus_clamav
* @copyright 2019 Didier Raboud, Liip AG.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class antivirus_clamav_tcpsockethost_setting extends antivirus_clamav_socket_setting {
/**
* Validate data.
*
* This ensures that Internet domain socket setting is correct and ClamAV is running.
*
* @param string $data
* @return mixed True on success, else error message.
*/
public function validate($data) {
$result = parent::validate($data);
if ($result !== true) {
return $result;
}
$runningmethod = get_config('antivirus_clamav', 'runningmethod');
$tcpport = get_config('antivirus_clamav', 'tcpsocketport');
if ($runningmethod === 'tcpsocket') {
return $this->validate_clamav_socket('tcp://' . $data . ':' . $tcpport);
}
return true;
}
}

@ -34,6 +34,7 @@ define('ANTIVIRUS_CLAMAV_SOCKET_CHUNKSIZE', 1024);
/**
* Class implementing ClamAV antivirus.
* @copyright 2015 Ruslan Kabalin, Lancaster University.
* @copyright 2019 Didier Raboud, Liip AG.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scanner extends \core\antivirus\scanner {
@ -47,6 +48,8 @@ class scanner extends \core\antivirus\scanner {
return (bool)$this->get_config('pathtoclam');
} else if ($this->get_config('runningmethod') === 'unixsocket') {
return (bool)$this->get_config('pathtounixsocket');
} else if ($this->get_config('runningmethod') === 'tcpsocket') {
return (bool)$this->get_config('tcpsockethost') && (bool)$this->get_config('tcpsocketport');
}
return false;
}
@ -67,12 +70,22 @@ class scanner extends \core\antivirus\scanner {
return self::SCAN_RESULT_ERROR;
}
// Execute the scan using preferable method.
$method = 'scan_file_execute_' . $this->get_config('runningmethod');
if (!method_exists($this, $method)) {
throw new \coding_exception('Attempting to call non-existing method ' . $method);
// We can do direct stream scanning if unixsocket or tcpsocket running methods are in use,
// if not, use default process.
$runningmethod = $this->get_config('runningmethod');
switch ($runningmethod) {
case 'unixsocket':
case 'tcpsocket':
$return = $this->scan_file_execute_socket($file, $runningmethod);
break;
case 'commandline':
$return = $this->scan_file_execute_commandline($file);
break;
default:
// This should not happen.
debugging('Unknown running method.');
return self::SCAN_RESULT_ERROR;
}
$return = $this->$method($file);
if ($return === self::SCAN_RESULT_ERROR) {
$this->message_admins($this->get_scanning_notice());
@ -92,10 +105,11 @@ class scanner extends \core\antivirus\scanner {
* @return int Scanning result constant.
*/
public function scan_data($data) {
// We can do direct stream scanning if unixsocket running method is in use,
// We can do direct stream scanning if unixsocket or tcpsocket running methods are in use,
// if not, use default process.
if ($this->get_config('runningmethod') === 'unixsocket') {
$return = $this->scan_data_execute_unixsocket($data);
$runningmethod = $this->get_config('runningmethod');
if (in_array($runningmethod, array('unixsocket', 'tcpsocket'))) {
$return = $this->scan_data_execute_socket($data, $runningmethod);
if ($return === self::SCAN_RESULT_ERROR) {
$this->message_admins($this->get_scanning_notice());
@ -111,6 +125,24 @@ class scanner extends \core\antivirus\scanner {
}
}
/**
* Returns a Unix domain socket destination url
*
* @return string The socket url, fit for stream_socket_client()
*/
private function get_unixsocket_destination() {
return 'unix://' . $this->get_config('pathtounixsocket');
}
/**
* Returns a Internet domain socket destination url
*
* @return string The socket url, fit for stream_socket_client()
*/
private function get_tcpsocket_destination() {
return 'tcp://' . $this->get_config('tcpsockethost') . ':' . $this->get_config('tcpsocketport');
}
/**
* Returns the string equivalent of a numeric clam error code
*
@ -189,13 +221,27 @@ class scanner extends \core\antivirus\scanner {
}
/**
* Scan file using Unix domain sockets.
* Scan file using sockets.
*
* @param string $file Full path to the file.
* @param string $type Either 'tcpsocket' or 'unixsocket'
* @return int Scanning result constant.
*/
public function scan_file_execute_unixsocket($file) {
$socket = stream_socket_client('unix://' . $this->get_config('pathtounixsocket'),
public function scan_file_execute_socket($file, $type) {
switch ($type) {
case "tcpsocket":
$socketurl = $this->get_tcpsocket_destination();
break;
case "unixsocket":
$socketurl = $this->get_unixsocket_destination();
break;
default;
// This should not happen.
debugging('Unknown socket type.');
return self::SCAN_RESULT_ERROR;
}
$socket = stream_socket_client($socketurl,
$errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
if (!$socket) {
// Can't open socket for some reason, notify admins.
@ -203,26 +249,57 @@ class scanner extends \core\antivirus\scanner {
$this->set_scanning_notice($notice);
return self::SCAN_RESULT_ERROR;
} else {
// Execute scanning. We are running SCAN command and passing file as an argument,
// it is the fastest option, but clamav user need to be able to access it, so
// we give group read permissions first and assume 'clamav' user is in web server
// group (in Debian the default webserver group is 'www-data').
// Using 'n' as command prefix is forcing clamav to only treat \n as newline delimeter,
// this is to avoid unexpected newline characters on different systems.
$perms = fileperms($file);
chmod($file, 0640);
fwrite($socket, "nSCAN ".$file."\n");
$output = stream_get_line($socket, 4096);
if ($type == "unixsocket") {
// Execute scanning. We are running SCAN command and passing file as an argument,
// it is the fastest option, but clamav user need to be able to access it, so
// we give group read permissions first and assume 'clamav' user is in web server
// group (in Debian the default webserver group is 'www-data').
// Using 'n' as command prefix is forcing clamav to only treat \n as newline delimeter,
// this is to avoid unexpected newline characters on different systems.
$perms = fileperms($file);
chmod($file, 0640);
// Actual scan.
fwrite($socket, "nSCAN ".$file."\n");
// Get ClamAV answer.
$output = stream_get_line($socket, 4096);
// After scanning we revert permissions to initial ones.
chmod($file, $perms);
} else if ($type == "tcpsocket") {
// Execute scanning by passing the entire file through the TCP socket.
// This is not fast, but is the only possibility over a network.
// Using 'n' as command prefix is forcing clamav to only treat \n as newline delimeter,
// this is to avoid unexpected newline characters on different systems.
// Actual scan.
fwrite($socket, "nINSTREAM\n");
// Open the file for reading.
$fhandle = fopen($file, 'rb');
while (!feof($fhandle)) {
// Read it by chunks; write them to the TCP socket.
$chunk = fread($fhandle, ANTIVIRUS_CLAMAV_SOCKET_CHUNKSIZE);
$size = pack('N', strlen($chunk));
fwrite($socket, $size);
fwrite($socket, $chunk);
}
// Terminate streaming.
fwrite($socket, pack('N', 0));
// Get ClamAV answer.
$output = stream_get_line($socket, 4096);
fclose($fhandle);
}
// Free up the ClamAV socket.
fclose($socket);
// After scanning we revert permissions to initial ones.
chmod($file, $perms);
// Parse the output.
return $this->parse_unixsocket_response($output);
return $this->parse_socket_response($output);
}
}
/**
* Scan data using unix socket.
* Scan data socket.
*
* We are running INSTREAM command and passing data stream in chunks.
* The format of the chunk is: <length><data> where <length> is the size of the following
@ -231,11 +308,25 @@ class scanner extends \core\antivirus\scanner {
* Do not exceed StreamMaxLength as defined in clamd.conf, otherwise clamd will
* reply with INSTREAM size limit exceeded and close the connection.
*
* @param string $data The varaible containing the data to scan.
* @param string $data The variable containing the data to scan.
* @param string $type Either 'tcpsocket' or 'unixsocket'
* @return int Scanning result constant.
*/
public function scan_data_execute_unixsocket($data) {
$socket = stream_socket_client('unix://' . $this->get_config('pathtounixsocket'), $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
public function scan_data_execute_socket($data, $type) {
switch ($type) {
case "tcpsocket":
$socketurl = $this->get_tcpsocket_destination();
break;
case "unixsocket":
$socketurl = $this->get_unixsocket_destination();
break;
default;
// This should not happen.
debugging('Unknown socket type!');
return self::SCAN_RESULT_ERROR;
}
$socket = stream_socket_client($socketurl, $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
if (!$socket) {
// Can't open socket for some reason, notify admins.
$notice = get_string('errorcantopensocket', 'antivirus_clamav', "$errstr ($errno)");
@ -261,17 +352,17 @@ class scanner extends \core\antivirus\scanner {
fclose($socket);
// Parse the output.
return $this->parse_unixsocket_response($output);
return $this->parse_socket_response($output);
}
}
/**
* Parse unix socket command response.
* Parse socket command response.
*
* @param string $output The unix socket command response.
* @param string $output The socket response.
* @return int Scanning result constant.
*/
private function parse_unixsocket_response($output) {
private function parse_socket_response($output) {
$splitoutput = explode(': ', $output);
$message = trim($splitoutput[1]);
if ($message === 'OK') {
@ -289,4 +380,35 @@ class scanner extends \core\antivirus\scanner {
}
}
}
/**
* Scan data using Unix domain socket.
*
* @deprecated since Moodle 3.9 MDL-64075 - please do not use this function any more.
* @see antivirus_clamav\scanner::scan_data_execute_socket()
*
* @param string $data The variable containing the data to scan.
* @return int Scanning result constant.
*/
public function scan_data_execute_unixsocket($data) {
debugging('antivirus_clamav\scanner::scan_data_execute_unixsocket() is deprecated. ' .
'Use antivirus_clamav\scanner::scan_data_execute_socket() instead.', DEBUG_DEVELOPER);
return $this->scan_data_execute_socket($data, "unixsocket");
}
/**
* Scan file using Unix domain socket.
*
* @deprecated since Moodle 3.9 MDL-64075 - please do not use this function any more.
* @see antivirus_clamav\scanner::scan_file_execute_socket()
*
* @param string $file Full path to the file.
* @return int Scanning result constant.
*/
public function scan_file_execute_unixsocket($file) {
debugging('antivirus_clamav\scanner::scan_file_execute_unixsocket() is deprecated. ' .
'Use antivirus_clamav\scanner::scan_file_execute_socket() instead.', DEBUG_DEVELOPER);
return $this->scan_file_execute_socket($file, "unixsocket");
}
}

@ -42,4 +42,9 @@ $string['runningmethod'] = 'Running method';
$string['runningmethoddesc'] = 'Method of running ClamAV. Command line is used by default, however on Unix systems better performance can be obtained by using system sockets.';
$string['runningmethodcommandline'] = 'Command line';
$string['runningmethodunixsocket'] = 'Unix domain socket';
$string['runningmethodtcpsocket'] = 'TCP socket';
$string['tcpsockethost'] = 'TCP socket hostname';
$string['tcpsockethostdesc'] = 'Domain name of the ClamAV server';
$string['tcpsocketport'] = 'TCP socket port';
$string['tcpsocketportdesc'] = 'The port to use when connecting to ClamAV';
$string['unknownerror'] = 'There was an unknown error with ClamAV.';

@ -32,6 +32,7 @@ if ($ADMIN->fulltree) {
$runningmethodchoice = array(
'commandline' => get_string('runningmethodcommandline', 'antivirus_clamav'),
'unixsocket' => get_string('runningmethodunixsocket', 'antivirus_clamav'),
'tcpsocket' => get_string('runningmethodtcpsocket', 'antivirus_clamav'),
);
$settings->add(new antivirus_clamav_runningmethod_setting('antivirus_clamav/runningmethod',
get_string('runningmethod', 'antivirus_clamav'),
@ -47,6 +48,16 @@ if ($ADMIN->fulltree) {
new lang_string('pathtounixsocket', 'antivirus_clamav'),
new lang_string('pathtounixsocketdesc', 'antivirus_clamav'), '', PARAM_PATH));
// Hostname to reach ClamAV tcp socket (used in tcp socket running method).
$settings->add(new antivirus_clamav_tcpsockethost_setting('antivirus_clamav/tcpsockethost',
new lang_string('tcpsockethost', 'antivirus_clamav'),
new lang_string('tcpsockethostdesc', 'antivirus_clamav'), '', PARAM_HOST));
// Port to reach ClamAV tcp socket (used in tcp socket running method).
$settings->add(new admin_setting_configtext('antivirus_clamav/tcpsocketport',
new lang_string('tcpsocketport', 'antivirus_clamav'),
new lang_string('tcpsocketportdesc', 'antivirus_clamav'), 3310, PARAM_INT));
// How to act on ClamAV failure.
$options = array(
'donothing' => new lang_string('configclamdonothing', 'antivirus_clamav'),

@ -44,8 +44,8 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
public function test_scan_file_not_exists() {
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods(array('scan_file_execute_commandline', 'message_admins'))
->getMock();
->setMethods(array('scan_file_execute_commandline', 'message_admins'))
->getMock();
// Test specifying file that does not exist.
$nonexistingfile = $this->tempfile . '_';
@ -58,21 +58,21 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
public function test_scan_file_no_virus() {
$methods = array(
'scan_file_execute_commandline',
'scan_file_execute_unixsocket',
'scan_file_execute_socket',
'message_admins',
'get_config',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Initiate mock scanning with configuration setting to use commandline.
$configmap = array(array('runningmethod', 'commandline'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Configure scan_file_execute_commandline and scan_file_execute_unixsocket
// Configure scan_file_execute_commandline and scan_file_execute_socket
// method stubs to behave as if no virus has been found (SCAN_RESULT_OK).
$antivirus->method('scan_file_execute_commandline')->willReturn(0);
$antivirus->method('scan_file_execute_unixsocket')->willReturn(0);
$antivirus->method('scan_file_execute_socket')->willReturn(0);
// Set expectation that message_admins is NOT called.
$antivirus->expects($this->never())->method('message_admins');
@ -87,26 +87,33 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
// Run mock scanning.
$this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
// Initiate mock scanning with configuration setting to use tcpsocket.
$configmap = array(array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Run mock scanning.
$this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
}
public function test_scan_file_virus() {
$methods = array(
'scan_file_execute_commandline',
'scan_file_execute_unixsocket',
'scan_file_execute_socket',
'message_admins',
'get_config',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Initiate mock scanning with configuration setting to use commandline.
$configmap = array(array('runningmethod', 'commandline'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Configure scan_file_execute_commandline and scan_file_execute_unixsocket
// Configure scan_file_execute_commandline and scan_file_execute_socket
// method stubs to behave as if virus has been found (SCAN_RESULT_FOUND).
$antivirus->method('scan_file_execute_commandline')->willReturn(1);
$antivirus->method('scan_file_execute_unixsocket')->willReturn(1);
$antivirus->method('scan_file_execute_socket')->willReturn(1);
// Set expectation that message_admins is NOT called.
$antivirus->expects($this->never())->method('message_admins');
@ -121,24 +128,31 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
// Run mock scanning.
$this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
// Initiate mock scanning with configuration setting to use tcpsocket.
$configmap = array(array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Run mock scanning.
$this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
}
public function test_scan_file_error_donothing() {
$methods = array(
'scan_file_execute_commandline',
'scan_file_execute_unixsocket',
'scan_file_execute_socket',
'message_admins',
'get_config',
'get_scanning_notice',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Configure scan_file_execute_commandline and scan_file_execute_unixsocket
// Configure scan_file_execute_commandline and scan_file_execute_socket
// method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
$antivirus->method('scan_file_execute_commandline')->willReturn(2);
$antivirus->method('scan_file_execute_unixsocket')->willReturn(2);
$antivirus->method('scan_file_execute_socket')->willReturn(2);
$antivirus->method('get_scanning_notice')->willReturn('someerror');
// Set expectation that message_admins is called.
@ -160,24 +174,32 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
// Run mock scanning.
$this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
// Initiate mock scanning with configuration setting to do nothing on
// scanning error and using tcpsocket.
$configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Run mock scanning.
$this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
}
public function test_scan_file_error_actlikevirus() {
$methods = array(
'scan_file_execute_commandline',
'scan_file_execute_unixsocket',
'scan_file_execute_socket',
'message_admins',
'get_config',
'get_scanning_notice',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Configure scan_file_execute_commandline and scan_file_execute_unixsocket
// Configure scan_file_execute_commandline and scan_file_execute_socket
// method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
$antivirus->method('scan_file_execute_commandline')->willReturn(2);
$antivirus->method('scan_file_execute_unixsocket')->willReturn(2);
$antivirus->method('scan_file_execute_socket')->willReturn(2);
$antivirus->method('get_scanning_notice')->willReturn('someerror');
// Set expectation that message_admins is called.
@ -201,24 +223,43 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
// Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
// require us to act like virus.
$this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
// Initiate mock scanning with configuration setting to act like virus on
// scanning error and using tcpsocket.
$configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
// require us to act like virus.
$this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
}
public function test_scan_data_no_virus() {
$methods = array(
'scan_data_execute_unixsocket',
'scan_data_execute_socket',
'message_admins',
'get_config',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Initiate mock scanning with configuration setting to use unixsocket.
$configmap = array(array('runningmethod', 'unixsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Configure scan_data_execute_unixsocket method stubs to behave as if
// Configure scan_data_execute_socket method stubs to behave as if
// no virus has been found (SCAN_RESULT_OK).
$antivirus->method('scan_data_execute_unixsocket')->willReturn(0);
$antivirus->method('scan_data_execute_socket')->willReturn(0);
// Set expectation that message_admins is NOT called.
$antivirus->expects($this->never())->method('message_admins');
// Run mock scanning.
$this->assertEquals(0, $antivirus->scan_data(''));
// Re-initiate mock scanning with configuration setting to use tcpsocket.
$configmap = array(array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Set expectation that message_admins is NOT called.
$antivirus->expects($this->never())->method('message_admins');
@ -229,20 +270,30 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
public function test_scan_data_virus() {
$methods = array(
'scan_data_execute_unixsocket',
'scan_data_execute_socket',
'message_admins',
'get_config',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Initiate mock scanning with configuration setting to use unixsocket.
$configmap = array(array('runningmethod', 'unixsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Configure scan_data_execute_unixsocket method stubs to behave as if
// Configure scan_data_execute_socket method stubs to behave as if
// no virus has been found (SCAN_RESULT_FOUND).
$antivirus->method('scan_data_execute_unixsocket')->willReturn(1);
$antivirus->method('scan_data_execute_socket')->willReturn(1);
// Set expectation that message_admins is NOT called.
$antivirus->expects($this->never())->method('message_admins');
// Run mock scanning.
$this->assertEquals(1, $antivirus->scan_data(''));
// Re-initiate mock scanning with configuration setting to use tcpsocket.
$configmap = array(array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Set expectation that message_admins is NOT called.
$antivirus->expects($this->never())->method('message_admins');
@ -253,22 +304,22 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
public function test_scan_data_error_donothing() {
$methods = array(
'scan_data_execute_unixsocket',
'scan_data_execute_socket',
'message_admins',
'get_config',
'get_scanning_notice',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Initiate mock scanning with configuration setting to do nothing on
// scanning error and using unixsocket.
$configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'unixsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Configure scan_data_execute_unixsocket method stubs to behave as if
// Configure scan_data_execute_socket method stubs to behave as if
// there is a scanning error (SCAN_RESULT_ERROR).
$antivirus->method('scan_data_execute_unixsocket')->willReturn(2);
$antivirus->method('scan_data_execute_socket')->willReturn(2);
$antivirus->method('get_scanning_notice')->willReturn('someerror');
// Set expectation that message_admins is called.
@ -276,27 +327,38 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
// Run mock scanning.
$this->assertEquals(2, $antivirus->scan_data(''));
// Re-initiate mock scanning with configuration setting to do nothing on
// scanning error and using tcsocket.
$configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Set expectation that message_admins is called.
$antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
// Run mock scanning.
$this->assertEquals(2, $antivirus->scan_data(''));
}
public function test_scan_data_error_actlikevirus() {
$methods = array(
'scan_data_execute_unixsocket',
'scan_data_execute_socket',
'message_admins',
'get_config',
'get_scanning_notice',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
->setMethods($methods)
->getMock();
->setMethods($methods)
->getMock();
// Initiate mock scanning with configuration setting to act like virus on
// scanning error and using unixsocket.
$configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'unixsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Configure scan_data_execute_unixsocket method stubs to behave as if
// Configure scan_data_execute_socket method stubs to behave as if
// there is a scanning error (SCAN_RESULT_ERROR).
$antivirus->method('scan_data_execute_unixsocket')->willReturn(2);
$antivirus->method('scan_data_execute_socket')->willReturn(2);
$antivirus->method('get_scanning_notice')->willReturn('someerror');
// Set expectation that message_admins is called.
@ -305,5 +367,17 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
// Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
// require us to act like virus.
$this->assertEquals(1, $antivirus->scan_data(''));
// Re-initiate mock scanning with configuration setting to act like virus on
// scanning error and using tcpsocket.
$configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'tcpsocket'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Set expectation that message_admins is called.
$antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
// Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
// require us to act like virus.
$this->assertEquals(1, $antivirus->scan_data(''));
}
}

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2019111800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2019122900; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2019111200; // Requires this Moodle version.
$plugin->component = 'antivirus_clamav'; // Full name of the plugin (used for diagnostics).