File: test_origin_pattern.py

package info (click to toggle)
unattended-upgrades 0.83.3.2%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 1,076 kB
  • ctags: 153
  • sloc: python: 1,934; sh: 139; makefile: 43
file content (188 lines) | stat: -rwxr-xr-x 6,138 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python3

import apt_pkg
import logging
import unittest

import unattended_upgrade
from unattended_upgrade import (
    check_changes_for_sanity,
    is_allowed_origin,
    get_distro_codename,
    match_whitelist_string,
    UnknownMatcherError,
)


class MockOrigin():
    trusted = True


class MockCandidate():
    pass


class MockPackage():
    pass


class MockCache(dict):
    def __iter__(self):
        for pkgname in self.keys():
            yield self[pkgname]


class MockDepCache():
    pass


class TestOriginPatern(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_match_whitelist_string(self):
        origin = self._get_mock_origin(
            "OriginUbuntu", "LabelUbuntu", "ArchiveUbuntu",
            "archive.ubuntu.com", "main")
        # good
        s = "o=OriginUbuntu"
        self.assertTrue(match_whitelist_string(s, origin))
        s = "o=OriginUbuntu,l=LabelUbuntu,a=ArchiveUbuntu," \
            "site=archive.ubuntu.com"
        self.assertTrue(match_whitelist_string(s, origin))
        # bad
        s = ""
        self.assertFalse(match_whitelist_string(s, origin))
        s = "o=something"
        self.assertFalse(match_whitelist_string(s, origin))
        s = "o=LabelUbuntu,a=no-match"
        self.assertFalse(match_whitelist_string(s, origin))
        # with escaping
        origin = self._get_mock_origin("Google, Inc.", archive="stable")
        # good
        s = "o=Google\, Inc.,a=stable"
        self.assertTrue(match_whitelist_string(s, origin))

    def test_match_whitelist_from_conffile(self):
        # read some
        apt_pkg.config.clear("Unattended-Upgrade")
        apt_pkg.read_config_file(
            apt_pkg.config, "./data/50unattended-upgrades.Test")
        allowed_origins = unattended_upgrade.get_allowed_origins()
        #print allowed_origins
        self.assertTrue("o=aOrigin,a=aArchive" in allowed_origins)
        self.assertTrue("s=aSite,l=aLabel" in allowed_origins)
        self.assertTrue("o=Google\, Inc.,suite=stable" in allowed_origins)

    def test_macro(self):
        codename = get_distro_codename()
        s = "a=${distro_codename}"
        origin = self._get_mock_origin("Foo", archive=codename)
        self.assertTrue(match_whitelist_string(s, origin))

    def test_compatiblity(self):
        apt_pkg.config.clear("Unattended-Upgrade")
        apt_pkg.read_config_file(
            apt_pkg.config, "./data/50unattended-upgrades.compat")
        allowed_origins = unattended_upgrade.get_allowed_origins()
        #print allowed_origins
        self.assertTrue("o=Google\, Inc.,a=stable" in allowed_origins)
        self.assertTrue("o=MoreCorp\, eink,a=stable" in allowed_origins)
        # test whitelist
        pkg = self._get_mock_package()
        self.assertTrue(is_allowed_origin(pkg.candidate, allowed_origins))

    def test_unkown_matcher(self):
        apt_pkg.config.clear("Unattended-Upgrade")
        s = "xxx=OriginUbuntu"
        with self.assertRaises(UnknownMatcherError):
            self.assertTrue(match_whitelist_string(s, None))

    def test_blacklist(self):
        # get the mocks
        pkg = self._get_mock_package("linux-image")
        cache = self._get_mock_cache()
        cache[pkg.name] = pkg
        # origins and blacklist
        allowed_origins = ["o=Ubuntu"]
        blacklist = ["linux-.*"]
        # with blacklist pkg
        self.assertFalse(
            check_changes_for_sanity(cache, allowed_origins, blacklist, [".*"]))
        # with "normal" pkg
        pkg.name = "apt"
        self.assertTrue(
            check_changes_for_sanity(cache, allowed_origins, blacklist, [".*"]))

    def test_whitelist_with_strict_whitelisting(self):
        cache = self._get_mock_cache()
        for pkgname in ["not-whitelisted", "whitelisted"]:
            pkg = self._get_mock_package(name=pkgname)
            cache[pkg.name] = pkg
        # origins and blacklist
        allowed_origins = ["o=Ubuntu"]
        whitelist = ["whitelisted"]
        # test with strict whitelist
        apt_pkg.config.set(
            "Unattended-Upgrade::Package-Whitelist-Strict", "true")
        # ensure that a not-whitelisted pkg will fail
        self.assertTrue(cache["not-whitelisted"].marked_upgrade)
        self.assertFalse(
            check_changes_for_sanity(cache, allowed_origins, [], whitelist))

    def _get_mock_cache(self):
        cache = MockCache()
        cache._depcache = MockDepCache()
        cache._depcache.broken_count = 0
        return cache

    def _get_mock_origin(self, aorigin="", label="", archive="",
                         site="", component=""):
        origin = MockOrigin()
        origin.origin = aorigin
        origin.label = label
        origin.archive = archive
        origin.site = site
        origin.compoent = component
        return origin

    def _get_mock_package(self, name="foo"):
        pkg = MockPackage()
        pkg._pkg = MockPackage()
        pkg._pkg.selected_state = 0
        pkg.name = name
        pkg.marked_install = True
        pkg.marked_upgrade = True
        pkg.marked_delete = False
        pkg.candidate = MockCandidate()
        pkg.candidate.origins = [self._get_mock_origin("Ubuntu"),
                                 self._get_mock_origin(aorigin="Google, Inc.",
                                                       archive="stable")]
        pkg.candidate.record = {}
        return pkg

    def test_match_whitelist_wildcard(self):
        origin = self._get_mock_origin(
            "OriginUbuntu", "LabelUbuntu", "ArchiveUbuntu",
            "archive.ubuntu.com", "main")
        # good
        s = "o=OriginU*"
        self.assertTrue(match_whitelist_string(s, origin))
        # bad
        s = "o=X*"
        self.assertFalse(match_whitelist_string(s, origin))
        # good
        s = "o=?riginUbunt?"
        self.assertTrue(match_whitelist_string(s, origin))
        # good
        s = "o=*Ubunt?"
        self.assertTrue(match_whitelist_string(s, origin))


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    unittest.main()