dom0_update.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2015 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  21. # USA.
  22. #
  23. import os
  24. import shutil
  25. import subprocess
  26. import tempfile
  27. import unittest
  28. import qubes
  29. VM_PREFIX = "test-"
  30. @unittest.skipUnless(os.path.exists('/usr/bin/rpmsign') and
  31. os.path.exists('/usr/bin/rpmbuild'),
  32. 'rpm-sign and/or rpm-build not installed')
  33. class TC_00_Dom0UpgradeMixin(qubes.tests.SystemTestsMixin):
  34. """
  35. Tests for downloading dom0 updates using VMs based on different templates
  36. """
  37. pkg_name = 'qubes-test-pkg'
  38. dom0_update_common_opts = ['--disablerepo=*', '--enablerepo=test',
  39. '--setopt=test.copy_local=1']
  40. @classmethod
  41. def generate_key(cls, keydir):
  42. gpg_opts = ['gpg', '--quiet', '--no-default-keyring',
  43. '--homedir', keydir]
  44. p = subprocess.Popen(gpg_opts + ['--gen-key', '--batch'],
  45. stdin=subprocess.PIPE,
  46. stderr=open(os.devnull, 'w'))
  47. p.stdin.write('''
  48. Key-Type: RSA
  49. Key-Length: 1024
  50. Key-Usage: sign
  51. Name-Real: Qubes test
  52. Expire-Date: 0
  53. %commit
  54. '''.format(keydir=keydir))
  55. p.stdin.close()
  56. p.wait()
  57. subprocess.check_call(gpg_opts + ['-a', '--export',
  58. '--output', os.path.join(keydir, 'pubkey.asc')])
  59. p = subprocess.Popen(gpg_opts + ['--with-colons', '--list-keys'],
  60. stdout=subprocess.PIPE)
  61. for line in p.stdout.readlines():
  62. fields = line.split(':')
  63. if fields[0] == 'pub':
  64. return fields[4][-8:].lower()
  65. raise RuntimeError
  66. @classmethod
  67. def setUpClass(cls):
  68. super(TC_00_Dom0UpgradeMixin, cls).setUpClass()
  69. cls.tmpdir = tempfile.mkdtemp()
  70. cls.keyid = cls.generate_key(cls.tmpdir)
  71. p = subprocess.Popen(['sudo', 'dd',
  72. 'status=none', 'of=/etc/yum.repos.d/test.repo'],
  73. stdin=subprocess.PIPE)
  74. p.stdin.write('''
  75. [test]
  76. name = Test
  77. baseurl = file:///tmp/repo
  78. enabled = 1
  79. ''')
  80. p.stdin.close()
  81. p.wait()
  82. @classmethod
  83. def tearDownClass(cls):
  84. subprocess.check_call(['sudo', 'rm', '-f',
  85. '/etc/yum.repos.d/test.repo'])
  86. shutil.rmtree(cls.tmpdir)
  87. def setUp(self):
  88. super(TC_00_Dom0UpgradeMixin, self).setUp()
  89. self.init_default_template(self.template)
  90. self.updatevm = self.app.add_new_vm(
  91. qubes.vm.appvm.AppVM,
  92. name=self.make_vm_name("updatevm"),
  93. label='red'
  94. )
  95. self.updatevm.create_on_disk()
  96. self.app.updatevm = self.updatevm
  97. self.app.save()
  98. subprocess.call(['sudo', 'rpm', '-e', self.pkg_name],
  99. stderr=open(os.devnull, 'w'))
  100. subprocess.check_call(['sudo', 'rpm', '--import',
  101. os.path.join(self.tmpdir, 'pubkey.asc')])
  102. self.updatevm.start()
  103. def tearDown(self):
  104. super(TC_00_Dom0UpgradeMixin, self).tearDown()
  105. subprocess.call(['sudo', 'rpm', '-e', self.pkg_name], stderr=open(
  106. os.devnull, 'w'))
  107. subprocess.call(['sudo', 'rpm', '-e', 'gpg-pubkey-{}'.format(
  108. self.keyid)], stderr=open(os.devnull, 'w'))
  109. for pkg in os.listdir(self.tmpdir):
  110. if pkg.endswith('.rpm'):
  111. os.unlink(pkg)
  112. def create_pkg(self, dir, name, version):
  113. spec_path = os.path.join(dir, name+'.spec')
  114. spec = open(spec_path, 'w')
  115. spec.write(
  116. '''
  117. Name: {name}
  118. Summary: Test Package
  119. Version: {version}
  120. Release: 1
  121. Vendor: Invisible Things Lab
  122. License: GPL
  123. Group: Qubes
  124. URL: http://www.qubes-os.org
  125. %description
  126. Test package
  127. %install
  128. %files
  129. '''.format(name=name, version=version)
  130. )
  131. spec.close()
  132. subprocess.check_call(
  133. ['rpmbuild', '--quiet', '-bb', '--define', '_rpmdir {}'.format(dir),
  134. spec_path])
  135. pkg_path = os.path.join(dir, 'x86_64',
  136. '{}-{}-1.x86_64.rpm'.format(name, version))
  137. subprocess.check_call(['sudo', 'chmod', 'go-rw', '/dev/tty'])
  138. subprocess.check_call(
  139. ['rpm', '--quiet', '--define=_gpg_path {}'.format(dir),
  140. '--define=_gpg_name {}'.format("Qubes test"),
  141. '--addsign', pkg_path],
  142. stdin=open(os.devnull),
  143. stdout=open(os.devnull, 'w'),
  144. stderr=subprocess.STDOUT)
  145. subprocess.check_call(['sudo', 'chmod', 'go+rw', '/dev/tty'])
  146. return pkg_path
  147. def send_pkg(self, filename):
  148. p = self.updatevm.run('mkdir -p /tmp/repo; cat > /tmp/repo/{}'.format(
  149. os.path.basename(
  150. filename)), passio_popen=True)
  151. p.stdin.write(open(filename).read())
  152. p.stdin.close()
  153. p.wait()
  154. retcode = self.updatevm.run('cd /tmp/repo; createrepo .', wait=True)
  155. if retcode == 127:
  156. self.skipTest("createrepo not installed in template {}".format(
  157. self.template))
  158. elif retcode != 0:
  159. self.skipTest("createrepo failed with code {}, cannot perform the "
  160. "test".format(retcode))
  161. def test_000_update(self):
  162. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  163. subprocess.check_call(['sudo', 'rpm', '-i', filename])
  164. filename = self.create_pkg(self.tmpdir, self.pkg_name, '2.0')
  165. self.send_pkg(filename)
  166. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  167. try:
  168. subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
  169. self.dom0_update_common_opts,
  170. stdout=open(logpath, 'w'),
  171. stderr=subprocess.STDOUT)
  172. except subprocess.CalledProcessError:
  173. self.fail("qubes-dom0-update failed: " + open(
  174. logpath).read())
  175. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  176. self.pkg_name)], stdout=open(os.devnull, 'w'))
  177. self.assertEqual(retcode, 1, 'Package {}-1.0 still installed after '
  178. 'update'.format(self.pkg_name))
  179. retcode = subprocess.call(['rpm', '-q', '{}-2.0'.format(
  180. self.pkg_name)], stdout=open(os.devnull, 'w'))
  181. self.assertEqual(retcode, 0, 'Package {}-2.0 not installed after '
  182. 'update'.format(self.pkg_name))
  183. def test_010_instal(self):
  184. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  185. self.send_pkg(filename)
  186. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  187. try:
  188. subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
  189. self.dom0_update_common_opts + [
  190. self.pkg_name],
  191. stdout=open(logpath, 'w'),
  192. stderr=subprocess.STDOUT)
  193. except subprocess.CalledProcessError:
  194. self.fail("qubes-dom0-update failed: " + open(
  195. logpath).read())
  196. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  197. self.pkg_name)], stdout=open('/dev/null', 'w'))
  198. self.assertEqual(retcode, 0, 'Package {}-1.0 not installed'.format(
  199. self.pkg_name))
  200. def test_020_install_wrong_sign(self):
  201. subprocess.call(['sudo', 'rpm', '-e', 'gpg-pubkey-{}'.format(
  202. self.keyid)])
  203. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  204. self.send_pkg(filename)
  205. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  206. try:
  207. subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
  208. self.dom0_update_common_opts + [
  209. self.pkg_name],
  210. stdout=open(logpath, 'w'),
  211. stderr=subprocess.STDOUT)
  212. self.fail("qubes-dom0-update unexpectedly succeeded: " + open(
  213. logpath).read())
  214. except subprocess.CalledProcessError:
  215. pass
  216. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  217. self.pkg_name)], stdout=open('/dev/null', 'w'))
  218. self.assertEqual(retcode, 1,
  219. 'Package {}-1.0 installed although '
  220. 'signature is invalid'.format(self.pkg_name))
  221. def test_030_install_unsigned(self):
  222. filename = self.create_pkg(self.tmpdir, self.pkg_name, '1.0')
  223. subprocess.check_call(['rpm', '--delsign', filename],
  224. stdout=open(os.devnull, 'w'),
  225. stderr=subprocess.STDOUT)
  226. self.send_pkg(filename)
  227. logpath = os.path.join(self.tmpdir, 'dom0-update-output.txt')
  228. try:
  229. subprocess.check_call(['sudo', '-E', 'qubes-dom0-update', '-y'] +
  230. self.dom0_update_common_opts +
  231. [self.pkg_name],
  232. stdout=open(logpath, 'w'),
  233. stderr=subprocess.STDOUT
  234. )
  235. self.fail("qubes-dom0-update unexpectedly succeeded: " + open(
  236. logpath).read())
  237. except subprocess.CalledProcessError:
  238. pass
  239. retcode = subprocess.call(['rpm', '-q', '{}-1.0'.format(
  240. self.pkg_name)], stdout=open('/dev/null', 'w'))
  241. self.assertEqual(retcode, 1,
  242. 'UNSIGNED package {}-1.0 installed'.format(self.pkg_name))
  243. def load_tests(loader, tests, pattern):
  244. try:
  245. app = qubes.Qubes()
  246. templates = [vm.name for vm in app.domains if
  247. isinstance(vm, qubes.vm.templatevm.TemplateVM)]
  248. except OSError:
  249. templates = []
  250. for template in templates:
  251. tests.addTests(loader.loadTestsFromTestCase(
  252. type(
  253. 'TC_00_Dom0Upgrade_' + template,
  254. (TC_00_Dom0UpgradeMixin, qubes.tests.QubesTestCase),
  255. {'template': template})))
  256. return tests