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