Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
17
namespace antivirus_clamav;
18
 
19
/**
20
 * Tests for ClamAV antivirus scanner class.
21
 *
22
 * @package    antivirus_clamav
23
 * @category   test
24
 * @copyright  2016 Ruslan Kabalin, Lancaster University.
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
1441 ariadna 27
final class scanner_test extends \advanced_testcase {
1 efrain 28
    /** @var string temporary file used in testing */
29
    protected $tempfile;
30
 
31
    protected function setUp(): void {
1441 ariadna 32
        parent::setUp();
1 efrain 33
        $this->resetAfterTest();
34
 
35
        // Create tempfile.
36
        $tempfolder = make_request_directory(false);
37
        $this->tempfile = $tempfolder . '/' . rand();
38
        touch($this->tempfile);
39
    }
40
 
41
    protected function tearDown(): void {
42
        @unlink($this->tempfile);
1441 ariadna 43
        parent::tearDown();
1 efrain 44
    }
45
 
11 efrain 46
    public function test_scan_file_not_exists(): void {
1 efrain 47
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
48
            ->onlyMethods(array('scan_file_execute_commandline', 'message_admins'))
49
            ->getMock();
50
 
51
        // Test specifying file that does not exist.
52
        $nonexistingfile = $this->tempfile . '_';
53
        $this->assertFileDoesNotExist($nonexistingfile);
54
        // Run mock scanning, we expect SCAN_RESULT_ERROR.
55
        $this->assertEquals(2, $antivirus->scan_file($nonexistingfile, ''));
56
        $this->assertDebuggingCalled();
57
    }
58
 
11 efrain 59
    public function test_scan_file_no_virus(): void {
1 efrain 60
        $methods = array(
61
            'scan_file_execute_commandline',
62
            'scan_file_execute_socket',
63
            'message_admins',
64
            'get_config',
65
        );
66
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
67
            ->onlyMethods($methods)
68
            ->getMock();
69
        // Initiate mock scanning with configuration setting to use commandline.
70
        $configmap = array(array('runningmethod', 'commandline'));
71
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
72
 
73
        // Configure scan_file_execute_commandline and scan_file_execute_socket
74
        // method stubs to behave as if no virus has been found (SCAN_RESULT_OK).
75
        $antivirus->method('scan_file_execute_commandline')->willReturn(0);
76
        $antivirus->method('scan_file_execute_socket')->willReturn(0);
77
 
78
        // Set expectation that message_admins is NOT called.
79
        $antivirus->expects($this->never())->method('message_admins');
80
 
81
        // Run mock scanning.
82
        $this->assertFileExists($this->tempfile);
83
        $this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
84
 
85
        // Initiate mock scanning with configuration setting to use unixsocket.
86
        $configmap = array(array('runningmethod', 'unixsocket'));
87
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
88
 
89
        // Run mock scanning.
90
        $this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
91
 
92
        // Initiate mock scanning with configuration setting to use tcpsocket.
93
        $configmap = array(array('runningmethod', 'tcpsocket'));
94
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
95
 
96
        // Run mock scanning.
97
        $this->assertEquals(0, $antivirus->scan_file($this->tempfile, ''));
98
    }
99
 
11 efrain 100
    public function test_scan_file_virus(): void {
1 efrain 101
        $methods = array(
102
            'scan_file_execute_commandline',
103
            'scan_file_execute_socket',
104
            'message_admins',
105
            'get_config',
106
        );
107
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
108
            ->onlyMethods($methods)
109
            ->getMock();
110
        // Initiate mock scanning with configuration setting to use commandline.
111
        $configmap = array(array('runningmethod', 'commandline'));
112
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
113
 
114
        // Configure scan_file_execute_commandline and scan_file_execute_socket
115
        // method stubs to behave as if virus has been found (SCAN_RESULT_FOUND).
116
        $antivirus->method('scan_file_execute_commandline')->willReturn(1);
117
        $antivirus->method('scan_file_execute_socket')->willReturn(1);
118
 
119
        // Set expectation that message_admins is NOT called.
120
        $antivirus->expects($this->never())->method('message_admins');
121
 
122
        // Run mock scanning.
123
        $this->assertFileExists($this->tempfile);
124
        $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
125
 
126
        // Initiate mock scanning with configuration setting to use unixsocket.
127
        $configmap = array(array('runningmethod', 'unixsocket'));
128
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
129
 
130
        // Run mock scanning.
131
        $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
132
 
133
        // Initiate mock scanning with configuration setting to use tcpsocket.
134
        $configmap = array(array('runningmethod', 'tcpsocket'));
135
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
136
 
137
        // Run mock scanning.
138
        $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
139
    }
140
 
11 efrain 141
    public function test_scan_file_error_donothing(): void {
1 efrain 142
        $methods = array(
143
            'scan_file_execute_commandline',
144
            'scan_file_execute_socket',
145
            'message_admins',
146
            'get_config',
147
            'get_scanning_notice',
148
        );
149
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
150
            ->onlyMethods($methods)
151
            ->getMock();
152
 
153
        // Configure scan_file_execute_commandline and scan_file_execute_socket
154
        // method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
155
        $antivirus->method('scan_file_execute_commandline')->willReturn(2);
156
        $antivirus->method('scan_file_execute_socket')->willReturn(2);
157
        $antivirus->method('get_scanning_notice')->willReturn('someerror');
158
 
159
        // Set expectation that message_admins is called.
160
        $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
161
 
162
        // Initiate mock scanning with configuration setting to do nothing on
163
        // scanning error and using commandline.
164
        $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'commandline'));
165
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
166
 
167
        // Run mock scanning.
168
        $this->assertFileExists($this->tempfile);
169
        $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
170
 
171
        // Initiate mock scanning with configuration setting to do nothing on
172
        // scanning error and using unixsocket.
173
        $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'unixsocket'));
174
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
175
 
176
        // Run mock scanning.
177
        $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
178
 
179
        // Initiate mock scanning with configuration setting to do nothing on
180
        // scanning error and using tcpsocket.
181
        $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'tcpsocket'));
182
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
183
 
184
        // Run mock scanning.
185
        $this->assertEquals(2, $antivirus->scan_file($this->tempfile, ''));
186
    }
187
 
11 efrain 188
    public function test_scan_file_error_actlikevirus(): void {
1 efrain 189
        $methods = array(
190
            'scan_file_execute_commandline',
191
            'scan_file_execute_socket',
192
            'message_admins',
193
            'get_config',
194
            'get_scanning_notice',
195
        );
196
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
197
            ->onlyMethods($methods)
198
            ->getMock();
199
 
200
        // Configure scan_file_execute_commandline and scan_file_execute_socket
201
        // method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
202
        $antivirus->method('scan_file_execute_commandline')->willReturn(2);
203
        $antivirus->method('scan_file_execute_socket')->willReturn(2);
204
        $antivirus->method('get_scanning_notice')->willReturn('someerror');
205
 
206
        // Set expectation that message_admins is called.
207
        $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
208
 
209
        // Initiate mock scanning with configuration setting to act like virus on
210
        // scanning error and using commandline.
211
        $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'commandline'));
212
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
213
 
214
        // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
215
        // require us to act like virus.
216
        $this->assertFileExists($this->tempfile);
217
        $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
218
 
219
        // Initiate mock scanning with configuration setting to act like virus on
220
        // scanning error and using unixsocket.
221
        $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'unixsocket'));
222
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
223
 
224
        // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
225
        // require us to act like virus.
226
        $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
227
 
228
        // Initiate mock scanning with configuration setting to act like virus on
229
        // scanning error and using tcpsocket.
230
        $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'tcpsocket'));
231
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
232
 
233
        // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
234
        // require us to act like virus.
235
        $this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
236
    }
237
 
11 efrain 238
    public function test_scan_file_error_tryagain(): void {
1 efrain 239
        $methods = array(
240
                'scan_file_execute_commandline',
241
                'scan_file_execute_unixsocket',
242
                'message_admins',
243
                'get_config',
244
                'get_scanning_notice',
245
        );
246
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')->onlyMethods($methods)->getMock();
247
 
248
        // Configure scan_file_execute_commandline and scan_file_execute_unixsocket
249
        // method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
250
        $antivirus->method('scan_file_execute_commandline')->willReturn(2);
251
        $antivirus->method('scan_file_execute_unixsocket')->willReturn(2);
252
        $antivirus->method('get_scanning_notice')->willReturn('someerror');
253
 
254
        // Set expectation that message_admins is called.
255
        $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
256
 
257
        // Initiate mock scanning with configuration setting to act like virus on
258
        // scanning error and using commandline.
259
        $configmap = array(array('clamfailureonupload', 'tryagain'), array('runningmethod', 'commandline'));
260
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
261
 
262
        // Run mock scanning.
263
        $this->assertFileExists($this->tempfile);
264
        $this->expectException(\core\antivirus\scanner_exception::class);
265
        $antivirus->scan_file($this->tempfile, '');
266
        $this->assertEquals('antivirusfailed', $this->getExpectedExceptionCode());
267
        $this->assertFileDoesNotExist($this->tempfile);
268
    }
269
 
11 efrain 270
    public function test_scan_data_no_virus(): void {
1 efrain 271
        $methods = array(
272
            'scan_data_execute_socket',
273
            'message_admins',
274
            'get_config',
275
        );
276
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
277
            ->onlyMethods($methods)
278
            ->getMock();
279
        // Initiate mock scanning with configuration setting to use unixsocket.
280
        $configmap = array(array('runningmethod', 'unixsocket'));
281
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
282
 
283
        // Configure scan_data_execute_socket method stubs to behave as if
284
        // no virus has been found (SCAN_RESULT_OK).
285
        $antivirus->method('scan_data_execute_socket')->willReturn(0);
286
 
287
        // Set expectation that message_admins is NOT called.
288
        $antivirus->expects($this->never())->method('message_admins');
289
 
290
        // Run mock scanning.
291
        $this->assertEquals(0, $antivirus->scan_data(''));
292
 
293
        // Re-initiate mock scanning with configuration setting to use tcpsocket.
294
        $configmap = array(array('runningmethod', 'tcpsocket'));
295
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
296
 
297
        // Set expectation that message_admins is NOT called.
298
        $antivirus->expects($this->never())->method('message_admins');
299
 
300
        // Run mock scanning.
301
        $this->assertEquals(0, $antivirus->scan_data(''));
302
    }
303
 
11 efrain 304
    public function test_scan_data_virus(): void {
1 efrain 305
        $methods = array(
306
            'scan_data_execute_socket',
307
            'message_admins',
308
            'get_config',
309
        );
310
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
311
            ->onlyMethods($methods)
312
            ->getMock();
313
        // Initiate mock scanning with configuration setting to use unixsocket.
314
        $configmap = array(array('runningmethod', 'unixsocket'));
315
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
316
 
317
        // Configure scan_data_execute_socket method stubs to behave as if
318
        // no virus has been found (SCAN_RESULT_FOUND).
319
        $antivirus->method('scan_data_execute_socket')->willReturn(1);
320
 
321
        // Set expectation that message_admins is NOT called.
322
        $antivirus->expects($this->never())->method('message_admins');
323
 
324
        // Run mock scanning.
325
        $this->assertEquals(1, $antivirus->scan_data(''));
326
 
327
        // Re-initiate mock scanning with configuration setting to use tcpsocket.
328
        $configmap = array(array('runningmethod', 'tcpsocket'));
329
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
330
 
331
        // Set expectation that message_admins is NOT called.
332
        $antivirus->expects($this->never())->method('message_admins');
333
 
334
        // Run mock scanning.
335
        $this->assertEquals(1, $antivirus->scan_data(''));
336
    }
337
 
11 efrain 338
    public function test_scan_data_error_donothing(): void {
1 efrain 339
        $methods = array(
340
            'scan_data_execute_socket',
341
            'message_admins',
342
            'get_config',
343
            'get_scanning_notice',
344
        );
345
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
346
            ->onlyMethods($methods)
347
            ->getMock();
348
        // Initiate mock scanning with configuration setting to do nothing on
349
        // scanning error and using unixsocket.
350
        $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'unixsocket'));
351
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
352
 
353
        // Configure scan_data_execute_socket method stubs to behave as if
354
        // there is a scanning error (SCAN_RESULT_ERROR).
355
        $antivirus->method('scan_data_execute_socket')->willReturn(2);
356
        $antivirus->method('get_scanning_notice')->willReturn('someerror');
357
 
358
        // Set expectation that message_admins is called.
359
        $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
360
 
361
        // Run mock scanning.
362
        $this->assertEquals(2, $antivirus->scan_data(''));
363
 
364
        // Re-initiate mock scanning with configuration setting to do nothing on
365
        // scanning error and using tcsocket.
366
        $configmap = array(array('clamfailureonupload', 'donothing'), array('runningmethod', 'tcpsocket'));
367
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
368
 
369
        // Set expectation that message_admins is called.
370
        $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
371
 
372
        // Run mock scanning.
373
        $this->assertEquals(2, $antivirus->scan_data(''));
374
    }
375
 
11 efrain 376
    public function test_scan_data_error_actlikevirus(): void {
1 efrain 377
        $methods = array(
378
            'scan_data_execute_socket',
379
            'message_admins',
380
            'get_config',
381
            'get_scanning_notice',
382
        );
383
        $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
384
            ->onlyMethods($methods)
385
            ->getMock();
386
 
387
        // Initiate mock scanning with configuration setting to act like virus on
388
        // scanning error and using unixsocket.
389
        $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'unixsocket'));
390
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
391
 
392
        // Configure scan_data_execute_socket method stubs to behave as if
393
        // there is a scanning error (SCAN_RESULT_ERROR).
394
        $antivirus->method('scan_data_execute_socket')->willReturn(2);
395
        $antivirus->method('get_scanning_notice')->willReturn('someerror');
396
 
397
        // Set expectation that message_admins is called.
398
        $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
399
 
400
        // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
401
        // require us to act like virus.
402
        $this->assertEquals(1, $antivirus->scan_data(''));
403
 
404
        // Re-initiate mock scanning with configuration setting to act like virus on
405
        // scanning error and using tcpsocket.
406
        $configmap = array(array('clamfailureonupload', 'actlikevirus'), array('runningmethod', 'tcpsocket'));
407
        $antivirus->method('get_config')->will($this->returnValueMap($configmap));
408
 
409
        // Set expectation that message_admins is called.
410
        $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
411
 
412
        // Run mock scanning, we expect SCAN_RESULT_FOUND since configuration
413
        // require us to act like virus.
414
        $this->assertEquals(1, $antivirus->scan_data(''));
415
    }
416
}