You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pre-upload-check.py 12 KiB

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