2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Tests for antivirus manager.
20 * @package core_antivirus
22 * @copyright 2016 Ruslan Kabalin, Lancaster University.
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
27 require_once(__DIR__
. '/fixtures/testable_antivirus.php');
29 class core_antivirus_testcase
extends advanced_testcase
{
32 protected function setUp() {
34 // Use our special testable fixture plugin.
35 $CFG->antiviruses
= 'testable';
37 $this->resetAfterTest();
40 $tempfolder = make_request_directory(false);
41 $this->tempfile
= $tempfolder . '/' . rand();
42 touch($this->tempfile
);
45 protected function tearDown() {
46 @unlink
($this->tempfile
);
49 public function test_manager_get_antivirus() {
50 // We are using clamav plugin in the test,
51 // as the only plugin we know exists for sure.
52 $antivirusviaget = \core\antivirus\manager
::get_antivirus('clamav');
53 $antivirusdirect = new \antivirus_clamav\
scanner();
54 $this->assertEquals($antivirusdirect, $antivirusviaget);
57 public function test_manager_scan_file_no_virus() {
59 $this->assertFileExists($this->tempfile
);
60 $this->assertEmpty(\core\antivirus\manager
::scan_file($this->tempfile
, 'OK', true));
61 // File expected to remain in place.
62 $this->assertFileExists($this->tempfile
);
65 public function test_manager_scan_file_error() {
67 $this->assertFileExists($this->tempfile
);
68 $this->assertEmpty(\core\antivirus\manager
::scan_file($this->tempfile
, 'ERROR', true));
69 // File expected to remain in place.
70 $this->assertFileExists($this->tempfile
);
73 public function test_manager_scan_file_virus() {
74 // Run mock scanning without deleting infected file.
75 $this->assertFileExists($this->tempfile
);
76 $this->expectException(\core\antivirus\scanner_exception
::class);
77 $this->assertEmpty(\core\antivirus\manager
::scan_file($this->tempfile
, 'FOUND', false));
78 // File expected to remain in place.
79 $this->assertFileExists($this->tempfile
);
81 // Run mock scanning with deleting infected file.
82 $this->expectException(\core\antivirus\scanner_exception
::class);
83 $this->assertEmpty(\core\antivirus\manager
::scan_file($this->tempfile
, 'FOUND', true));
84 // File expected to be deleted.
85 $this->assertFileNotExists($this->tempfile
);
88 public function test_manager_scan_data_no_virus() {
90 $this->assertEmpty(\core\antivirus\manager
::scan_data('OK'));
93 public function test_manager_scan_data_error() {
95 $this->assertEmpty(\core\antivirus\manager
::scan_data('ERROR'));
98 public function test_manager_scan_data_virus() {
100 $this->expectException(\core\antivirus\scanner_exception
::class);
101 $this->assertEmpty(\core\antivirus\manager
::scan_data('FOUND'));