]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | import sys | |
4 | import os | |
5 | import glob | |
6 | import os.path | |
7 | import shutil | |
8 | import time | |
9 | from subprocess import call, PIPE | |
10 | ||
11 | import unittest | |
12 | ||
13 | stdout = os.open("/dev/null",0) #sys.stdout | |
14 | stderr = os.open("/dev/null",0) # sys.stderr | |
15 | ||
16 | apt_args = [] | |
17 | #apt_args = ["-o","Debug::pkgAcquire::Auth=true"] | |
18 | ||
19 | class testAptAuthenticationReliability(unittest.TestCase): | |
20 | """ | |
21 | test if the spec https://wiki.ubuntu.com/AptAuthenticationReliability | |
22 | is properly implemented | |
23 | """ | |
24 | #apt = "../bin/apt-get" | |
25 | apt = "apt-get" | |
26 | ||
27 | def setUp(self): | |
28 | if os.path.exists("/tmp/autFailure"): | |
29 | os.unlink("/tmp/authFailure"); | |
30 | if os.path.exists("/tmp/autFailure2"): | |
31 | os.unlink("/tmp/authFailure2"); | |
32 | def testRepositorySigFailure(self): | |
33 | """ | |
34 | test if a repository that used to be authenticated and fails on | |
35 | apt-get update refuses to update and uses the old state | |
36 | """ | |
37 | # copy valid signatures into lists (those are ok, even | |
38 | # if the name is "-broken-" ... | |
39 | for f in glob.glob("./authReliability/lists/*"): | |
40 | shutil.copy(f,"/var/lib/apt/lists") | |
41 | # ensure we do *not* get a I-M-S hit | |
42 | os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (0,0)) | |
43 | res = call([self.apt, | |
44 | "update", | |
45 | "-o","Dir::Etc::sourcelist=./authReliability/sources.list.failure", | |
46 | "-o",'APT::Update::Auth-Failure::=touch /tmp/authFailure', | |
47 | ] + apt_args, | |
48 | stdout=stdout, stderr=stderr) | |
49 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Release.gpg"), | |
50 | "The gpg file disappeared, this should not happen") | |
51 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Packages"), | |
52 | "The Packages file disappeared, this should not happen") | |
53 | self.assert_(os.path.exists("/tmp/authFailure"), | |
54 | "The APT::Update::Auth-Failure script did not run (1)") | |
55 | # the same with i-m-s hit this time | |
56 | for f in glob.glob("./authReliability/lists/*"): | |
57 | shutil.copy(f,"/var/lib/apt/lists") | |
58 | os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (time.time(),time.time())) | |
59 | res = call([self.apt, | |
60 | "update", | |
61 | "-o","Dir::Etc::sourcelist=./authReliability/sources.list.failure", | |
62 | "-o",'APT::Update::Auth-Failure::=touch /tmp/authFailure2', | |
63 | ] + apt_args, | |
64 | stdout=stdout, stderr=stderr) | |
65 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Release.gpg"), | |
66 | "The gpg file disappeared, this should not happen") | |
67 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-broken_Packages"), | |
68 | "The Packages file disappeared, this should not happen") | |
69 | self.assert_(os.path.exists("/tmp/authFailure2"), | |
70 | "The APT::Update::Auth-Failure script did not run (2)") | |
71 | def testRepositorySigGood(self): | |
72 | """ | |
73 | test that a regular repository with good data stays good | |
74 | """ | |
75 | res = call([self.apt, | |
76 | "update", | |
77 | "-o","Dir::Etc::sourcelist=./authReliability/sources.list.good" | |
78 | ] + apt_args, | |
79 | stdout=stdout, stderr=stderr) | |
80 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Release.gpg"), | |
81 | "The gpg file disappeared after a regular download, this should not happen") | |
82 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Packages"), | |
83 | "The Packages file disappeared, this should not happen") | |
84 | # test good is still good after non I-M-S hit and a previous files in lists/ | |
85 | for f in glob.glob("./authReliability/lists/*"): | |
86 | shutil.copy(f,"/var/lib/apt/lists") | |
87 | # ensure we do *not* get a I-M-S hit | |
88 | os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (0,0)) | |
89 | res = call([self.apt, | |
90 | "update", | |
91 | "-o","Dir::Etc::sourcelist=./authReliability/sources.list.good" | |
92 | ] + apt_args, | |
93 | stdout=stdout, stderr=stderr) | |
94 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Release.gpg"), | |
95 | "The gpg file disappeared after a I-M-S hit, this should not happen") | |
96 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Packages"), | |
97 | "The Packages file disappeared, this should not happen") | |
98 | # test good is still good after I-M-S hit | |
99 | for f in glob.glob("./authReliability/lists/*"): | |
100 | shutil.copy(f,"/var/lib/apt/lists") | |
101 | # ensure we do get a I-M-S hit | |
102 | os.utime("/var/lib/apt/lists/%s" % os.path.basename(f), (time.time(),time.time())) | |
103 | res = call([self.apt, | |
104 | "update", | |
105 | "-o","Dir::Etc::sourcelist=./authReliability/sources.list.good" | |
106 | ] + apt_args, | |
107 | stdout=stdout, stderr=stderr) | |
108 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Release.gpg"), | |
109 | "The gpg file disappeared, this should not happen") | |
110 | self.assert_(os.path.exists("/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_auth-test-suit_gpg-package-ok_Packages"), | |
111 | "The Packages file disappeared, this should not happen") | |
112 | ||
113 | ||
114 | class testAuthentication(unittest.TestCase): | |
115 | """ | |
116 | test if the authentication is working, the repository | |
117 | of the test-data can be found here: | |
118 | bzr get http://people.ubuntu.com/~mvo/bzr/apt/apt-auth-test-suit/ | |
119 | """ | |
120 | ||
121 | # some class wide data | |
122 | apt = "apt-get" | |
123 | pkg = "libglib2.0-data" | |
124 | pkgver = "2.13.6-1ubuntu1" | |
125 | pkgpath = "/var/cache/apt/archives/libglib2.0-data_2.13.6-1ubuntu1_all.deb" | |
126 | ||
127 | def setUp(self): | |
128 | for f in glob.glob("testkeys/*,key"): | |
129 | call(["apt-key", "add", f], stdout=stdout, stderr=stderr) | |
130 | ||
131 | def _cleanup(self): | |
132 | " make sure we get new lists and no i-m-s " | |
133 | call(["rm","-f", "/var/lib/apt/lists/*"]) | |
134 | if os.path.exists(self.pkgpath): | |
135 | os.unlink(self.pkgpath) | |
136 | ||
137 | def _expectedRes(self, resultstr): | |
138 | if resultstr == 'ok': | |
139 | return 0 | |
140 | elif resultstr == 'broken': | |
141 | return 100 | |
142 | ||
143 | ||
144 | def testPackages(self): | |
145 | for f in glob.glob("testsources.list/sources.list*package*"): | |
146 | self._cleanup() | |
147 | (prefix, testtype, result) = f.split("-") | |
148 | expected_res = self._expectedRes(result) | |
149 | # update first | |
150 | call([self.apt,"update", | |
151 | "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args, | |
152 | stdout=stdout, stderr=stderr) | |
153 | # then get the pkg | |
154 | cmd = ["install", "-y", "-d", "--reinstall", | |
155 | "%s=%s" % (self.pkg, self.pkgver), | |
156 | "-o","Dir::state::Status=./fake-status"] | |
157 | res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args, | |
158 | stdout=stdout, stderr=stderr) | |
159 | self.assert_(res == expected_res, | |
160 | "test '%s' failed (got %s expected %s" % (f,res,expected_res)) | |
161 | ||
162 | ||
163 | def testGPG(self): | |
164 | for f in glob.glob("testsources.list/sources.list*gpg*"): | |
165 | self._cleanup() | |
166 | (prefix, testtype, result) = f.split("-") | |
167 | expected_res = self._expectedRes(result) | |
168 | # update first | |
169 | call([self.apt,"update", | |
170 | "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args, | |
171 | stdout=stdout, stderr=stderr) | |
172 | cmd = ["install", "-y", "-d", "--reinstall", | |
173 | "%s=%s" % (self.pkg, self.pkgver), | |
174 | "-o","Dir::state::Status=./fake-status"] | |
175 | res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+ | |
176 | cmd+apt_args, | |
177 | stdout=stdout, stderr=stderr) | |
178 | self.assert_(res == expected_res, | |
179 | "test '%s' failed (got %s expected %s" % (f,res,expected_res)) | |
180 | ||
181 | def testRelease(self): | |
182 | for f in glob.glob("testsources.list/sources.list*release*"): | |
183 | self._cleanup() | |
184 | (prefix, testtype, result) = f.split("-") | |
185 | expected_res = self._expectedRes(result) | |
186 | cmd = ["update"] | |
187 | res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args, | |
188 | stdout=stdout, stderr=stderr) | |
189 | self.assert_(res == expected_res, | |
190 | "test '%s' failed (got %s expected %s" % (f,res,expected_res)) | |
191 | if expected_res == 0: | |
192 | self.assert_(len(glob.glob("/var/lib/apt/lists/partial/*")) == 0, | |
193 | "partial/ dir has leftover files: %s" % glob.glob("/var/lib/apt/lists/partial/*")) | |
194 | ||
195 | ||
196 | class testLocalRepositories(unittest.TestCase): | |
197 | " test local repository regressions " | |
198 | ||
199 | repo_dir = "local-repo" | |
200 | apt = "apt-get" | |
201 | pkg = "gdebi-test4" | |
202 | ||
203 | def setUp(self): | |
204 | self.repo = os.path.abspath(os.path.join(os.getcwd(), self.repo_dir)) | |
205 | self.sources = os.path.join(self.repo, "sources.list") | |
206 | s = open(self.sources,"w") | |
207 | s.write("deb file://%s/ /\n" % self.repo) | |
208 | s.close() | |
209 | ||
210 | def testLocalRepoAuth(self): | |
211 | # two times to get at least one i-m-s hit | |
212 | for i in range(2): | |
213 | self.assert_(os.path.exists(self.sources)) | |
214 | cmd = [self.apt,"update","-o", "Dir::Etc::sourcelist=%s" % self.sources]+apt_args | |
215 | res = call(cmd, stdout=stdout, stderr=stderr) | |
216 | self.assertEqual(res, 0, "local repo test failed") | |
217 | self.assert_(os.path.exists(os.path.join(self.repo,"Packages.gz")), | |
218 | "Packages.gz vanished from local repo") | |
219 | ||
220 | def testInstallFromLocalRepo(self): | |
221 | apt = [self.apt,"-o", "Dir::Etc::sourcelist=%s"% self.sources]+apt_args | |
222 | cmd = apt+["update"] | |
223 | res = call(cmd, stdout=stdout, stderr=stderr) | |
224 | self.assertEqual(res, 0) | |
225 | res = call(apt+["-y","install","--reinstall",self.pkg], | |
226 | stdout=stdout, stderr=stderr) | |
227 | self.assert_(res == 0, | |
228 | "installing %s failed (got %s)" % (self.pkg, res)) | |
229 | res = call(apt+["-y","remove",self.pkg], | |
230 | stdout=stdout, stderr=stderr) | |
231 | self.assert_(res == 0, | |
232 | "removing %s failed (got %s)" % (self.pkg, res)) | |
233 | ||
234 | def testPythonAptInLocalRepo(self): | |
235 | import apt, apt_pkg | |
236 | apt_pkg.Config.Set("Dir::Etc::sourcelist",self.sources) | |
237 | cache = apt.Cache() | |
238 | cache.update() | |
239 | pkg = cache["apt"] | |
240 | self.assert_(pkg.name == 'apt') | |
241 | ||
242 | ||
243 | ||
244 | if __name__ == "__main__": | |
245 | print "Runing simple testsuit on current apt-get and libapt" | |
246 | if len(sys.argv) > 1 and sys.argv[1] == "-v": | |
247 | stdout = sys.stdout | |
248 | stderr = sys.stderr | |
249 | ||
250 | # run only one for now | |
251 | #unittest.main(defaultTest="testAptAuthenticationReliability") | |
252 | unittest.main() |