mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-50907 antivirus_clamav: Add test coverage for scan_data.
This commit is contained in:
parent
694cb8eb21
commit
30e1797f29
2 changed files with 106 additions and 2 deletions
|
@ -88,7 +88,7 @@ class scanner extends \core\antivirus\scanner {
|
|||
/**
|
||||
* Scan data.
|
||||
*
|
||||
* @param string $data The varaible containing the data to scan.
|
||||
* @param string $data The variable containing the data to scan.
|
||||
* @return int Scanning result constant.
|
||||
*/
|
||||
public function scan_data($data) {
|
||||
|
@ -234,7 +234,7 @@ class scanner extends \core\antivirus\scanner {
|
|||
* @param string $data The varaible containing the data to scan.
|
||||
* @return int Scanning result constant.
|
||||
*/
|
||||
private function scan_data_execute_unixsocket($data) {
|
||||
public function scan_data_execute_unixsocket($data) {
|
||||
$socket = stream_socket_client('unix://' . $this->get_config('pathtounixsocket'), $errno, $errstr, ANTIVIRUS_CLAMAV_SOCKET_TIMEOUT);
|
||||
if (!$socket) {
|
||||
// Can't open socket for some reason, notify admins.
|
||||
|
|
|
@ -202,4 +202,108 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
|
|||
// 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',
|
||||
'message_admins',
|
||||
'get_config',
|
||||
);
|
||||
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
|
||||
->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
|
||||
// no virus has been found (SCAN_RESULT_OK).
|
||||
$antivirus->method('scan_data_execute_unixsocket')->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(''));
|
||||
}
|
||||
|
||||
public function test_scan_data_virus() {
|
||||
$methods = array(
|
||||
'scan_data_execute_unixsocket',
|
||||
'message_admins',
|
||||
'get_config',
|
||||
);
|
||||
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
|
||||
->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
|
||||
// no virus has been found (SCAN_RESULT_FOUND).
|
||||
$antivirus->method('scan_data_execute_unixsocket')->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(''));
|
||||
}
|
||||
|
||||
public function test_scan_data_error_donothing() {
|
||||
$methods = array(
|
||||
'scan_data_execute_unixsocket',
|
||||
'message_admins',
|
||||
'get_config',
|
||||
'get_scanning_notice',
|
||||
);
|
||||
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
|
||||
->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
|
||||
// there is a scanning error (SCAN_RESULT_ERROR).
|
||||
$antivirus->method('scan_data_execute_unixsocket')->willReturn(2);
|
||||
$antivirus->method('get_scanning_notice')->willReturn('someerror');
|
||||
|
||||
// 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',
|
||||
'message_admins',
|
||||
'get_config',
|
||||
'get_scanning_notice',
|
||||
);
|
||||
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
|
||||
->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
|
||||
// there is a scanning error (SCAN_RESULT_ERROR).
|
||||
$antivirus->method('scan_data_execute_unixsocket')->willReturn(2);
|
||||
$antivirus->method('get_scanning_notice')->willReturn('someerror');
|
||||
|
||||
// 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(''));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue