1# Copyright (C) 2019 Armin Kuster <akuster808@gmail.com>
2#
3import re
4from tempfile import mkstemp
5
6from oeqa.runtime.case import OERuntimeTestCase
7from oeqa.core.decorator.depends import OETestDepends
8from oeqa.runtime.decorator.package import OEHasPackage
9
10
11class ClamavTest(OERuntimeTestCase):
12
13    @classmethod
14    def setUpClass(cls):
15        cls.tmp_fd, cls.tmp_path = mkstemp()
16        with os.fdopen(cls.tmp_fd, 'w') as f:
17            # use gooled public dns
18            f.write("nameserver 8.8.8.8")
19            f.write(os.linesep)
20            f.write("nameserver 8.8.4.4")
21            f.write(os.linesep)
22            f.write("nameserver 127.0.0.1")
23            f.write(os.linesep)
24
25    @classmethod
26    def tearDownClass(cls):
27        os.remove(cls.tmp_path)
28
29    @OEHasPackage(['clamav'])
30    @OETestDepends(['ssh.SSHTest.test_ssh'])
31    def test_freshclam_help(self):
32        status, output = self.target.run('freshclam --help ')
33        msg = ('freshclam --hlep  command does not work as expected. ',
34           'Status and output:%s and %s' % (status, output))
35        self.assertEqual(status, 0, msg = msg)
36
37    @OETestDepends(['clamav.ClamavTest.test_freshclam_help'])
38    @OEHasPackage(['openssh-scp', 'dropbear'])
39    def test_ping_clamav_net(self):
40        dst = '/etc/resolv.conf'
41        self.tc.target.run('rm -f %s' % dst)
42        (status, output) = self.tc.target.copyTo(self.tmp_path, dst)
43        msg = 'File could not be copied. Output: %s' % output
44        self.assertEqual(status, 0, msg=msg)
45
46        status, output = self.target.run('ping -c 1 database.clamav.net')
47        msg = ('ping database.clamav.net failed: output is:\n%s' % output)
48        self.assertEqual(status, 0, msg = msg)
49
50    @OETestDepends(['clamav.ClamavTest.test_ping_clamav_net'])
51    def test_freshclam_check_mirrors(self):
52        status, output = self.target.run('freshclam --list-mirrors')
53        match = re.search('Failures: 0', output)
54        if not match:
55            msg = ('freshclam --list-mirrors: failed. '
56               'Status and output:%s and %s' % (status, output))
57            self.assertEqual(status, 1, msg = msg)
58
59    @OETestDepends(['clamav.ClamavTest.test_freshclam_check_mirrors'])
60    def test_freshclam_download(self):
61        status, output = self.target.run('freshclam --show-progress')
62        match = re.search('Database updated', output)
63        #match = re.search('main.cvd is up to date', output)
64        if not match:
65            msg = ('freshclam : DB dowbload failed. '
66               'Status and output:%s and %s' % (status, output))
67            self.assertEqual(status, 1, msg = msg)
68
69