1# Copyright (C) 2019 - 2022 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_download(self):
52        status, output = self.target.run('freshclam --show-progress')
53        msg = ('freshclam : DB dowbload failed. '
54            'Status and output:%s and %s' % (status, output))
55        self.assertEqual(status, 0, msg = msg)
56