dispvm.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Marek Marczykowski-Górecki
  5. # <marmarek@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. from distutils import spawn
  22. import qubes.tests
  23. import subprocess
  24. import tempfile
  25. import unittest
  26. import os
  27. import time
  28. class TC_04_DispVM(qubes.tests.SystemTestsMixin,
  29. qubes.tests.QubesTestCase):
  30. def setUp(self):
  31. super(TC_04_DispVM, self).setUp()
  32. self.init_default_template()
  33. self.disp_base = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  34. name=self.make_vm_name('dvm'),
  35. label='red',
  36. )
  37. self.disp_base.create_on_disk()
  38. self.app.default_dispvm = self.disp_base
  39. self.testvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  40. name=self.make_vm_name('vm'),
  41. label='red',
  42. )
  43. self.testvm.create_on_disk()
  44. self.app.save()
  45. @unittest.expectedFailure
  46. def test_002_cleanup(self):
  47. self.testvm.start()
  48. p = self.testvm.run("qvm-run --dispvm bash", passio_popen=True)
  49. (stdout, _) = p.communicate(input=b"echo test; qubesdb-read /name; "
  50. b"echo ERROR\n")
  51. self.assertEqual(p.returncode, 0)
  52. lines = stdout.decode('ascii').splitlines()
  53. self.assertEqual(lines[0], "test")
  54. dispvm_name = lines[1]
  55. # wait for actual DispVM destruction
  56. time.sleep(1)
  57. self.assertNotIn(dispvm_name, self.app.domains)
  58. @unittest.expectedFailure
  59. def test_003_cleanup_destroyed(self):
  60. """
  61. Check if DispVM is properly removed even if it terminated itself (#1660)
  62. :return:
  63. """
  64. self.testvm.start()
  65. p = self.testvm.run("qvm-run --dispvm bash; true", passio_popen=True)
  66. p.stdin.write(b"qubesdb-read /name\n")
  67. p.stdin.write(b"echo ERROR\n")
  68. p.stdin.write(b"sudo poweroff\n")
  69. # do not close p.stdin on purpose - wait to automatic disconnect when
  70. # domain is destroyed
  71. timeout = 30
  72. while timeout > 0:
  73. if p.poll():
  74. break
  75. time.sleep(1)
  76. timeout -= 1
  77. # includes check for None - timeout
  78. self.assertEqual(p.returncode, 0)
  79. lines = p.stdout.read().splitlines()
  80. self.assertTrue(lines, 'No output received from DispVM')
  81. dispvm_name = lines[0]
  82. self.assertNotEquals(dispvm_name, b"ERROR")
  83. self.assertNotIn(dispvm_name, self.app.domains)
  84. class TC_20_DispVMMixin(qubes.tests.SystemTestsMixin):
  85. def setUp(self):
  86. super(TC_20_DispVMMixin, self).setUp()
  87. self.init_default_template(self.template)
  88. self.disp_base = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  89. name=self.make_vm_name('dvm'),
  90. label='red',
  91. )
  92. self.disp_base.create_on_disk()
  93. self.app.default_dispvm = self.disp_base
  94. self.app.save()
  95. def test_010_simple_dvm_run(self):
  96. dispvm = qubes.vm.dispvm.DispVM.from_appvm(self.disp_base)
  97. try:
  98. dispvm.start()
  99. p = dispvm.run_service('qubes.VMShell', passio_popen=True)
  100. (stdout, _) = p.communicate(input=b"echo test")
  101. self.assertEqual(stdout, b"test\n")
  102. finally:
  103. dispvm.cleanup()
  104. @unittest.skipUnless(spawn.find_executable('xdotool'),
  105. "xdotool not installed")
  106. def test_020_gui_app(self):
  107. dispvm = qubes.vm.dispvm.DispVM.from_appvm(self.disp_base)
  108. try:
  109. dispvm.start()
  110. p = dispvm.run_service('qubes.VMShell', passio_popen=True)
  111. # wait for DispVM startup:
  112. p.stdin.write(b"echo test\n")
  113. p.stdin.flush()
  114. l = p.stdout.readline()
  115. self.assertEqual(l, b"test\n")
  116. self.assertTrue(dispvm.is_running())
  117. try:
  118. window_title = 'user@%s' % (dispvm.name,)
  119. p.stdin.write("xterm -e "
  120. "\"sh -c 'echo \\\"\033]0;{}\007\\\";read x;'\"\n".
  121. format(window_title).encode())
  122. self.wait_for_window(window_title)
  123. time.sleep(0.5)
  124. self.enter_keys_in_window(window_title, ['Return'])
  125. # Wait for window to close
  126. self.wait_for_window(window_title, show=False)
  127. finally:
  128. p.stdin.close()
  129. finally:
  130. dispvm.cleanup()
  131. self.assertNotIn(dispvm.name, self.app.domains,
  132. "DispVM not removed from qubes.xml")
  133. def _handle_editor(self, winid):
  134. (window_title, _) = subprocess.Popen(
  135. ['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE).\
  136. communicate()
  137. window_title = window_title.decode().strip().\
  138. replace('(', '\(').replace(')', '\)')
  139. time.sleep(1)
  140. if "gedit" in window_title:
  141. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  142. 'type', 'Test test 2'])
  143. subprocess.check_call(['xdotool', 'key', '--window', winid,
  144. 'key', 'Return'])
  145. time.sleep(0.5)
  146. subprocess.check_call(['xdotool',
  147. 'key', 'ctrl+s', 'ctrl+q'])
  148. elif "LibreOffice" in window_title:
  149. # wait for actual editor (we've got splash screen)
  150. search = subprocess.Popen(['xdotool', 'search', '--sync',
  151. '--onlyvisible', '--all', '--name', '--class', 'disp*|Writer'],
  152. stdout=subprocess.PIPE,
  153. stderr=open(os.path.devnull, 'w'))
  154. retcode = search.wait()
  155. if retcode == 0:
  156. winid = search.stdout.read().strip()
  157. time.sleep(0.5)
  158. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  159. 'type', 'Test test 2'])
  160. subprocess.check_call(['xdotool', 'key', '--window', winid,
  161. 'key', 'Return'])
  162. time.sleep(0.5)
  163. subprocess.check_call(['xdotool',
  164. 'key', '--delay', '100', 'ctrl+s',
  165. 'Return', 'ctrl+q'])
  166. elif "emacs" in window_title:
  167. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  168. 'type', 'Test test 2'])
  169. subprocess.check_call(['xdotool', 'key', '--window', winid,
  170. 'key', 'Return'])
  171. time.sleep(0.5)
  172. subprocess.check_call(['xdotool',
  173. 'key', 'ctrl+x', 'ctrl+s'])
  174. subprocess.check_call(['xdotool',
  175. 'key', 'ctrl+x', 'ctrl+c'])
  176. elif "vim" in window_title or "user@" in window_title:
  177. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  178. 'key', 'i', 'type', 'Test test 2'])
  179. subprocess.check_call(['xdotool', 'key', '--window', winid,
  180. 'key', 'Return'])
  181. subprocess.check_call(
  182. ['xdotool',
  183. 'key', 'Escape', 'colon', 'w', 'q', 'Return'])
  184. else:
  185. self.fail("Unknown editor window: {}".format(window_title))
  186. @unittest.skipUnless(spawn.find_executable('xdotool'),
  187. "xdotool not installed")
  188. @unittest.expectedFailure
  189. def test_030_edit_file(self):
  190. testvm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  191. name=self.make_vm_name('vm1'),
  192. label='red',
  193. template=self.app.domains[self.template])
  194. testvm1.create_on_disk()
  195. self.app.save()
  196. testvm1.start()
  197. testvm1.run("echo test1 > /home/user/test.txt", wait=True)
  198. p = testvm1.run("qvm-open-in-dvm /home/user/test.txt",
  199. passio_popen=True)
  200. wait_count = 0
  201. winid = None
  202. while True:
  203. search = subprocess.Popen(['xdotool', 'search',
  204. '--onlyvisible', '--class', 'disp*'],
  205. stdout=subprocess.PIPE,
  206. stderr=open(os.path.devnull, 'w'))
  207. retcode = search.wait()
  208. if retcode == 0:
  209. winid = search.stdout.read().strip()
  210. # get window title
  211. (window_title, _) = subprocess.Popen(
  212. ['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE). \
  213. communicate()
  214. window_title = window_title.decode().strip()
  215. # ignore LibreOffice splash screen and window with no title
  216. # set yet
  217. if window_title and not window_title.startswith("LibreOffice")\
  218. and not window_title == 'VMapp command':
  219. break
  220. wait_count += 1
  221. if wait_count > 100:
  222. self.fail("Timeout while waiting for editor window")
  223. time.sleep(0.3)
  224. time.sleep(0.5)
  225. self._handle_editor(winid)
  226. p.wait()
  227. p = testvm1.run("cat /home/user/test.txt",
  228. passio_popen=True)
  229. (test_txt_content, _) = p.communicate()
  230. # Drop BOM if added by editor
  231. if test_txt_content.startswith(b'\xef\xbb\xbf'):
  232. test_txt_content = test_txt_content[3:]
  233. self.assertEqual(test_txt_content, b"Test test 2\ntest1\n")
  234. def load_tests(loader, tests, pattern):
  235. try:
  236. app = qubes.Qubes()
  237. templates = [vm.name for vm in app.domains if
  238. isinstance(vm, qubes.vm.templatevm.TemplateVM)]
  239. except OSError:
  240. templates = []
  241. for template in templates:
  242. tests.addTests(loader.loadTestsFromTestCase(
  243. type(
  244. 'TC_20_DispVM_' + template,
  245. (TC_20_DispVMMixin, qubes.tests.QubesTestCase),
  246. {'template': template})))
  247. return tests