File: test_applications.py

package info (click to toggle)
ufw 0.36.2-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,824 kB
  • sloc: python: 8,731; sh: 5,950; makefile: 200
file content (155 lines) | stat: -rw-r--r-- 6,109 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#
# Copyright 2012-2018 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import unittest
import tests.unit.support
import ufw.applications


class ApplicationsTestCase(unittest.TestCase):
    def setUp(self):
        apps = os.path.join(ufw.common.config_dir, "ufw/applications.d")
        self.profiles = ufw.applications.get_profiles(apps)

    def tearDown(self):
        pass

    def test_get_profiles(self):
        '''Test get_profiles()'''
        try:
            ufw.applications.get_profiles("foo")
            self.assertFalse(True)
        except ufw.common.UFWError:
            pass

        self.assertTrue('WWW' in self.profiles.keys(), "Could not find 'WWW'")
        self.assertEqual(self.profiles['WWW']['ports'], "80/tcp")
        self.assertEqual(self.profiles['WWW']['title'], "Web Server")
        self.assertEqual(self.profiles['WWW']['description'], "Web server")

    def test_valid_profile_name(self):
        '''Test valid_profile_name()'''
        self.assertTrue(ufw.applications.valid_profile_name('ABC'))
        self.assertFalse(ufw.applications.valid_profile_name('#ABC'))
        self.assertFalse(ufw.applications.valid_profile_name('all'))
        self.assertFalse(ufw.applications.valid_profile_name('123'))
        self.assertFalse(ufw.applications.valid_profile_name('AB*C'))

    def test_verify_profile(self):
        '''Test verify_profile()'''
        profiles = [{'title': 'test both',
                     'description': 'dns',
                     'ports': '53'},
                    {'title': 'test tcp',
                     'description': 'desc',
                     'ports': '22/tcp'},
                    {'title': 'test udp',
                     'description': 'desc',
                     'ports': '123/udp'},
                    {'title': 'test multi comma',
                     'description': 'desc',
                     'ports': '80,443/tcp'},
                    {'title': 'test multi range',
                     'description': 'desc',
                     'ports': '60000:65000/udp'},
                    {'title': 'test different',
                     'description': 'desc',
                     'ports': '123/udp|80/tcp'},
                    {'title': 'test man page',
                     'description': 'desc',
                     'ports': '12/udp|34|56,78:90/tcp'},
                    ]
        for p in profiles:
            self.assertTrue(ufw.applications.verify_profile('TESTPROFILE', p))

    def test_verify_profile_bad(self):
        '''Test verify_profile() - bad'''
        profiles = [{'description': 'missing title',
                     'ports': '53'},
                    {'title': 'missing description',
                     'ports': '22/tcp'},
                    {'title': 'missing ports',
                     'description': 'desc'},
                    {'title': '',
                     'description': 'empty title',
                     'ports': '80'},
                    {'title': 'empty description',
                     'description': '',
                     'ports': '80'},
                    {'title': 'empty ports',
                     'description': 'desc',
                     'ports': ''},
                    {'title': 'bad missing proto - list',
                     'description': 'desc',
                     'ports': '80,443'},
                    {'title': 'bad missing proto - range',
                     'description': 'desc',
                     'ports': '80:443'},
                    {'title': 'bad range too big',
                     'description': 'desc',
                     'ports': '80:70000/tcp'},
                    {'title': 'bad protocol - ah',
                     'description': 'desc',
                     'ports': '80/ah'},
                    {'title': 'bad protocol - esp',
                     'description': 'desc',
                     'ports': '80/esp'},
                    {'title': 'bad protocol - gre',
                     'description': 'desc',
                     'ports': '80/gre'},
                    {'title': 'bad protocol - igmp',
                     'description': 'desc',
                     'ports': '80/igmp'},
                    {'title': 'bad protocol - ipv6',
                     'description': 'desc',
                     'ports': '80/ipv6'},
                    {'title': 'bad protocol - vrrp',
                     'description': 'desc',
                     'ports': '80/vrrp'},
                    ]
        for p in profiles:
            print(" %s" % p)
            tests.unit.support.check_for_exception(self,
                                                   ufw.common.UFWError,
                                                   ufw.applications.verify_profile,
                                                   'TESTPROFILE', p)

    def test_get_title(self):
        '''Test get_title()'''
        self.assertEqual(ufw.applications.get_title(self.profiles['WWW']),
                'Web Server')

    def test_get_description(self):
        '''Test get_description()'''
        self.assertEqual(ufw.applications.get_description(self.profiles['WWW']),
                'Web server')

    def test_get_ports(self):
        '''Test get_ports()'''
        expected_ports = ['80/tcp']
        self.assertEqual(ufw.applications.get_ports(self.profiles['WWW']),
                expected_ports)


def test_main(): # used by runner.py
    tests.unit.support.run_unittest(
            ApplicationsTestCase
    )


if __name__ == "__main__": # used when standalone
    unittest.main()