dispvm.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. import os
  22. import subprocess
  23. import tempfile
  24. import time
  25. import unittest
  26. from distutils import spawn
  27. import qubes.tests
  28. class TC_04_DispVM(qubes.tests.SystemTestCase):
  29. def setUp(self):
  30. super(TC_04_DispVM, self).setUp()
  31. self.init_default_template()
  32. self.disp_base = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  33. name=self.make_vm_name('dvm'),
  34. label='red',
  35. )
  36. self.disp_base.create_on_disk()
  37. self.app.default_dispvm = self.disp_base
  38. self.testvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  39. name=self.make_vm_name('vm'),
  40. label='red',
  41. )
  42. self.testvm.create_on_disk()
  43. self.app.save()
  44. @unittest.expectedFailure
  45. def test_002_cleanup(self):
  46. self.testvm.start()
  47. p = self.testvm.run("qvm-run --dispvm bash", passio_popen=True)
  48. (stdout, _) = p.communicate(input=b"echo test; qubesdb-read /name; "
  49. b"echo ERROR\n")
  50. self.assertEqual(p.returncode, 0)
  51. lines = stdout.decode('ascii').splitlines()
  52. self.assertEqual(lines[0], "test")
  53. dispvm_name = lines[1]
  54. # wait for actual DispVM destruction
  55. time.sleep(1)
  56. self.assertNotIn(dispvm_name, self.app.domains)
  57. @unittest.expectedFailure
  58. def test_003_cleanup_destroyed(self):
  59. """
  60. Check if DispVM is properly removed even if it terminated itself (#1660)
  61. :return:
  62. """
  63. self.testvm.start()
  64. p = self.testvm.run("qvm-run --dispvm bash; true", passio_popen=True)
  65. p.stdin.write(b"qubesdb-read /name\n")
  66. p.stdin.write(b"echo ERROR\n")
  67. p.stdin.write(b"sudo poweroff\n")
  68. # do not close p.stdin on purpose - wait to automatic disconnect when
  69. # domain is destroyed
  70. timeout = 30
  71. while timeout > 0:
  72. if p.poll():
  73. break
  74. time.sleep(1)
  75. timeout -= 1
  76. # includes check for None - timeout
  77. self.assertEqual(p.returncode, 0)
  78. lines = p.stdout.read().splitlines()
  79. self.assertTrue(lines, 'No output received from DispVM')
  80. dispvm_name = lines[0]
  81. self.assertNotEquals(dispvm_name, b"ERROR")
  82. self.assertNotIn(dispvm_name, self.app.domains)
  83. class TC_20_DispVMMixin(object):
  84. def setUp(self):
  85. super(TC_20_DispVMMixin, self).setUp()
  86. self.init_default_template(self.template)
  87. self.disp_base = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  88. name=self.make_vm_name('dvm'),
  89. label='red',
  90. )
  91. self.disp_base.create_on_disk()
  92. self.app.default_dispvm = self.disp_base
  93. self.app.save()
  94. def test_010_simple_dvm_run(self):
  95. dispvm = qubes.vm.dispvm.DispVM.from_appvm(self.disp_base)
  96. try:
  97. dispvm.start()
  98. p = dispvm.run_service('qubes.VMShell', passio_popen=True)
  99. (stdout, _) = p.communicate(input=b"echo test")
  100. self.assertEqual(stdout, b"test\n")
  101. finally:
  102. dispvm.cleanup()
  103. @unittest.skipUnless(spawn.find_executable('xdotool'),
  104. "xdotool not installed")
  105. def test_020_gui_app(self):
  106. dispvm = qubes.vm.dispvm.DispVM.from_appvm(self.disp_base)
  107. try:
  108. dispvm.start()
  109. p = dispvm.run_service('qubes.VMShell', passio_popen=True)
  110. # wait for DispVM startup:
  111. p.stdin.write(b"echo test\n")
  112. p.stdin.flush()
  113. l = p.stdout.readline()
  114. self.assertEqual(l, b"test\n")
  115. self.assertTrue(dispvm.is_running())
  116. try:
  117. window_title = 'user@%s' % (dispvm.name,)
  118. p.stdin.write("xterm -e "
  119. "\"sh -c 'echo \\\"\033]0;{}\007\\\";read x;'\"\n".
  120. format(window_title).encode())
  121. self.wait_for_window(window_title)
  122. time.sleep(0.5)
  123. self.enter_keys_in_window(window_title, ['Return'])
  124. # Wait for window to close
  125. self.wait_for_window(window_title, show=False)
  126. finally:
  127. p.stdin.close()
  128. finally:
  129. dispvm.cleanup()
  130. self.assertNotIn(dispvm.name, self.app.domains,
  131. "DispVM not removed from qubes.xml")
  132. def _handle_editor(self, winid):
  133. (window_title, _) = subprocess.Popen(
  134. ['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE).\
  135. communicate()
  136. window_title = window_title.decode().strip().\
  137. replace('(', '\(').replace(')', '\)')
  138. time.sleep(1)
  139. if "gedit" in window_title:
  140. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  141. 'type', 'Test test 2'])
  142. subprocess.check_call(['xdotool', 'key', '--window', winid,
  143. 'key', 'Return'])
  144. time.sleep(0.5)
  145. subprocess.check_call(['xdotool',
  146. 'key', 'ctrl+s', 'ctrl+q'])
  147. elif "LibreOffice" in window_title:
  148. # wait for actual editor (we've got splash screen)
  149. search = subprocess.Popen(['xdotool', 'search', '--sync',
  150. '--onlyvisible', '--all', '--name', '--class', 'disp*|Writer'],
  151. stdout=subprocess.PIPE,
  152. stderr=open(os.path.devnull, 'w'))
  153. retcode = search.wait()
  154. if retcode == 0:
  155. winid = search.stdout.read().strip()
  156. time.sleep(0.5)
  157. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  158. 'type', 'Test test 2'])
  159. subprocess.check_call(['xdotool', 'key', '--window', winid,
  160. 'key', 'Return'])
  161. time.sleep(0.5)
  162. subprocess.check_call(['xdotool',
  163. 'key', '--delay', '100', 'ctrl+s',
  164. 'Return', 'ctrl+q'])
  165. elif "emacs" in window_title:
  166. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  167. 'type', 'Test test 2'])
  168. subprocess.check_call(['xdotool', 'key', '--window', winid,
  169. 'key', 'Return'])
  170. time.sleep(0.5)
  171. subprocess.check_call(['xdotool',
  172. 'key', 'ctrl+x', 'ctrl+s'])
  173. subprocess.check_call(['xdotool',
  174. 'key', 'ctrl+x', 'ctrl+c'])
  175. elif "vim" in window_title or "user@" in window_title:
  176. subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
  177. 'key', 'i', 'type', 'Test test 2'])
  178. subprocess.check_call(['xdotool', 'key', '--window', winid,
  179. 'key', 'Return'])
  180. subprocess.check_call(
  181. ['xdotool',
  182. 'key', 'Escape', 'colon', 'w', 'q', 'Return'])
  183. else:
  184. self.fail("Unknown editor window: {}".format(window_title))
  185. @unittest.skipUnless(spawn.find_executable('xdotool'),
  186. "xdotool not installed")
  187. @unittest.expectedFailure
  188. def test_030_edit_file(self):
  189. testvm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  190. name=self.make_vm_name('vm1'),
  191. label='red',
  192. template=self.app.domains[self.template])
  193. testvm1.create_on_disk()
  194. self.app.save()
  195. testvm1.start()
  196. testvm1.run("echo test1 > /home/user/test.txt", wait=True)
  197. p = testvm1.run("qvm-open-in-dvm /home/user/test.txt",
  198. passio_popen=True)
  199. wait_count = 0
  200. winid = None
  201. while True:
  202. search = subprocess.Popen(['xdotool', 'search',
  203. '--onlyvisible', '--class', 'disp*'],
  204. stdout=subprocess.PIPE,
  205. stderr=open(os.path.devnull, 'w'))
  206. retcode = search.wait()
  207. if retcode == 0:
  208. winid = search.stdout.read().strip()
  209. # get window title
  210. (window_title, _) = subprocess.Popen(
  211. ['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE). \
  212. communicate()
  213. window_title = window_title.decode().strip()
  214. # ignore LibreOffice splash screen and window with no title
  215. # set yet
  216. if window_title and not window_title.startswith("LibreOffice")\
  217. and not window_title == 'VMapp command':
  218. break
  219. wait_count += 1
  220. if wait_count > 100:
  221. self.fail("Timeout while waiting for editor window")
  222. time.sleep(0.3)
  223. time.sleep(0.5)
  224. self._handle_editor(winid)
  225. p.wait()
  226. p = testvm1.run("cat /home/user/test.txt",
  227. passio_popen=True)
  228. (test_txt_content, _) = p.communicate()
  229. # Drop BOM if added by editor
  230. if test_txt_content.startswith(b'\xef\xbb\xbf'):
  231. test_txt_content = test_txt_content[3:]
  232. self.assertEqual(test_txt_content, b"Test test 2\ntest1\n")
  233. def load_tests(loader, tests, pattern):
  234. for template in qubes.tests.list_templates():
  235. tests.addTests(loader.loadTestsFromTestCase(
  236. type(
  237. 'TC_20_DispVM_' + template,
  238. (TC_20_DispVMMixin, qubes.tests.SystemTestCase),
  239. {'template': template})))
  240. return tests