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 SuricataTest(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 google 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(['suricata'])
30    @OETestDepends(['ssh.SSHTest.test_ssh'])
31    def test_suricata_help(self):
32        status, output = self.target.run('suricata --help')
33        msg = ('suricata command does not work as expected. '
34               'Status and output:%s and %s' % (status, output))
35        self.assertEqual(status, 1, msg = msg)
36
37    @OETestDepends(['suricata.SuricataTest.test_suricata_help'])
38    def test_ping_openinfosecfoundation_org(self):
39        dst = '/etc/resolv.conf'
40        self.tc.target.run('rm -f %s' % dst)
41        (status, output) = self.tc.target.copyTo(self.tmp_path, dst)
42        msg = 'File could not be copied. Output: %s' % output
43        self.assertEqual(status, 0, msg=msg)
44
45        status, output = self.target.run('ping -c 1 openinfosecfoundation.org')
46        msg = ('ping openinfosecfoundation.org failed: output is:\n%s' % output)
47        self.assertEqual(status, 0, msg = msg)
48
49    @OEHasPackage(['python3-suricata-update'])
50    @OETestDepends(['suricata.SuricataTest.test_ping_openinfosecfoundation_org'])
51    def test_suricata_update(self):
52        status, output = self.tc.target.run('suricata-update')
53        msg = ('suricata-update had an unexpected failure. '
54           'Status and output:%s and %s' % (status, output))
55        self.assertEqual(status, 0, msg = msg)
56
57    @OETestDepends(['suricata.SuricataTest.test_suricata_update'])
58    def test_suricata_update_sources_list(self):
59        status, output = self.tc.target.run('suricata-update list-sources')
60        msg = ('suricata-update list-sources had an unexpected failure. '
61           'Status and output:%s and %s' % (status, output))
62        self.assertEqual(status, 0, msg = msg)
63
64    @OETestDepends(['suricata.SuricataTest.test_suricata_update_sources_list'])
65    def test_suricata_update_sources(self):
66        status, output = self.tc.target.run('suricata-update update-sources')
67        msg = ('suricata-update update-sources had an unexpected failure. '
68           'Status and output:%s and %s' % (status, output))
69        self.assertEqual(status, 0, msg = msg)
70
71    @OETestDepends(['suricata.SuricataTest.test_suricata_update_sources'])
72    def test_suricata_update_enable_source(self):
73        status, output = self.tc.target.run('suricata-update enable-source oisf/trafficid')
74        msg = ('suricata-update enable-source oisf/trafficid  had an unexpected failure. '
75           'Status and output:%s and %s' % (status, output))
76        self.assertEqual(status, 0, msg = msg)
77