qvm_start_gui.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (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 Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import os
  21. import tempfile
  22. import unittest.mock
  23. import asyncio
  24. import qubesadmin.tests
  25. import qubesadmin.tools.qvm_start_gui
  26. class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
  27. def setUp(self):
  28. super(TC_00_qvm_start_gui, self).setUp()
  29. self.launcher = qubesadmin.tools.qvm_start_gui.GUILauncher(self.app)
  30. @unittest.mock.patch('subprocess.check_output')
  31. def test_000_kde_args(self, proc_mock):
  32. self.app.expected_calls[
  33. ('dom0', 'mgmt.vm.List', None, None)] = \
  34. b'0\x00test-vm class=AppVM state=Running\n'
  35. self.app.expected_calls[
  36. ('test-vm', 'mgmt.vm.property.Get', 'label', None)] = \
  37. b'0\x00default=False type=label red'
  38. self.app.expected_calls[
  39. ('dom0', 'mgmt.label.List', None, None)] = \
  40. b'0\x00red\n'
  41. proc_mock.side_effect = [
  42. b'KWIN_RUNNING = 0x1\n',
  43. b'access control enabled, only authorized clients can connect\n'
  44. b'SI:localuser:root\n'
  45. b'SI:localuser:' + os.environ['USER'].encode() + b'\n',
  46. ]
  47. args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
  48. self.assertEqual(args, ['-T', '-p',
  49. '_KDE_NET_WM_COLOR_SCHEME=s:' +
  50. os.path.expanduser('~/.local/share/qubes-kde/red.colors')])
  51. self.assertAllCalled()
  52. @unittest.mock.patch('subprocess.check_output')
  53. def test_001_kde_args_none(self, proc_mock):
  54. self.app.expected_calls[
  55. ('dom0', 'mgmt.vm.List', None, None)] = \
  56. b'0\x00test-vm class=AppVM state=Running\n'
  57. proc_mock.side_effect = [b'']
  58. args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
  59. self.assertEqual(args, [])
  60. self.assertAllCalled()
  61. def test_010_common_args(self):
  62. self.app.expected_calls[
  63. ('dom0', 'mgmt.vm.List', None, None)] = \
  64. b'0\x00test-vm class=AppVM state=Running\n'
  65. self.app.expected_calls[
  66. ('test-vm', 'mgmt.vm.property.Get', 'label', None)] = \
  67. b'0\x00default=False type=label red'
  68. self.app.expected_calls[
  69. ('test-vm', 'mgmt.vm.property.Get', 'debug', None)] = \
  70. b'0\x00default=False type=bool False'
  71. self.app.expected_calls[
  72. ('dom0', 'mgmt.label.List', None, None)] = \
  73. b'0\x00red\n'
  74. self.app.expected_calls[
  75. ('dom0', 'mgmt.label.Get', 'red', None)] = \
  76. b'0\x000xff0000'
  77. self.app.expected_calls[
  78. ('dom0', 'mgmt.label.Index', 'red', None)] = \
  79. b'0\x001'
  80. with unittest.mock.patch.object(self.launcher, 'kde_guid_args') as \
  81. kde_mock:
  82. kde_mock.return_value = []
  83. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  84. self.assertEqual(args, [
  85. '/usr/bin/qubes-guid', '-N', 'test-vm',
  86. '-c', '0xff0000',
  87. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  88. '-l', '1', '-q'])
  89. self.assertAllCalled()
  90. def test_011_common_args_debug(self):
  91. self.app.expected_calls[
  92. ('dom0', 'mgmt.vm.List', None, None)] = \
  93. b'0\x00test-vm class=AppVM state=Running\n'
  94. self.app.expected_calls[
  95. ('test-vm', 'mgmt.vm.property.Get', 'label', None)] = \
  96. b'0\x00default=False type=label red'
  97. self.app.expected_calls[
  98. ('test-vm', 'mgmt.vm.property.Get', 'debug', None)] = \
  99. b'0\x00default=False type=bool True'
  100. self.app.expected_calls[
  101. ('dom0', 'mgmt.label.List', None, None)] = \
  102. b'0\x00red\n'
  103. self.app.expected_calls[
  104. ('dom0', 'mgmt.label.Get', 'red', None)] = \
  105. b'0\x000xff0000'
  106. self.app.expected_calls[
  107. ('dom0', 'mgmt.label.Index', 'red', None)] = \
  108. b'0\x001'
  109. with unittest.mock.patch.object(self.launcher, 'kde_guid_args') as \
  110. kde_mock:
  111. kde_mock.return_value = []
  112. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  113. self.assertEqual(args, [
  114. '/usr/bin/qubes-guid', '-N', 'test-vm',
  115. '-c', '0xff0000',
  116. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  117. '-l', '1', '-v', '-v'])
  118. self.assertAllCalled()
  119. @unittest.mock.patch('asyncio.create_subprocess_exec')
  120. def test_020_start_gui_for_vm(self, proc_mock):
  121. self.app.expected_calls[
  122. ('dom0', 'mgmt.vm.List', None, None)] = \
  123. b'0\x00test-vm class=AppVM state=Running\n'
  124. self.app.expected_calls[
  125. ('test-vm', 'mgmt.vm.property.Get', 'xid', None)] = \
  126. b'0\x00default=False type=int 3000'
  127. self.app.expected_calls[
  128. ('test-vm', 'mgmt.vm.property.Get', 'hvm', None)] = \
  129. b'0\x00default=False type=bool False'
  130. with unittest.mock.patch.object(self.launcher,
  131. 'common_guid_args', lambda vm: []):
  132. self.launcher.start_gui_for_vm(self.app.domains['test-vm'])
  133. # common arguments dropped for simplicity
  134. proc_mock.assert_called_once_with('-d', '3000')
  135. self.assertAllCalled()
  136. @unittest.mock.patch('asyncio.create_subprocess_exec')
  137. def test_021_start_gui_for_vm_hvm(self, proc_mock):
  138. self.app.expected_calls[
  139. ('dom0', 'mgmt.vm.List', None, None)] = \
  140. b'0\x00test-vm class=AppVM state=Running\n'
  141. self.app.expected_calls[
  142. ('test-vm', 'mgmt.vm.property.Get', 'xid', None)] = \
  143. b'0\x00default=False type=int 3000'
  144. self.app.expected_calls[
  145. ('test-vm', 'mgmt.vm.property.Get', 'stubdom_xid', None)] = \
  146. b'0\x00default=False type=int 3001'
  147. self.app.expected_calls[
  148. ('test-vm', 'mgmt.vm.property.Get', 'hvm', None)] = \
  149. b'0\x00default=False type=bool True'
  150. self.app.expected_calls[
  151. ('test-vm', 'mgmt.vm.property.Get', 'debug', None)] = \
  152. b'0\x00default=False type=bool False'
  153. self.app.expected_calls[
  154. ('test-vm', 'mgmt.vm.feature.CheckWithTemplate', 'rpc-clipboard',
  155. None)] = \
  156. b'0\x00True'
  157. with unittest.mock.patch.object(self.launcher,
  158. 'common_guid_args', lambda vm: []):
  159. self.launcher.start_gui_for_vm(self.app.domains['test-vm'])
  160. # common arguments dropped for simplicity
  161. proc_mock.assert_called_once_with('-d', '3000', '-n', '-Q')
  162. self.assertAllCalled()
  163. def test_022_start_gui_for_vm_hvm_stubdom(self):
  164. self.app.expected_calls[
  165. ('dom0', 'mgmt.vm.List', None, None)] = \
  166. b'0\x00test-vm class=AppVM state=Running\n'
  167. self.app.expected_calls[
  168. ('test-vm', 'mgmt.vm.property.Get', 'xid', None)] = \
  169. b'0\x00default=False type=int 3000'
  170. self.app.expected_calls[
  171. ('test-vm', 'mgmt.vm.property.Get', 'stubdom_xid', None)] = \
  172. b'0\x00default=False type=int 3001'
  173. self.app.expected_calls[
  174. ('test-vm', 'mgmt.vm.property.Get', 'hvm', None)] = \
  175. b'0\x00default=False type=bool True'
  176. self.app.expected_calls[
  177. ('test-vm', 'mgmt.vm.property.Get', 'debug', None)] = \
  178. b'0\x00default=False type=bool False'
  179. self.app.expected_calls[
  180. ('test-vm', 'mgmt.vm.feature.CheckWithTemplate', 'rpc-clipboard',
  181. None)] = \
  182. b'0\x00True'
  183. pidfile = tempfile.NamedTemporaryFile()
  184. pidfile.write(b'1234\n')
  185. pidfile.flush()
  186. self.addCleanup(pidfile.close)
  187. patch_proc = unittest.mock.patch('asyncio.create_subprocess_exec')
  188. patch_args = unittest.mock.patch.object(self.launcher,
  189. 'common_guid_args', lambda vm: [])
  190. patch_pidfile = unittest.mock.patch.object(self.launcher,
  191. 'guid_pidfile', lambda vm: pidfile.name)
  192. try:
  193. mock_proc = patch_proc.start()
  194. patch_args.start()
  195. patch_pidfile.start()
  196. self.launcher.start_gui_for_vm(self.app.domains['test-vm'])
  197. # common arguments dropped for simplicity
  198. mock_proc.assert_called_once_with(
  199. '-d', '3000', '-n', '-Q', '-K', '1234')
  200. finally:
  201. unittest.mock.patch.stopall()
  202. self.assertAllCalled()
  203. def test_030_start_gui_for_stubdomain(self):
  204. self.app.expected_calls[
  205. ('dom0', 'mgmt.vm.List', None, None)] = \
  206. b'0\x00test-vm class=AppVM state=Running\n'
  207. self.app.expected_calls[
  208. ('test-vm', 'mgmt.vm.property.Get', 'xid', None)] = \
  209. b'0\x00default=False type=int 3000'
  210. self.app.expected_calls[
  211. ('test-vm', 'mgmt.vm.property.Get', 'stubdom_xid', None)] = \
  212. b'0\x00default=False type=int 3001'
  213. with unittest.mock.patch('asyncio.create_subprocess_exec') as proc_mock:
  214. with unittest.mock.patch.object(self.launcher,
  215. 'common_guid_args', lambda vm: []):
  216. self.launcher.start_gui_for_stubdomain(
  217. self.app.domains['test-vm'])
  218. # common arguments dropped for simplicity
  219. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  220. self.assertAllCalled()
  221. @asyncio.coroutine
  222. def mock_coroutine(self, mock, *args):
  223. mock(*args)
  224. def test_040_start_gui(self):
  225. loop = asyncio.new_event_loop()
  226. asyncio.set_event_loop(loop)
  227. self.addCleanup(loop.close)
  228. self.app.expected_calls[
  229. ('dom0', 'mgmt.vm.List', None, None)] = \
  230. b'0\x00test-vm class=AppVM state=Running\n'
  231. self.app.expected_calls[
  232. ('test-vm', 'mgmt.vm.List', None, None)] = \
  233. b'0\x00test-vm class=AppVM state=Running\n'
  234. self.app.expected_calls[
  235. ('test-vm', 'mgmt.vm.feature.CheckWithTemplate', 'gui', None)] = \
  236. b'0\x00True'
  237. self.app.expected_calls[
  238. ('test-vm', 'mgmt.vm.feature.CheckWithTemplate',
  239. 'no-monitor-layout', None)] = \
  240. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  241. self.app.expected_calls[
  242. ('test-vm', 'mgmt.vm.property.Get', 'hvm', None)] = \
  243. b'0\x00default=False type=bool True'
  244. self.app.expected_calls[
  245. ('test-vm', 'mgmt.vm.property.Get', 'xid', None)] = \
  246. b'0\x00default=False type=int 3000'
  247. self.app.expected_calls[
  248. ('test-vm', 'mgmt.vm.property.Get', 'stubdom_xid', None)] = \
  249. b'0\x00default=False type=int 3001'
  250. vm = self.app.domains['test-vm']
  251. mock_start_vm = unittest.mock.Mock()
  252. mock_start_stubdomain = unittest.mock.Mock()
  253. patch_start_vm = unittest.mock.patch.object(
  254. self.launcher, 'start_gui_for_vm', lambda vm_:
  255. self.mock_coroutine(mock_start_vm, vm_))
  256. patch_start_stubdomain = unittest.mock.patch.object(
  257. self.launcher, 'start_gui_for_stubdomain', lambda vm_:
  258. self.mock_coroutine(mock_start_stubdomain, vm_))
  259. try:
  260. patch_start_vm.start()
  261. patch_start_stubdomain.start()
  262. loop.run_until_complete(self.launcher.start_gui(vm))
  263. mock_start_vm.assert_called_once_with(vm)
  264. mock_start_stubdomain.assert_called_once_with(vm)
  265. finally:
  266. unittest.mock.patch.stopall()
  267. def test_041_start_gui_running(self):
  268. # simulate existing pidfiles, should not start processes
  269. self.skipTest('todo')
  270. def test_042_start_gui_pvh(self):
  271. # PVH - no stubdomain
  272. self.skipTest('todo')
  273. @unittest.mock.patch('subprocess.Popen')
  274. def test_050_get_monitor_layout1(self, proc_mock):
  275. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  276. HDMI1 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 518mm x 324mm
  277. 1920x1200 59.95*+
  278. 1920x1080 60.00 50.00 59.94
  279. 1920x1080i 60.00 50.00 59.94
  280. 1600x1200 60.00
  281. 1680x1050 59.88
  282. 1280x1024 60.02
  283. 1440x900 59.90
  284. 1280x960 60.00
  285. 1280x720 60.00 50.00 59.94
  286. 1024x768 60.00
  287. 800x600 60.32
  288. 720x576 50.00
  289. 720x480 60.00 59.94
  290. 720x480i 60.00 59.94
  291. 640x480 60.00 59.94
  292. HDMI2 disconnected (normal left inverted right x axis y axis)
  293. VGA1 disconnected (normal left inverted right x axis y axis)
  294. VIRTUAL1 disconnected (normal left inverted right x axis y axis)
  295. '''.splitlines()
  296. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  297. ['1920 1200 0 0\n'])
  298. @unittest.mock.patch('subprocess.Popen')
  299. def test_051_get_monitor_layout_multiple(self, proc_mock):
  300. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 2880 x 1024, maximum 32767 x 32767
  301. LVDS1 connected 1600x900+0+0 (normal left inverted right x axis y axis)
  302. VGA1 connected 1280x1024+1600+0 (normal left inverted right x axis y axis)
  303. '''.splitlines()
  304. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  305. ['1600 900 0 0\n', '1280 1024 1600 0\n'])
  306. @unittest.mock.patch('subprocess.Popen')
  307. def test_052_get_monitor_layout_hidpi1(self, proc_mock):
  308. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  309. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 372mm x 208mm
  310. 1920x1200 60.00*+
  311. '''.splitlines()
  312. dpi = 150
  313. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  314. ['2560 1920 0 0 {} {}\n'.format(
  315. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  316. @unittest.mock.patch('subprocess.Popen')
  317. def test_052_get_monitor_layout_hidpi2(self, proc_mock):
  318. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  319. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 310mm x 174mm
  320. 1920x1200 60.00*+
  321. '''.splitlines()
  322. dpi = 200
  323. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  324. ['2560 1920 0 0 {} {}\n'.format(
  325. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  326. @unittest.mock.patch('subprocess.Popen')
  327. def test_052_get_monitor_layout_hidpi3(self, proc_mock):
  328. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  329. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x 116mm
  330. 1920x1200 60.00*+
  331. '''.splitlines()
  332. dpi = 300
  333. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  334. ['2560 1920 0 0 {} {}\n'.format(
  335. int(2560/dpi*254/10), int(1920/dpi*254/10))])