Skip to content

Commit

Permalink
MDL-50907 antivirus_clamav: Add test coverage for scan_data.
Browse files Browse the repository at this point in the history
  • Loading branch information
kabalin authored and andrewnicols committed Apr 12, 2018
1 parent 694cb8e commit 30e1797
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/antivirus/clamav/classes/scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function scan_file($file, $filename) {
/**
* 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) {
Expand Down Expand Up @@ -234,7 +234,7 @@ public function scan_file_execute_unixsocket($file) {
* @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.
Expand Down
104 changes: 104 additions & 0 deletions lib/antivirus/clamav/tests/scanner_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,108 @@ public function test_scan_file_error_actlikevirus() {
// 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(''));
}
}

0 comments on commit 30e1797

Please sign in to comment.