MDL-67638 Course: update course timemodified on bulk category move
[moodle.git] / lib / antivirus / clamav / tests / scanner_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Tests for ClamAV antivirus scanner class.
19  *
20  * @package    antivirus_clamav
21  * @category   phpunit
22  * @copyright  2016 Ruslan Kabalin, Lancaster University.
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 class antivirus_clamav_scanner_testcase extends advanced_testcase {
29     /** @var string temporary file used in testing */
30     protected $tempfile;
32     protected function setUp() {
33         $this->resetAfterTest();
35         // Create tempfile.
36         $tempfolder = make_request_directory(false);
37         $this->tempfile = $tempfolder . '/' . rand();
38         touch($this->tempfile);
39     }
41     protected function tearDown() {
42         @unlink($this->tempfile);
43     }
45     public function test_scan_file_not_exists() {
46         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
47                 ->setMethods(array('scan_file_execute_commandline', 'message_admins'))
48                 ->getMock();
50         // Test specifying file that does not exist.
51         $nonexistingfile = $this->tempfile . '_';
52         $this->assertFileNotExists($nonexistingfile);
53         // Run mock scanning, we expect SCAN_RESULT_ERROR.
54         $this->assertEquals(2, $antivirus->scan_file($nonexistingfile, ''));
55         $this->assertDebuggingCalled();
56     }
58     public function test_scan_file_no_virus() {
59         $methods = array(
60             'scan_file_execute_commandline',
61             'scan_file_execute_unixsocket',
62             'message_admins',
63             'get_config',
64         );
65         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
66                 ->setMethods($methods)
67                 ->getMock();
68         // Initiate mock scanning with configuration setting to use commandline.
69         $configmap = array(array('runningmethod', 'commandline'));
70         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
72         // Configure scan_file_execute_commandline and scan_file_execute_unixsocket
73         // method stubs to behave as if no virus has been found (SCAN_RESULT_OK).
74         $antivirus->method('scan_file_execute_commandline')->willReturn(0);
75         $antivirus->method('scan_file_execute_unixsocket')->willReturn(0);
77         // Set expectation that message_admins is NOT called.
78         $antivirus->expects($this->never())->method('message_admins');
80         // Run mock scanning.
81         $this->assertFileExists($this->tempfile);
82         $this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
84         // Initiate mock scanning with configuration setting to use unixsocket.
85         $configmap = array(array('runningmethod', 'unixsocket'));
86         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
88         // Run mock scanning.
89         $this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
90     }
92     public function test_scan_file_virus() {
93         $methods = array(
94             'scan_file_execute_commandline',
95             'scan_file_execute_unixsocket',
96             'message_admins',
97             'get_config',
98         );
99         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
100                 ->setMethods($methods)
101                 ->getMock();
102         // Initiate mock scanning with configuration setting to use commandline.
103         $configmap = array(array('runningmethod', 'commandline'));
104         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
106         // Configure scan_file_execute_commandline and scan_file_execute_unixsocket
107         // method stubs to behave as if virus has been found (SCAN_RESULT_FOUND).
108         $antivirus->method('scan_file_execute_commandline')->willReturn(1);
109         $antivirus->method('scan_file_execute_unixsocket')->willReturn(1);
111         // Set expectation that message_admins is NOT called.
112         $antivirus->expects($this->never())->method('message_admins');
114         // Run mock scanning.
115         $this->assertFileExists($this->tempfile);
116         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
118         // Initiate mock scanning with configuration setting to use unixsocket.
119         $configmap = array(array('runningmethod', 'unixsocket'));
120         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
122         // Run mock scanning.
123         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
124     }
126     public function test_scan_file_error_donothing() {
127         $methods = array(
128             'scan_file_execute_commandline',
129             'scan_file_execute_unixsocket',
130             'message_admins',
131             'get_config',
132             'get_scanning_notice',
133         );
134         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
135                 ->setMethods($methods)
136                 ->getMock();
138         // Configure scan_file_execute_commandline and scan_file_execute_unixsocket
139         // method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
140         $antivirus->method('scan_file_execute_commandline')->willReturn(2);
141         $antivirus->method('scan_file_execute_unixsocket')->willReturn(2);
142         $antivirus->method('get_scanning_notice')->willReturn('someerror');
144         // Set expectation that message_admins is called.
145         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
147         // Initiate mock scanning with configuration setting to do nothing on
148         // scanning error and using commandline.
149         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'commandline'));
150         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
152         // Run mock scanning.
153         $this->assertFileExists($this->tempfile);
154         $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
156         // Initiate mock scanning with configuration setting to do nothing on
157         // scanning error and using unixsocket.
158         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'unixsocket'));
159         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
161         // Run mock scanning.
162         $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
163     }
165     public function test_scan_file_error_actlikevirus() {
166         $methods = array(
167             'scan_file_execute_commandline',
168             'scan_file_execute_unixsocket',
169             'message_admins',
170             'get_config',
171             'get_scanning_notice',
172         );
173         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
174                 ->setMethods($methods)
175                 ->getMock();
177         // Configure scan_file_execute_commandline and scan_file_execute_unixsocket
178         // method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
179         $antivirus->method('scan_file_execute_commandline')->willReturn(2);
180         $antivirus->method('scan_file_execute_unixsocket')->willReturn(2);
181         $antivirus->method('get_scanning_notice')->willReturn('someerror');
183         // Set expectation that message_admins is called.
184         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
186         // Initiate mock scanning with configuration setting to act like virus on
187         // scanning error and using commandline.
188         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'commandline'));
189         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
191         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
192         // require us to act like virus.
193         $this->assertFileExists($this->tempfile);
194         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
196         // Initiate mock scanning with configuration setting to act like virus on
197         // scanning error and using unixsocket.
198         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'unixsocket'));
199         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
201         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
202         // require us to act like virus.
203         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
204     }
206     public function test_scan_data_no_virus() {
207         $methods = array(
208             'scan_data_execute_unixsocket',
209             'message_admins',
210             'get_config',
211         );
212         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
213                 ->setMethods($methods)
214                 ->getMock();
215         // Initiate mock scanning with configuration setting to use unixsocket.
216         $configmap = array(array('runningmethod', 'unixsocket'));
217         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
219         // Configure scan_data_execute_unixsocket method stubs to behave as if
220         // no virus has been found (SCAN_RESULT_OK).
221         $antivirus->method('scan_data_execute_unixsocket')->willReturn(0);
223         // Set expectation that message_admins is NOT called.
224         $antivirus->expects($this->never())->method('message_admins');
226         // Run mock scanning.
227         $this->assertEquals(0, $antivirus->scan_data(''));
228     }
230     public function test_scan_data_virus() {
231         $methods = array(
232             'scan_data_execute_unixsocket',
233             'message_admins',
234             'get_config',
235         );
236         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
237                 ->setMethods($methods)
238                 ->getMock();
239         // Initiate mock scanning with configuration setting to use unixsocket.
240         $configmap = array(array('runningmethod', 'unixsocket'));
241         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
243         // Configure scan_data_execute_unixsocket method stubs to behave as if
244         // no virus has been found (SCAN_RESULT_FOUND).
245         $antivirus->method('scan_data_execute_unixsocket')->willReturn(1);
247         // Set expectation that message_admins is NOT called.
248         $antivirus->expects($this->never())->method('message_admins');
250         // Run mock scanning.
251         $this->assertEquals(1, $antivirus->scan_data(''));
252     }
254     public function test_scan_data_error_donothing() {
255         $methods = array(
256             'scan_data_execute_unixsocket',
257             'message_admins',
258             'get_config',
259             'get_scanning_notice',
260         );
261         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
262                 ->setMethods($methods)
263                 ->getMock();
264         // Initiate mock scanning with configuration setting to do nothing on
265         // scanning error and using unixsocket.
266         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'unixsocket'));
267         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
269         // Configure scan_data_execute_unixsocket method stubs to behave as if
270         // there is a scanning error (SCAN_RESULT_ERROR).
271         $antivirus->method('scan_data_execute_unixsocket')->willReturn(2);
272         $antivirus->method('get_scanning_notice')->willReturn('someerror');
274         // Set expectation that message_admins is called.
275         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
277         // Run mock scanning.
278         $this->assertEquals(2, $antivirus->scan_data(''));
279     }
281     public function test_scan_data_error_actlikevirus() {
282         $methods = array(
283             'scan_data_execute_unixsocket',
284             'message_admins',
285             'get_config',
286             'get_scanning_notice',
287         );
288         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
289                 ->setMethods($methods)
290                 ->getMock();
292         // Initiate mock scanning with configuration setting to act like virus on
293         // scanning error and using unixsocket.
294         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'unixsocket'));
295         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
297         // Configure scan_data_execute_unixsocket method stubs to behave as if
298         // there is a scanning error (SCAN_RESULT_ERROR).
299         $antivirus->method('scan_data_execute_unixsocket')->willReturn(2);
300         $antivirus->method('get_scanning_notice')->willReturn('someerror');
302         // Set expectation that message_admins is called.
303         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
305         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
306         // require us to act like virus.
307         $this->assertEquals(1, $antivirus->scan_data(''));
308     }