pre-upload-check.py 11 KB

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