2016-08-17 04:15:35 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2016-08-17 04:15:35 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2016-08-17 04:15:35 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2016-08-17 04:15:35 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2016-08-17 04:15:35 +02:00
|
|
|
#
|
|
|
|
|
2017-08-28 14:28:17 +02:00
|
|
|
import os
|
2016-08-17 04:15:35 +02:00
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
import time
|
2017-08-28 14:28:17 +02:00
|
|
|
import unittest
|
2018-09-15 03:53:30 +02:00
|
|
|
from contextlib import suppress
|
2017-08-28 14:28:17 +02:00
|
|
|
|
|
|
|
from distutils import spawn
|
|
|
|
|
2017-09-28 02:55:24 +02:00
|
|
|
import asyncio
|
|
|
|
|
2018-07-15 21:57:34 +02:00
|
|
|
import sys
|
|
|
|
|
2017-08-28 14:28:17 +02:00
|
|
|
import qubes.tests
|
2016-08-17 04:15:35 +02:00
|
|
|
|
2017-07-12 19:01:15 +02:00
|
|
|
class TC_04_DispVM(qubes.tests.SystemTestCase):
|
2016-08-17 04:15:35 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TC_04_DispVM, self).setUp()
|
|
|
|
self.init_default_template()
|
|
|
|
self.disp_base = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('dvm'),
|
|
|
|
label='red',
|
|
|
|
)
|
2017-09-28 02:55:24 +02:00
|
|
|
self.loop.run_until_complete(self.disp_base.create_on_disk())
|
2017-10-04 03:07:28 +02:00
|
|
|
self.disp_base.template_for_dispvms = True
|
2016-08-17 04:15:35 +02:00
|
|
|
self.app.default_dispvm = self.disp_base
|
|
|
|
self.testvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('vm'),
|
|
|
|
label='red',
|
|
|
|
)
|
2017-09-28 02:55:24 +02:00
|
|
|
self.loop.run_until_complete(self.testvm.create_on_disk())
|
2016-08-17 04:15:35 +02:00
|
|
|
self.app.save()
|
|
|
|
|
2017-10-04 03:07:28 +02:00
|
|
|
def tearDown(self):
|
|
|
|
self.app.default_dispvm = None
|
|
|
|
super(TC_04_DispVM, self).tearDown()
|
|
|
|
|
2016-08-17 04:15:35 +02:00
|
|
|
def test_002_cleanup(self):
|
2017-09-28 02:55:24 +02:00
|
|
|
self.loop.run_until_complete(self.testvm.start())
|
2016-08-17 04:15:35 +02:00
|
|
|
|
2017-10-04 03:07:28 +02:00
|
|
|
try:
|
|
|
|
(stdout, _) = self.loop.run_until_complete(
|
|
|
|
self.testvm.run_for_stdio("qvm-run-vm --dispvm bash",
|
|
|
|
input=b"echo test; qubesdb-read /name; echo ERROR\n"))
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
self.fail('qvm-run-vm failed with {} code, stderr: {}'.format(
|
|
|
|
err.returncode, err.stderr))
|
2017-02-23 00:17:25 +01:00
|
|
|
lines = stdout.decode('ascii').splitlines()
|
2016-08-17 04:15:35 +02:00
|
|
|
self.assertEqual(lines[0], "test")
|
|
|
|
dispvm_name = lines[1]
|
|
|
|
# wait for actual DispVM destruction
|
2018-07-15 22:29:22 +02:00
|
|
|
self.loop.run_until_complete(asyncio.sleep(5))
|
2016-08-17 04:15:35 +02:00
|
|
|
self.assertNotIn(dispvm_name, self.app.domains)
|
|
|
|
|
|
|
|
def test_003_cleanup_destroyed(self):
|
|
|
|
"""
|
|
|
|
Check if DispVM is properly removed even if it terminated itself (#1660)
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2017-09-28 02:55:24 +02:00
|
|
|
self.loop.run_until_complete(self.testvm.start())
|
2016-08-17 04:15:35 +02:00
|
|
|
|
2017-09-28 02:55:24 +02:00
|
|
|
p = self.loop.run_until_complete(
|
2017-10-04 03:07:28 +02:00
|
|
|
self.testvm.run("qvm-run-vm --dispvm bash; true",
|
2017-09-28 02:55:24 +02:00
|
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE))
|
2017-02-19 00:07:16 +01:00
|
|
|
p.stdin.write(b"qubesdb-read /name\n")
|
|
|
|
p.stdin.write(b"echo ERROR\n")
|
|
|
|
p.stdin.write(b"sudo poweroff\n")
|
2016-08-17 04:15:35 +02:00
|
|
|
# do not close p.stdin on purpose - wait to automatic disconnect when
|
|
|
|
# domain is destroyed
|
2018-07-15 22:29:22 +02:00
|
|
|
timeout = 70
|
2017-09-28 02:55:24 +02:00
|
|
|
lines_task = asyncio.ensure_future(p.stdout.read())
|
|
|
|
self.loop.run_until_complete(asyncio.wait_for(p.wait(), timeout))
|
|
|
|
self.loop.run_until_complete(lines_task)
|
|
|
|
lines = lines_task.result().splitlines()
|
2016-09-04 21:00:02 +02:00
|
|
|
self.assertTrue(lines, 'No output received from DispVM')
|
2016-08-17 04:15:35 +02:00
|
|
|
dispvm_name = lines[0]
|
2017-02-19 00:07:16 +01:00
|
|
|
self.assertNotEquals(dispvm_name, b"ERROR")
|
2016-08-17 04:15:35 +02:00
|
|
|
|
|
|
|
self.assertNotIn(dispvm_name, self.app.domains)
|
|
|
|
|
2017-07-12 19:01:15 +02:00
|
|
|
class TC_20_DispVMMixin(object):
|
2016-08-17 04:15:35 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TC_20_DispVMMixin, self).setUp()
|
|
|
|
self.init_default_template(self.template)
|
|
|
|
self.disp_base = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
|
|
|
name=self.make_vm_name('dvm'),
|
2017-09-05 01:31:40 +02:00
|
|
|
label='red', template_for_dispvms=True,
|
2016-08-17 04:15:35 +02:00
|
|
|
)
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(self.disp_base.create_on_disk())
|
2016-08-17 04:15:35 +02:00
|
|
|
self.app.default_dispvm = self.disp_base
|
|
|
|
self.app.save()
|
|
|
|
|
2017-10-04 03:07:28 +02:00
|
|
|
def tearDown(self):
|
|
|
|
self.app.default_dispvm = None
|
|
|
|
super(TC_20_DispVMMixin, self).tearDown()
|
|
|
|
|
2016-08-17 04:15:35 +02:00
|
|
|
def test_010_simple_dvm_run(self):
|
2017-09-05 01:31:40 +02:00
|
|
|
dispvm = self.loop.run_until_complete(
|
|
|
|
qubes.vm.dispvm.DispVM.from_appvm(self.disp_base))
|
2016-08-17 04:15:35 +02:00
|
|
|
try:
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(dispvm.start())
|
2017-09-28 02:55:24 +02:00
|
|
|
(stdout, _) = self.loop.run_until_complete(
|
|
|
|
dispvm.run_service_for_stdio('qubes.VMShell',
|
|
|
|
input=b"echo test"))
|
2017-02-19 00:07:16 +01:00
|
|
|
self.assertEqual(stdout, b"test\n")
|
2016-08-17 04:15:35 +02:00
|
|
|
finally:
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(dispvm.cleanup())
|
2016-08-17 04:15:35 +02:00
|
|
|
|
|
|
|
@unittest.skipUnless(spawn.find_executable('xdotool'),
|
|
|
|
"xdotool not installed")
|
|
|
|
def test_020_gui_app(self):
|
2017-09-05 01:31:40 +02:00
|
|
|
dispvm = self.loop.run_until_complete(
|
|
|
|
qubes.vm.dispvm.DispVM.from_appvm(self.disp_base))
|
2016-08-17 04:15:35 +02:00
|
|
|
try:
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(dispvm.start())
|
2017-10-04 03:07:28 +02:00
|
|
|
self.loop.run_until_complete(self.wait_for_session(dispvm))
|
2017-09-05 01:31:40 +02:00
|
|
|
p = self.loop.run_until_complete(
|
2017-09-28 02:55:24 +02:00
|
|
|
dispvm.run_service('qubes.VMShell',
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE))
|
2016-08-17 04:15:35 +02:00
|
|
|
# wait for DispVM startup:
|
2017-02-19 00:07:16 +01:00
|
|
|
p.stdin.write(b"echo test\n")
|
2017-09-28 02:55:24 +02:00
|
|
|
self.loop.run_until_complete(p.stdin.drain())
|
2017-09-05 01:31:40 +02:00
|
|
|
l = self.loop.run_until_complete(p.stdout.readline())
|
2017-02-19 00:07:16 +01:00
|
|
|
self.assertEqual(l, b"test\n")
|
2016-08-17 04:15:35 +02:00
|
|
|
|
|
|
|
self.assertTrue(dispvm.is_running())
|
|
|
|
try:
|
|
|
|
window_title = 'user@%s' % (dispvm.name,)
|
2017-10-28 23:25:48 +02:00
|
|
|
# close xterm on Return, but after short delay, to allow
|
|
|
|
# xdotool to send also keyup event
|
2016-08-17 04:15:35 +02:00
|
|
|
p.stdin.write("xterm -e "
|
2017-10-28 23:25:48 +02:00
|
|
|
"\"sh -c 'echo \\\"\033]0;{}\007\\\";read x;"
|
|
|
|
"sleep 0.1;'\"\n".
|
2017-02-19 00:07:16 +01:00
|
|
|
format(window_title).encode())
|
2017-10-20 03:19:18 +02:00
|
|
|
self.loop.run_until_complete(p.stdin.drain())
|
2016-08-17 04:15:35 +02:00
|
|
|
self.wait_for_window(window_title)
|
|
|
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
self.enter_keys_in_window(window_title, ['Return'])
|
|
|
|
# Wait for window to close
|
|
|
|
self.wait_for_window(window_title, show=False)
|
|
|
|
p.stdin.close()
|
2018-09-15 03:53:30 +02:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
asyncio.wait_for(p.wait(), 30))
|
|
|
|
except:
|
|
|
|
with suppress(ProcessLookupError):
|
|
|
|
p.terminate()
|
|
|
|
self.loop.run_until_complete(p.wait())
|
|
|
|
raise
|
|
|
|
finally:
|
2017-10-20 02:41:14 +02:00
|
|
|
del p
|
2016-08-17 04:15:35 +02:00
|
|
|
finally:
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(dispvm.cleanup())
|
2017-10-04 03:07:28 +02:00
|
|
|
dispvm_name = dispvm.name
|
|
|
|
del dispvm
|
2016-08-17 04:15:35 +02:00
|
|
|
|
2017-10-20 02:41:14 +02:00
|
|
|
# give it a time for shutdown + cleanup
|
2018-07-15 22:29:22 +02:00
|
|
|
self.loop.run_until_complete(asyncio.sleep(5))
|
2017-10-20 02:41:14 +02:00
|
|
|
|
2017-10-04 03:07:28 +02:00
|
|
|
self.assertNotIn(dispvm_name, self.app.domains,
|
2016-08-17 04:15:35 +02:00
|
|
|
"DispVM not removed from qubes.xml")
|
|
|
|
|
|
|
|
def _handle_editor(self, winid):
|
|
|
|
(window_title, _) = subprocess.Popen(
|
|
|
|
['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE).\
|
|
|
|
communicate()
|
2017-02-19 00:07:16 +01:00
|
|
|
window_title = window_title.decode().strip().\
|
2016-08-17 04:15:35 +02:00
|
|
|
replace('(', '\(').replace(')', '\)')
|
|
|
|
time.sleep(1)
|
2018-10-01 06:02:46 +02:00
|
|
|
if "gedit" in window_title or 'KWrite' in window_title:
|
2016-08-17 04:15:35 +02:00
|
|
|
subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
|
|
|
|
'type', 'Test test 2'])
|
|
|
|
subprocess.check_call(['xdotool', 'key', '--window', winid,
|
|
|
|
'key', 'Return'])
|
|
|
|
time.sleep(0.5)
|
|
|
|
subprocess.check_call(['xdotool',
|
|
|
|
'key', 'ctrl+s', 'ctrl+q'])
|
|
|
|
elif "LibreOffice" in window_title:
|
|
|
|
# wait for actual editor (we've got splash screen)
|
|
|
|
search = subprocess.Popen(['xdotool', 'search', '--sync',
|
|
|
|
'--onlyvisible', '--all', '--name', '--class', 'disp*|Writer'],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=open(os.path.devnull, 'w'))
|
|
|
|
retcode = search.wait()
|
|
|
|
if retcode == 0:
|
|
|
|
winid = search.stdout.read().strip()
|
|
|
|
time.sleep(0.5)
|
|
|
|
subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
|
|
|
|
'type', 'Test test 2'])
|
|
|
|
subprocess.check_call(['xdotool', 'key', '--window', winid,
|
|
|
|
'key', 'Return'])
|
|
|
|
time.sleep(0.5)
|
|
|
|
subprocess.check_call(['xdotool',
|
|
|
|
'key', '--delay', '100', 'ctrl+s',
|
|
|
|
'Return', 'ctrl+q'])
|
|
|
|
elif "emacs" in window_title:
|
|
|
|
subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
|
|
|
|
'type', 'Test test 2'])
|
|
|
|
subprocess.check_call(['xdotool', 'key', '--window', winid,
|
|
|
|
'key', 'Return'])
|
|
|
|
time.sleep(0.5)
|
|
|
|
subprocess.check_call(['xdotool',
|
|
|
|
'key', 'ctrl+x', 'ctrl+s'])
|
|
|
|
subprocess.check_call(['xdotool',
|
|
|
|
'key', 'ctrl+x', 'ctrl+c'])
|
|
|
|
elif "vim" in window_title or "user@" in window_title:
|
|
|
|
subprocess.check_call(['xdotool', 'windowactivate', '--sync', winid,
|
|
|
|
'key', 'i', 'type', 'Test test 2'])
|
|
|
|
subprocess.check_call(['xdotool', 'key', '--window', winid,
|
|
|
|
'key', 'Return'])
|
|
|
|
subprocess.check_call(
|
|
|
|
['xdotool',
|
|
|
|
'key', 'Escape', 'colon', 'w', 'q', 'Return'])
|
|
|
|
else:
|
|
|
|
self.fail("Unknown editor window: {}".format(window_title))
|
|
|
|
|
|
|
|
@unittest.skipUnless(spawn.find_executable('xdotool'),
|
|
|
|
"xdotool not installed")
|
|
|
|
def test_030_edit_file(self):
|
2017-10-04 03:07:28 +02:00
|
|
|
self.testvm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2016-08-17 04:15:35 +02:00
|
|
|
name=self.make_vm_name('vm1'),
|
|
|
|
label='red',
|
|
|
|
template=self.app.domains[self.template])
|
2017-10-04 03:07:28 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.create_on_disk())
|
2016-08-17 04:15:35 +02:00
|
|
|
self.app.save()
|
|
|
|
|
2017-10-04 03:07:28 +02:00
|
|
|
self.loop.run_until_complete(self.testvm1.start())
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(
|
2017-10-04 03:07:28 +02:00
|
|
|
self.testvm1.run_for_stdio("echo test1 > /home/user/test.txt"))
|
2016-08-17 04:15:35 +02:00
|
|
|
|
2017-09-05 01:31:40 +02:00
|
|
|
p = self.loop.run_until_complete(
|
2017-10-04 03:07:28 +02:00
|
|
|
self.testvm1.run("qvm-open-in-dvm /home/user/test.txt"))
|
2016-08-17 04:15:35 +02:00
|
|
|
|
2018-10-15 05:16:29 +02:00
|
|
|
# if first 5 windows isn't expected editor, there is no hope
|
2016-08-17 04:15:35 +02:00
|
|
|
winid = None
|
2018-10-15 05:16:29 +02:00
|
|
|
for _ in range(5):
|
|
|
|
winid = self.wait_for_window('disp[0-9]*', search_class=True)
|
|
|
|
# get window title
|
|
|
|
(window_title, _) = subprocess.Popen(
|
|
|
|
['xdotool', 'getwindowname', winid], stdout=subprocess.PIPE). \
|
|
|
|
communicate()
|
|
|
|
window_title = window_title.decode().strip()
|
|
|
|
# ignore LibreOffice splash screen and window with no title
|
|
|
|
# set yet
|
|
|
|
if window_title and not window_title.startswith("LibreOffice")\
|
|
|
|
and not window_title == 'VMapp command' \
|
|
|
|
and 'whonixcheck' not in window_title \
|
|
|
|
and not window_title == 'NetworkManager Applet':
|
|
|
|
break
|
|
|
|
self.loop.run_until_complete(asyncio.sleep(1))
|
|
|
|
winid = None
|
|
|
|
if winid is None:
|
|
|
|
self.fail('Timeout waiting for editor window')
|
2016-08-17 04:15:35 +02:00
|
|
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
self._handle_editor(winid)
|
2017-09-05 01:31:40 +02:00
|
|
|
self.loop.run_until_complete(p.wait())
|
|
|
|
(test_txt_content, _) = self.loop.run_until_complete(
|
2017-10-04 03:07:28 +02:00
|
|
|
self.testvm1.run_for_stdio("cat /home/user/test.txt"))
|
2016-08-17 04:15:35 +02:00
|
|
|
# Drop BOM if added by editor
|
2017-02-19 00:07:16 +01:00
|
|
|
if test_txt_content.startswith(b'\xef\xbb\xbf'):
|
2016-08-17 04:15:35 +02:00
|
|
|
test_txt_content = test_txt_content[3:]
|
2017-02-19 00:07:16 +01:00
|
|
|
self.assertEqual(test_txt_content, b"Test test 2\ntest1\n")
|
2016-08-17 04:15:35 +02:00
|
|
|
|
2018-10-07 19:44:48 +02:00
|
|
|
|
|
|
|
def create_testcases_for_templates():
|
|
|
|
return qubes.tests.create_testcases_for_templates('TC_20_DispVM',
|
|
|
|
TC_20_DispVMMixin, qubes.tests.SystemTestCase,
|
|
|
|
module=sys.modules[__name__])
|
|
|
|
|
2016-08-17 04:15:35 +02:00
|
|
|
def load_tests(loader, tests, pattern):
|
2018-03-30 03:04:15 +02:00
|
|
|
tests.addTests(loader.loadTestsFromNames(
|
2018-10-07 19:44:48 +02:00
|
|
|
create_testcases_for_templates()))
|
2016-10-18 13:10:06 +02:00
|
|
|
return tests
|
2018-10-07 19:44:48 +02:00
|
|
|
|
|
|
|
qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)
|