Merge branch 'MDL-67377-master' of git://github.com/ferranrecio/moodle
[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_socket',
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_socket
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_socket')->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, ''));
91         // Initiate mock scanning with configuration setting to use tcpsocket.
92         $configmap = array(array('runningmethod', 'tcpsocket'));
93         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
95         // Run mock scanning.
96         $this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
97     }
99     public function test_scan_file_virus() {
100         $methods = array(
101             'scan_file_execute_commandline',
102             'scan_file_execute_socket',
103             'message_admins',
104             'get_config',
105         );
106         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
107             ->setMethods($methods)
108             ->getMock();
109         // Initiate mock scanning with configuration setting to use commandline.
110         $configmap = array(array('runningmethod', 'commandline'));
111         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
113         // Configure scan_file_execute_commandline and scan_file_execute_socket
114         // method stubs to behave as if virus has been found (SCAN_RESULT_FOUND).
115         $antivirus->method('scan_file_execute_commandline')->willReturn(1);
116         $antivirus->method('scan_file_execute_socket')->willReturn(1);
118         // Set expectation that message_admins is NOT called.
119         $antivirus->expects($this->never())->method('message_admins');
121         // Run mock scanning.
122         $this->assertFileExists($this->tempfile);
123         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
125         // Initiate mock scanning with configuration setting to use unixsocket.
126         $configmap = array(array('runningmethod', 'unixsocket'));
127         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
129         // Run mock scanning.
130         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
132         // Initiate mock scanning with configuration setting to use tcpsocket.
133         $configmap = array(array('runningmethod', 'tcpsocket'));
134         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
136         // Run mock scanning.
137         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
138     }
140     public function test_scan_file_error_donothing() {
141         $methods = array(
142             'scan_file_execute_commandline',
143             'scan_file_execute_socket',
144             'message_admins',
145             'get_config',
146             'get_scanning_notice',
147         );
148         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
149             ->setMethods($methods)
150             ->getMock();
152         // Configure scan_file_execute_commandline and scan_file_execute_socket
153         // method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
154         $antivirus->method('scan_file_execute_commandline')->willReturn(2);
155         $antivirus->method('scan_file_execute_socket')->willReturn(2);
156         $antivirus->method('get_scanning_notice')->willReturn('someerror');
158         // Set expectation that message_admins is called.
159         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
161         // Initiate mock scanning with configuration setting to do nothing on
162         // scanning error and using commandline.
163         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'commandline'));
164         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
166         // Run mock scanning.
167         $this->assertFileExists($this->tempfile);
168         $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
170         // Initiate mock scanning with configuration setting to do nothing on
171         // scanning error and using unixsocket.
172         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'unixsocket'));
173         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
175         // Run mock scanning.
176         $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
178         // Initiate mock scanning with configuration setting to do nothing on
179         // scanning error and using tcpsocket.
180         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'tcpsocket'));
181         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
183         // Run mock scanning.
184         $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
185     }
187     public function test_scan_file_error_actlikevirus() {
188         $methods = array(
189             'scan_file_execute_commandline',
190             'scan_file_execute_socket',
191             'message_admins',
192             'get_config',
193             'get_scanning_notice',
194         );
195         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
196             ->setMethods($methods)
197             ->getMock();
199         // Configure scan_file_execute_commandline and scan_file_execute_socket
200         // method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
201         $antivirus->method('scan_file_execute_commandline')->willReturn(2);
202         $antivirus->method('scan_file_execute_socket')->willReturn(2);
203         $antivirus->method('get_scanning_notice')->willReturn('someerror');
205         // Set expectation that message_admins is called.
206         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
208         // Initiate mock scanning with configuration setting to act like virus on
209         // scanning error and using commandline.
210         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'commandline'));
211         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
213         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
214         // require us to act like virus.
215         $this->assertFileExists($this->tempfile);
216         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
218         // Initiate mock scanning with configuration setting to act like virus on
219         // scanning error and using unixsocket.
220         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'unixsocket'));
221         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
223         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
224         // require us to act like virus.
225         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
227         // Initiate mock scanning with configuration setting to act like virus on
228         // scanning error and using tcpsocket.
229         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'tcpsocket'));
230         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
232         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
233         // require us to act like virus.
234         $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
235     }
237     public function test_scan_data_no_virus() {
238         $methods = array(
239             'scan_data_execute_socket',
240             'message_admins',
241             'get_config',
242         );
243         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
244             ->setMethods($methods)
245             ->getMock();
246         // Initiate mock scanning with configuration setting to use unixsocket.
247         $configmap = array(array('runningmethod', 'unixsocket'));
248         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
250         // Configure scan_data_execute_socket method stubs to behave as if
251         // no virus has been found (SCAN_RESULT_OK).
252         $antivirus->method('scan_data_execute_socket')->willReturn(0);
254         // Set expectation that message_admins is NOT called.
255         $antivirus->expects($this->never())->method('message_admins');
257         // Run mock scanning.
258         $this->assertEquals(0, $antivirus->scan_data(''));
260         // Re-initiate mock scanning with configuration setting to use tcpsocket.
261         $configmap = array(array('runningmethod', 'tcpsocket'));
262         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
264         // Set expectation that message_admins is NOT called.
265         $antivirus->expects($this->never())->method('message_admins');
267         // Run mock scanning.
268         $this->assertEquals(0, $antivirus->scan_data(''));
269     }
271     public function test_scan_data_virus() {
272         $methods = array(
273             'scan_data_execute_socket',
274             'message_admins',
275             'get_config',
276         );
277         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
278             ->setMethods($methods)
279             ->getMock();
280         // Initiate mock scanning with configuration setting to use unixsocket.
281         $configmap = array(array('runningmethod', 'unixsocket'));
282         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
284         // Configure scan_data_execute_socket method stubs to behave as if
285         // no virus has been found (SCAN_RESULT_FOUND).
286         $antivirus->method('scan_data_execute_socket')->willReturn(1);
288         // Set expectation that message_admins is NOT called.
289         $antivirus->expects($this->never())->method('message_admins');
291         // Run mock scanning.
292         $this->assertEquals(1, $antivirus->scan_data(''));
294         // Re-initiate mock scanning with configuration setting to use tcpsocket.
295         $configmap = array(array('runningmethod', 'tcpsocket'));
296         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
298         // Set expectation that message_admins is NOT called.
299         $antivirus->expects($this->never())->method('message_admins');
301         // Run mock scanning.
302         $this->assertEquals(1, $antivirus->scan_data(''));
303     }
305     public function test_scan_data_error_donothing() {
306         $methods = array(
307             'scan_data_execute_socket',
308             'message_admins',
309             'get_config',
310             'get_scanning_notice',
311         );
312         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
313             ->setMethods($methods)
314             ->getMock();
315         // Initiate mock scanning with configuration setting to do nothing on
316         // scanning error and using unixsocket.
317         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'unixsocket'));
318         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
320         // Configure scan_data_execute_socket method stubs to behave as if
321         // there is a scanning error (SCAN_RESULT_ERROR).
322         $antivirus->method('scan_data_execute_socket')->willReturn(2);
323         $antivirus->method('get_scanning_notice')->willReturn('someerror');
325         // Set expectation that message_admins is called.
326         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
328         // Run mock scanning.
329         $this->assertEquals(2, $antivirus->scan_data(''));
331         // Re-initiate mock scanning with configuration setting to do nothing on
332         // scanning error and using tcsocket.
333         $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'tcpsocket'));
334         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
336         // Set expectation that message_admins is called.
337         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
339         // Run mock scanning.
340         $this->assertEquals(2, $antivirus->scan_data(''));
341     }
343     public function test_scan_data_error_actlikevirus() {
344         $methods = array(
345             'scan_data_execute_socket',
346             'message_admins',
347             'get_config',
348             'get_scanning_notice',
349         );
350         $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
351             ->setMethods($methods)
352             ->getMock();
354         // Initiate mock scanning with configuration setting to act like virus on
355         // scanning error and using unixsocket.
356         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'unixsocket'));
357         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
359         // Configure scan_data_execute_socket method stubs to behave as if
360         // there is a scanning error (SCAN_RESULT_ERROR).
361         $antivirus->method('scan_data_execute_socket')->willReturn(2);
362         $antivirus->method('get_scanning_notice')->willReturn('someerror');
364         // Set expectation that message_admins is called.
365         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
367         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
368         // require us to act like virus.
369         $this->assertEquals(1, $antivirus->scan_data(''));
371         // Re-initiate mock scanning with configuration setting to act like virus on
372         // scanning error and using tcpsocket.
373         $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'tcpsocket'));
374         $antivirus->method('get_config')->will($this->returnValueMap($configmap));
376         // Set expectation that message_admins is called.
377         $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
379         // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
380         // require us to act like virus.
381         $this->assertEquals(1, $antivirus->scan_data(''));
382     }