2016-02-22 16:05:14 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-05-18 16:53:03 +10:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
require_once(__DIR__ . '/fixtures/testable_antivirus.php');
|
|
|
|
|
2016-02-22 16:05:14 +00:00
|
|
|
/**
|
|
|
|
* Tests for antivirus manager.
|
|
|
|
*
|
|
|
|
* @package core_antivirus
|
2021-05-18 16:53:03 +10:00
|
|
|
* @category test
|
2016-02-22 16:05:14 +00:00
|
|
|
* @copyright 2016 Ruslan Kabalin, Lancaster University.
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2021-05-18 16:53:03 +10:00
|
|
|
class antivirus_test extends advanced_testcase {
|
2016-02-22 16:05:14 +00:00
|
|
|
|
2021-05-18 16:53:03 +10:00
|
|
|
/**
|
|
|
|
* @var string Path to the tempfile created for use with AV scanner tests
|
|
|
|
*/
|
2016-04-14 10:37:44 +01:00
|
|
|
protected $tempfile;
|
|
|
|
|
2020-08-02 10:59:12 +02:00
|
|
|
protected function setUp(): void {
|
2016-04-14 10:37:44 +01:00
|
|
|
global $CFG;
|
|
|
|
// Use our special testable fixture plugin.
|
|
|
|
$CFG->antiviruses = 'testable';
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
// Create tempfile.
|
|
|
|
$tempfolder = make_request_directory(false);
|
|
|
|
$this->tempfile = $tempfolder . '/' . rand();
|
|
|
|
touch($this->tempfile);
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:53:03 +10:00
|
|
|
/**
|
|
|
|
* Enable logging.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function enable_logging() {
|
|
|
|
$this->preventResetByRollback();
|
|
|
|
set_config('enabled_stores', 'logstore_standard', 'tool_log');
|
|
|
|
set_config('buffersize', 0, 'logstore_standard');
|
|
|
|
set_config('logguests', 1, 'logstore_standard');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return check api status for the antivirus check.
|
|
|
|
*
|
|
|
|
* @return string Based on status of \core\check\result.
|
|
|
|
*/
|
|
|
|
protected function get_check_api_antivirus_status_result() {
|
|
|
|
$av = new \core\check\environment\antivirus();
|
|
|
|
return $av->get_result()->status;
|
|
|
|
}
|
|
|
|
|
2020-08-02 10:59:12 +02:00
|
|
|
protected function tearDown(): void {
|
2016-04-14 10:37:44 +01:00
|
|
|
@unlink($this->tempfile);
|
|
|
|
}
|
2016-02-22 16:05:14 +00:00
|
|
|
|
|
|
|
public function test_manager_get_antivirus() {
|
|
|
|
// We are using clamav plugin in the test,
|
|
|
|
// as the only plugin we know exists for sure.
|
|
|
|
$antivirusviaget = \core\antivirus\manager::get_antivirus('clamav');
|
|
|
|
$antivirusdirect = new \antivirus_clamav\scanner();
|
|
|
|
$this->assertEquals($antivirusdirect, $antivirusviaget);
|
|
|
|
}
|
2016-04-14 10:37:44 +01:00
|
|
|
|
|
|
|
public function test_manager_scan_file_no_virus() {
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
2017-07-20 12:12:38 +01:00
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
2016-04-14 10:37:44 +01:00
|
|
|
// File expected to remain in place.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_scan_file_error() {
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
2017-07-20 12:12:38 +01:00
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true));
|
2016-04-14 10:37:44 +01:00
|
|
|
// File expected to remain in place.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:53:03 +10:00
|
|
|
// Check API for NA status i.e. when no scanners are enabled.
|
|
|
|
public function test_antivirus_check_na() {
|
|
|
|
global $CFG;
|
|
|
|
$CFG->antiviruses = '';
|
|
|
|
// Enable logs.
|
|
|
|
$this->enable_logging();
|
|
|
|
set_config('enabled_stores', 'logstore_standard', 'tool_log');
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
|
|
|
$this->assertEquals(\core\check\result::NA, $this->get_check_api_antivirus_status_result());
|
|
|
|
// File expected to remain in place.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check API for UNKNOWN status i.e. when the system's logstore reader is not '\core\log\sql_internal_table_reader'.
|
|
|
|
public function test_antivirus_check_unknown() {
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
|
|
|
$this->assertEquals(\core\check\result::UNKNOWN, $this->get_check_api_antivirus_status_result());
|
|
|
|
// File expected to remain in place.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check API for OK status i.e. antivirus enabled, logstore is ok, no scanner issues occurred recently.
|
|
|
|
public function test_antivirus_check_ok() {
|
|
|
|
// Enable logs.
|
|
|
|
$this->enable_logging();
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
|
|
|
|
$this->assertEquals(\core\check\result::OK, $this->get_check_api_antivirus_status_result());
|
|
|
|
// File expected to remain in place.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check API for ERROR status i.e. scanner issue within a certain timeframe/threshold.
|
|
|
|
public function test_antivirus_check_error() {
|
|
|
|
global $USER, $DB;
|
|
|
|
// Enable logs.
|
|
|
|
$this->enable_logging();
|
|
|
|
// Set threshold / lookback.
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true));
|
|
|
|
$this->assertEquals(\core\check\result::ERROR, $this->get_check_api_antivirus_status_result());
|
|
|
|
// File expected to remain in place.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
}
|
|
|
|
|
2016-04-14 10:37:44 +01:00
|
|
|
public function test_manager_scan_file_virus() {
|
|
|
|
// Run mock scanning without deleting infected file.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
2017-07-20 12:12:38 +01:00
|
|
|
$this->expectException(\core\antivirus\scanner_exception::class);
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', false));
|
2016-04-14 10:37:44 +01:00
|
|
|
// File expected to remain in place.
|
|
|
|
$this->assertFileExists($this->tempfile);
|
|
|
|
|
|
|
|
// Run mock scanning with deleting infected file.
|
2017-07-20 12:12:38 +01:00
|
|
|
$this->expectException(\core\antivirus\scanner_exception::class);
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true));
|
2016-04-14 10:37:44 +01:00
|
|
|
// File expected to be deleted.
|
2021-02-19 16:24:16 +01:00
|
|
|
$this->assertFileDoesNotExist($this->tempfile);
|
2016-04-14 10:37:44 +01:00
|
|
|
}
|
2017-07-20 12:12:38 +01:00
|
|
|
|
2020-03-15 20:09:48 +11:00
|
|
|
public function test_manager_send_message_to_user_email_scan_file_virus() {
|
|
|
|
$sink = $this->redirectEmails();
|
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
set_config('notifyemail', 'fake@example.com', 'antivirus');
|
|
|
|
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
$result = $sink->get_messages();
|
|
|
|
$this->assertCount(1, $result);
|
2020-09-01 00:44:13 +02:00
|
|
|
$this->assertStringContainsString('fake@example.com', $result[0]->to);
|
2020-03-15 20:09:48 +11:00
|
|
|
$sink->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_send_message_to_admin_email_scan_file_virus() {
|
|
|
|
$sink = $this->redirectMessages();
|
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
$result = $sink->get_messages();
|
|
|
|
$admins = array_keys(get_admins());
|
|
|
|
$this->assertCount(1, $admins);
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
$this->assertEquals($result[0]->useridto, reset($admins));
|
|
|
|
$sink->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_quarantine_file_virus() {
|
|
|
|
try {
|
|
|
|
set_config('enablequarantine', true, 'antivirus');
|
|
|
|
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
// Quarantined files.
|
|
|
|
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
|
|
|
$this->assertEquals(1, count($quarantinedfiles));
|
|
|
|
// Clean up.
|
|
|
|
\core\antivirus\quarantine::clean_up_quarantine_folder(time());
|
|
|
|
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
|
|
|
$this->assertEquals(0, count($quarantinedfiles));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_none_quarantine_file_virus() {
|
|
|
|
try {
|
|
|
|
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
|
|
|
$this->assertEquals(0, count($quarantinedfiles));
|
|
|
|
}
|
|
|
|
|
2017-07-20 12:12:38 +01:00
|
|
|
public function test_manager_scan_data_no_virus() {
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_data('OK'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_scan_data_error() {
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_data('ERROR'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_scan_data_virus() {
|
|
|
|
// Run mock scanning.
|
|
|
|
$this->expectException(\core\antivirus\scanner_exception::class);
|
|
|
|
$this->assertEmpty(\core\antivirus\manager::scan_data('FOUND'));
|
|
|
|
}
|
2020-03-15 20:09:48 +11:00
|
|
|
|
|
|
|
public function test_manager_send_message_to_user_email_scan_data_virus() {
|
|
|
|
$sink = $this->redirectEmails();
|
|
|
|
set_config('notifyemail', 'fake@example.com', 'antivirus');
|
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
\core\antivirus\manager::scan_data('FOUND');
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
$result = $sink->get_messages();
|
|
|
|
$this->assertCount(1, $result);
|
2020-09-01 00:44:13 +02:00
|
|
|
$this->assertStringContainsString('fake@example.com', $result[0]->to);
|
2020-03-15 20:09:48 +11:00
|
|
|
$sink->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_send_message_to_admin_email_scan_data_virus() {
|
|
|
|
$sink = $this->redirectMessages();
|
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
\core\antivirus\manager::scan_data('FOUND');
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
$result = $sink->get_messages();
|
|
|
|
$admins = array_keys(get_admins());
|
|
|
|
$this->assertCount(1, $admins);
|
|
|
|
$this->assertCount(1, $result);
|
|
|
|
$this->assertEquals($result[0]->useridto, reset($admins));
|
|
|
|
$sink->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_manager_quarantine_data_virus() {
|
|
|
|
set_config('enablequarantine', true, 'antivirus');
|
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
\core\antivirus\manager::scan_data('FOUND');
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
// Quarantined files.
|
|
|
|
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
|
|
|
$this->assertEquals(1, count($quarantinedfiles));
|
|
|
|
// Clean up.
|
|
|
|
\core\antivirus\quarantine::clean_up_quarantine_folder(time());
|
|
|
|
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
|
|
|
$this->assertEquals(0, count($quarantinedfiles));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function test_manager_none_quarantine_data_virus() {
|
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
\core\antivirus\manager::scan_data('FOUND');
|
|
|
|
} catch (\core\antivirus\scanner_exception $ex) {
|
|
|
|
$exception = $ex;
|
|
|
|
}
|
|
|
|
$this->assertNotEmpty($exception);
|
|
|
|
// No Quarantined files.
|
|
|
|
$quarantinedfiles = \core\antivirus\quarantine::get_quarantined_files();
|
|
|
|
$this->assertEquals(0, count($quarantinedfiles));
|
|
|
|
}
|
2016-02-22 16:05:14 +00:00
|
|
|
}
|