xref: /openbmc/openbmc/meta-security/lib/oeqa/runtime/cases/clamav.py (revision 96e4b4e121e0e2da1535d7d537d6a982a6ff5bc0)
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
9from oeqa.core.decorator.data import skipIfFeature
10
11
12class ClamavTest(OERuntimeTestCase):
13
14    @classmethod
15    def setUpClass(cls):
16        cls.tmp_fd, cls.tmp_path = mkstemp()
17        with os.fdopen(cls.tmp_fd, 'w') as f:
18            # use gooled public dns
19            f.write("nameserver 8.8.8.8")
20            f.write(os.linesep)
21            f.write("nameserver 8.8.4.4")
22            f.write(os.linesep)
23            f.write("nameserver 127.0.0.1")
24            f.write(os.linesep)
25
26    @classmethod
27    def tearDownClass(cls):
28        os.remove(cls.tmp_path)
29
30    @OEHasPackage(['clamav'])
31    @OETestDepends(['ssh.SSHTest.test_ssh'])
32    def test_freshclam_help(self):
33        status, output = self.target.run('freshclam --help ')
34        msg = ('freshclam --hlep  command does not work as expected. ',
35           'Status and output:%s and %s' % (status, output))
36        self.assertEqual(status, 0, msg = msg)
37
38    @OETestDepends(['clamav.ClamavTest.test_freshclam_help'])
39    @OEHasPackage(['openssh-scp', 'dropbear'])
40    def test_ping_clamav_net(self):
41        dst = '/etc/resolv.conf'
42        self.tc.target.run('rm -f %s' % dst)
43        (status, output) = self.tc.target.copyTo(self.tmp_path, dst)
44        msg = 'File could not be copied. Output: %s' % output
45        self.assertEqual(status, 0, msg=msg)
46
47        status, output = self.target.run('ping -c 1 database.clamav.net || curl http://database.clamav.net')
48        msg = ('ping database.clamav.net failed: output is:\n%s' % output)
49        self.assertEqual(status, 0, msg = msg)
50
51    @OETestDepends(['clamav.ClamavTest.test_ping_clamav_net'])
52    @skipIfFeature('systemd','systemd in DISTRO_FEATURES means update job is already running')
53    def test_freshclam_download(self):
54        status, output = self.target.run('freshclam --show-progress')
55        msg = ('freshclam : DB dowbload failed. '
56            'Status and output:%s and %s' % (status, output))
57        self.assertEqual(status, 0, msg = msg)
58