[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/antivirus/clamav/tests/ -> scanner_test.php (source)

   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  /**
  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   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  class antivirus_clamav_scanner_testcase extends advanced_testcase {
  29      protected $tempfile;
  30  
  31      protected function setUp() {
  32          $this->resetAfterTest();
  33  
  34          // Create tempfile.
  35          $tempfolder = make_request_directory(false);
  36          $this->tempfile = $tempfolder . '/' . rand();
  37          touch($this->tempfile);
  38      }
  39  
  40      protected function tearDown() {
  41          @unlink($this->tempfile);
  42      }
  43  
  44      public function test_scan_file_not_exists() {
  45          $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
  46                  ->setMethods(array('scan_file_execute_commandline', 'message_admins'))
  47                  ->getMock();
  48  
  49          // Test specifying file that does not exist.
  50          $nonexistingfile = $this->tempfile . '_';
  51          $this->assertFileNotExists($nonexistingfile);
  52          // Run mock scanning with deleting infected file.
  53          $antivirus->scan_file($nonexistingfile, '', true);
  54          $this->assertDebuggingCalled();
  55      }
  56  
  57      public function test_scan_file_no_virus() {
  58          $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
  59                  ->setMethods(array('scan_file_execute_commandline', 'message_admins'))
  60                  ->getMock();
  61  
  62          // Configure scan_file_execute_commandline method stub to behave
  63          // as if no virus has been found.
  64          $antivirus->method('scan_file_execute_commandline')->willReturn(array(0, ''));
  65  
  66          // Set expectation that message_admins is NOT called.
  67          $antivirus->expects($this->never())->method('message_admins');
  68  
  69          // Run mock scanning with deleting infected file.
  70          $this->assertFileExists($this->tempfile);
  71          try {
  72              $antivirus->scan_file($this->tempfile, '', true);
  73          } catch (\core\antivirus\scanner_exception $e) {
  74              $this->fail('Exception scanner_exception is not expected in clean file scanning.');
  75          }
  76          // File expected to remain in place.
  77          $this->assertFileExists($this->tempfile);
  78      }
  79  
  80      public function test_scan_file_virus() {
  81          $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
  82                  ->setMethods(array('scan_file_execute_commandline', 'message_admins'))
  83                  ->getMock();
  84  
  85          // Configure scan_file_execute_commandline method stub to behave
  86          // as if virus has been found.
  87          $antivirus->method('scan_file_execute_commandline')->willReturn(array(1, ''));
  88  
  89          // Set expectation that message_admins is NOT called.
  90          $antivirus->expects($this->never())->method('message_admins');
  91  
  92          // Run mock scanning without deleting infected file.
  93          $this->assertFileExists($this->tempfile);
  94          try {
  95              $antivirus->scan_file($this->tempfile, '', false);
  96          } catch (\moodle_exception $e) {
  97              $this->assertInstanceOf('\core\antivirus\scanner_exception', $e);
  98          }
  99          // File expected to remain in place.
 100          $this->assertFileExists($this->tempfile);
 101  
 102          // Run mock scanning with deleting infected file.
 103          try {
 104              $antivirus->scan_file($this->tempfile, '', true);
 105          } catch (\moodle_exception $e) {
 106              $this->assertInstanceOf('\core\antivirus\scanner_exception', $e);
 107          }
 108          // File expected to be deleted.
 109          $this->assertFileNotExists($this->tempfile);
 110      }
 111  
 112      public function test_scan_file_error_donothing() {
 113          $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
 114                  ->setMethods(array('scan_file_execute_commandline', 'message_admins', 'get_config'))
 115                  ->getMock();
 116  
 117          // Configure scan_file_execute_commandline method stub to behave
 118          // as if there is a scanning error.
 119          $antivirus->method('scan_file_execute_commandline')->willReturn(array(2, 'someerror'));
 120  
 121          // Set expectation that message_admins is called.
 122          $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
 123  
 124          // Initiate mock scanning with configuration setting to do nothing on scanning error.
 125          $configmap = array(array('clamfailureonupload', 'donothing'));
 126          $antivirus->method('get_config')->will($this->returnValueMap($configmap));
 127  
 128          // Run mock scanning with deleting infected file.
 129          $this->assertFileExists($this->tempfile);
 130          try {
 131              $antivirus->scan_file($this->tempfile, '', true);
 132          } catch (\core\antivirus\scanner_exception $e) {
 133              $this->fail('Exception scanner_exception is not expected with config setting to do nothing on error.');
 134          }
 135          // File expected to remain in place.
 136          $this->assertFileExists($this->tempfile);
 137      }
 138  
 139      public function test_scan_file_error_actlikevirus() {
 140          $antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')
 141                  ->setMethods(array('scan_file_execute_commandline', 'message_admins', 'get_config'))
 142                  ->getMock();
 143  
 144          // Configure scan_file_execute_commandline method stub to behave
 145          // as if there is a scanning error.
 146          $antivirus->method('scan_file_execute_commandline')->willReturn(array(2, 'someerror'));
 147  
 148          // Set expectation that message_admins is called.
 149          $antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
 150  
 151          // Initiate mock scanning with configuration setting to act like virus on scanning error.
 152          $configmap = array(array('clamfailureonupload', 'actlikevirus'));
 153          $antivirus->method('get_config')->will($this->returnValueMap($configmap));
 154  
 155          // Run mock scanning without deleting infected file.
 156          $this->assertFileExists($this->tempfile);
 157          try {
 158              $antivirus->scan_file($this->tempfile, '', false);
 159          } catch (\moodle_exception $e) {
 160              $this->assertInstanceOf('\core\antivirus\scanner_exception', $e);
 161          }
 162          // File expected to remain in place.
 163          $this->assertFileExists($this->tempfile);
 164  
 165          // Run mock scanning with deleting infected file.
 166          try {
 167              $antivirus->scan_file($this->tempfile, '', true);
 168          } catch (\moodle_exception $e) {
 169              $this->assertInstanceOf('\core\antivirus\scanner_exception', $e);
 170          }
 171          // File expected to be deleted.
 172          $this->assertFileNotExists($this->tempfile);
 173      }
 174  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1