<?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/>.
> defined('MOODLE_INTERNAL') || die();
/**
> require_once(__DIR__ . '/fixtures/testable_antivirus.php');
* Tests for antivirus manager.
>
*
* @package core_antivirus
< * @category phpunit
> * @category test
* @copyright 2016 Ruslan Kabalin, Lancaster University.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
> class antivirus_test extends advanced_testcase {
< defined('MOODLE_INTERNAL') || die();
< require_once(__DIR__ . '/fixtures/testable_antivirus.php');
<
< class core_antivirus_testcase extends advanced_testcase {
> /**
> * @var string Path to the tempfile created for use with AV scanner tests
> */
protected $tempfile;
protected function setUp(): void {
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);
}
> /**
protected function tearDown(): void {
> * Enable logging.
@unlink($this->tempfile);
> *
}
> * @return void
> */
public function test_manager_get_antivirus() {
> protected function enable_logging() {
// We are using clamav plugin in the test,
> $this->preventResetByRollback();
// as the only plugin we know exists for sure.
> set_config('enabled_stores', 'logstore_standard', 'tool_log');
$antivirusviaget = \core\antivirus\manager::get_antivirus('clamav');
> set_config('buffersize', 0, 'logstore_standard');
$antivirusdirect = new \antivirus_clamav\scanner();
> set_config('logguests', 1, 'logstore_standard');
$this->assertEquals($antivirusdirect, $antivirusviaget);
> }
}
>
> /**
public function test_manager_scan_file_no_virus() {
> * Return check api status for the antivirus check.
// Run mock scanning.
> *
$this->assertFileExists($this->tempfile);
> * @return string Based on status of \core\check\result.
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
> */
// File expected to remain in place.
> protected function get_check_api_antivirus_status_result() {
$this->assertFileExists($this->tempfile);
> $av = new \core\check\environment\antivirus();
}
> return $av->get_result()->status;
> }
public function test_manager_scan_file_error() {
>
// Run mock scanning.
$this->assertFileExists($this->tempfile);
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true));
// File expected to remain in place.
$this->assertFileExists($this->tempfile);
}
> // Check API for NA status i.e. when no scanners are enabled.
public function test_manager_scan_file_virus() {
> public function test_antivirus_check_na() {
// Run mock scanning without deleting infected file.
> global $CFG;
$this->assertFileExists($this->tempfile);
> $CFG->antiviruses = '';
$this->expectException(\core\antivirus\scanner_exception::class);
> // Enable logs.
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', false));
> $this->enable_logging();
// File expected to remain in place.
> set_config('enabled_stores', 'logstore_standard', 'tool_log');
$this->assertFileExists($this->tempfile);
> // Run mock scanning.
> $this->assertFileExists($this->tempfile);
// Run mock scanning with deleting infected file.
> $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
$this->expectException(\core\antivirus\scanner_exception::class);
> $this->assertEquals(\core\check\result::NA, $this->get_check_api_antivirus_status_result());
$this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true));
> // File expected to remain in place.
// File expected to be deleted.
> $this->assertFileExists($this->tempfile);
$this->assertFileNotExists($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_manager_send_message_to_user_email_scan_file_virus() {
> public function test_antivirus_check_unknown() {
$sink = $this->redirectEmails();
> // Run mock scanning.
$exception = null;
> $this->assertFileExists($this->tempfile);
try {
> $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
set_config('notifyemail', 'fake@example.com', 'antivirus');
> $this->assertEquals(\core\check\result::UNKNOWN, $this->get_check_api_antivirus_status_result());
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
> // File expected to remain in place.
} catch (\core\antivirus\scanner_exception $ex) {
> $this->assertFileExists($this->tempfile);
$exception = $ex;
> }
}
>
$this->assertNotEmpty($exception);
> // Check API for OK status i.e. antivirus enabled, logstore is ok, no scanner issues occurred recently.
$result = $sink->get_messages();
> public function test_antivirus_check_ok() {
$this->assertCount(1, $result);
> // Enable logs.
$this->assertStringContainsString('fake@example.com', $result[0]->to);
> $this->enable_logging();
$sink->close();
> // Run mock scanning.
}
> $this->assertFileExists($this->tempfile);
> $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
public function test_manager_send_message_to_admin_email_scan_file_virus() {
> $this->assertEquals(\core\check\result::OK, $this->get_check_api_antivirus_status_result());
$sink = $this->redirectMessages();
> // File expected to remain in place.
$exception = null;
> $this->assertFileExists($this->tempfile);
try {
> }
\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true);
>
} catch (\core\antivirus\scanner_exception $ex) {
> // Check API for ERROR status i.e. scanner issue within a certain timeframe/threshold.
$exception = $ex;
> public function test_antivirus_check_error() {
}
> global $USER, $DB;
$this->assertNotEmpty($exception);
> // Enable logs.
$result = $sink->get_messages();
> $this->enable_logging();
$admins = array_keys(get_admins());
> // Set threshold / lookback.
$this->assertCount(1, $admins);
> // Run mock scanning.
$this->assertCount(1, $result);
> $this->assertFileExists($this->tempfile);
$this->assertEquals($result[0]->useridto, reset($admins));
> $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true));
$sink->close();
> $this->assertEquals(\core\check\result::ERROR, $this->get_check_api_antivirus_status_result());
}
> // File expected to remain in place.
> $this->assertFileExists($this->tempfile);
public function test_manager_quarantine_file_virus() {
> }
try {
>
< $this->assertFileNotExists($this->tempfile);
> $this->assertFileDoesNotExist($this->tempfile);
\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));
}
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'));
}
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);
$this->assertStringContainsString('fake@example.com', $result[0]->to);
$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));
}
}